matvecmult.c 6.0 KB

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