matvecmult.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <starpu_opencl.h>
  19. #include <pthread.h>
  20. #include <math.h>
  21. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  22. #ifdef STARPU_USE_OPENCL
  23. struct starpu_opencl_program opencl_code;
  24. void opencl_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  25. {
  26. cl_kernel kernel;
  27. cl_command_queue queue;
  28. int id, devid, err, n;
  29. cl_mem matrix = (cl_mem)STARPU_MATRIX_GET_PTR(descr[0]);
  30. cl_mem vector = (cl_mem)STARPU_VECTOR_GET_PTR(descr[1]);
  31. cl_mem mult = (cl_mem)STARPU_VECTOR_GET_PTR(descr[2]);
  32. int nx = STARPU_MATRIX_GET_NX(descr[0]);
  33. int ny = STARPU_MATRIX_GET_NY(descr[0]);
  34. cl_event event;
  35. id = starpu_worker_get_id();
  36. devid = starpu_worker_get_devid(id);
  37. err = starpu_opencl_load_kernel(&kernel, &queue, &opencl_code, "matVecMult", devid);
  38. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  39. n=0;
  40. err = clSetKernelArg(kernel, n++, sizeof(matrix), &matrix);
  41. err |= clSetKernelArg(kernel, n++, sizeof(vector), &vector);
  42. err |= clSetKernelArg(kernel, n++, sizeof(nx), (void*)&nx);
  43. err |= clSetKernelArg(kernel, n++, sizeof(ny), (void*)&ny);
  44. err |= clSetKernelArg(kernel, n++, sizeof(mult), &mult);
  45. if (err) STARPU_OPENCL_REPORT_ERROR(err);
  46. {
  47. size_t global=nx*ny;
  48. err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, NULL, 0, NULL, &event);
  49. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  50. }
  51. clFinish(queue);
  52. starpu_opencl_collect_stats(event);
  53. clReleaseEvent(event);
  54. starpu_opencl_release_kernel(kernel);
  55. }
  56. #endif
  57. void fillArray(float* pfData, int iSize) {
  58. int i;
  59. const float fScale = 1.0f / (float)RAND_MAX;
  60. for (i = 0; i < iSize; ++i) {
  61. pfData[i] = fScale * rand();
  62. }
  63. }
  64. void printArray(float* pfData, int iSize) {
  65. int i;
  66. for (i = 0; i < iSize; ++i) {
  67. FPRINTF(stderr, "%f ", pfData[i]);
  68. }
  69. FPRINTF(stderr, "\n");
  70. }
  71. void matVecMult(const float *matrix, const float *vector, int width, int height, float *mult) {
  72. int i, j;
  73. for (i = 0; i < height; ++i) {
  74. double sum = 0;
  75. for (j = 0; j < width; ++j) {
  76. double a = matrix[i * width + j];
  77. double b = vector[j];
  78. sum += a * b;
  79. }
  80. mult[i] = (float)sum;
  81. }
  82. }
  83. int compareL2fe(const float* reference, const float* data, const unsigned int len, const float epsilon) {
  84. float error = 0;
  85. float ref = 0;
  86. unsigned int i;
  87. for(i = 0; i < len; ++i) {
  88. float diff = reference[i] - data[i];
  89. error += diff * diff;
  90. ref += reference[i] * reference[i];
  91. }
  92. float normRef = sqrtf(ref);
  93. if (fabs(ref) < 1e-7) return 1;
  94. float normError = sqrtf(error);
  95. error = normError / normRef;
  96. return error < epsilon ? 0 : 1;
  97. }
  98. int main(int argc, char **argv)
  99. {
  100. struct starpu_codelet cl = {};
  101. struct starpu_conf conf = {
  102. .ncpus = 0,
  103. .ncuda = 0,
  104. .nopencl = 1,
  105. };
  106. /* int width=1100; */
  107. /* int height=244021; */
  108. int width=20;
  109. int height=4;
  110. float *matrix, *vector, *mult;
  111. float *correctResult;
  112. unsigned int mem_size_matrix, mem_size_vector, mem_size_mult;
  113. starpu_data_handle_t matrix_handle, vector_handle, mult_handle;
  114. int ret, submit;
  115. ret = starpu_init(&conf);
  116. if (STARPU_UNLIKELY(ret == -ENODEV)) {
  117. FPRINTF(stderr, "This application requires an OpenCL worker.\n");
  118. starpu_shutdown();
  119. return 77;
  120. }
  121. mem_size_matrix = width * height * sizeof(float);
  122. matrix = (float*)malloc(mem_size_matrix);
  123. mem_size_vector = width * sizeof(float);
  124. vector = (float*)malloc(mem_size_vector);
  125. mem_size_mult = height * sizeof(float);
  126. mult = (float*)malloc(mem_size_mult);
  127. correctResult = (float*)malloc(mem_size_mult);
  128. assert(matrix);
  129. assert(vector);
  130. assert(mult);
  131. assert(correctResult);
  132. fillArray(matrix, width*height);
  133. fillArray(vector, width);
  134. fillArray(mult, height);
  135. matVecMult(matrix, vector, width, height, correctResult);
  136. starpu_matrix_data_register(&matrix_handle, 0, (uintptr_t)matrix, width, width, height, sizeof(float));
  137. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector, width, sizeof(float));
  138. starpu_vector_data_register(&mult_handle, 0, (uintptr_t)mult, height, sizeof(float));
  139. #ifdef STARPU_USE_OPENCL
  140. starpu_opencl_load_opencl_from_file("examples/matvecmult/matvecmult_kernel.cl", &opencl_code, NULL);
  141. #endif
  142. cl.where = STARPU_OPENCL;
  143. #ifdef STARPU_USE_OPENCL
  144. cl.opencl_funcs[0] = opencl_codelet;
  145. #endif
  146. cl.nbuffers = 3;
  147. cl.model = NULL;
  148. struct starpu_task *task = starpu_task_create();
  149. task->cl = &cl;
  150. task->callback_func = NULL;
  151. task->buffers[0].handle = matrix_handle;
  152. task->buffers[0].mode = STARPU_R;
  153. task->buffers[1].handle = vector_handle;
  154. task->buffers[1].mode = STARPU_R;
  155. task->buffers[2].handle = mult_handle;
  156. task->buffers[2].mode = STARPU_RW;
  157. submit = starpu_task_submit(task);
  158. if (STARPU_UNLIKELY(submit == -ENODEV)) {
  159. FPRINTF(stderr, "No worker may execute this task. This application requires an OpenCL worker.\n");
  160. }
  161. else {
  162. starpu_task_wait_for_all();
  163. }
  164. starpu_data_unregister(matrix_handle);
  165. starpu_data_unregister(vector_handle);
  166. starpu_data_unregister(mult_handle);
  167. if (STARPU_LIKELY(submit != -ENODEV)) {
  168. int res = compareL2fe(correctResult, mult, height, 1e-6f);
  169. FPRINTF(stdout, "TEST %s\n\n", (res == 0) ? "PASSED" : "FAILED !!!");
  170. }
  171. #if 0
  172. printArray(matrix, width*height);
  173. printArray(vector, width);
  174. printArray(mult, height);
  175. #endif
  176. starpu_shutdown();
  177. return 0;
  178. }