fplan_notautomatic.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2018 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 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_cpu(void *descr[], void *args)
  39. {
  40. (void)descr;
  41. // starpu_data_handle_t data_handle = starpu_data_lookup((void*)STARPU_VECTOR_GET_PTR(descr[0]));
  42. starpu_data_handle_t value_handle, sub_handles[PARTS];
  43. starpu_codelet_unpack_args(args, &value_handle, &sub_handles);
  44. FPRINTF(stderr, "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_cpu(void *descr[], void *args)
  48. {
  49. (void)descr;
  50. // starpu_data_handle_t data_handle = starpu_data_lookup((void*)STARPU_VECTOR_GET_PTR(descr[0]));
  51. starpu_data_handle_t sub_handles[PARTS];
  52. int add;
  53. starpu_codelet_unpack_args(args, &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 merge_cpu(void *descr[], void *args)
  66. {
  67. (void)descr;
  68. // starpu_data_handle_t value_handle = starpu_data_lookup((void*)STARPU_VECTOR_GET_PTR(descr[0]));
  69. starpu_data_handle_t value_handle, sub_handles[PARTS];
  70. starpu_codelet_unpack_args(args, &value_handle, &sub_handles);
  71. FPRINTF(stderr, "Unpartition for handle %p from handles %p %p and %p\n", value_handle, sub_handles[0], sub_handles[1], sub_handles[2]);
  72. starpu_data_unpartition_submit_sequential_consistency(value_handle, PARTS, sub_handles, STARPU_MAIN_RAM, 0);
  73. }
  74. // Codelets
  75. struct starpu_codelet task_codelet =
  76. {
  77. .cpu_funcs = {task_cpu},
  78. .nbuffers = 1,
  79. .modes = {STARPU_RW},
  80. .name = "task_codelet"
  81. };
  82. struct starpu_codelet supertask_codelet =
  83. {
  84. .cpu_funcs = {supertask_cpu},
  85. .nbuffers = 1,
  86. .modes = {STARPU_RW},
  87. .name = "supertask_codelet"
  88. };
  89. struct starpu_codelet split_codelet =
  90. {
  91. .cpu_funcs = {split_cpu},
  92. .nbuffers = 1,
  93. .modes = {STARPU_RW},
  94. .name = "split_codelet"
  95. };
  96. struct starpu_codelet merge_codelet =
  97. {
  98. .cpu_funcs = {merge_cpu},
  99. .nbuffers = 1,
  100. .modes = {STARPU_RW},
  101. .name = "merge_codelet"
  102. };
  103. int main(void)
  104. {
  105. int ret, i;
  106. int values[NX];
  107. int check[NX];
  108. int add=1;
  109. starpu_data_handle_t value_handle;
  110. starpu_data_handle_t sub_handles[PARTS];
  111. ret = starpu_init(NULL);
  112. if (ret == -ENODEV)
  113. exit(77);
  114. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  115. if (starpu_cpu_worker_get_count() == 0)
  116. {
  117. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  118. starpu_shutdown();
  119. return 77;
  120. }
  121. struct starpu_data_filter f =
  122. {
  123. .filter_func = starpu_vector_filter_block,
  124. .nchildren = PARTS
  125. };
  126. values[NX-1] = 2;
  127. for(i=NX-2 ; i>= 0 ; i--) values[i] = values[i+1] * 2;
  128. for(i=0 ; i<NX ; i++) check[i] = values[i] + (4 * add);
  129. starpu_vector_data_register(&value_handle, STARPU_MAIN_RAM, (uintptr_t)&values[0], NX, sizeof(values[0]));
  130. starpu_data_partition_plan(value_handle, &f, sub_handles);
  131. // tell StarPU not to partition data, the application will decide itself when to do it
  132. starpu_data_partition_not_automatic(value_handle);
  133. for(i=0 ; i<PARTS ; i++)
  134. starpu_data_partition_not_automatic(sub_handles[i]);
  135. // insert a task on the whole data
  136. ret = starpu_task_insert(&task_codelet, STARPU_RW, value_handle,
  137. STARPU_VALUE, &add, sizeof(add),
  138. STARPU_NAME, "task_1", 0);
  139. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  140. // insert a task to split the data
  141. ret = starpu_task_insert(&split_codelet, STARPU_RW, value_handle,
  142. STARPU_VALUE, &value_handle, sizeof(starpu_data_handle_t),
  143. STARPU_VALUE, sub_handles, PARTS*sizeof(starpu_data_handle_t),
  144. STARPU_NAME, "split", 0);
  145. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  146. // insert a task that will work on the subdata
  147. ret = starpu_task_insert(&supertask_codelet, STARPU_RW, value_handle,
  148. STARPU_VALUE, sub_handles, PARTS*sizeof(starpu_data_handle_t),
  149. STARPU_VALUE, &add, sizeof(add),
  150. STARPU_NAME, "supertask_1", 0);
  151. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  152. // insert another task that will work on the subdata
  153. ret = starpu_task_insert(&supertask_codelet, STARPU_RW, value_handle,
  154. STARPU_VALUE, sub_handles, PARTS*sizeof(starpu_data_handle_t),
  155. STARPU_VALUE, &add, sizeof(add),
  156. STARPU_NAME, "supertask_2", 0);
  157. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  158. // insert a task to merge the data
  159. ret = starpu_task_insert(&merge_codelet, STARPU_RW, value_handle,
  160. STARPU_VALUE, &value_handle, sizeof(starpu_data_handle_t),
  161. STARPU_VALUE, sub_handles, PARTS*sizeof(starpu_data_handle_t),
  162. STARPU_NAME, "merge", 0);
  163. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  164. // insert a task that will work on the whole data
  165. ret = starpu_task_insert(&task_codelet, STARPU_RW, value_handle,
  166. STARPU_VALUE, &add, sizeof(add),
  167. STARPU_NAME, "task_2", 0);
  168. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  169. starpu_task_wait_for_all();
  170. starpu_data_partition_clean(value_handle, PARTS, sub_handles);
  171. starpu_data_unregister(value_handle);
  172. FPRINTF(stderr, "Values : ");
  173. for(i=0 ; i<NX ; i++)
  174. {
  175. FPRINTF(stderr, "%d ", values[i]);
  176. }
  177. FPRINTF(stderr, "\n");
  178. for(i=0 ; i<NX ; i++)
  179. {
  180. if (values[i] != check[i])
  181. {
  182. FPRINTF(stderr, "Incorrect value for %d. %d != %d\n", i, values[i], check[i]);
  183. ret = 1;
  184. }
  185. }
  186. starpu_shutdown();
  187. return ret;
  188. }