matvecmult.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 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. #ifdef STARPU_USE_OPENCL
  22. struct starpu_opencl_program opencl_code;
  23. void opencl_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  24. {
  25. cl_kernel kernel;
  26. cl_command_queue queue;
  27. int id, devid, err, n;
  28. float *matrix = (float *)STARPU_MATRIX_GET_PTR(descr[0]);
  29. float *vector = (float *)STARPU_VECTOR_GET_PTR(descr[1]);
  30. float *mult = (float *)STARPU_VECTOR_GET_PTR(descr[2]);
  31. int nx = STARPU_MATRIX_GET_NX(descr[0]);
  32. int ny = STARPU_MATRIX_GET_NY(descr[0]);
  33. cl_event event;
  34. id = starpu_worker_get_id();
  35. devid = starpu_worker_get_devid(id);
  36. err = starpu_opencl_load_kernel(&kernel, &queue, &opencl_code, "matVecMult", devid);
  37. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  38. n=0;
  39. err = clSetKernelArg(kernel, n++, sizeof(cl_mem), &matrix);
  40. err |= clSetKernelArg(kernel, n++, sizeof(cl_mem), &vector);
  41. err |= clSetKernelArg(kernel, n++, sizeof(int), (void*)&nx);
  42. err |= clSetKernelArg(kernel, n++, sizeof(int), (void*)&ny);
  43. err |= clSetKernelArg(kernel, n++, sizeof(cl_mem), &mult);
  44. if (err) STARPU_OPENCL_REPORT_ERROR(err);
  45. {
  46. size_t global=nx*ny;
  47. err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, NULL, 0, NULL, &event);
  48. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  49. }
  50. clFinish(queue);
  51. starpu_opencl_collect_stats(event);
  52. clReleaseEvent(event);
  53. starpu_opencl_release_kernel(kernel);
  54. }
  55. #endif
  56. void fillArray(float* pfData, int iSize) {
  57. int i;
  58. const float fScale = 1.0f / (float)RAND_MAX;
  59. for (i = 0; i < iSize; ++i) {
  60. pfData[i] = fScale * rand();
  61. }
  62. }
  63. void printArray(float* pfData, int iSize) {
  64. int i;
  65. for (i = 0; i < iSize; ++i) {
  66. fprintf(stderr, "%f ", pfData[i]);
  67. }
  68. fprintf(stderr, "\n");
  69. }
  70. void matVecMult(const float *matrix, const float *vector, int width, int height, float *mult) {
  71. int i, j;
  72. for (i = 0; i < height; ++i) {
  73. double sum = 0;
  74. for (j = 0; j < width; ++j) {
  75. double a = matrix[i * width + j];
  76. double b = vector[j];
  77. sum += a * b;
  78. }
  79. mult[i] = (float)sum;
  80. }
  81. }
  82. int compareL2fe(const float* reference, const float* data, const unsigned int len, const float epsilon) {
  83. float error = 0;
  84. float ref = 0;
  85. unsigned int i;
  86. for(i = 0; i < len; ++i) {
  87. float diff = reference[i] - data[i];
  88. error += diff * diff;
  89. ref += reference[i] * reference[i];
  90. }
  91. float normRef = sqrtf(ref);
  92. if (fabs(ref) < 1e-7) return 1;
  93. float normError = sqrtf(error);
  94. error = normError / normRef;
  95. return error < epsilon ? 0 : 1;
  96. }
  97. int main(int argc, char **argv)
  98. {
  99. starpu_codelet cl = {};
  100. struct starpu_conf conf = {
  101. .ncpus = 0,
  102. .ncuda = 0,
  103. .nopencl = 1,
  104. };
  105. //int width=1100;
  106. //int height=244021;
  107. int width=20;
  108. int height=4;
  109. float *matrix, *vector, *mult;
  110. float *correctResult;
  111. unsigned int mem_size_matrix, mem_size_vector, mem_size_mult;
  112. starpu_data_handle matrix_handle, vector_handle, mult_handle;
  113. starpu_init(&conf);
  114. mem_size_matrix = width * height * sizeof(float);
  115. matrix = (float*)malloc(mem_size_matrix);
  116. mem_size_vector = width * sizeof(float);
  117. vector = (float*)malloc(mem_size_vector);
  118. mem_size_mult = height * sizeof(float);
  119. mult = (float*)malloc(mem_size_mult);
  120. correctResult = (float*)malloc(mem_size_mult);
  121. assert(matrix);
  122. assert(vector);
  123. assert(mult);
  124. assert(correctResult);
  125. fillArray(matrix, width*height);
  126. fillArray(vector, width);
  127. fillArray(mult, height);
  128. matVecMult(matrix, vector, width, height, correctResult);
  129. starpu_matrix_data_register(&matrix_handle, 0, (uintptr_t)matrix, width, width, height, sizeof(float));
  130. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector, width, sizeof(float));
  131. starpu_vector_data_register(&mult_handle, 0, (uintptr_t)mult, height, sizeof(float));
  132. #ifdef STARPU_USE_OPENCL
  133. starpu_opencl_load_opencl_from_file("examples/matvecmult/matvecmult_kernel.cl", &opencl_code);
  134. #endif
  135. cl.where = STARPU_OPENCL;
  136. #ifdef STARPU_USE_OPENCL
  137. cl.opencl_func = opencl_codelet;
  138. #endif
  139. cl.nbuffers = 3;
  140. cl.model = NULL;
  141. struct starpu_task *task = starpu_task_create();
  142. task->cl = &cl;
  143. task->callback_func = NULL;
  144. task->buffers[0].handle = matrix_handle;
  145. task->buffers[0].mode = STARPU_R;
  146. task->buffers[1].handle = vector_handle;
  147. task->buffers[1].mode = STARPU_R;
  148. task->buffers[2].handle = mult_handle;
  149. task->buffers[2].mode = STARPU_RW;
  150. int ret = starpu_task_submit(task);
  151. if (STARPU_UNLIKELY(ret == -ENODEV)) {
  152. fprintf(stderr, "No worker may execute this task. This application requires an OpenCL worker.\n");
  153. exit(0);
  154. }
  155. starpu_task_wait_for_all();
  156. /* update the array in RAM */
  157. starpu_data_acquire(matrix_handle, STARPU_R);
  158. starpu_data_acquire(vector_handle, STARPU_R);
  159. starpu_data_acquire(mult_handle, STARPU_R);
  160. int res = compareL2fe(correctResult, mult, height, 1e-6f);
  161. printf("TEST %s\n\n", (res == 0) ? "PASSED" : "FAILED !!!");
  162. #if 0
  163. printArray(matrix, width*height);
  164. printArray(vector, width);
  165. printArray(mult, height);
  166. #endif
  167. starpu_data_release(matrix_handle);
  168. starpu_data_release(vector_handle);
  169. starpu_data_release(mult_handle);
  170. starpu_shutdown();
  171. return 0;
  172. }