dw_mult.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 "dw_mult.h"
  17. #define TAG(taskx, tasky) ((((unsigned long long)(taskx))<<32) | (unsigned long long)(tasky))
  18. float *A, *B, *C;
  19. starpu_data_handle A_handle, B_handle, C_handle;
  20. /*
  21. * That program should compute C = A * B
  22. *
  23. * A of size (z,y)
  24. * B of size (x,z)
  25. * C of size (x,y)
  26. |---------------|
  27. z | B |
  28. |---------------|
  29. z x
  30. |----| |---------------|
  31. | | | |
  32. | | | |
  33. | A | y | C |
  34. | | | |
  35. | | | |
  36. |----| |---------------|
  37. */
  38. void terminate(void)
  39. {
  40. fprintf(stderr, "unpartition !!\n");
  41. starpu_data_unpartition(C_handle, 0);
  42. starpu_data_unregister(C_handle);
  43. gettimeofday(&end, NULL);
  44. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  45. display_stats(timing);
  46. #ifdef CHECK_OUTPUT
  47. /* check results */
  48. /* compute C = C - AB */
  49. SGEMM("N", "N", ydim, xdim, zdim, -1.0f, A, ydim, B, zdim, 1.0f, C, ydim);
  50. /* make sure C = 0 */
  51. float err;
  52. err = SASUM(xdim*ydim, C, 1);
  53. if (err < xdim*ydim*0.001) {
  54. fprintf(stderr, "Results are OK\n");
  55. }
  56. else {
  57. fprintf(stderr, "There were errors ... err = %f\n", err);
  58. }
  59. #endif // CHECK_OUTPUT
  60. }
  61. void callback_func(void *arg)
  62. {
  63. /* do some accounting */
  64. int id = starpu_worker_get_id();
  65. flop_per_worker[id] += BLAS3_FLOP(conf.m, conf.n, conf.k);
  66. ls_per_worker[id] += BLAS3_LS(conf.m, conf.n, conf.k);
  67. }
  68. static void init_problem_data(void)
  69. {
  70. unsigned i,j;
  71. #ifdef STARPU_USE_CUDA
  72. if (pin) {
  73. starpu_data_malloc_pinned_if_possible((void **)&A, zdim*ydim*sizeof(float));
  74. starpu_data_malloc_pinned_if_possible((void **)&B, xdim*zdim*sizeof(float));
  75. starpu_data_malloc_pinned_if_possible((void **)&C, xdim*ydim*sizeof(float));
  76. } else
  77. #endif
  78. {
  79. #ifdef STARPU_HAVE_POSIX_MEMALIGN
  80. posix_memalign((void **)&A, 4096, zdim*ydim*sizeof(float));
  81. posix_memalign((void **)&B, 4096, xdim*zdim*sizeof(float));
  82. posix_memalign((void **)&C, 4096, xdim*ydim*sizeof(float));
  83. #else
  84. A = malloc(zdim*ydim*sizeof(float));
  85. B = malloc(xdim*zdim*sizeof(float));
  86. C = malloc(xdim*ydim*sizeof(float));
  87. #endif
  88. }
  89. /* fill the A and B matrices */
  90. if (norandom) {
  91. for (j=0; j < ydim; j++) {
  92. for (i=0; i < zdim; i++) {
  93. A[j+i*ydim] = (float)(i);
  94. }
  95. }
  96. for (j=0; j < zdim; j++) {
  97. for (i=0; i < xdim; i++) {
  98. B[j+i*zdim] = (float)(j);
  99. }
  100. }
  101. }
  102. else {
  103. #ifdef NORANDOM
  104. srand(2008);
  105. STARPU_ABORT();
  106. #endif
  107. for (j=0; j < ydim; j++) {
  108. for (i=0; i < zdim; i++) {
  109. A[j+i*ydim] = (float)(starpu_drand48());
  110. }
  111. }
  112. for (j=0; j < zdim; j++) {
  113. for (i=0; i < xdim; i++) {
  114. B[j+i*zdim] = (float)(starpu_drand48());
  115. }
  116. }
  117. }
  118. for (j=0; j < ydim; j++) {
  119. for (i=0; i < xdim; i++) {
  120. C[j+i*ydim] = (float)(0);
  121. }
  122. }
  123. display_memory_consumption();
  124. }
  125. static void partition_mult_data(void)
  126. {
  127. gettimeofday(&start, NULL);
  128. starpu_matrix_data_register(&A_handle, 0, (uintptr_t)A,
  129. ydim, ydim, zdim, sizeof(float));
  130. starpu_matrix_data_register(&B_handle, 0, (uintptr_t)B,
  131. zdim, zdim, xdim, sizeof(float));
  132. starpu_matrix_data_register(&C_handle, 0, (uintptr_t)C,
  133. ydim, ydim, xdim, sizeof(float));
  134. starpu_data_set_wb_mask(C_handle, 1<<0);
  135. conf.k = zdim;
  136. conf.m = ydim/nslicesy;
  137. conf.n = xdim/nslicesx;
  138. struct starpu_data_filter f;
  139. f.filter_func = starpu_vertical_block_filter_func;
  140. f.nchildren = nslicesx;
  141. f.get_nchildren = NULL;
  142. f.get_child_ops = NULL;
  143. struct starpu_data_filter f2;
  144. f2.filter_func = starpu_block_filter_func;
  145. f2.nchildren = nslicesy;
  146. f2.get_nchildren = NULL;
  147. f2.get_child_ops = NULL;
  148. starpu_data_partition(B_handle, &f);
  149. starpu_data_partition(A_handle, &f2);
  150. starpu_data_map_filters(C_handle, 2, &f, &f2);
  151. }
  152. static starpu_codelet cl = {
  153. .where = STARPU_CPU|STARPU_CUDA|STARPU_GORDON,
  154. .cpu_func = cpu_mult,
  155. #ifdef STARPU_USE_CUDA
  156. .cuda_func = cublas_mult,
  157. #endif
  158. #ifdef STARPU_USE_GORDON
  159. #ifdef SPU_FUNC_SGEMM
  160. .gordon_func = SPU_FUNC_SGEMM,
  161. #else
  162. #warning SPU_FUNC_SGEMM is not available
  163. #endif
  164. #endif
  165. .nbuffers = 3
  166. };
  167. static void launch_codelets(void)
  168. {
  169. #ifdef STARPU_USE_FXT
  170. _starpu_fxt_register_thread(0);
  171. #endif
  172. /* partition the work into slices */
  173. unsigned taskx, tasky;
  174. srand(time(NULL));
  175. /* should we use a single performance model for all archs and use an
  176. * acceleration factor ? */
  177. if (use_common_model) {
  178. cl.model = &sgemm_model_common;
  179. }
  180. else {
  181. cl.model = &sgemm_model;
  182. }
  183. for (taskx = 0; taskx < nslicesx; taskx++)
  184. {
  185. for (tasky = 0; tasky < nslicesy; tasky++)
  186. {
  187. /* A B[task] = C[task] */
  188. struct starpu_task *task = starpu_task_create();
  189. task->cl = &cl;
  190. task->cl_arg = &conf;
  191. task->cl_arg_size = sizeof(struct block_conf);
  192. task->callback_func = callback_func;
  193. task->callback_arg = NULL;
  194. starpu_tag_t tag = TAG(taskx, tasky);
  195. task->use_tag = 1;
  196. task->tag_id = tag;
  197. task->buffers[0].handle = starpu_data_get_sub_data(A_handle, 1, tasky);
  198. task->buffers[0].mode = STARPU_R;
  199. task->buffers[1].handle = starpu_data_get_sub_data(B_handle, 1, taskx);
  200. task->buffers[1].mode = STARPU_R;
  201. task->buffers[2].handle =
  202. starpu_data_get_sub_data(C_handle, 2, taskx, tasky);
  203. task->buffers[2].mode = STARPU_RW;
  204. starpu_task_submit(task);
  205. }
  206. }
  207. }
  208. int main(__attribute__ ((unused)) int argc,
  209. __attribute__ ((unused)) char **argv)
  210. {
  211. parse_args(argc, argv);
  212. /* start the runtime */
  213. starpu_init(NULL);
  214. starpu_helper_cublas_init();
  215. init_problem_data();
  216. partition_mult_data();
  217. launch_codelets();
  218. starpu_task_wait_for_all();
  219. terminate();
  220. starpu_helper_cublas_shutdown();
  221. starpu_shutdown();
  222. return 0;
  223. }