max_fpga_mux.c 8.8 KB

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