shadow2d.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  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, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  90. void cpu_func(void *buffers[], void *cl_arg)
  91. {
  92. /* length of the shadowed source matrix */
  93. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  94. unsigned n = STARPU_MATRIX_GET_NX(buffers[0]);
  95. unsigned m = STARPU_MATRIX_GET_NY(buffers[0]);
  96. /* local copy of the shadowed source matrix pointer */
  97. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  98. /* length of the destination matrix */
  99. unsigned ld2 = STARPU_MATRIX_GET_LD(buffers[1]);
  100. unsigned n2 = STARPU_MATRIX_GET_NX(buffers[1]);
  101. unsigned m2 = STARPU_MATRIX_GET_NY(buffers[1]);
  102. /* local copy of the destination matrix pointer */
  103. int *val2 = (int *)STARPU_MATRIX_GET_PTR(buffers[1]);
  104. unsigned i, j;
  105. /* If things go right, sizes should match */
  106. STARPU_ASSERT(n == n2);
  107. STARPU_ASSERT(m == m2);
  108. for (j = 0; j < m; j++)
  109. for (i = 0; i < n; i++)
  110. val2[j*ld2+i] = val[j*ld+i];
  111. }
  112. #ifdef STARPU_USE_CUDA
  113. void cuda_func(void *buffers[], void *cl_arg)
  114. {
  115. /* length of the shadowed source matrix */
  116. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  117. unsigned n = STARPU_MATRIX_GET_NX(buffers[0]);
  118. unsigned m = STARPU_MATRIX_GET_NY(buffers[0]);
  119. /* local copy of the shadowed source matrix pointer */
  120. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  121. /* length of the destination matrix */
  122. unsigned ld2 = STARPU_MATRIX_GET_LD(buffers[1]);
  123. unsigned n2 = STARPU_MATRIX_GET_NX(buffers[1]);
  124. unsigned m2 = STARPU_MATRIX_GET_NY(buffers[1]);
  125. /* local copy of the destination matrix pointer */
  126. int *val2 = (int *)STARPU_MATRIX_GET_PTR(buffers[1]);
  127. /* If things go right, sizes should match */
  128. STARPU_ASSERT(n == n2);
  129. STARPU_ASSERT(m == m2);
  130. cudaMemcpy2DAsync(val2, ld2*sizeof(*val2), val, ld*sizeof(*val), n*sizeof(*val), m, cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  131. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  132. }
  133. #endif
  134. int main(int argc, char **argv)
  135. {
  136. unsigned i, j, k, l;
  137. int matrix[NY + 2*SHADOWY][NX + 2*SHADOWX];
  138. int matrix2[NY + PARTSY*2*SHADOWY][NX + PARTSX*2*SHADOWX];
  139. starpu_data_handle_t handle, handle2;
  140. int ret;
  141. struct starpu_codelet cl =
  142. {
  143. .cpu_funcs = {cpu_func, NULL},
  144. #ifdef STARPU_USE_CUDA
  145. .cuda_funcs = {cuda_func, NULL},
  146. #endif
  147. .nbuffers = 2,
  148. .modes = {STARPU_R, STARPU_W}
  149. };
  150. memset(matrix, -1, sizeof(matrix));
  151. for(j=1 ; j<=NY ; j++)
  152. for(i=1 ; i<=NX ; i++)
  153. matrix[SHADOWY+j-1][SHADOWX+i-1] = i+j;
  154. /* Copy borders */
  155. for (j = SHADOWY ; j<SHADOWY+NY ; j++)
  156. for(i=0 ; i<SHADOWX ; i++) {
  157. matrix[j][i] = matrix[j][i+NX];
  158. matrix[j][SHADOWX+NX+i] = matrix[j][SHADOWX+i];
  159. }
  160. for(j=0 ; j<SHADOWY ; j++)
  161. for(i=SHADOWX ; i<SHADOWX+NX ; i++) {
  162. matrix[j][i] = matrix[j+NY][i];
  163. matrix[SHADOWY+NY+j][i] = matrix[SHADOWY+j][i];
  164. }
  165. /* Copy corners */
  166. for(j=0 ; j<SHADOWY ; j++)
  167. for(i=0 ; i<SHADOWX ; i++) {
  168. matrix[j][i] = matrix[j+NY][i+NX];
  169. matrix[j][SHADOWX+NX+i] = matrix[j+NY][SHADOWX+i];
  170. matrix[SHADOWY+NY+j][i] = matrix[SHADOWY+j][i+NX];
  171. matrix[SHADOWY+NY+j][SHADOWX+NX+i] = matrix[SHADOWY+j][SHADOWX+i];
  172. }
  173. FPRINTF(stderr,"IN Matrix:\n");
  174. for(j=0 ; j<NY + 2*SHADOWY ; j++)
  175. {
  176. for(i=0 ; i<NX + 2*SHADOWX ; i++)
  177. FPRINTF(stderr, "%5d ", matrix[j][i]);
  178. FPRINTF(stderr,"\n");
  179. }
  180. FPRINTF(stderr,"\n");
  181. ret = starpu_init(NULL);
  182. if (ret == -ENODEV)
  183. exit(77);
  184. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  185. /* Declare source matrix to StarPU */
  186. starpu_matrix_data_register(&handle, 0, (uintptr_t)matrix, NX + 2*SHADOWX, NX + 2*SHADOWX, NY + 2*SHADOWY, sizeof(matrix[0][0]));
  187. /* Declare destination matrix to StarPU */
  188. starpu_matrix_data_register(&handle2, 0, (uintptr_t)matrix2, NX + PARTSX*2*SHADOWX, NX + PARTSX*2*SHADOWX, NY + PARTSY*2*SHADOWY, sizeof(matrix2[0][0]));
  189. /* Partition the source matrix in PARTSY*PARTSX sub-matrices with shadows */
  190. /* NOTE: the resulting handles should only be used in read-only mode,
  191. * as StarPU will not know how the overlapping parts would have to be
  192. * combined. */
  193. struct starpu_data_filter fy =
  194. {
  195. .filter_func = starpu_matrix_filter_vertical_block_shadow,
  196. .nchildren = PARTSY,
  197. .filter_arg_ptr = (void*)(uintptr_t) SHADOWY /* Shadow width */
  198. };
  199. struct starpu_data_filter fx =
  200. {
  201. .filter_func = starpu_matrix_filter_block_shadow,
  202. .nchildren = PARTSX,
  203. .filter_arg_ptr = (void*)(uintptr_t) SHADOWX /* Shadow width */
  204. };
  205. starpu_data_map_filters(handle, 2, &fy, &fx);
  206. /* Partition the destination matrix in PARTSY*PARTSX sub-matrices */
  207. struct starpu_data_filter fy2 =
  208. {
  209. .filter_func = starpu_matrix_filter_vertical_block,
  210. .nchildren = PARTSY,
  211. };
  212. struct starpu_data_filter fx2 =
  213. {
  214. .filter_func = starpu_matrix_filter_block,
  215. .nchildren = PARTSX,
  216. };
  217. starpu_data_map_filters(handle2, 2, &fy2, &fx2);
  218. /* Submit a task on each sub-matrix */
  219. for (j=0; j<PARTSY; j++)
  220. {
  221. for (i=0; i<PARTSX; i++)
  222. {
  223. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 2, j, i);
  224. starpu_data_handle_t sub_handle2 = starpu_data_get_sub_data(handle2, 2, j, i);
  225. struct starpu_task *task = starpu_task_create();
  226. task->handles[0] = sub_handle;
  227. task->handles[1] = sub_handle2;
  228. task->cl = &cl;
  229. task->synchronous = 1;
  230. ret = starpu_task_submit(task);
  231. if (ret == -ENODEV) goto enodev;
  232. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  233. }
  234. }
  235. starpu_data_unpartition(handle, 0);
  236. starpu_data_unpartition(handle2, 0);
  237. starpu_data_unregister(handle);
  238. starpu_data_unregister(handle2);
  239. starpu_shutdown();
  240. FPRINTF(stderr,"OUT Matrix:\n");
  241. for(j=0 ; j<NY + PARTSY*2*SHADOWY ; j++)
  242. {
  243. for(i=0 ; i<NX + PARTSX*2*SHADOWX ; i++)
  244. FPRINTF(stderr, "%5d ", matrix2[j][i]);
  245. FPRINTF(stderr,"\n");
  246. }
  247. FPRINTF(stderr,"\n");
  248. for(j=0 ; j<PARTSY ; j++)
  249. for(i=0 ; i<PARTSX ; i++)
  250. for (l=0 ; l<NY/PARTSY + 2*SHADOWY ; l++)
  251. for (k=0 ; k<NX/PARTSX + 2*SHADOWX ; k++)
  252. STARPU_ASSERT(matrix2[j*(NY/PARTSY+2*SHADOWY)+l][i*(NX/PARTSX+2*SHADOWX)+k] == matrix[j*(NY/PARTSY)+l][i*(NX/PARTSX)+k]);
  253. return 0;
  254. enodev:
  255. FPRINTF(stderr, "WARNING: No one can execute this task\n");
  256. starpu_shutdown();
  257. return 77;
  258. }