max_fpga_mux.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_LOCAL_OR_CPU, STARPU_SPECIFIC_NODE_LOCAL_OR_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. .specific_nodes = 1,
  146. .nodes = {STARPU_SPECIFIC_NODE_LOCAL_OR_CPU, STARPU_SPECIFIC_NODE_LOCAL_OR_CPU, STARPU_SPECIFIC_NODE_LOCAL},
  147. };
  148. void fpga_impl3(void *buffers[], void *cl_arg)
  149. {
  150. (void)cl_arg;
  151. int32_t *ptrAT3 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[0]);
  152. int32_t *ptrBT3 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[1]);
  153. int32_t *ptrCT3 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[2]);
  154. enum starpu_node_kind kindAT3 = starpu_node_get_kind(starpu_task_get_current_data_node(0));
  155. enum starpu_node_kind kindBT3 = starpu_node_get_kind(starpu_task_get_current_data_node(1));
  156. enum starpu_node_kind kindCT3 = starpu_node_get_kind(starpu_task_get_current_data_node(2));
  157. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  158. max_engine_t *engine = starpu_fpga_get_local_engine();;
  159. printf("T3 with %p %p %p\n", ptrAT3, ptrBT3, ptrCT3);
  160. /* C = A+B */
  161. max_actions_t *acts = max_actions_init(maxfile, NULL);
  162. max_ignore_scalar(acts, "Task1", "run_cycle_count");
  163. max_ignore_scalar(acts, "Task2", "run_cycle_count");
  164. max_set_ticks(acts, "Task3", size);
  165. setupReadData(AT3);
  166. setupReadData(BT3);
  167. setupWriteData(CT3);
  168. ignoreReadData(AT1);
  169. ignoreReadData(BT1);
  170. ignoreWriteData(CT1);
  171. ignoreReadData(AT2);
  172. ignoreReadData(BT2);
  173. ignoreWriteData(CT2);
  174. max_run(engine, acts);
  175. max_actions_free(acts);
  176. printf("T3 finished\n");
  177. }
  178. static struct starpu_codelet cl3 =
  179. {
  180. .fpga_funcs = {fpga_impl3},
  181. .nbuffers = 3,
  182. .modes = {STARPU_R, STARPU_R, STARPU_W},
  183. .specific_nodes = 1,
  184. .nodes = {STARPU_SPECIFIC_NODE_LOCAL_OR_CPU, STARPU_SPECIFIC_NODE_LOCAL_OR_CPU, STARPU_SPECIFIC_NODE_CPU},
  185. };
  186. int main(int argc, char **argv)
  187. {
  188. struct starpu_conf conf;
  189. starpu_data_handle_t handle_a, handle_b, handle_ct1, handle_ct2, handle_c;
  190. int ret;
  191. maxfile = MyTasksMux_init();
  192. struct starpu_max_load load[2];
  193. load[0].file = maxfile;
  194. load[0].engine_id_pattern = "*";
  195. load[1].file = NULL;
  196. starpu_conf_init(&conf);
  197. conf.sched_policy_name = "eager";
  198. conf.calibrate = 0;
  199. conf.fpga_load = load;
  200. ret = starpu_initialize(&conf, &argc, &argv);
  201. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  202. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  203. /* Enable profiling */
  204. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  205. int32_t a[SIZE];
  206. int32_t b[SIZE];
  207. int32_t c[SIZE];
  208. int i;
  209. for(i = 0; i < SIZE; ++i)
  210. {
  211. a[i] = random() % 100;
  212. b[i] = random() % 100;
  213. }
  214. starpu_vector_data_register(&handle_a, STARPU_MAIN_RAM, (uintptr_t) &a, SIZE, sizeof(a[0]));
  215. starpu_vector_data_register(&handle_b, STARPU_MAIN_RAM, (uintptr_t) &b, SIZE, sizeof(b[0]));
  216. starpu_vector_data_register(&handle_ct1, -1, 0, SIZE, sizeof(c[0]));
  217. starpu_vector_data_register(&handle_ct2, -1, 0, SIZE, sizeof(c[0]));
  218. starpu_vector_data_register(&handle_c, STARPU_MAIN_RAM, (uintptr_t) &c, SIZE, sizeof(c[0]));
  219. ret = starpu_task_insert(&cl1, STARPU_R, handle_a, STARPU_R, handle_b, STARPU_W, handle_ct1, 0);
  220. fprintf(stderr,"task submitted %d\n", ret);
  221. ret = starpu_task_insert(&cl2, STARPU_R, handle_ct1, STARPU_R, handle_ct1, STARPU_W, handle_ct2, 0);
  222. fprintf(stderr,"task submitted %d\n", ret);
  223. ret = starpu_task_insert(&cl3, STARPU_R, handle_ct2, STARPU_R, handle_ct2, STARPU_W, handle_c, 0);
  224. fprintf(stderr,"task submitted %d\n", ret);
  225. starpu_data_unregister(handle_a);
  226. starpu_data_unregister(handle_b);
  227. starpu_data_unregister(handle_c);
  228. ret = EXIT_SUCCESS;
  229. for (i = 0; i < SIZE; ++i)
  230. {
  231. int ct1 = a[i] + b[i];
  232. int ct2 = ct1 * ct1;
  233. int ct3 = ct2 + ct2;
  234. if (c[i] != ct3)
  235. ret = EXIT_FAILURE;
  236. if (i < 10)
  237. {
  238. printf("%d == %d\n", c[i], ct3);
  239. if (c[i] != ct3)
  240. printf("OOOPS\n");
  241. }
  242. }
  243. starpu_shutdown();
  244. if (ret == EXIT_SUCCESS)
  245. printf("OK!\n");
  246. return ret;
  247. }