fmultiple_submit.c 5.7 KB

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