max_fpga_mux.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2019-2021 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. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <starpu_scheduler.h>
  20. #include "../helper.h"
  21. /* This examples shows the case of letting the runtime determing whether data
  22. * should be in CPU memory or DFE memory, by integrating a multiplexer in a
  23. * design that can be toggled dynamically. */
  24. #include "MyTasksMux.h"
  25. #include <MaxSLiCInterface.h>
  26. #define SIZE (192/sizeof(int32_t))
  27. static max_file_t *maxfile;
  28. /*
  29. * Dynamically configure multiplexer and streaming from CPU or from LMem (ignoring the other)
  30. */
  31. #define setupReadData(name) do { \
  32. if (kind##name == STARPU_CPU_RAM) { \
  33. max_route(acts, "joinIn"#name".inCPU", "joinIn"#name".join"); \
  34. max_queue_input(acts, "in"#name"CPU", ptr##name, size * sizeof(uint32_t)); \
  35. max_ignore_memctl(acts, "MemoryControllerPro0", "in"#name"LMem"); \
  36. } else { \
  37. max_route(acts, "joinIn"#name".inLMem", "joinIn"#name".join"); \
  38. max_ignore_stream(acts, "in"#name"CPU"); \
  39. max_memctl_linear(acts, "MemoryControllerPro0", "in"#name"LMem", (size_t) ptr##name, size * sizeof(int32_t)); \
  40. } \
  41. } while (0)
  42. /*
  43. * Ignore data from unused input
  44. */
  45. #define ignoreReadData(name) do { \
  46. max_route(acts, "joinIn"#name".inLMem", "joinIn"#name".join"); \
  47. max_ignore_stream(acts, "in"#name"CPU"); \
  48. max_ignore_memctl(acts, "MemoryControllerPro0", "in"#name"LMem"); \
  49. } while (0)
  50. /*
  51. * Configure demultiplexer and streaming to CPU or to LMem (ignoring the other)
  52. */
  53. #define setupWriteData(name) do { \
  54. if (kind##name == STARPU_CPU_RAM) { \
  55. max_route(acts, "forkOut"#name, "outCPU"); \
  56. max_queue_output(acts, "out"#name"CPU", ptr##name, size * sizeof(uint32_t)); \
  57. max_ignore_memctl(acts, "MemoryControllerPro0", "out"#name"LMem"); \
  58. } else { \
  59. max_route(acts, "forkOut"#name, "outLMem"); \
  60. max_ignore_stream(acts, "out"#name"CPU"); \
  61. max_memctl_linear(acts, "MemoryControllerPro0", "out"#name"LMem", (size_t) ptr##name, size * sizeof(uint32_t)); \
  62. } \
  63. } while (0)
  64. /*
  65. * Ignore data from unused output
  66. */
  67. #define ignoreWriteData(name) do { \
  68. max_route(acts, "forkOut"#name, "outLMem"); \
  69. max_ignore_stream(acts, "out"#name"CPU"); \
  70. max_ignore_memctl(acts, "MemoryControllerPro0", "out"#name"LMem"); \
  71. } while (0)
  72. void fpga_impl1(void *buffers[], void *cl_arg)
  73. {
  74. (void)cl_arg;
  75. int32_t *ptrAT1 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[0]);
  76. int32_t *ptrBT1 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[1]);
  77. int32_t *ptrCT1 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[2]);
  78. enum starpu_node_kind kindAT1 = starpu_node_get_kind(starpu_task_get_current_data_node(0));
  79. enum starpu_node_kind kindBT1 = starpu_node_get_kind(starpu_task_get_current_data_node(1));
  80. enum starpu_node_kind kindCT1 = starpu_node_get_kind(starpu_task_get_current_data_node(2));
  81. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  82. max_engine_t *engine = starpu_fpga_get_local_engine();;
  83. printf("T1 with %p %p %p\n", ptrAT1, ptrBT1, ptrCT1);
  84. /* C = A+B */
  85. max_actions_t *acts = max_actions_init(maxfile, NULL);
  86. max_set_ticks(acts, "Task1", size);
  87. max_ignore_scalar(acts, "Task2", "run_cycle_count");
  88. max_ignore_scalar(acts, "Task3", "run_cycle_count");
  89. setupReadData(AT1);
  90. setupReadData(BT1);
  91. setupWriteData(CT1);
  92. ignoreReadData(AT2);
  93. ignoreReadData(BT2);
  94. ignoreWriteData(CT2);
  95. ignoreReadData(AT3);
  96. ignoreReadData(BT3);
  97. ignoreWriteData(CT3);
  98. max_run(engine, acts);
  99. max_actions_free(acts);
  100. printf("T1 finished\n");
  101. }
  102. static struct starpu_codelet cl1 =
  103. {
  104. .fpga_funcs = {fpga_impl1},
  105. .nbuffers = 3,
  106. .modes = {STARPU_R, STARPU_R, STARPU_W},
  107. .specific_nodes = 1,
  108. .nodes = {STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_LOCAL},
  109. };
  110. void fpga_impl2(void *buffers[], void *cl_arg)
  111. {
  112. (void)cl_arg;
  113. int32_t *ptrAT2 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[0]);
  114. int32_t *ptrBT2 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[1]);
  115. int32_t *ptrCT2 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[2]);
  116. enum starpu_node_kind kindAT2 = starpu_node_get_kind(starpu_task_get_current_data_node(0));
  117. enum starpu_node_kind kindBT2 = starpu_node_get_kind(starpu_task_get_current_data_node(1));
  118. enum starpu_node_kind kindCT2 = starpu_node_get_kind(starpu_task_get_current_data_node(2));
  119. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  120. max_engine_t *engine = starpu_fpga_get_local_engine();;
  121. printf("T2 with %p %p %p\n", ptrAT2, ptrBT2, ptrCT2);
  122. /* C = A*B */
  123. max_actions_t *acts = max_actions_init(maxfile, NULL);
  124. max_ignore_scalar(acts, "Task1", "run_cycle_count");
  125. max_set_ticks(acts, "Task2", size);
  126. max_ignore_scalar(acts, "Task3", "run_cycle_count");
  127. setupReadData(AT2);
  128. setupReadData(BT2);
  129. setupWriteData(CT2);
  130. ignoreReadData(AT1);
  131. ignoreReadData(BT1);
  132. ignoreWriteData(CT1);
  133. ignoreReadData(AT3);
  134. ignoreReadData(BT3);
  135. ignoreWriteData(CT3);
  136. max_run(engine, acts);
  137. max_actions_free(acts);
  138. printf("T2 finished\n");
  139. }
  140. static struct starpu_codelet cl2 =
  141. {
  142. .fpga_funcs = {fpga_impl2},
  143. .nbuffers = 3,
  144. .modes = {STARPU_R, STARPU_R, STARPU_W}
  145. /* local by default */
  146. };
  147. void fpga_impl3(void *buffers[], void *cl_arg)
  148. {
  149. (void)cl_arg;
  150. int32_t *ptrAT3 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[0]);
  151. int32_t *ptrBT3 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[1]);
  152. int32_t *ptrCT3 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[2]);
  153. enum starpu_node_kind kindAT3 = starpu_node_get_kind(starpu_task_get_current_data_node(0));
  154. enum starpu_node_kind kindBT3 = starpu_node_get_kind(starpu_task_get_current_data_node(1));
  155. enum starpu_node_kind kindCT3 = starpu_node_get_kind(starpu_task_get_current_data_node(2));
  156. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  157. max_engine_t *engine = starpu_fpga_get_local_engine();;
  158. printf("T3 with %p %p %p\n", ptrAT3, ptrBT3, ptrCT3);
  159. /* C = A+B */
  160. max_actions_t *acts = max_actions_init(maxfile, NULL);
  161. max_ignore_scalar(acts, "Task1", "run_cycle_count");
  162. max_ignore_scalar(acts, "Task2", "run_cycle_count");
  163. max_set_ticks(acts, "Task3", size);
  164. setupReadData(AT3);
  165. setupReadData(BT3);
  166. setupWriteData(CT3);
  167. ignoreReadData(AT1);
  168. ignoreReadData(BT1);
  169. ignoreWriteData(CT1);
  170. ignoreReadData(AT2);
  171. ignoreReadData(BT2);
  172. ignoreWriteData(CT2);
  173. max_run(engine, acts);
  174. max_actions_free(acts);
  175. printf("T3 finished\n");
  176. }
  177. static struct starpu_codelet cl3 =
  178. {
  179. .fpga_funcs = {fpga_impl3},
  180. .nbuffers = 3,
  181. .modes = {STARPU_R, STARPU_R, STARPU_W},
  182. .specific_nodes = 1,
  183. .nodes = {STARPU_SPECIFIC_NODE_LOCAL, STARPU_SPECIFIC_NODE_LOCAL, STARPU_SPECIFIC_NODE_CPU},
  184. };
  185. int main(int argc, char **argv)
  186. {
  187. struct starpu_conf conf;
  188. starpu_data_handle_t handle_a, handle_b, handle_ct1, handle_ct2, handle_c;
  189. int ret;
  190. maxfile = MyTasksMux_init();
  191. struct starpu_max_load load[2];
  192. load[0].file = maxfile;
  193. load[0].engine_id_pattern = "*";
  194. load[1].file = NULL;
  195. starpu_conf_init(&conf);
  196. conf.sched_policy_name = "eager";
  197. conf.calibrate = 0;
  198. conf.fpga_load = load;
  199. ret = starpu_initialize(&conf, &argc, &argv);
  200. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  201. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  202. /* Enable profiling */
  203. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  204. int32_t a[SIZE];
  205. int32_t b[SIZE];
  206. int32_t c[SIZE];
  207. int i;
  208. for(i = 0; i < SIZE; ++i)
  209. {
  210. a[i] = random() % 100;
  211. b[i] = random() % 100;
  212. }
  213. starpu_vector_data_register(&handle_a, STARPU_MAIN_RAM, (uintptr_t) &a, SIZE, sizeof(a[0]));
  214. starpu_vector_data_register(&handle_b, STARPU_MAIN_RAM, (uintptr_t) &b, SIZE, sizeof(b[0]));
  215. starpu_vector_data_register(&handle_ct1, -1, 0, SIZE, sizeof(c[0]));
  216. starpu_vector_data_register(&handle_ct2, -1, 0, SIZE, sizeof(c[0]));
  217. starpu_vector_data_register(&handle_c, STARPU_MAIN_RAM, (uintptr_t) &c, SIZE, sizeof(c[0]));
  218. ret = starpu_task_insert(&cl1, STARPU_R, handle_a, STARPU_R, handle_b, STARPU_W, handle_ct1, 0);
  219. fprintf(stderr,"task submitted %d\n", ret);
  220. ret = starpu_task_insert(&cl2, STARPU_R, handle_ct1, STARPU_R, handle_ct1, STARPU_W, handle_ct2, 0);
  221. fprintf(stderr,"task submitted %d\n", ret);
  222. ret = starpu_task_insert(&cl3, STARPU_R, handle_ct2, STARPU_R, handle_ct2, STARPU_W, handle_c, 0);
  223. fprintf(stderr,"task submitted %d\n", ret);
  224. starpu_data_unregister(handle_a);
  225. starpu_data_unregister(handle_b);
  226. starpu_data_unregister(handle_c);
  227. ret = EXIT_SUCCESS;
  228. for (i = 0; i < SIZE; ++i)
  229. {
  230. int ct1 = a[i] + b[i];
  231. int ct2 = ct1 * ct1;
  232. int ct3 = ct2 + ct2;
  233. if (c[i] != ct3)
  234. ret = EXIT_FAILURE;
  235. if (i < 10)
  236. {
  237. printf("%d == %d\n", c[i], ct3);
  238. if (c[i] != ct3)
  239. printf("OOOPS\n");
  240. }
  241. }
  242. starpu_shutdown();
  243. if (ret == EXIT_SUCCESS)
  244. printf("OK!\n");
  245. return ret;
  246. }