basic.c 7.6 KB

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