matvecmult.c 6.8 KB

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