fmultiple_submit.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2017 CNRS
  4. * Copyright (C) 2015 Université de Bordeaux
  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 how to access the same matrix with different partitioned
  19. * views, doing the coherency through partition planning.
  20. * We first run a kernel on the whole matrix to fill it, then run a kernel on
  21. * each vertical slice to check the value and multiply it by two, then run a
  22. * kernel on each horizontal slice to do the same.
  23. */
  24. #include <starpu.h>
  25. #define NX 6
  26. #define NY 6
  27. #define PARTS 2
  28. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  29. void matrix_fill(void *buffers[], void *cl_arg)
  30. {
  31. unsigned i, j;
  32. (void)cl_arg;
  33. /* length of the matrix */
  34. unsigned nx = STARPU_MATRIX_GET_NX(buffers[0]);
  35. unsigned ny = STARPU_MATRIX_GET_NY(buffers[0]);
  36. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  37. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  38. for(j=0; j<ny ; j++)
  39. {
  40. for(i=0; i<nx ; i++)
  41. val[(j*ld)+i] = i+100*j;
  42. }
  43. }
  44. struct starpu_codelet cl_fill =
  45. {
  46. .cpu_funcs = {matrix_fill},
  47. .cpu_funcs_name = {"matrix_fill"},
  48. .nbuffers = 1,
  49. .modes = {STARPU_W},
  50. .name = "matrix_fill"
  51. };
  52. void fmultiple_check_scale(void *buffers[], void *cl_arg)
  53. {
  54. int start, factor;
  55. unsigned i, j;
  56. /* length of the matrix */
  57. unsigned nx = STARPU_MATRIX_GET_NX(buffers[0]);
  58. unsigned ny = STARPU_MATRIX_GET_NY(buffers[0]);
  59. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  60. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  61. starpu_codelet_unpack_args(cl_arg, &start, &factor);
  62. for(j=0; j<ny ; j++)
  63. {
  64. for(i=0; i<nx ; i++)
  65. {
  66. STARPU_ASSERT(val[(j*ld)+i] == start + factor*((int)(i+100*j)));
  67. val[(j*ld)+i] *= 2;
  68. }
  69. }
  70. }
  71. #ifdef STARPU_USE_CUDA
  72. extern void fmultiple_check_scale_cuda(void *buffers[], void *cl_arg);
  73. #endif
  74. struct starpu_codelet cl_check_scale =
  75. {
  76. #ifdef STARPU_USE_CUDA
  77. .cuda_funcs = {fmultiple_check_scale_cuda},
  78. .cuda_flags = {STARPU_CUDA_ASYNC},
  79. #else
  80. /* Only enable it on CPUs if we don't have a CUDA device, to force remote execution on the CUDA device */
  81. .cpu_funcs = {fmultiple_check_scale},
  82. .cpu_funcs_name = {"fmultiple_check_scale"},
  83. #endif
  84. .nbuffers = 1,
  85. .modes = {STARPU_RW},
  86. .name = "fmultiple_check_scale"
  87. };
  88. int main(void)
  89. {
  90. unsigned j, n=1;
  91. int matrix[NX][NY];
  92. int ret, i;
  93. /* We haven't taken care otherwise */
  94. STARPU_ASSERT((NX%PARTS) == 0);
  95. STARPU_ASSERT((NY%PARTS) == 0);
  96. starpu_data_handle_t handle;
  97. starpu_data_handle_t vert_handle[PARTS];
  98. starpu_data_handle_t horiz_handle[PARTS];
  99. ret = starpu_init(NULL);
  100. if (ret == -ENODEV)
  101. return 77;
  102. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  103. /* Declare the whole matrix to StarPU */
  104. starpu_matrix_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)matrix, NX, NX, NY, sizeof(matrix[0][0]));
  105. /* Partition the matrix in PARTS vertical slices */
  106. struct starpu_data_filter f_vert =
  107. {
  108. .filter_func = starpu_matrix_filter_block,
  109. .nchildren = PARTS
  110. };
  111. starpu_data_partition_plan(handle, &f_vert, vert_handle);
  112. /* Partition the matrix in PARTS horizontal slices */
  113. struct starpu_data_filter f_horiz =
  114. {
  115. .filter_func = starpu_matrix_filter_vertical_block,
  116. .nchildren = PARTS
  117. };
  118. starpu_data_partition_plan(handle, &f_horiz, horiz_handle);
  119. /* Fill the matrix */
  120. ret = starpu_task_insert(&cl_fill, STARPU_W, handle, 0);
  121. if (ret == -ENODEV) goto enodev;
  122. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  123. /* Now switch to vertical view of the matrix */
  124. starpu_data_partition_submit(handle, PARTS, vert_handle);
  125. /* Check the values of the vertical slices */
  126. for (i = 0; i < PARTS; i++)
  127. {
  128. int factor = 1;
  129. int start = i*(NX/PARTS);
  130. ret = starpu_task_insert(&cl_check_scale,
  131. STARPU_RW, vert_handle[i],
  132. STARPU_VALUE, &start, sizeof(start),
  133. STARPU_VALUE, &factor, sizeof(factor),
  134. 0);
  135. if (ret == -ENODEV) goto enodev;
  136. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  137. }
  138. /* Now switch back to total view of the matrix */
  139. starpu_data_unpartition_submit(handle, PARTS, vert_handle, -1);
  140. /* And switch to horizontal view of the matrix */
  141. starpu_data_partition_submit(handle, PARTS, horiz_handle);
  142. /* Check the values of the horizontal slices */
  143. for (i = 0; i < PARTS; i++)
  144. {
  145. int factor = 2;
  146. int start = factor*100*i*(NY/PARTS);
  147. ret = starpu_task_insert(&cl_check_scale,
  148. STARPU_RW, horiz_handle[i],
  149. STARPU_VALUE, &start, sizeof(start),
  150. STARPU_VALUE, &factor, sizeof(factor),
  151. 0);
  152. if (ret == -ENODEV) goto enodev;
  153. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  154. }
  155. /* Now switch back to total view of the matrix */
  156. starpu_data_unpartition_submit(handle, PARTS, horiz_handle, -1);
  157. /* And check the values of the whole matrix */
  158. int factor = 4;
  159. int start = 0;
  160. ret = starpu_task_insert(&cl_check_scale,
  161. STARPU_RW, handle,
  162. STARPU_VALUE, &start, sizeof(start),
  163. STARPU_VALUE, &factor, sizeof(factor),
  164. 0);
  165. if (ret == -ENODEV) goto enodev;
  166. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  167. /*
  168. * Unregister data from StarPU and shutdown.
  169. */
  170. starpu_data_partition_clean(handle, PARTS, vert_handle);
  171. starpu_data_partition_clean(handle, PARTS, horiz_handle);
  172. starpu_data_unregister(handle);
  173. starpu_shutdown();
  174. return ret;
  175. enodev:
  176. starpu_shutdown();
  177. return 77;
  178. }