fplan_notautomatic.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2018, 2019 CNRS
  4. * Copyright (C) 2018 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. #include <starpu.h>
  18. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  19. #define NX 9
  20. #define PARTS 3
  21. struct starpu_codelet task_codelet;
  22. // CPU implementations
  23. void task_cpu(void *descr[], void *args)
  24. {
  25. int *values = (int*)STARPU_VECTOR_GET_PTR(descr[0]);
  26. int nx = STARPU_VECTOR_GET_NX(descr[0]);
  27. int i, add;
  28. char message[10000];
  29. int cur = 0;
  30. starpu_codelet_unpack_args(args, &add);
  31. cur += snprintf(&message[cur], 10000-cur, "Values ");
  32. for(i=0 ; i<nx ; i++)
  33. {
  34. values[i] += add;
  35. cur += snprintf(&message[cur], 10000-cur, "%d ", values[i]);
  36. }
  37. FPRINTF(stderr, "%s\n", message);
  38. }
  39. void split_callback(void *arg)
  40. {
  41. (arg);
  42. struct starpu_task *task = starpu_task_get_current();
  43. starpu_data_handle_t value_handle, sub_handles[PARTS];
  44. starpu_codelet_unpack_args(task->cl_arg, &value_handle, &sub_handles);
  45. FPRINTF(stderr, "[callback] Partition for handle %p into handles %p %p and %p\n", value_handle, sub_handles[0], sub_handles[1], sub_handles[2]);
  46. starpu_data_partition_submit_sequential_consistency(value_handle, PARTS, sub_handles, 0);
  47. }
  48. void supertask_callback(void *arg)
  49. {
  50. (arg);
  51. starpu_data_handle_t sub_handles[PARTS];
  52. int add;
  53. struct starpu_task *task = starpu_task_get_current();
  54. starpu_codelet_unpack_args(task->cl_arg, &sub_handles, &add);
  55. FPRINTF(stderr, "Submitting tasks on %d subdata (add %d)\n", PARTS, add);
  56. int i;
  57. for(i=0 ; i<PARTS ; i++)
  58. {
  59. int ret = starpu_task_insert(&task_codelet,
  60. STARPU_RW, sub_handles[i],
  61. STARPU_VALUE, &add, sizeof(add),
  62. 0);
  63. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  64. }
  65. }
  66. void release(void *arg)
  67. {
  68. struct starpu_task *task = (struct starpu_task *)arg;
  69. starpu_task_end_dep_release(task);
  70. }
  71. void merge_callback(void *arg)
  72. {
  73. (void)arg;
  74. struct starpu_task *task = starpu_task_get_current();
  75. starpu_data_handle_t value_handle, sub_handles[PARTS];
  76. starpu_codelet_unpack_args(task->cl_arg, &value_handle, &sub_handles);
  77. FPRINTF(stderr, "Unpartition for handle %p from handles %p %p and %p\n", value_handle, sub_handles[0], sub_handles[1], sub_handles[2]);
  78. starpu_data_unpartition_submit_sequential_consistency_cb(value_handle, PARTS, sub_handles, STARPU_MAIN_RAM, 0, release, task);
  79. }
  80. // Codelets
  81. struct starpu_codelet task_codelet =
  82. {
  83. .cpu_funcs = {task_cpu},
  84. .nbuffers = 1,
  85. .modes = {STARPU_RW},
  86. .name = "task_codelet"
  87. };
  88. struct starpu_codelet supertask_codelet =
  89. {
  90. .where= STARPU_NOWHERE,
  91. .nbuffers = 1,
  92. .modes = {STARPU_RW},
  93. .name = "supertask_codelet"
  94. };
  95. struct starpu_codelet split_codelet =
  96. {
  97. .where= STARPU_NOWHERE,
  98. .nbuffers = 1,
  99. .modes = {STARPU_RW},
  100. .name = "split_codelet"
  101. };
  102. struct starpu_codelet merge_codelet =
  103. {
  104. .where= STARPU_NOWHERE,
  105. .nbuffers = 1,
  106. .modes = {STARPU_RW},
  107. .name = "merge_codelet"
  108. };
  109. int main(void)
  110. {
  111. int ret, i;
  112. int values[NX];
  113. int check[NX];
  114. int add1=1;
  115. int add2=2;
  116. starpu_data_handle_t value_handle;
  117. starpu_data_handle_t sub_handles[PARTS];
  118. ret = starpu_init(NULL);
  119. if (ret == -ENODEV)
  120. exit(77);
  121. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  122. if (starpu_cpu_worker_get_count() == 0)
  123. {
  124. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  125. starpu_shutdown();
  126. return 77;
  127. }
  128. struct starpu_data_filter f =
  129. {
  130. .filter_func = starpu_vector_filter_block,
  131. .nchildren = PARTS
  132. };
  133. values[NX-1] = 2;
  134. for(i=NX-2 ; i>= 0 ; i--) values[i] = values[i+1] * 2;
  135. for(i=0 ; i<NX ; i++) check[i] = values[i] + (2 * add1) + (2 * add2);
  136. starpu_vector_data_register(&value_handle, STARPU_MAIN_RAM, (uintptr_t)&values[0], NX, sizeof(values[0]));
  137. starpu_data_partition_plan(value_handle, &f, sub_handles);
  138. // tell StarPU not to partition data, the application will decide itself when to do it
  139. starpu_data_partition_not_automatic(value_handle);
  140. for(i=0 ; i<PARTS ; i++)
  141. starpu_data_partition_not_automatic(sub_handles[i]);
  142. // insert a task on the whole data
  143. ret = starpu_task_insert(&task_codelet, STARPU_RW, value_handle,
  144. STARPU_VALUE, &add1, sizeof(add1),
  145. STARPU_NAME, "task_1", 0);
  146. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  147. // insert a task to split the data
  148. ret = starpu_task_insert(&split_codelet, STARPU_RW, value_handle,
  149. STARPU_VALUE, &value_handle, sizeof(starpu_data_handle_t),
  150. STARPU_VALUE, sub_handles, PARTS*sizeof(starpu_data_handle_t),
  151. STARPU_NAME, "split",
  152. STARPU_PROLOGUE_CALLBACK, split_callback,
  153. 0);
  154. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  155. // insert a task that will work on the subdata
  156. ret = starpu_task_insert(&supertask_codelet, STARPU_RW, value_handle,
  157. STARPU_VALUE, sub_handles, PARTS*sizeof(starpu_data_handle_t),
  158. STARPU_VALUE, &add1, sizeof(add1),
  159. STARPU_NAME, "supertask_1",
  160. STARPU_PROLOGUE_CALLBACK, supertask_callback,
  161. 0);
  162. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  163. // insert another task that will work on the subdata
  164. ret = starpu_task_insert(&supertask_codelet, STARPU_RW, value_handle,
  165. STARPU_VALUE, sub_handles, PARTS*sizeof(starpu_data_handle_t),
  166. STARPU_VALUE, &add2, sizeof(add2),
  167. STARPU_NAME, "supertask_2",
  168. STARPU_PROLOGUE_CALLBACK, supertask_callback,
  169. 0);
  170. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  171. // insert a task to merge the data
  172. ret = starpu_task_insert(&merge_codelet, STARPU_RW, value_handle,
  173. STARPU_VALUE, &value_handle, sizeof(starpu_data_handle_t),
  174. STARPU_VALUE, sub_handles, PARTS*sizeof(starpu_data_handle_t),
  175. STARPU_NAME, "merge",
  176. STARPU_PROLOGUE_CALLBACK, merge_callback,
  177. STARPU_TASK_END_DEP, 1,
  178. 0);
  179. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  180. // insert a task that will work on the whole data
  181. ret = starpu_task_insert(&task_codelet, STARPU_RW, value_handle,
  182. STARPU_VALUE, &add2, sizeof(add2),
  183. STARPU_NAME, "task_2", 0);
  184. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  185. starpu_task_wait_for_all();
  186. starpu_data_partition_clean(value_handle, PARTS, sub_handles);
  187. starpu_data_unregister(value_handle);
  188. FPRINTF(stderr, "Values : ");
  189. for(i=0 ; i<NX ; i++)
  190. {
  191. FPRINTF(stderr, "%d ", values[i]);
  192. }
  193. FPRINTF(stderr, "\n");
  194. for(i=0 ; i<NX ; i++)
  195. {
  196. if (values[i] != check[i])
  197. {
  198. FPRINTF(stderr, "Incorrect value for %d. %d != %d\n", i, values[i], check[i]);
  199. ret = 1;
  200. }
  201. }
  202. starpu_shutdown();
  203. return ret;
  204. }