shadow2d.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2010 Mehdi Juhoor
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /*
  18. * This examplifies the use of the matrix shadow filters: a source "matrix" of
  19. * NX*NY elements (plus 2*NX*SHADOWX+2*NY*SHADOWY+4*SHADOWX*SHADOWY wrap-around
  20. * elements) is partitioned into matrices with some shadowing, and these are
  21. * copied into a destination "matrix2" of
  22. * NRPARTSX*NPARTSY*((NX/NPARTSX+2*SHADOWX)*(NY/NPARTSY+2*SHADOWY)) elements,
  23. * partitioned in the traditionnal way, thus showing how shadowing shows up.
  24. *
  25. * For instance, with NX=NY=8, SHADOWX=SHADOWY=1, and NPARTSX=NPARTSY=4:
  26. *
  27. * matrix
  28. * 0123456789
  29. * 1234567890
  30. * 2345678901
  31. * 3456789012
  32. * 4567890123
  33. * 5678901234
  34. * 6789012345
  35. * 7890123456
  36. * 8901234567
  37. * 9012345678
  38. *
  39. * is partitioned into 4*4 pieces:
  40. *
  41. * 0123 2345 4567 6789
  42. * 1234 3456 5678 7890
  43. * 2345 4567 6789 8901
  44. * 3456 5678 7890 9012
  45. *
  46. * 2345 4567 6789 8901
  47. * 3456 5678 7890 9012
  48. * 4567 6789 8901 0123
  49. * 5678 7890 9012 1234
  50. *
  51. * 4567 6789 8901 0123
  52. * 5678 7890 9012 1234
  53. * 6789 8901 0123 2345
  54. * 7890 9012 1234 3456
  55. *
  56. * 6789 8901 0123 2345
  57. * 7890 9012 1234 3456
  58. * 8901 0123 2345 4567
  59. * 9012 1234 3456 5678
  60. *
  61. * which are copied into the 4*4 destination subparts of matrix2, thus getting in
  62. * the end:
  63. *
  64. * 0123234545676789
  65. * 1234345656787890
  66. * 2345456767898901
  67. * 3456567878909012
  68. * 2345456767898901
  69. * 3456567878909012
  70. * 4567678989010123
  71. * 5678789090121234
  72. * 4567678989010123
  73. * 5678789090121234
  74. * 6789890101232345
  75. * 7890901212343456
  76. * 6789890101232345
  77. * 7890901212343456
  78. * 8901012323454567
  79. * 9012123434565678
  80. */
  81. #include <starpu.h>
  82. /* Shadow width */
  83. #define SHADOWX 3
  84. #define SHADOWY 2
  85. #define NX 20
  86. #define NY 30
  87. #define PARTSX 2
  88. #define PARTSY 3
  89. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  90. void cpu_func(void *buffers[], void *cl_arg)
  91. {
  92. (void)cl_arg;
  93. /* length of the shadowed source matrix */
  94. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  95. unsigned n = STARPU_MATRIX_GET_NX(buffers[0]);
  96. unsigned m = STARPU_MATRIX_GET_NY(buffers[0]);
  97. /* local copy of the shadowed source matrix pointer */
  98. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  99. /* length of the destination matrix */
  100. unsigned ld2 = STARPU_MATRIX_GET_LD(buffers[1]);
  101. unsigned n2 = STARPU_MATRIX_GET_NX(buffers[1]);
  102. unsigned m2 = STARPU_MATRIX_GET_NY(buffers[1]);
  103. /* local copy of the destination matrix pointer */
  104. int *val2 = (int *)STARPU_MATRIX_GET_PTR(buffers[1]);
  105. unsigned i, j;
  106. /* If things go right, sizes should match */
  107. STARPU_ASSERT(n == n2);
  108. STARPU_ASSERT(m == m2);
  109. for (j = 0; j < m; j++)
  110. for (i = 0; i < n; i++)
  111. val2[j*ld2+i] = val[j*ld+i];
  112. }
  113. #ifdef STARPU_USE_CUDA
  114. void cuda_func(void *buffers[], void *cl_arg)
  115. {
  116. (void)cl_arg;
  117. cudaError_t cures;
  118. /* length of the shadowed source matrix */
  119. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  120. unsigned n = STARPU_MATRIX_GET_NX(buffers[0]);
  121. unsigned m = STARPU_MATRIX_GET_NY(buffers[0]);
  122. /* local copy of the shadowed source matrix pointer */
  123. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  124. /* length of the destination matrix */
  125. unsigned ld2 = STARPU_MATRIX_GET_LD(buffers[1]);
  126. unsigned n2 = STARPU_MATRIX_GET_NX(buffers[1]);
  127. unsigned m2 = STARPU_MATRIX_GET_NY(buffers[1]);
  128. /* local copy of the destination matrix pointer */
  129. int *val2 = (int *)STARPU_MATRIX_GET_PTR(buffers[1]);
  130. /* If things go right, sizes should match */
  131. STARPU_ASSERT(n == n2);
  132. STARPU_ASSERT(m == m2);
  133. cures = cudaMemcpy2DAsync(val2, ld2*sizeof(*val2), val, ld*sizeof(*val), n*sizeof(*val), m, cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  134. if (STARPU_UNLIKELY(cures)) STARPU_CUDA_REPORT_ERROR(cures);
  135. }
  136. #endif
  137. int main(void)
  138. {
  139. unsigned i, j, k, l;
  140. int matrix[NY + 2*SHADOWY][NX + 2*SHADOWX];
  141. int matrix2[NY + PARTSY*2*SHADOWY][NX + PARTSX*2*SHADOWX];
  142. starpu_data_handle_t handle, handle2;
  143. int ret;
  144. struct starpu_codelet cl =
  145. {
  146. .cpu_funcs = {cpu_func},
  147. .cpu_funcs_name = {"cpu_func"},
  148. #ifdef STARPU_USE_CUDA
  149. .cuda_funcs = {cuda_func},
  150. .cuda_flags = {STARPU_CUDA_ASYNC},
  151. #endif
  152. .nbuffers = 2,
  153. .modes = {STARPU_R, STARPU_W}
  154. };
  155. memset(matrix, -1, sizeof(matrix));
  156. for(j=1 ; j<=NY ; j++)
  157. for(i=1 ; i<=NX ; i++)
  158. matrix[SHADOWY+j-1][SHADOWX+i-1] = i+j;
  159. /* Copy borders */
  160. for (j = SHADOWY ; j<SHADOWY+NY ; j++)
  161. for(i=0 ; i<SHADOWX ; i++)
  162. {
  163. matrix[j][i] = matrix[j][i+NX];
  164. matrix[j][SHADOWX+NX+i] = matrix[j][SHADOWX+i];
  165. }
  166. for(j=0 ; j<SHADOWY ; j++)
  167. for(i=SHADOWX ; i<SHADOWX+NX ; i++)
  168. {
  169. matrix[j][i] = matrix[j+NY][i];
  170. matrix[SHADOWY+NY+j][i] = matrix[SHADOWY+j][i];
  171. }
  172. /* Copy corners */
  173. for(j=0 ; j<SHADOWY ; j++)
  174. for(i=0 ; i<SHADOWX ; i++)
  175. {
  176. matrix[j][i] = matrix[j+NY][i+NX];
  177. matrix[j][SHADOWX+NX+i] = matrix[j+NY][SHADOWX+i];
  178. matrix[SHADOWY+NY+j][i] = matrix[SHADOWY+j][i+NX];
  179. matrix[SHADOWY+NY+j][SHADOWX+NX+i] = matrix[SHADOWY+j][SHADOWX+i];
  180. }
  181. FPRINTF(stderr,"IN Matrix:\n");
  182. for(j=0 ; j<NY + 2*SHADOWY ; j++)
  183. {
  184. for(i=0 ; i<NX + 2*SHADOWX ; i++)
  185. FPRINTF(stderr, "%5d ", matrix[j][i]);
  186. FPRINTF(stderr,"\n");
  187. }
  188. FPRINTF(stderr,"\n");
  189. ret = starpu_init(NULL);
  190. if (ret == -ENODEV)
  191. exit(77);
  192. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  193. /* Declare source matrix to StarPU */
  194. starpu_matrix_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)matrix, NX + 2*SHADOWX, NX + 2*SHADOWX, NY + 2*SHADOWY, sizeof(matrix[0][0]));
  195. /* Declare destination matrix to StarPU */
  196. starpu_matrix_data_register(&handle2, STARPU_MAIN_RAM, (uintptr_t)matrix2, NX + PARTSX*2*SHADOWX, NX + PARTSX*2*SHADOWX, NY + PARTSY*2*SHADOWY, sizeof(matrix2[0][0]));
  197. /* Partition the source matrix in PARTSY*PARTSX sub-matrices with shadows */
  198. /* NOTE: the resulting handles should only be used in read-only mode,
  199. * as StarPU will not know how the overlapping parts would have to be
  200. * combined. */
  201. struct starpu_data_filter fy =
  202. {
  203. .filter_func = starpu_matrix_filter_vertical_block_shadow,
  204. .nchildren = PARTSY,
  205. .filter_arg_ptr = (void*)(uintptr_t) SHADOWY /* Shadow width */
  206. };
  207. struct starpu_data_filter fx =
  208. {
  209. .filter_func = starpu_matrix_filter_block_shadow,
  210. .nchildren = PARTSX,
  211. .filter_arg_ptr = (void*)(uintptr_t) SHADOWX /* Shadow width */
  212. };
  213. starpu_data_map_filters(handle, 2, &fy, &fx);
  214. /* Partition the destination matrix in PARTSY*PARTSX sub-matrices */
  215. struct starpu_data_filter fy2 =
  216. {
  217. .filter_func = starpu_matrix_filter_vertical_block,
  218. .nchildren = PARTSY,
  219. };
  220. struct starpu_data_filter fx2 =
  221. {
  222. .filter_func = starpu_matrix_filter_block,
  223. .nchildren = PARTSX,
  224. };
  225. starpu_data_map_filters(handle2, 2, &fy2, &fx2);
  226. /* Submit a task on each sub-matrix */
  227. for (j=0; j<PARTSY; j++)
  228. {
  229. for (i=0; i<PARTSX; i++)
  230. {
  231. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 2, j, i);
  232. starpu_data_handle_t sub_handle2 = starpu_data_get_sub_data(handle2, 2, j, i);
  233. struct starpu_task *task = starpu_task_create();
  234. task->handles[0] = sub_handle;
  235. task->handles[1] = sub_handle2;
  236. task->cl = &cl;
  237. task->synchronous = 1;
  238. ret = starpu_task_submit(task);
  239. if (ret == -ENODEV) goto enodev;
  240. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  241. }
  242. }
  243. starpu_data_unpartition(handle, STARPU_MAIN_RAM);
  244. starpu_data_unpartition(handle2, STARPU_MAIN_RAM);
  245. starpu_data_unregister(handle);
  246. starpu_data_unregister(handle2);
  247. starpu_shutdown();
  248. FPRINTF(stderr,"OUT Matrix:\n");
  249. for(j=0 ; j<NY + PARTSY*2*SHADOWY ; j++)
  250. {
  251. for(i=0 ; i<NX + PARTSX*2*SHADOWX ; i++)
  252. FPRINTF(stderr, "%5d ", matrix2[j][i]);
  253. FPRINTF(stderr,"\n");
  254. }
  255. FPRINTF(stderr,"\n");
  256. for(j=0 ; j<PARTSY ; j++)
  257. for(i=0 ; i<PARTSX ; i++)
  258. for (l=0 ; l<NY/PARTSY + 2*SHADOWY ; l++)
  259. for (k=0 ; k<NX/PARTSX + 2*SHADOWX ; k++)
  260. STARPU_ASSERT(matrix2[j*(NY/PARTSY+2*SHADOWY)+l][i*(NX/PARTSX+2*SHADOWX)+k] == matrix[j*(NY/PARTSY)+l][i*(NX/PARTSX)+k]);
  261. return 0;
  262. enodev:
  263. FPRINTF(stderr, "WARNING: No one can execute this task\n");
  264. starpu_shutdown();
  265. return 77;
  266. }