testmap.c 7.5 KB

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