basicsplit.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 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);\n\
  38. size_t y = get_global_id(1);\n\
  39. size_t w = get_global_size(0); \n\
  40. int idx = y*w+x; \n\
  41. #ifdef SOCL_DEVICE_TYPE_GPU \n\
  42. d[idx] = s1[idx] + s2[idx];\n\
  43. #endif \n\
  44. #ifdef SOCL_DEVICE_TYPE_CPU \n\
  45. d[idx] = s1[idx] + 2* s2[idx];\n\
  46. #endif \n\
  47. #ifdef SOCL_DEVICE_TYPE_ACCELERATOR \n\
  48. d[idx] = s1[idx] + 3 * s2[idx];\n\
  49. #endif \n\
  50. #ifdef SOCL_DEVICE_TYPE_UNKNOWN \n\
  51. d[idx] = s1[idx] + 4 * s2[idx];\n\
  52. #endif \n\
  53. }";
  54. cl_kernel kernel;
  55. cl_context context;
  56. TYPE s1[SIZE],s2[SIZE],d[SIZE];
  57. typedef cl_int (*split_func_t)(cl_command_queue, cl_uint, cl_uint, const size_t *, const size_t *, const size_t *, const cl_event, cl_event *);
  58. void add(cl_command_queue cq, cl_uint size, TYPE * s1, TYPE *s2, TYPE*d, cl_uint num_events, cl_event * events, cl_event *event) {
  59. cl_int err;
  60. printf("Creating buffers...\n");
  61. cl_mem s1m = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, size * sizeof(TYPE), s1, &err);
  62. check(err, "clCreateBuffer s1");
  63. cl_mem s2m = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, size * sizeof(TYPE), s2, &err);
  64. check(err, "clCreateBuffer s2");
  65. cl_mem dm = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, size * sizeof(TYPE), d, &err);
  66. check(err, "clCreateBuffer d");
  67. err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &s1m);
  68. check(err, "clSetKernelArg 0");
  69. err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &s2m);
  70. check(err, "clSetKernelArg 1");
  71. err = clSetKernelArg(kernel, 2, sizeof(cl_mem), &dm);
  72. check(err, "clSetKernelArg 2");
  73. printf("Enqueueing NDRangeKernel...\n");
  74. size_t local[3] = {16, 1, 1};
  75. size_t global[3] = {size, 1, 1};
  76. cl_event eventK;
  77. err = clEnqueueNDRangeKernel(cq, kernel, 3, NULL, global, local, num_events, events, &eventK);
  78. check(err, "clEnqueueNDRangeKernel");
  79. clEnqueueMapBuffer(cq, dm, CL_FALSE, CL_MAP_READ, 0, size * sizeof(TYPE), 1, &eventK, event, &err);
  80. check(err, "clEnqueueMapBuffer");
  81. clReleaseMemObject(s1m);
  82. clReleaseMemObject(s2m);
  83. clReleaseMemObject(dm);
  84. }
  85. cl_int split_func(cl_command_queue cq, cl_uint split_factor, void * data, cl_event before, cl_event * after) {
  86. cl_event evs[split_factor];
  87. printf("Partition with factor %d\n", split_factor);
  88. cl_uint size = ((SIZE)/split_factor) - (SIZE/split_factor % 16);
  89. cl_uint i;
  90. for (i=0; i<split_factor; i++) {
  91. cl_uint offset = size * i;
  92. add(cq, size, &s1[offset], &s2[offset], &d[offset], 1, &before, &evs[i]);
  93. }
  94. clEnqueueMarkerWithWaitList(cq, split_factor, evs, after);
  95. return CL_SUCCESS;
  96. }
  97. int main(int UNUSED(argc), char** UNUSED(argv)) {
  98. cl_platform_id platforms[15];
  99. cl_uint num_platforms;
  100. cl_device_id devices[15];
  101. cl_uint num_devices;
  102. cl_program program;
  103. cl_command_queue cq;
  104. cl_int err;
  105. unsigned int i;
  106. {
  107. for (i=0; i<SIZE; i++) {
  108. s1[i] = 2.0;
  109. s2[i] = 7.0;
  110. d[i] = 98.0;
  111. }
  112. }
  113. printf("Querying platform...\n");
  114. clGetPlatformIDs(0, NULL, &num_platforms);
  115. if (num_platforms == 0) {
  116. printf("No OpenCL platform found.\n");
  117. exit(77);
  118. }
  119. err = clGetPlatformIDs(sizeof(platforms)/sizeof(cl_platform_id), platforms, NULL);
  120. check(err, "clGetPlatformIDs");
  121. unsigned int platform_idx = -1;
  122. for (i=0; i<num_platforms;i++) {
  123. char vendor[256];
  124. clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, sizeof(vendor), vendor, NULL);
  125. if (strcmp(vendor, "Inria") == 0) {
  126. platform_idx = i;
  127. }
  128. }
  129. if (platform_idx == -1) {
  130. printf("SOCL platform not found.\n");
  131. exit(77);
  132. }
  133. printf("Querying devices...\n");
  134. err = clGetDeviceIDs(platforms[platform_idx], CL_DEVICE_TYPE_ALL, sizeof(devices)/sizeof(cl_device_id), devices, &num_devices);
  135. check(err, "clGetDeviceIDs");
  136. if (num_devices == 0) {
  137. printf("No OpenCL device found\n");
  138. exit(77);
  139. }
  140. printf("Creating context...\n");
  141. cl_context_properties properties[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[platform_idx], 0};
  142. context = clCreateContext(properties, num_devices, devices, NULL, NULL, &err);
  143. check(err, "clCreateContext");
  144. printf("Creating program...\n");
  145. program = clCreateProgramWithSource(context, 1, &kernel_src, NULL, &err);
  146. check(err, "clCreateProgram");
  147. printf("Building program...\n");
  148. err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
  149. check(err, "clBuildProgram");
  150. printf("Creating kernel...\n");
  151. kernel = clCreateKernel(program, "add", &err);
  152. check(err, "clCreateKernel");
  153. printf("Creating command queue...\n");
  154. cq = clCreateCommandQueue(context, NULL, CL_QUEUE_PROFILING_ENABLE | CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &err);
  155. check(err, "clCreateCommandQueue");
  156. printf("Setting split parameters...\n");
  157. err = clSetKernelArg(kernel, -1, sizeof(void*), split_func);
  158. check(err, "clSetKernelArg split func");
  159. cl_uint split_space = 10;
  160. err = clSetKernelArg(kernel, -2, sizeof(void*), &split_space);
  161. check(err, "clSetKernelArg split space");
  162. cl_uint niter = 15;
  163. for (i=0; i<niter; i++) {
  164. printf("Iteration %u...\n", i);
  165. add(cq, SIZE, s1, s2, d, 0, NULL, NULL);
  166. printf("Finishing iteration...\n");
  167. clFinish(cq);
  168. }
  169. printf("Data...\n");
  170. {
  171. int i;
  172. for (i=0; i<SIZE; i++) {
  173. printf("%f ", d[i]);
  174. }
  175. printf("\n");
  176. }
  177. #ifdef PROFILING
  178. #define DURATION(event,label) do { \
  179. cl_ulong t0,t1; \
  180. err = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &t0, NULL);\
  181. check(err, "clGetEventProfilingInfo");\
  182. err = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &t1, NULL);\
  183. check(err, "clGetEventProfilingInfo");\
  184. printf("Profiling %s: %lu nanoseconds\n", label, t1-t0);\
  185. } while (0);
  186. DURATION(eventW1, "first buffer writing");
  187. DURATION(eventW2, "second buffer writing");
  188. DURATION(eventK, "kernel execution");
  189. DURATION(eventR, "result buffer reading");
  190. #endif
  191. printf("Releasing command queue...\n");
  192. err = clReleaseCommandQueue(cq);
  193. check(err, "clReleaseCommandQueue");
  194. printf("Releasing kernel...\n");
  195. err = clReleaseKernel(kernel);
  196. check(err, "clReleaseKernel");
  197. printf("Releasing program...\n");
  198. err = clReleaseProgram(program);
  199. check(err, "clReleaseProgram");
  200. printf("Releasing context...\n");
  201. err = clReleaseContext(context);
  202. check(err, "clReleaseContext");
  203. #ifdef HAVE_CLGETEXTENSIONFUNCTIONADDRESSFORPLATFORM
  204. void (*clShutdown)(void) = clGetExtensionFunctionAddressForPlatform(platforms[platform_idx], "clShutdown");
  205. if (clShutdown != NULL) {
  206. printf("Calling clShutdown :)\n");
  207. clShutdown();
  208. }
  209. #endif
  210. return 0;
  211. }