basic.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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);\
  34. size_t y = get_global_id(1);\
  35. size_t w = get_global_size(0); \
  36. int idx = y*w+x; \
  37. d[idx] = s1[idx] + s2[idx];\
  38. }";
  39. int main(int UNUSED(argc), char** UNUSED(argv)) {
  40. cl_platform_id platforms[15];
  41. cl_uint num_platforms;
  42. cl_device_id devices[15];
  43. cl_uint num_devices;
  44. cl_context context;
  45. cl_program program;
  46. cl_kernel kernel;
  47. cl_mem s1m, s2m, dm;
  48. cl_command_queue cq;
  49. cl_int err;
  50. TYPE s1[SIZE],s2[SIZE],d[SIZE];
  51. {
  52. int i;
  53. for (i=0; i<SIZE; i++) {
  54. s1[i] = 2.0;
  55. s2[i] = 7.0;
  56. d[i] = 98.0;
  57. }
  58. }
  59. printf("Querying platform...\n");
  60. err = clGetPlatformIDs(0, NULL, &num_platforms);
  61. if (num_platforms == 0) {
  62. printf("No OpenCL platform found. If you use SOCL, this could mean StarPU wasn't configured for OpenCL. Try disabling CUDA support in StarPU (export STARPU_NCUDA=0).\n");
  63. exit(77);
  64. }
  65. err = clGetPlatformIDs(sizeof(platforms)/sizeof(cl_platform_id), platforms, NULL);
  66. check(err, "clGetPlatformIDs");
  67. printf("Querying devices...\n");
  68. unsigned int platform_idx;
  69. for (platform_idx=0; platform_idx<num_platforms; platform_idx++) {
  70. err = clGetDeviceIDs(platforms[platform_idx], CL_DEVICE_TYPE_GPU, sizeof(devices)/sizeof(cl_device_id), devices, &num_devices);
  71. check(err, "clGetDeviceIDs");
  72. if (num_devices != 0)
  73. break;
  74. }
  75. if (num_devices == 0)
  76. error("No OpenCL device found\n");
  77. printf("Creating context...\n");
  78. cl_context_properties properties[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[platform_idx], 0};
  79. context = clCreateContext(properties, num_devices, devices, NULL, NULL, &err);
  80. check(err, "clCreateContext");
  81. printf("Creating program...\n");
  82. program = clCreateProgramWithSource(context, 1, &kernel_src, NULL, &err);
  83. check(err, "clCreateProgram");
  84. printf("Building program...\n");
  85. err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
  86. check(err, "clBuildProgram");
  87. printf("Creating kernel...\n");
  88. kernel = clCreateKernel(program, "add", &err);
  89. check(err, "clCreateKernel");
  90. printf("Creating buffers...\n");
  91. s1m = clCreateBuffer(context, CL_MEM_READ_WRITE, REALSIZE, NULL, &err);
  92. check(err, "clCreateBuffer s1");
  93. s2m = clCreateBuffer(context, CL_MEM_READ_ONLY, REALSIZE, NULL, &err);
  94. check(err, "clCreateBuffer s2");
  95. dm = clCreateBuffer(context, CL_MEM_WRITE_ONLY, REALSIZE, NULL, &err);
  96. check(err, "clCreateBuffer d");
  97. printf("Creating command queue...\n");
  98. cl_event eventW1, eventW2, eventK, eventR;
  99. #ifdef PROFILING
  100. cq = clCreateCommandQueue(context, devices[0], CL_QUEUE_PROFILING_ENABLE, &err);
  101. #else
  102. cq = clCreateCommandQueue(context, devices[0], 0, &err);
  103. #endif
  104. check(err, "clCreateCommandQueue");
  105. printf("Enqueueing WriteBuffers...\n");
  106. err = clEnqueueWriteBuffer(cq, s1m, CL_FALSE, 0, REALSIZE, s1, 0, NULL, &eventW1);
  107. check(err, "clEnqueueWriteBuffer s1");
  108. err = clEnqueueWriteBuffer(cq, s2m, CL_FALSE, 0, REALSIZE, s2, 0, NULL, &eventW2);
  109. check(err, "clEnqueueWriteBuffer s2");
  110. printf("Setting kernel arguments...\n");
  111. err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &s1m);
  112. check(err, "clSetKernelArg 0");
  113. err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &s2m);
  114. check(err, "clSetKernelArg 1");
  115. err = clSetKernelArg(kernel, 2, sizeof(cl_mem), &dm);
  116. check(err, "clSetKernelArg 2");
  117. printf("Enqueueing NDRangeKernel...\n");
  118. size_t local[3] = {16, 1, 1};
  119. size_t global[3] = {1024, 1, 1};
  120. cl_event deps[] = {eventW1,eventW2};
  121. err = clEnqueueNDRangeKernel(cq, kernel, 3, NULL, global, local, 2, deps, &eventK);
  122. check(err, "clEnqueueNDRangeKernel");
  123. printf("Enqueueing ReadBuffer...\n");
  124. err = clEnqueueReadBuffer(cq, dm, CL_FALSE, 0, REALSIZE, d, 0, NULL, &eventR);
  125. check(err, "clEnqueueReadBuffer");
  126. printf("Finishing queue...\n");
  127. clFinish(cq);
  128. printf("Data...\n");
  129. {
  130. int i;
  131. for (i=0; i<SIZE; i++) {
  132. printf("%f ", d[i]);
  133. }
  134. printf("\n");
  135. }
  136. #ifdef PROFILING
  137. #define DURATION(event,label) do { \
  138. cl_ulong t0,t1; \
  139. err = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &t0, NULL);\
  140. check(err, "clGetEventProfilingInfo");\
  141. err = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &t1, NULL);\
  142. check(err, "clGetEventProfilingInfo");\
  143. printf("Profiling %s: %lu nanoseconds\n", label, t1-t0);\
  144. } while (0);
  145. DURATION(eventW1, "first buffer writing");
  146. DURATION(eventW2, "second buffer writing");
  147. DURATION(eventK, "kernel execution");
  148. DURATION(eventR, "result buffer reading");
  149. #endif
  150. printf("Releasing events...\n");
  151. err = clReleaseEvent(eventW1);
  152. err |= clReleaseEvent(eventW2);
  153. err |= clReleaseEvent(eventK);
  154. err |= clReleaseEvent(eventR);
  155. check(err, "clReleaseCommandQueue");
  156. printf("Releasing command queue...\n");
  157. err = clReleaseCommandQueue(cq);
  158. check(err, "clReleaseCommandQueue");
  159. printf("Releasing buffers...\n");
  160. err = clReleaseMemObject(s1m);
  161. check(err, "clReleaseMemObject s1");
  162. err = clReleaseMemObject(s2m);
  163. check(err, "clReleaseMemObject s2");
  164. err = clReleaseMemObject(dm);
  165. check(err, "clReleaseMemObject d");
  166. printf("Releasing kernel...\n");
  167. err = clReleaseKernel(kernel);
  168. check(err, "clReleaseKernel");
  169. printf("Releasing program...\n");
  170. err = clReleaseProgram(program);
  171. check(err, "clReleaseProgram");
  172. printf("Releasing context...\n");
  173. err = clReleaseContext(context);
  174. check(err, "clReleaseContext");
  175. return 0;
  176. }