matvecmult.c 6.7 KB

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