testmap.c 7.5 KB

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