fmultiple_manual.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015 Université Bordeaux
  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. /*
  17. * This examplifies how to access the same matrix with different partitioned
  18. * views.
  19. * We first run a kernel on the whole matrix to fill it, then run a kernel on
  20. * each vertical slice, then run a kernel on each horizontal slice.
  21. */
  22. #include <starpu.h>
  23. #define NX 6
  24. #define NY 6
  25. #define PARTS 2
  26. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  27. void matrix_fill(void *buffers[], void *cl_arg STARPU_ATTRIBUTE_UNUSED)
  28. {
  29. unsigned i, j;
  30. /* length of the matrix */
  31. unsigned nx = STARPU_MATRIX_GET_NX(buffers[0]);
  32. unsigned ny = STARPU_MATRIX_GET_NY(buffers[0]);
  33. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  34. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  35. for(j=0; j<ny ; j++)
  36. {
  37. for(i=0; i<nx ; i++)
  38. val[(j*ld)+i] = i+100*j;
  39. }
  40. }
  41. struct starpu_codelet cl_fill =
  42. {
  43. .cpu_funcs = {matrix_fill},
  44. .cpu_funcs_name = {"matrix_fill"},
  45. .nbuffers = 1,
  46. .modes = {STARPU_W},
  47. .name = "matrix_fill"
  48. };
  49. void fmultiple_check(void *buffers[], void *cl_arg)
  50. {
  51. int start, factor;
  52. unsigned i, j;
  53. /* length of the matrix */
  54. unsigned nx = STARPU_MATRIX_GET_NX(buffers[0]);
  55. unsigned ny = STARPU_MATRIX_GET_NY(buffers[0]);
  56. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  57. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  58. starpu_codelet_unpack_args(cl_arg, &start, &factor);
  59. for(j=0; j<ny ; j++)
  60. {
  61. for(i=0; i<nx ; i++)
  62. {
  63. STARPU_ASSERT(val[(j*ld)+i] == start + factor*((int)(i+100*j)));
  64. val[(j*ld)+i] *= 2;
  65. }
  66. }
  67. }
  68. #ifdef STARPU_USE_CUDA
  69. extern void fmultiple_check_cuda(void *buffers[], void *cl_arg);
  70. #endif
  71. struct starpu_codelet cl_check =
  72. {
  73. #ifdef STARPU_USE_CUDA
  74. .cuda_funcs = {fmultiple_check_cuda},
  75. .cuda_flags = {STARPU_CUDA_ASYNC},
  76. #else
  77. /* Only enable it on CPUs if we don't have a CUDA device, to force remote execution on the CUDA device */
  78. .cpu_funcs = {fmultiple_check},
  79. .cpu_funcs_name = {"fmultiple_check"},
  80. #endif
  81. .nbuffers = 1,
  82. .modes = {STARPU_RW},
  83. .name = "fmultiple_check"
  84. };
  85. void empty(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *cl_arg STARPU_ATTRIBUTE_UNUSED)
  86. {
  87. /* This doesn't need to do anything, it's simply used to make coherency
  88. * between the two views, by simply running on the home node of the
  89. * data, thus getting back all data pieces there. */
  90. }
  91. struct starpu_codelet cl_switch =
  92. {
  93. .cpu_funcs = {empty},
  94. .nbuffers = STARPU_VARIABLE_NBUFFERS,
  95. .name = "switch"
  96. };
  97. int main(int argc, char **argv)
  98. {
  99. unsigned j, n=1;
  100. int matrix[NX][NY];
  101. int ret, i;
  102. /* We haven't taken care otherwise */
  103. STARPU_ASSERT((NX%PARTS) == 0);
  104. STARPU_ASSERT((NY%PARTS) == 0);
  105. starpu_data_handle_t handle;
  106. starpu_data_handle_t vert_handle[PARTS];
  107. starpu_data_handle_t horiz_handle[PARTS];
  108. ret = starpu_init(NULL);
  109. if (ret == -ENODEV)
  110. return 77;
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  112. /* Declare the whole matrix to StarPU */
  113. starpu_matrix_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)matrix, NX, NX, NY, sizeof(matrix[0]));
  114. /* Also declare the vertical slices to StarPU */
  115. for (i = 0; i < PARTS; i++)
  116. {
  117. starpu_matrix_data_register(&vert_handle[i], STARPU_MAIN_RAM, (uintptr_t)&matrix[0][i*(NX/PARTS)], NX, NX/PARTS, NY, sizeof(matrix[0][0]));
  118. /* But make it invalid for now, we'll access data through the whole matrix first */
  119. starpu_data_invalidate(vert_handle[i]);
  120. }
  121. /* And the horizontal slices to StarPU */
  122. for (i = 0; i < PARTS; i++)
  123. {
  124. starpu_matrix_data_register(&horiz_handle[i], STARPU_MAIN_RAM, (uintptr_t)&matrix[i*(NY/PARTS)][0], NX, NX, NY/PARTS, sizeof(matrix[0][0]));
  125. starpu_data_invalidate(horiz_handle[i]);
  126. }
  127. /* Fill the matrix */
  128. ret = starpu_task_insert(&cl_fill, STARPU_W, handle, 0);
  129. if (ret == -ENODEV) goto enodev;
  130. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  131. /* Now switch to vertical view of the matrix */
  132. struct starpu_data_descr vert_descr[PARTS];
  133. for (i = 0; i < PARTS; i++)
  134. {
  135. vert_descr[i].handle = vert_handle[i];
  136. vert_descr[i].mode = STARPU_W;
  137. }
  138. ret = starpu_task_insert(&cl_switch, STARPU_RW, handle, STARPU_DATA_MODE_ARRAY, vert_descr, PARTS, 0);
  139. if (ret == -ENODEV) goto enodev;
  140. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  141. /* And make sure we don't accidentally access the matrix through the whole-matrix handle */
  142. starpu_data_invalidate_submit(handle);
  143. /* Check the values of the vertical slices */
  144. for (i = 0; i < PARTS; i++)
  145. {
  146. int factor = 1;
  147. int start = i*(NX/PARTS);
  148. ret = starpu_task_insert(&cl_check,
  149. STARPU_RW, vert_handle[i],
  150. STARPU_VALUE, &start, sizeof(start),
  151. STARPU_VALUE, &factor, sizeof(factor),
  152. 0);
  153. if (ret == -ENODEV) goto enodev;
  154. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  155. }
  156. /* Now switch back to total view of the matrix */
  157. for (i = 0; i < PARTS; i++)
  158. vert_descr[i].mode = STARPU_RW;
  159. ret = starpu_task_insert(&cl_switch, STARPU_DATA_MODE_ARRAY, vert_descr, PARTS, STARPU_W, handle, 0);
  160. if (ret == -ENODEV) goto enodev;
  161. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  162. /* And make sure we don't accidentally access the matrix through the vertical slices */
  163. for (i = 0; i < PARTS; i++)
  164. starpu_data_invalidate_submit(vert_handle[i]);
  165. /* And switch to horizontal view of the matrix */
  166. struct starpu_data_descr horiz_descr[PARTS];
  167. for (i = 0; i < PARTS; i++)
  168. {
  169. horiz_descr[i].handle = horiz_handle[i];
  170. horiz_descr[i].mode = STARPU_W;
  171. }
  172. ret = starpu_task_insert(&cl_switch, STARPU_RW, handle, STARPU_DATA_MODE_ARRAY, horiz_descr, PARTS, 0);
  173. if (ret == -ENODEV) goto enodev;
  174. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  175. /* And make sure we don't accidentally access the matrix through the whole-matrix handle */
  176. starpu_data_invalidate_submit(handle);
  177. /* Check the values of the horizontal slices */
  178. for (i = 0; i < PARTS; i++)
  179. {
  180. int factor = 2;
  181. int start = factor*100*i*(NY/PARTS);
  182. ret = starpu_task_insert(&cl_check,
  183. STARPU_RW, horiz_handle[i],
  184. STARPU_VALUE, &start, sizeof(start),
  185. STARPU_VALUE, &factor, sizeof(factor),
  186. 0);
  187. if (ret == -ENODEV) goto enodev;
  188. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  189. }
  190. /*
  191. * Unregister data from StarPU and shutdown It does not really matter
  192. * which view is active at unregistration here, since all views cover
  193. * the whole matrix, so it will be completely updated in the main memory.
  194. */
  195. for (i = 0; i < PARTS; i++)
  196. {
  197. starpu_data_unregister(vert_handle[i]);
  198. starpu_data_unregister(horiz_handle[i]);
  199. }
  200. starpu_data_unregister(handle);
  201. starpu_shutdown();
  202. return ret;
  203. enodev:
  204. starpu_shutdown();
  205. return 77;
  206. }