fplan_notautomatic.c 6.9 KB

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