testmap.c 7.6 KB

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