max_fpga_advanced_static.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 determining statically whether data is in CPU
  22. * memory or DFE memory, and using the advanced Maxeler interface */
  23. #include "MyTasks.h"
  24. #include <MaxSLiCInterface.h>
  25. #define SIZE (192/sizeof(int32_t))
  26. void fpga_impl(void *buffers[], void *cl_arg)
  27. {
  28. (void)cl_arg;
  29. int32_t *ptrA = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[0]);
  30. int32_t *ptrB = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[1]);
  31. int32_t *ptrC = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[2]);
  32. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  33. // XXX: would rather use a scratch buffer
  34. size_t ptrCT1 = 0x00000000000000c0;
  35. size_t ptrAT2 = ptrCT1;
  36. size_t ptrBT2 = ptrCT1;
  37. size_t ptrCT2 = 0x0000000000000180;
  38. size_t ptrAT3 = ptrCT2;
  39. size_t ptrBT3 = ptrCT2;
  40. max_engine_t *engine = starpu_fpga_get_local_engine();;
  41. printf("Loading DFE memory.\n");
  42. /* C = A+B */
  43. MyTasks_interfaceT1_actions_t actT1 = {
  44. .param_N = size,
  45. .param_ptrC1 = ptrCT1,
  46. .instream_inAT1 = ptrA,
  47. .instream_inBT1 = ptrB,
  48. };
  49. MyTasks_interfaceT1_run(engine, &actT1);
  50. printf("T1 finished\n");
  51. /* C = A*B */
  52. MyTasks_interfaceT2_actions_t actT2 = {
  53. .param_N = size,
  54. .param_ptrA2 = ptrAT2,
  55. .param_ptrB2 = ptrBT2,
  56. .param_ptrC2 = ptrCT2,
  57. };
  58. MyTasks_interfaceT2_run(engine, &actT2);
  59. printf("T2 finished\n");
  60. /* C = A+B */
  61. MyTasks_interfaceT3_actions_t actT3 = {
  62. .param_N = size,
  63. .param_ptrA3 = ptrAT3,
  64. .param_ptrB3 = ptrBT3,
  65. .outstream_outCT3 = ptrC,
  66. };
  67. MyTasks_interfaceT3_run(engine, &actT3);
  68. printf("T3 finished\n");
  69. printf("Running DFE.\n");
  70. }
  71. static struct starpu_codelet cl =
  72. {
  73. .fpga_funcs = {fpga_impl},
  74. .nbuffers = 3,
  75. .modes = {STARPU_R, STARPU_R, STARPU_W},
  76. .specific_nodes = 1,
  77. .nodes = {STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_CPU},
  78. };
  79. void fpga_impl1(void *buffers[], void *cl_arg)
  80. {
  81. (void)cl_arg;
  82. int32_t *ptrA = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[0]);
  83. int32_t *ptrB = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[1]);
  84. size_t ptrC = (size_t) STARPU_VECTOR_GET_PTR(buffers[2]); /* FPGA */
  85. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  86. max_engine_t *engine = starpu_fpga_get_local_engine();;
  87. printf("T1 with %p %p %zu\n", ptrA, ptrB, ptrC);
  88. /* C = A+B */
  89. MyTasks_interfaceT1_actions_t act = {
  90. .param_N = size,
  91. .param_ptrC1 = ptrC,
  92. .instream_inAT1 = ptrA,
  93. .instream_inBT1 = ptrB,
  94. };
  95. MyTasks_interfaceT1_run(engine, &act);
  96. printf("T1 finished\n");
  97. }
  98. static struct starpu_codelet cl1 =
  99. {
  100. .fpga_funcs = {fpga_impl1},
  101. .nbuffers = 3,
  102. .modes = {STARPU_R, STARPU_R, STARPU_W},
  103. .specific_nodes = 1,
  104. .nodes = {STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_LOCAL},
  105. };
  106. void fpga_impl2(void *buffers[], void *cl_arg)
  107. {
  108. (void)cl_arg;
  109. size_t ptrA = (size_t) STARPU_VECTOR_GET_PTR(buffers[0]); /* FPGA */
  110. size_t ptrB = (size_t) STARPU_VECTOR_GET_PTR(buffers[1]); /* FPGA */
  111. size_t ptrC = (size_t) STARPU_VECTOR_GET_PTR(buffers[2]); /* FPGA */
  112. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  113. max_engine_t *engine = starpu_fpga_get_local_engine();;
  114. printf("T2 with %zu %zu %zu\n", ptrA, ptrB, ptrC);
  115. /* C = A*B */
  116. MyTasks_interfaceT2_actions_t act = {
  117. .param_N = size,
  118. .param_ptrA2 = ptrA,
  119. .param_ptrB2 = ptrB,
  120. .param_ptrC2 = ptrC,
  121. };
  122. MyTasks_interfaceT2_run(engine, &act);
  123. printf("T2 finished\n");
  124. }
  125. static struct starpu_codelet cl2 =
  126. {
  127. .fpga_funcs = {fpga_impl2},
  128. .nbuffers = 3,
  129. .modes = {STARPU_R, STARPU_R, STARPU_W}
  130. /* local by default */
  131. };
  132. void fpga_impl3(void *buffers[], void *cl_arg)
  133. {
  134. (void)cl_arg;
  135. size_t ptrA = (size_t) STARPU_VECTOR_GET_PTR(buffers[0]); /* FPGA */
  136. size_t ptrB = (size_t) STARPU_VECTOR_GET_PTR(buffers[1]); /* FPGA */
  137. int32_t *ptrC = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[2]);
  138. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  139. max_engine_t *engine = starpu_fpga_get_local_engine();;
  140. printf("T3 with %zu %zu %p\n", ptrA, ptrB, ptrC);
  141. /* C = A+B */
  142. MyTasks_interfaceT3_actions_t act = {
  143. .param_N = size,
  144. .param_ptrA3 = ptrA,
  145. .param_ptrB3 = ptrB,
  146. .outstream_outCT3 = ptrC,
  147. };
  148. MyTasks_interfaceT3_run(engine, &act);
  149. printf("T3 finished\n");
  150. }
  151. static struct starpu_codelet cl3 =
  152. {
  153. .fpga_funcs = {fpga_impl3},
  154. .nbuffers = 3,
  155. .modes = {STARPU_R, STARPU_R, STARPU_W},
  156. .specific_nodes = 1,
  157. .nodes = {STARPU_SPECIFIC_NODE_LOCAL, STARPU_SPECIFIC_NODE_LOCAL, STARPU_SPECIFIC_NODE_CPU},
  158. };
  159. int main(int argc, char **argv)
  160. {
  161. struct starpu_conf conf;
  162. starpu_data_handle_t handle_a, handle_b, handle_ct1, handle_ct2, handle_c;
  163. int ret;
  164. struct starpu_max_load load[2];
  165. load[0].file = MyTasks_init();
  166. load[0].engine_id_pattern = "*";
  167. load[1].file = NULL;
  168. starpu_conf_init(&conf);
  169. conf.sched_policy_name = "eager";
  170. conf.calibrate = 0;
  171. conf.fpga_load = load;
  172. ret = starpu_initialize(&conf, &argc, &argv);
  173. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  174. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  175. /* Enable profiling */
  176. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  177. int32_t a[SIZE];
  178. int32_t b[SIZE];
  179. int32_t c[SIZE];
  180. int i;
  181. for(i = 0; i < SIZE; ++i)
  182. {
  183. a[i] = random() % 100;
  184. b[i] = random() % 100;
  185. }
  186. starpu_vector_data_register(&handle_a, STARPU_MAIN_RAM, (uintptr_t) &a, SIZE, sizeof(a[0]));
  187. starpu_vector_data_register(&handle_b, STARPU_MAIN_RAM, (uintptr_t) &b, SIZE, sizeof(b[0]));
  188. starpu_vector_data_register(&handle_ct1, -1, 0, SIZE, sizeof(c[0]));
  189. starpu_vector_data_register(&handle_ct2, -1, 0, SIZE, sizeof(c[0]));
  190. starpu_vector_data_register(&handle_c, STARPU_MAIN_RAM, (uintptr_t) &c, SIZE, sizeof(c[0]));
  191. #if 0
  192. ret = starpu_task_insert(&cl, STARPU_R, handle_a, STARPU_R, handle_b, STARPU_W, handle_c, STARPU_TASK_SYNCHRONOUS, 1, 0);
  193. fprintf(stderr,"task submitted %d\n", ret);
  194. #else
  195. ret = starpu_task_insert(&cl1, STARPU_R, handle_a, STARPU_R, handle_b, STARPU_W, handle_ct1, 0);
  196. fprintf(stderr,"task submitted %d\n", ret);
  197. ret = starpu_task_insert(&cl2, STARPU_R, handle_ct1, STARPU_R, handle_ct1, STARPU_W, handle_ct2, 0);
  198. fprintf(stderr,"task submitted %d\n", ret);
  199. ret = starpu_task_insert(&cl3, STARPU_R, handle_ct2, STARPU_R, handle_ct2, STARPU_W, handle_c, 0);
  200. fprintf(stderr,"task submitted %d\n", ret);
  201. #endif
  202. starpu_data_unregister(handle_a);
  203. starpu_data_unregister(handle_b);
  204. starpu_data_unregister(handle_c);
  205. ret = EXIT_SUCCESS;
  206. for (i = 0; i < SIZE; ++i)
  207. {
  208. int ct1 = a[i] + b[i];
  209. int ct2 = ct1 * ct1;
  210. int ct3 = ct2 + ct2;
  211. if (c[i] != ct3)
  212. ret = EXIT_FAILURE;
  213. if (i < 10)
  214. {
  215. printf("%d == %d\n", c[i], ct3);
  216. if (c[i] != ct3)
  217. printf("OOOPS\n");
  218. }
  219. }
  220. starpu_shutdown();
  221. if (ret == EXIT_SUCCESS)
  222. printf("OK!\n");
  223. return ret;
  224. }