max_fpga_mux.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. static max_engine_t *engine;
  26. /*
  27. * Dynamically configure multiplexer and streaming from CPU or from LMem (ignoring the other)
  28. */
  29. #define setupReadData(name) do { \
  30. if (kind##name == STARPU_CPU_RAM) { \
  31. max_route(acts, "joinIn"#name".inCPU", "joinIn"#name".join"); \
  32. max_queue_input(acts, "in"#name"CPU", ptr##name, size * sizeof(uint32_t)); \
  33. max_ignore_memctl(acts, "MemoryControllerPro0", "in"#name"LMem"); \
  34. } else { \
  35. max_route(acts, "joinIn"#name".inLMem", "joinIn"#name".join"); \
  36. max_ignore_stream(acts, "in"#name"CPU"); \
  37. max_memctl_linear(acts, "MemoryControllerPro0", "in"#name"LMem", (size_t) ptr##name, size * sizeof(int32_t)); \
  38. } \
  39. } while (0)
  40. /*
  41. * Ignore data from unused input
  42. */
  43. #define ignoreReadData(name) do { \
  44. max_route(acts, "joinIn"#name".inLMem", "joinIn"#name".join"); \
  45. max_ignore_stream(acts, "in"#name"CPU"); \
  46. max_ignore_memctl(acts, "MemoryControllerPro0", "in"#name"LMem"); \
  47. } while (0)
  48. /*
  49. * Configure demultiplexer and streaming to CPU or to LMem (ignoring the other)
  50. */
  51. #define setupWriteData(name) do { \
  52. if (kind##name == STARPU_CPU_RAM) { \
  53. max_route(acts, "forkOut"#name, "outCPU"); \
  54. max_queue_output(acts, "out"#name"CPU", ptr##name, size * sizeof(uint32_t)); \
  55. max_ignore_memctl(acts, "MemoryControllerPro0", "out"#name"LMem"); \
  56. } else { \
  57. max_route(acts, "forkOut"#name, "outLMem"); \
  58. max_ignore_stream(acts, "out"#name"CPU"); \
  59. max_memctl_linear(acts, "MemoryControllerPro0", "out"#name"LMem", (size_t) ptr##name, size * sizeof(uint32_t)); \
  60. } \
  61. } while (0)
  62. /*
  63. * Ignore data from unused output
  64. */
  65. #define ignoreWriteData(name) do { \
  66. max_route(acts, "forkOut"#name, "outLMem"); \
  67. max_ignore_stream(acts, "out"#name"CPU"); \
  68. max_ignore_memctl(acts, "MemoryControllerPro0", "out"#name"LMem"); \
  69. } while (0)
  70. void fpga_impl1(void *buffers[], void *cl_arg)
  71. {
  72. (void)cl_arg;
  73. int32_t *ptrAT1 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[0]);
  74. int32_t *ptrBT1 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[1]);
  75. int32_t *ptrCT1 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[2]);
  76. enum starpu_node_kind kindAT1 = starpu_node_get_kind(starpu_task_get_current_data_node(0));
  77. enum starpu_node_kind kindBT1 = starpu_node_get_kind(starpu_task_get_current_data_node(1));
  78. enum starpu_node_kind kindCT1 = starpu_node_get_kind(starpu_task_get_current_data_node(2));
  79. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  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. printf("T2 with %p %p %p\n", ptrAT2, ptrBT2, ptrCT2);
  118. /* C = A*B */
  119. max_actions_t *acts = max_actions_init(maxfile, NULL);
  120. max_ignore_scalar(acts, "Task1", "run_cycle_count");
  121. max_set_ticks(acts, "Task2", size);
  122. max_ignore_scalar(acts, "Task3", "run_cycle_count");
  123. setupReadData(AT2);
  124. setupReadData(BT2);
  125. setupWriteData(CT2);
  126. ignoreReadData(AT1);
  127. ignoreReadData(BT1);
  128. ignoreWriteData(CT1);
  129. ignoreReadData(AT3);
  130. ignoreReadData(BT3);
  131. ignoreWriteData(CT3);
  132. max_run(engine, acts);
  133. max_actions_free(acts);
  134. printf("T2 finished\n");
  135. }
  136. static struct starpu_codelet cl2 =
  137. {
  138. .fpga_funcs = {fpga_impl2},
  139. .nbuffers = 3,
  140. .modes = {STARPU_R, STARPU_R, STARPU_W}
  141. /* local by default */
  142. };
  143. void fpga_impl3(void *buffers[], void *cl_arg)
  144. {
  145. (void)cl_arg;
  146. int32_t *ptrAT3 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[0]);
  147. int32_t *ptrBT3 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[1]);
  148. int32_t *ptrCT3 = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[2]);
  149. enum starpu_node_kind kindAT3 = starpu_node_get_kind(starpu_task_get_current_data_node(0));
  150. enum starpu_node_kind kindBT3 = starpu_node_get_kind(starpu_task_get_current_data_node(1));
  151. enum starpu_node_kind kindCT3 = starpu_node_get_kind(starpu_task_get_current_data_node(2));
  152. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  153. printf("T3 with %p %p %p\n", ptrAT3, ptrBT3, ptrCT3);
  154. /* C = A+B */
  155. max_actions_t *acts = max_actions_init(maxfile, NULL);
  156. max_ignore_scalar(acts, "Task1", "run_cycle_count");
  157. max_ignore_scalar(acts, "Task2", "run_cycle_count");
  158. max_set_ticks(acts, "Task3", size);
  159. setupReadData(AT3);
  160. setupReadData(BT3);
  161. setupWriteData(CT3);
  162. ignoreReadData(AT1);
  163. ignoreReadData(BT1);
  164. ignoreWriteData(CT1);
  165. ignoreReadData(AT2);
  166. ignoreReadData(BT2);
  167. ignoreWriteData(CT2);
  168. max_run(engine, acts);
  169. max_actions_free(acts);
  170. printf("T3 finished\n");
  171. }
  172. static struct starpu_codelet cl3 =
  173. {
  174. .fpga_funcs = {fpga_impl3},
  175. .nbuffers = 3,
  176. .modes = {STARPU_R, STARPU_R, STARPU_W},
  177. .specific_nodes = 1,
  178. .nodes = {STARPU_SPECIFIC_NODE_LOCAL, STARPU_SPECIFIC_NODE_LOCAL, STARPU_SPECIFIC_NODE_CPU},
  179. };
  180. int main(int argc, char **argv)
  181. {
  182. /* Enable profiling */
  183. starpu_profiling_status_set(1);
  184. struct starpu_conf conf;
  185. starpu_data_handle_t handle_a, handle_b, handle_ct1, handle_ct2, handle_c;
  186. int ret;
  187. int size=1234;
  188. maxfile = MyTasksMux_init();
  189. engine = max_load(maxfile, "*");
  190. starpu_conf_init(&conf);
  191. conf.sched_policy_name = "eager";
  192. conf.calibrate = 0;
  193. ret = starpu_initialize(&conf, &argc, &argv);
  194. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  195. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  196. int32_t a[SIZE];
  197. int32_t b[SIZE];
  198. int32_t c[SIZE];
  199. int i;
  200. for(i = 0; i < SIZE; ++i)
  201. {
  202. a[i] = random() % 100;
  203. b[i] = random() % 100;
  204. }
  205. starpu_vector_data_register(&handle_a, STARPU_MAIN_RAM, (uintptr_t) &a, SIZE, sizeof(a[0]));
  206. starpu_vector_data_register(&handle_b, STARPU_MAIN_RAM, (uintptr_t) &b, SIZE, sizeof(b[0]));
  207. starpu_vector_data_register(&handle_ct1, -1, 0, SIZE, sizeof(c[0]));
  208. starpu_vector_data_register(&handle_ct2, -1, 0, SIZE, sizeof(c[0]));
  209. starpu_vector_data_register(&handle_c, STARPU_MAIN_RAM, (uintptr_t) &c, SIZE, sizeof(c[0]));
  210. ret = starpu_task_insert(&cl1, STARPU_R, handle_a, STARPU_R, handle_b, STARPU_W, handle_ct1, 0);
  211. fprintf(stderr,"task submitted %d\n", ret);
  212. ret = starpu_task_insert(&cl2, STARPU_R, handle_ct1, STARPU_R, handle_ct1, STARPU_W, handle_ct2, 0);
  213. fprintf(stderr,"task submitted %d\n", ret);
  214. ret = starpu_task_insert(&cl3, STARPU_R, handle_ct2, STARPU_R, handle_ct2, STARPU_W, handle_c, 0);
  215. fprintf(stderr,"task submitted %d\n", ret);
  216. starpu_data_unregister(handle_a);
  217. starpu_data_unregister(handle_b);
  218. starpu_data_unregister(handle_c);
  219. ret = EXIT_SUCCESS;
  220. for (i = 0; i < SIZE; ++i)
  221. {
  222. int ct1 = a[i] + b[i];
  223. int ct2 = ct1 * ct1;
  224. int ct3 = ct2 + ct2;
  225. if (c[i] != ct3)
  226. ret = EXIT_FAILURE;
  227. if (i < 10)
  228. {
  229. printf("%d == %d\n", c[i], ct3);
  230. if (c[i] != ct3)
  231. printf("OOOPS\n");
  232. }
  233. }
  234. starpu_shutdown();
  235. if (ret == EXIT_SUCCESS)
  236. printf("OK!\n");
  237. max_unload(engine);
  238. return ret;
  239. }