fplan_notautomatic.c 6.7 KB

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