matrix_as_vector.c 6.6 KB

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