basic.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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);\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. int main(int UNUSED(argc), char** UNUSED(argv)) {
  55. cl_platform_id platforms[15];
  56. cl_uint num_platforms;
  57. cl_device_id devices[15];
  58. cl_uint num_devices;
  59. cl_context context;
  60. cl_program program;
  61. cl_kernel kernel;
  62. cl_mem s1m, s2m, dm;
  63. cl_command_queue cq;
  64. cl_int err;
  65. unsigned int i;
  66. TYPE s1[SIZE],s2[SIZE],d[SIZE];
  67. {
  68. for (i=0; i<SIZE; i++) {
  69. s1[i] = 2.0;
  70. s2[i] = 7.0;
  71. d[i] = 98.0;
  72. }
  73. }
  74. printf("Querying platform...\n");
  75. clGetPlatformIDs(0, NULL, &num_platforms);
  76. if (num_platforms == 0) {
  77. printf("No OpenCL platform found.\n");
  78. exit(77);
  79. }
  80. err = clGetPlatformIDs(sizeof(platforms)/sizeof(cl_platform_id), platforms, &num_platforms);
  81. check(err, "clGetPlatformIDs");
  82. int platform_idx = -1;
  83. for (i=0; i<num_platforms;i++) {
  84. char vendor[256];
  85. clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, sizeof(vendor), vendor, NULL);
  86. if (strcmp(vendor, "Inria") == 0) {
  87. platform_idx = i;
  88. }
  89. }
  90. if (platform_idx == -1) {
  91. printf("SOCL platform not found.\n");
  92. exit(77);
  93. }
  94. printf("Querying devices...\n");
  95. err = clGetDeviceIDs(platforms[platform_idx], CL_DEVICE_TYPE_ALL, sizeof(devices)/sizeof(cl_device_id), devices, &num_devices);
  96. check(err, "clGetDeviceIDs");
  97. if (num_devices == 0) {
  98. printf("No OpenCL device found\n");
  99. exit(77);
  100. }
  101. printf("Creating context...\n");
  102. cl_context_properties properties[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[platform_idx], 0};
  103. context = clCreateContext(properties, num_devices, devices, NULL, NULL, &err);
  104. check(err, "clCreateContext");
  105. printf("Creating program...\n");
  106. program = clCreateProgramWithSource(context, 1, &kernel_src, NULL, &err);
  107. check(err, "clCreateProgram");
  108. printf("Building program...\n");
  109. err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
  110. check(err, "clBuildProgram");
  111. printf("Creating kernel...\n");
  112. kernel = clCreateKernel(program, "add", &err);
  113. check(err, "clCreateKernel");
  114. printf("Creating buffers...\n");
  115. s1m = clCreateBuffer(context, CL_MEM_READ_WRITE, REALSIZE, NULL, &err);
  116. check(err, "clCreateBuffer s1");
  117. s2m = clCreateBuffer(context, CL_MEM_READ_ONLY, REALSIZE, NULL, &err);
  118. check(err, "clCreateBuffer s2");
  119. dm = clCreateBuffer(context, CL_MEM_WRITE_ONLY, REALSIZE, NULL, &err);
  120. check(err, "clCreateBuffer d");
  121. printf("Creating command queue...\n");
  122. cl_event eventW1, eventW2, eventK, eventR;
  123. cq = clCreateCommandQueue(context, NULL, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | CL_QUEUE_PROFILING_ENABLE, &err);
  124. check(err, "clCreateCommandQueue");
  125. printf("Enqueueing WriteBuffers...\n");
  126. err = clEnqueueWriteBuffer(cq, s1m, CL_FALSE, 0, REALSIZE, s1, 0, NULL, &eventW1);
  127. check(err, "clEnqueueWriteBuffer s1");
  128. err = clEnqueueWriteBuffer(cq, s2m, CL_FALSE, 0, REALSIZE, s2, 0, NULL, &eventW2);
  129. check(err, "clEnqueueWriteBuffer s2");
  130. printf("Setting kernel arguments...\n");
  131. err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &s1m);
  132. check(err, "clSetKernelArg 0");
  133. err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &s2m);
  134. check(err, "clSetKernelArg 1");
  135. err = clSetKernelArg(kernel, 2, sizeof(cl_mem), &dm);
  136. check(err, "clSetKernelArg 2");
  137. printf("Enqueueing NDRangeKernel...\n");
  138. size_t local[3] = {16, 1, 1};
  139. size_t global[3] = {1024, 1, 1};
  140. cl_event deps[] = {eventW1,eventW2};
  141. err = clEnqueueNDRangeKernel(cq, kernel, 3, NULL, global, local, 2, deps, &eventK);
  142. check(err, "clEnqueueNDRangeKernel");
  143. printf("Enqueueing ReadBuffer...\n");
  144. err = clEnqueueReadBuffer(cq, dm, CL_FALSE, 0, REALSIZE, d, 1, &eventK, &eventR);
  145. check(err, "clEnqueueReadBuffer");
  146. printf("Finishing queue...\n");
  147. clFinish(cq);
  148. printf("Data...\n");
  149. {
  150. int j;
  151. for (j=0; j<SIZE; j++) {
  152. printf("%f ", d[j]);
  153. }
  154. printf("\n");
  155. }
  156. #define DURATION(event,label) do { \
  157. cl_ulong t0,t1; \
  158. err = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &t0, NULL);\
  159. check(err, "clGetEventProfilingInfo");\
  160. err = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &t1, NULL);\
  161. check(err, "clGetEventProfilingInfo");\
  162. printf("Profiling %s: %llu nanoseconds\n", label, (unsigned long long) (t1-t0));\
  163. } while (0);
  164. DURATION(eventW1, "first buffer writing");
  165. DURATION(eventW2, "second buffer writing");
  166. DURATION(eventK, "kernel execution");
  167. DURATION(eventR, "result buffer reading");
  168. printf("Releasing events...\n");
  169. err = clReleaseEvent(eventW1);
  170. err |= clReleaseEvent(eventW2);
  171. err |= clReleaseEvent(eventK);
  172. err |= clReleaseEvent(eventR);
  173. check(err, "clReleaseEvents");
  174. printf("Releasing command queue...\n");
  175. err = clReleaseCommandQueue(cq);
  176. check(err, "clReleaseCommandQueue");
  177. printf("Releasing buffers...\n");
  178. err = clReleaseMemObject(s1m);
  179. check(err, "clReleaseMemObject s1");
  180. err = clReleaseMemObject(s2m);
  181. check(err, "clReleaseMemObject s2");
  182. err = clReleaseMemObject(dm);
  183. check(err, "clReleaseMemObject d");
  184. printf("Releasing kernel...\n");
  185. err = clReleaseKernel(kernel);
  186. check(err, "clReleaseKernel");
  187. printf("Releasing program...\n");
  188. err = clReleaseProgram(program);
  189. check(err, "clReleaseProgram");
  190. printf("Releasing context...\n");
  191. err = clReleaseContext(context);
  192. check(err, "clReleaseContext");
  193. #ifdef HAVE_CLGETEXTENSIONFUNCTIONADDRESSFORPLATFORM
  194. void (*clShutdown)(void) = clGetExtensionFunctionAddressForPlatform(platforms[platform_idx], "clShutdown");
  195. if (clShutdown != NULL) {
  196. printf("Calling clShutdown :)\n");
  197. clShutdown();
  198. }
  199. #endif
  200. return 0;
  201. }