subdata.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012, 2013, 2015 CNRS
  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. #include <starpu.h>
  17. #define NX 6
  18. #define NY 4
  19. #define PARTS 2
  20. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  21. void cpu_func(void *buffers[], void *cl_arg)
  22. {
  23. unsigned i, j;
  24. int *factor = (int *) cl_arg;
  25. /* length of the matrix */
  26. unsigned nx = STARPU_MATRIX_GET_NX(buffers[0]);
  27. unsigned ny = STARPU_MATRIX_GET_NY(buffers[0]);
  28. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  29. /* local copy of the matrix pointer */
  30. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  31. FPRINTF(stderr, "applying factor %d\n", *factor);
  32. for(j=0; j<ny ; j++)
  33. {
  34. for(i=0; i<nx ; i++)
  35. {
  36. FPRINTF(stderr, "%4d ", val[(j*ld)+i]);
  37. val[(j*ld)+i] *= *factor;
  38. }
  39. FPRINTF(stderr,"\n");
  40. }
  41. FPRINTF(stderr,"\n");
  42. }
  43. struct starpu_codelet cl =
  44. {
  45. .cpu_funcs = {cpu_func},
  46. .cpu_funcs_name = {"cpu_func"},
  47. .nbuffers = 1,
  48. .modes = {STARPU_RW},
  49. .name = "matrix_scal"
  50. };
  51. void split_func(void *buffers[], void *cl_arg)
  52. {
  53. unsigned i, j;
  54. int *factor = (int *) cl_arg;
  55. /* length of the matrix */
  56. unsigned nx = STARPU_MATRIX_GET_NX(buffers[0]);
  57. unsigned ny = STARPU_MATRIX_GET_NY(buffers[0]);
  58. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  59. /* local copy of the matrix pointer */
  60. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  61. FPRINTF(stderr, "splitting\n");
  62. for(j=0; j<ny ; j++)
  63. {
  64. for(i=0; i<nx ; i++)
  65. {
  66. FPRINTF(stderr, "%4d ", val[(j*ld)+i]);
  67. }
  68. FPRINTF(stderr,"\n");
  69. }
  70. FPRINTF(stderr,"\n");
  71. starpu_data_handle_t submatrix = starpu_data_lookup(val);
  72. /* Partition the sub-matrix in PARTS sub-sub-matrices */
  73. struct starpu_data_filter f =
  74. {
  75. .filter_func = starpu_matrix_filter_block,
  76. .nchildren = PARTS
  77. };
  78. starpu_data_partition(submatrix, &f);
  79. /* Submit a task on each sub-vector */
  80. for (i=0; i<starpu_data_get_nb_children(submatrix); i++)
  81. {
  82. struct starpu_task *task = starpu_task_create();
  83. task->handles[0] = starpu_data_get_sub_data(submatrix, 1, i);
  84. task->cl = &cl;
  85. task->cl_arg = factor;
  86. task->cl_arg_size = sizeof(*factor);
  87. int ret = starpu_task_submit(task);
  88. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  89. }
  90. //starpu_data_unpartition(submatrix, STARPU_MAIN_RAM);
  91. }
  92. int main(int argc, char **argv)
  93. {
  94. unsigned j, n=1;
  95. int matrix[NX*NY];
  96. int ret, i;
  97. int factor = 12;
  98. FPRINTF(stderr,"IN Matrix: \n");
  99. for(j=0 ; j<NY ; j++)
  100. {
  101. for(i=0 ; i<NX ; i++)
  102. {
  103. matrix[(j*NX)+i] = n++;
  104. FPRINTF(stderr, "%4d ", matrix[(j*NX)+i]);
  105. }
  106. FPRINTF(stderr,"\n");
  107. }
  108. FPRINTF(stderr,"\n");
  109. starpu_data_handle_t handle;
  110. struct starpu_codelet split_cl =
  111. {
  112. .cpu_funcs = {split_func},
  113. .cpu_funcs_name = {"split_func"},
  114. .nbuffers = 1,
  115. .modes = {STARPU_RW},
  116. .name = "split_matrix"
  117. };
  118. ret = starpu_init(NULL);
  119. if (ret == -ENODEV)
  120. return 77;
  121. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  122. /* Declare data to StarPU */
  123. starpu_matrix_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)matrix, NX, NX, NY, sizeof(matrix[0]));
  124. /* Partition the matrix in PARTS sub-matrices */
  125. struct starpu_data_filter f =
  126. {
  127. .filter_func = starpu_matrix_filter_block,
  128. .nchildren = PARTS
  129. };
  130. starpu_data_partition(handle, &f);
  131. /* Submit a task on each sub-vector */
  132. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  133. {
  134. struct starpu_task *task = starpu_task_create();
  135. starpu_data_handle_t subdata = starpu_data_get_sub_data(handle, 1, i);
  136. task->handles[0] = subdata;
  137. task->cl = &split_cl;
  138. task->cl_arg = &factor;
  139. task->cl_arg_size = sizeof(factor);
  140. ret = starpu_task_submit(task);
  141. if (ret == -ENODEV) goto enodev;
  142. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  143. }
  144. starpu_task_wait_for_all();
  145. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  146. {
  147. starpu_data_handle_t subdata = starpu_data_get_sub_data(handle, 1, i);
  148. starpu_data_unpartition(subdata, STARPU_MAIN_RAM);
  149. }
  150. /* Unpartition the data, unregister it from StarPU and shutdown */
  151. starpu_data_unpartition(handle, STARPU_MAIN_RAM);
  152. starpu_data_unregister(handle);
  153. starpu_shutdown();
  154. /* Print result matrix */
  155. n=1;
  156. FPRINTF(stderr,"OUT Matrix: \n");
  157. for(j=0 ; j<NY ; j++)
  158. {
  159. for(i=0 ; i<NX ; i++)
  160. {
  161. FPRINTF(stderr, "%4d ", matrix[(j*NX)+i]);
  162. if (matrix[(j*NX)+i] != n*12)
  163. {
  164. FPRINTF(stderr, "Incorrect result %4d != %4d", matrix[(j*NX)+i], n*12);
  165. ret=1;
  166. }
  167. n++;
  168. }
  169. FPRINTF(stderr,"\n");
  170. }
  171. FPRINTF(stderr,"\n");
  172. return ret;
  173. enodev:
  174. starpu_shutdown();
  175. return 77;
  176. }