matvecmult.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 <starpu.h>
  17. #include <math.h>
  18. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  19. #ifdef STARPU_USE_OPENCL
  20. struct starpu_opencl_program opencl_code;
  21. void opencl_codelet(void *descr[], void *_args)
  22. {
  23. (void)_args;
  24. cl_kernel kernel;
  25. cl_command_queue queue;
  26. int id, devid, err, n;
  27. cl_mem matrix = (cl_mem)STARPU_MATRIX_GET_DEV_HANDLE(descr[0]);
  28. cl_mem vector = (cl_mem)STARPU_VECTOR_GET_DEV_HANDLE(descr[1]);
  29. cl_mem mult = (cl_mem)STARPU_VECTOR_GET_DEV_HANDLE(descr[2]);
  30. int nx = STARPU_MATRIX_GET_NX(descr[0]);
  31. int ny = STARPU_MATRIX_GET_NY(descr[0]);
  32. int ld = STARPU_MATRIX_GET_LD(descr[0]);
  33. id = starpu_worker_get_id_check();
  34. devid = starpu_worker_get_devid(id);
  35. err = starpu_opencl_load_kernel(&kernel, &queue, &opencl_code, "matVecMult", devid);
  36. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  37. n=0;
  38. err = clSetKernelArg(kernel, n++, sizeof(matrix), &matrix);
  39. err |= clSetKernelArg(kernel, n++, sizeof(vector), &vector);
  40. err |= clSetKernelArg(kernel, n++, sizeof(nx), (void*)&nx);
  41. err |= clSetKernelArg(kernel, n++, sizeof(ny), (void*)&ny);
  42. err |= clSetKernelArg(kernel, n++, sizeof(mult), &mult);
  43. err |= clSetKernelArg(kernel, n++, sizeof(ld), (void*)&ld);
  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, NULL);
  48. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  49. }
  50. starpu_opencl_release_kernel(kernel);
  51. }
  52. #endif
  53. void fillArray(float* pfData, int iSize)
  54. {
  55. int i;
  56. const float fScale = 1.0f / (float)RAND_MAX;
  57. for (i = 0; i < iSize; ++i)
  58. {
  59. pfData[i] = fScale * rand();
  60. }
  61. }
  62. #if 0
  63. void printArray(float* pfData, int iSize)
  64. {
  65. int i;
  66. for (i = 0; i < iSize; ++i)
  67. {
  68. FPRINTF(stderr, "%f ", pfData[i]);
  69. }
  70. FPRINTF(stderr, "\n");
  71. }
  72. #endif
  73. void matVecMult(const float *matrix, const float *vector, int width, int height, float *mult)
  74. {
  75. int i, j;
  76. for (i = 0; i < height; ++i)
  77. {
  78. double sum = 0;
  79. for (j = 0; j < width; ++j)
  80. {
  81. double a = matrix[i * width + j];
  82. double b = vector[j];
  83. sum += a * b;
  84. }
  85. mult[i] = (float)sum;
  86. }
  87. }
  88. int compareL2fe(const float* reference, const float* data, const unsigned int len, const float epsilon)
  89. {
  90. float error = 0;
  91. float ref = 0;
  92. unsigned int i;
  93. for(i = 0; i < len; ++i)
  94. {
  95. float diff = reference[i] - data[i];
  96. error += diff * diff;
  97. ref += reference[i] * reference[i];
  98. }
  99. float normRef = sqrtf(ref);
  100. if (fabs(ref) < 1e-7) return 1;
  101. float normError = sqrtf(error);
  102. error = normError / normRef;
  103. return error < epsilon ? 0 : 1;
  104. }
  105. static struct starpu_perfmodel starpu_matvecmult_model =
  106. {
  107. .type = STARPU_HISTORY_BASED,
  108. .symbol = "matvecmult"
  109. };
  110. static struct starpu_codelet cl =
  111. {
  112. #ifdef STARPU_USE_OPENCL
  113. .opencl_funcs = {opencl_codelet},
  114. .opencl_flags = {STARPU_OPENCL_ASYNC},
  115. #endif
  116. .nbuffers = 3,
  117. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  118. .model = &starpu_matvecmult_model
  119. };
  120. int main(void)
  121. {
  122. struct starpu_conf conf;
  123. starpu_conf_init(&conf);
  124. starpu_conf_noworker(&conf);
  125. conf.nopencl = 1;
  126. /* int width=1100; */
  127. /* int height=244021; */
  128. int width=20;
  129. int height=4;
  130. float *matrix, *vector, *mult;
  131. float *correctResult;
  132. unsigned int mem_size_matrix, mem_size_vector, mem_size_mult;
  133. starpu_data_handle_t matrix_handle, vector_handle, mult_handle;
  134. int ret, submit;
  135. ret = starpu_init(&conf);
  136. if (STARPU_UNLIKELY(ret == -ENODEV))
  137. {
  138. FPRINTF(stderr, "This application requires an OpenCL worker.\n");
  139. return 77;
  140. }
  141. mem_size_matrix = width * height * sizeof(float);
  142. matrix = (float*)malloc(mem_size_matrix);
  143. mem_size_vector = width * sizeof(float);
  144. vector = (float*)malloc(mem_size_vector);
  145. mem_size_mult = height * sizeof(float);
  146. mult = (float*)malloc(mem_size_mult);
  147. correctResult = (float*)malloc(mem_size_mult);
  148. assert(matrix);
  149. assert(vector);
  150. assert(mult);
  151. assert(correctResult);
  152. fillArray(matrix, width*height);
  153. fillArray(vector, width);
  154. fillArray(mult, height);
  155. matVecMult(matrix, vector, width, height, correctResult);
  156. starpu_matrix_data_register(&matrix_handle, STARPU_MAIN_RAM, (uintptr_t)matrix, width, width, height, sizeof(float));
  157. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector, width, sizeof(float));
  158. starpu_vector_data_register(&mult_handle, STARPU_MAIN_RAM, (uintptr_t)mult, height, sizeof(float));
  159. #ifdef STARPU_USE_OPENCL
  160. ret = starpu_opencl_load_opencl_from_file("examples/matvecmult/matvecmult_kernel.cl", &opencl_code, NULL);
  161. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  162. #endif
  163. struct starpu_task *task = starpu_task_create();
  164. task->cl = &cl;
  165. task->callback_func = NULL;
  166. task->handles[0] = matrix_handle;
  167. task->handles[1] = vector_handle;
  168. task->handles[2] = mult_handle;
  169. submit = starpu_task_submit(task);
  170. if (STARPU_UNLIKELY(submit == -ENODEV))
  171. {
  172. FPRINTF(stderr, "No worker may execute this task. This application requires an OpenCL worker.\n");
  173. }
  174. else
  175. {
  176. starpu_task_wait_for_all();
  177. }
  178. starpu_data_unregister(matrix_handle);
  179. starpu_data_unregister(vector_handle);
  180. starpu_data_unregister(mult_handle);
  181. if (STARPU_LIKELY(submit != -ENODEV))
  182. {
  183. int res = compareL2fe(correctResult, mult, height, 1e-6f);
  184. FPRINTF(stdout, "TEST %s\n\n", (res == 0) ? "PASSED" : "FAILED !!!");
  185. }
  186. #if 0
  187. printArray(matrix, width*height);
  188. printArray(vector, width);
  189. printArray(mult, height);
  190. #endif
  191. free(matrix);
  192. free(vector);
  193. free(mult);
  194. free(correctResult);
  195. starpu_shutdown();
  196. return (submit == -ENODEV) ? 77 : 0;
  197. }