mansched.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #ifdef __APPLE_CC__
  21. #include <OpenCL/opencl.h>
  22. #else
  23. #include <CL/cl.h>
  24. #endif
  25. #define error(...) do { fprintf(stderr, "Error: " __VA_ARGS__); exit(EXIT_FAILURE); } while(0)
  26. #define check(err, str) do { if(err != CL_SUCCESS) { fprintf(stderr, "OpenCL Error (%d): %s\n",err, str); exit(EXIT_FAILURE); }} while(0)
  27. #ifdef UNUSED
  28. #elif defined(__GNUC__)
  29. # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
  30. #else
  31. # define UNUSED(x) x
  32. #endif
  33. #define SIZE 1024
  34. #define TYPE float
  35. #define REALSIZE (SIZE * sizeof(TYPE))
  36. const char * kernel_src = "__kernel void add(__global float*s1, __global float*s2, __global float*d) { \
  37. size_t x = get_global_id(0);\
  38. size_t y = get_global_id(1);\
  39. size_t w = get_global_size(0); \
  40. int idx = y*w+x; \
  41. d[idx] = s1[idx] + s2[idx];\
  42. }";
  43. int main(int UNUSED(argc), char** UNUSED(argv)) {
  44. cl_platform_id platforms[15];
  45. cl_uint num_platforms;
  46. cl_device_id devices[15];
  47. cl_uint num_devices;
  48. cl_context context;
  49. cl_program program;
  50. cl_kernel kernel;
  51. cl_mem s1m, s2m, dm;
  52. cl_command_queue cq;
  53. unsigned int d;
  54. cl_int err;
  55. TYPE s1[SIZE],s2[SIZE],dst[SIZE];
  56. {
  57. int i;
  58. for (i=0; i<SIZE; i++) {
  59. s1[i] = 2.0;
  60. s2[i] = 7.0;
  61. dst[i] = 98.0;
  62. }
  63. }
  64. printf("Querying platform...\n");
  65. clGetPlatformIDs(0, NULL, &num_platforms);
  66. if (num_platforms == 0) {
  67. printf("No OpenCL platform found.\n");
  68. exit(77);
  69. }
  70. err = clGetPlatformIDs(sizeof(platforms)/sizeof(cl_platform_id), platforms, &num_platforms);
  71. check(err, "clGetPlatformIDs");
  72. printf("Querying devices...\n");
  73. unsigned int platform_idx;
  74. for (platform_idx=0; platform_idx<num_platforms; platform_idx++) {
  75. err = clGetDeviceIDs(platforms[platform_idx], CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_CPU, sizeof(devices)/sizeof(cl_device_id), devices, &num_devices);
  76. check(err, "clGetDeviceIDs");
  77. for (d=0; d<num_devices; d++) {
  78. printf("Creating context...\n");
  79. cl_context_properties properties[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[platform_idx], 0};
  80. context = clCreateContext(properties, 1, &devices[d], NULL, NULL, &err);
  81. check(err, "clCreateContext");
  82. printf("Creating program...\n");
  83. program = clCreateProgramWithSource(context, 1, &kernel_src, NULL, &err);
  84. check(err, "clCreateProgram");
  85. printf("Building program...\n");
  86. err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
  87. check(err, "clBuildProgram");
  88. printf("Creating kernel...\n");
  89. kernel = clCreateKernel(program, "add", &err);
  90. check(err, "clCreateKernel");
  91. printf("Creating buffers...\n");
  92. s1m = clCreateBuffer(context, CL_MEM_READ_WRITE, REALSIZE, NULL, &err);
  93. check(err, "clCreateBuffer s1");
  94. s2m = clCreateBuffer(context, CL_MEM_READ_ONLY, REALSIZE, NULL, &err);
  95. check(err, "clCreateBuffer s2");
  96. dm = clCreateBuffer(context, CL_MEM_WRITE_ONLY, REALSIZE, NULL, &err);
  97. check(err, "clCreateBuffer dst");
  98. printf("Creating command queue...\n");
  99. cl_event eventW1, eventW2, eventK, eventR;
  100. #ifdef PROFILING
  101. cq = clCreateCommandQueue(context, devices[d], CL_QUEUE_PROFILING_ENABLE, &err);
  102. #else
  103. cq = clCreateCommandQueue(context, devices[d], 0, &err);
  104. #endif
  105. check(err, "clCreateCommandQueue");
  106. printf("Enqueueing WriteBuffers...\n");
  107. err = clEnqueueWriteBuffer(cq, s1m, CL_FALSE, 0, REALSIZE, s1, 0, NULL, &eventW1);
  108. check(err, "clEnqueueWriteBuffer s1");
  109. err = clEnqueueWriteBuffer(cq, s2m, CL_FALSE, 0, REALSIZE, s2, 0, NULL, &eventW2);
  110. check(err, "clEnqueueWriteBuffer s2");
  111. printf("Setting kernel arguments...\n");
  112. err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &s1m);
  113. check(err, "clSetKernelArg 0");
  114. err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &s2m);
  115. check(err, "clSetKernelArg 1");
  116. err = clSetKernelArg(kernel, 2, sizeof(cl_mem), &dm);
  117. check(err, "clSetKernelArg 2");
  118. printf("Enqueueing NDRangeKernel...\n");
  119. size_t local[3] = {16, 1, 1};
  120. size_t global[3] = {1024, 1, 1};
  121. cl_event deps[] = {eventW1,eventW2};
  122. err = clEnqueueNDRangeKernel(cq, kernel, 3, NULL, global, local, 2, deps, &eventK);
  123. check(err, "clEnqueueNDRangeKernel");
  124. printf("Enqueueing ReadBuffer...\n");
  125. err = clEnqueueReadBuffer(cq, dm, CL_FALSE, 0, REALSIZE, dst, 0, NULL, &eventR);
  126. check(err, "clEnqueueReadBuffer");
  127. printf("Finishing queue...\n");
  128. clFinish(cq);
  129. printf("Data...\n");
  130. {
  131. int i;
  132. for (i=0; i<SIZE; i++) {
  133. printf("%f ", dst[i]);
  134. }
  135. printf("\n");
  136. }
  137. #ifdef PROFILING
  138. #define DURATION(event,label) do { \
  139. cl_ulong t0,t1; \
  140. err = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &t0, NULL);\
  141. check(err, "clGetEventProfilingInfo");\
  142. err = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &t1, NULL);\
  143. check(err, "clGetEventProfilingInfo");\
  144. printf("Profiling %s: %lu nanoseconds\n", label, t1-t0);\
  145. } while (0);
  146. DURATION(eventW1, "first buffer writing");
  147. DURATION(eventW2, "second buffer writing");
  148. DURATION(eventK, "kernel execution");
  149. DURATION(eventR, "result buffer reading");
  150. #endif
  151. printf("Releasing events...\n");
  152. err = clReleaseEvent(eventW1);
  153. err |= clReleaseEvent(eventW2);
  154. err |= clReleaseEvent(eventK);
  155. err |= clReleaseEvent(eventR);
  156. check(err, "clReleaseCommandQueue");
  157. printf("Releasing command queue...\n");
  158. err = clReleaseCommandQueue(cq);
  159. check(err, "clReleaseCommandQueue");
  160. printf("Releasing buffers...\n");
  161. err = clReleaseMemObject(s1m);
  162. check(err, "clReleaseMemObject s1");
  163. err = clReleaseMemObject(s2m);
  164. check(err, "clReleaseMemObject s2");
  165. err = clReleaseMemObject(dm);
  166. check(err, "clReleaseMemObject d");
  167. printf("Releasing kernel...\n");
  168. err = clReleaseKernel(kernel);
  169. check(err, "clReleaseKernel");
  170. printf("Releasing program...\n");
  171. err = clReleaseProgram(program);
  172. check(err, "clReleaseProgram");
  173. printf("Releasing context...\n");
  174. err = clReleaseContext(context);
  175. check(err, "clReleaseContext");
  176. }
  177. }
  178. return 0;
  179. }