matrix_as_vector.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2015,2017 CNRS
  4. * Copyright (C) 2013 Inria
  5. * Copyright (C) 2012-2017,2019 Université de Bordeaux
  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 "../helper.h"
  20. #ifdef STARPU_USE_CUDA
  21. # include <starpu_cublas_v2.h>
  22. #endif
  23. /*
  24. * Compare the efficiency of matrix and vector interfaces
  25. */
  26. #ifdef STARPU_QUICK_CHECK
  27. #define LOOPS 5
  28. #elif !defined(STARPU_LONG_CHECK)
  29. #define LOOPS 30
  30. #else
  31. #define LOOPS 100
  32. #endif
  33. void vector_cpu_func(void *descr[], void *cl_arg)
  34. {
  35. (void)cl_arg;
  36. STARPU_SKIP_IF_VALGRIND;
  37. float *matrix = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  38. int nx = STARPU_VECTOR_GET_NX(descr[0]);
  39. int i;
  40. float sum=0;
  41. for(i=0 ; i<nx ; i++) sum+=matrix[i];
  42. matrix[0] = sum/nx;
  43. }
  44. #ifdef STARPU_USE_CUDA
  45. static
  46. void vector_cuda_func(void *descr[], void *cl_arg)
  47. {
  48. (void)cl_arg;
  49. STARPU_SKIP_IF_VALGRIND;
  50. float *matrix = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  51. int nx = STARPU_VECTOR_GET_NX(descr[0]);
  52. float sum;
  53. cublasStatus_t status = cublasSasum(starpu_cublas_get_local_handle(), nx, matrix, 1, &sum);
  54. if (status != CUBLAS_STATUS_SUCCESS)
  55. STARPU_CUBLAS_REPORT_ERROR(status);
  56. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  57. sum /= nx;
  58. cudaMemcpyAsync(matrix, &sum, sizeof(matrix[0]), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  59. }
  60. #endif /* STARPU_USE_CUDA */
  61. void matrix_cpu_func(void *descr[], void *cl_arg)
  62. {
  63. (void)cl_arg;
  64. STARPU_SKIP_IF_VALGRIND;
  65. float *matrix = (float *)STARPU_MATRIX_GET_PTR(descr[0]);
  66. int nx = STARPU_MATRIX_GET_NX(descr[0]);
  67. int ny = STARPU_MATRIX_GET_NY(descr[0]);
  68. int i;
  69. float sum=0;
  70. for(i=0 ; i<nx*ny ; i++) sum+=matrix[i];
  71. matrix[0] = sum / (nx*ny);
  72. }
  73. #ifdef STARPU_USE_CUDA
  74. static
  75. void matrix_cuda_func(void *descr[], void *cl_arg)
  76. {
  77. (void)cl_arg;
  78. STARPU_SKIP_IF_VALGRIND;
  79. float *matrix = (float *)STARPU_MATRIX_GET_PTR(descr[0]);
  80. int nx = STARPU_MATRIX_GET_NX(descr[0]);
  81. int ny = STARPU_MATRIX_GET_NY(descr[0]);
  82. float sum;
  83. cublasStatus_t status = cublasSasum(starpu_cublas_get_local_handle(), nx*ny, matrix, 1, &sum);
  84. if (status != CUBLAS_STATUS_SUCCESS)
  85. STARPU_CUBLAS_REPORT_ERROR(status);
  86. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  87. sum /= nx*ny;
  88. cudaMemcpyAsync(matrix, &sum, sizeof(matrix[0]), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  89. }
  90. #endif /* STARPU_USE_CUDA */
  91. static
  92. int check_size(int nx, struct starpu_codelet *vector_codelet, struct starpu_codelet *matrix_codelet, char *device_name)
  93. {
  94. float *matrix, mean;
  95. starpu_data_handle_t vector_handle, matrix_handle;
  96. int ret, i, loop, maxloops;
  97. double vector_timing, matrix_timing;
  98. double start;
  99. double end;
  100. starpu_malloc((void **) &matrix, nx*sizeof(matrix[0]));
  101. maxloops = LOOPS;
  102. #ifdef STARPU_HAVE_VALGRIND_H
  103. if (RUNNING_ON_VALGRIND)
  104. /* computations are skipped when running on valgrind, there is no need to have several loops */
  105. maxloops=1;
  106. #endif /* STARPU_HAVE_VALGRIND_H */
  107. start = starpu_timing_now();
  108. for(loop=1 ; loop<=maxloops ; loop++)
  109. {
  110. for(i=0 ; i<nx ; i++) matrix[i] = i;
  111. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)matrix, nx, sizeof(matrix[0]));
  112. ret = starpu_task_insert(vector_codelet, STARPU_RW, vector_handle, 0);
  113. starpu_data_unregister(vector_handle);
  114. if (ret == -ENODEV) goto end;
  115. }
  116. end = starpu_timing_now();
  117. vector_timing = end - start;
  118. vector_timing /= maxloops;
  119. mean = matrix[0];
  120. start = starpu_timing_now();
  121. for(loop=1 ; loop<=maxloops ; loop++)
  122. {
  123. for(i=0 ; i<nx ; i++) matrix[i] = i;
  124. starpu_matrix_data_register(&matrix_handle, STARPU_MAIN_RAM, (uintptr_t)matrix, nx/2, nx/2, 2, sizeof(matrix[0]));
  125. ret = starpu_task_insert(matrix_codelet, STARPU_RW, matrix_handle, 0);
  126. starpu_data_unregister(matrix_handle);
  127. if (ret == -ENODEV) goto end;
  128. }
  129. end = starpu_timing_now();
  130. matrix_timing = end - start;
  131. matrix_timing /= maxloops;
  132. if (fabs(mean - matrix[0]) < 0.00001)
  133. {
  134. fprintf(stderr, "%d\t%f\t%f\n", nx, vector_timing, matrix_timing);
  135. {
  136. char *output_dir = getenv("STARPU_BENCH_DIR");
  137. char *bench_id = getenv("STARPU_BENCH_ID");
  138. if (output_dir && bench_id)
  139. {
  140. char file[1024];
  141. FILE *f;
  142. snprintf(file, sizeof(file), "%s/matrix_as_vector_%s.dat", output_dir, device_name);
  143. f = fopen(file, "a");
  144. fprintf(f, "%s\t%d\t%f\t%f\n", bench_id, nx, vector_timing, matrix_timing);
  145. fclose(f);
  146. }
  147. }
  148. ret = EXIT_SUCCESS;
  149. }
  150. else
  151. {
  152. fprintf(stderr, "# Incorrect result nx=%7d --> mean=%7f != %7f\n", nx, matrix[0], mean);
  153. ret = EXIT_FAILURE;
  154. }
  155. end:
  156. if (ret == -ENODEV)
  157. fprintf(stderr, "# Uh, ENODEV?!");
  158. starpu_free(matrix);
  159. starpu_task_wait_for_all();
  160. return ret;
  161. }
  162. #define NX_MIN 1024
  163. #define NX_MAX 1024*1024
  164. static
  165. int check_size_on_device(uint32_t where, char *device_name)
  166. {
  167. int nx, ret;
  168. struct starpu_codelet vector_codelet;
  169. struct starpu_codelet matrix_codelet;
  170. fprintf(stderr, "# Device: %s\n", device_name);
  171. fprintf(stderr, "# nx vector_timing matrix_timing\n");
  172. starpu_codelet_init(&vector_codelet);
  173. vector_codelet.modes[0] = STARPU_RW;
  174. vector_codelet.nbuffers = 1;
  175. if (where == STARPU_CPU) vector_codelet.cpu_funcs[0] = vector_cpu_func;
  176. #ifdef STARPU_USE_CUDA
  177. if (where == STARPU_CUDA)
  178. {
  179. vector_codelet.cuda_funcs[0] = vector_cuda_func;
  180. vector_codelet.cuda_flags[0] = STARPU_CUDA_ASYNC;
  181. }
  182. #endif
  183. // if (where == STARPU_OPENCL) vector_codelet.opencl_funcs[0] = vector_opencl_func;
  184. starpu_codelet_init(&matrix_codelet);
  185. matrix_codelet.modes[0] = STARPU_RW;
  186. matrix_codelet.nbuffers = 1;
  187. if (where == STARPU_CPU) matrix_codelet.cpu_funcs[0] = matrix_cpu_func;
  188. #ifdef STARPU_USE_CUDA
  189. if (where == STARPU_CUDA)
  190. {
  191. matrix_codelet.cuda_funcs[0] = matrix_cuda_func;
  192. matrix_codelet.cuda_flags[0] = STARPU_CUDA_ASYNC;
  193. }
  194. #endif
  195. // if (where == STARPU_OPENCL) matrix_codelet.opencl_funcs[0] = matrix_opencl_func;
  196. for(nx=NX_MIN ; nx<=NX_MAX ; nx*=2)
  197. {
  198. ret = check_size(nx, &vector_codelet, &matrix_codelet, device_name);
  199. if (ret != EXIT_SUCCESS) break;
  200. }
  201. return ret;
  202. }
  203. int main(void)
  204. {
  205. int ret;
  206. unsigned devices;
  207. #ifdef STARPU_USE_CUDA
  208. int cublas_version;
  209. #endif
  210. ret = starpu_init(NULL);
  211. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  212. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  213. devices = starpu_cpu_worker_get_count();
  214. if (devices)
  215. {
  216. ret = check_size_on_device(STARPU_CPU, "STARPU_CPU");
  217. if (ret) goto error;
  218. }
  219. #ifdef STARPU_USE_CUDA
  220. devices = starpu_cuda_worker_get_count();
  221. if (devices)
  222. {
  223. cublasHandle_t handle;
  224. cublasCreate(&handle);
  225. cublasGetVersion(handle, &cublas_version);
  226. cublasDestroy(handle);
  227. if (cublas_version >= 7050)
  228. {
  229. starpu_cublas_init();
  230. ret = check_size_on_device(STARPU_CUDA, "STARPU_CUDA");
  231. if (ret) goto error;
  232. starpu_cublas_shutdown();
  233. }
  234. }
  235. #endif
  236. #if 0
  237. devices = starpu_opencl_worker_get_count();
  238. if (devices)
  239. {
  240. ret = check_size_on_device(STARPU_OPENCL, "STARPU_OPENCL");
  241. if (ret) goto error;
  242. }
  243. #endif
  244. error:
  245. if (ret == -ENODEV) ret=STARPU_TEST_SKIPPED;
  246. starpu_shutdown();
  247. STARPU_RETURN(ret);
  248. }