matrix_as_vector.c 6.9 KB

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