max_fpga_dynamic.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 dynamic Maxeler interface */
  23. #include "MyTasks.h"
  24. #include <MaxSLiCInterface.h>
  25. #define SIZE (192/sizeof(int32_t))
  26. static max_file_t *maxfile;
  27. void fpga_impl1(void *buffers[], void *cl_arg)
  28. {
  29. (void)cl_arg;
  30. int32_t *ptrA = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[0]);
  31. int32_t *ptrB = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[1]);
  32. size_t ptrC = (size_t) STARPU_VECTOR_GET_PTR(buffers[2]); /* FPGA */
  33. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  34. max_engine_t *engine = starpu_fpga_get_local_engine();;
  35. printf("T1 with %p %p %zu\n", ptrA, ptrB, ptrC);
  36. /* C = A+B */
  37. max_actions_t *acts = max_actions_init(maxfile, NULL);
  38. max_set_ticks(acts, "Task1", size);
  39. max_ignore_scalar(acts, "Task2", "run_cycle_count");
  40. max_ignore_scalar(acts, "Task3", "run_cycle_count");
  41. max_queue_input(acts, "inAT1", ptrA, size * sizeof(uint32_t));
  42. max_queue_input(acts, "inBT1", ptrB, size * sizeof(uint32_t));
  43. max_memctl_linear(acts, "MemoryControllerPro0", "outCT1", ptrC, size * sizeof(uint32_t));
  44. max_ignore_memctl(acts, "MemoryControllerPro0", "inAT2");
  45. max_ignore_memctl(acts, "MemoryControllerPro0", "inBT2");
  46. max_ignore_memctl(acts, "MemoryControllerPro0", "outCT2");
  47. max_ignore_memctl(acts, "MemoryControllerPro0", "inAT3");
  48. max_ignore_memctl(acts, "MemoryControllerPro0", "inBT3");
  49. max_ignore_stream(acts, "outCT3");
  50. max_run(engine, acts);
  51. max_actions_free(acts);
  52. printf("T1 finished\n");
  53. }
  54. static struct starpu_codelet cl1 =
  55. {
  56. .fpga_funcs = {fpga_impl1},
  57. .nbuffers = 3,
  58. .modes = {STARPU_R, STARPU_R, STARPU_W},
  59. .specific_nodes = 1,
  60. .nodes = {STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_LOCAL},
  61. };
  62. void fpga_impl2(void *buffers[], void *cl_arg)
  63. {
  64. (void)cl_arg;
  65. size_t ptrA = (size_t) STARPU_VECTOR_GET_PTR(buffers[0]); /* FPGA */
  66. size_t ptrB = (size_t) STARPU_VECTOR_GET_PTR(buffers[1]); /* FPGA */
  67. size_t ptrC = (size_t) STARPU_VECTOR_GET_PTR(buffers[2]); /* FPGA */
  68. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  69. max_engine_t *engine = starpu_fpga_get_local_engine();;
  70. printf("T2 with %zu %zu %zu\n", ptrA, ptrB, ptrC);
  71. /* C = A*B */
  72. max_actions_t *acts = max_actions_init(maxfile, NULL);
  73. max_ignore_scalar(acts, "Task1", "run_cycle_count");
  74. max_set_ticks(acts, "Task2", size);
  75. max_ignore_scalar(acts, "Task3", "run_cycle_count");
  76. max_ignore_stream(acts, "inAT1");
  77. max_ignore_stream(acts, "inBT1");
  78. max_ignore_memctl(acts, "MemoryControllerPro0", "outCT1");
  79. max_memctl_linear(acts, "MemoryControllerPro0", "inAT2", ptrA, size * sizeof(uint32_t));
  80. max_memctl_linear(acts, "MemoryControllerPro0", "inBT2", ptrB, size * sizeof(uint32_t));
  81. max_memctl_linear(acts, "MemoryControllerPro0", "outCT2", ptrC, size * sizeof(uint32_t));
  82. max_ignore_memctl(acts, "MemoryControllerPro0", "inAT3");
  83. max_ignore_memctl(acts, "MemoryControllerPro0", "inBT3");
  84. max_ignore_stream(acts, "outCT3");
  85. max_run(engine, acts);
  86. max_actions_free(acts);
  87. printf("T2 finished\n");
  88. }
  89. static struct starpu_codelet cl2 =
  90. {
  91. .fpga_funcs = {fpga_impl2},
  92. .nbuffers = 3,
  93. .modes = {STARPU_R, STARPU_R, STARPU_W}
  94. /* local by default */
  95. };
  96. void fpga_impl3(void *buffers[], void *cl_arg)
  97. {
  98. (void)cl_arg;
  99. size_t ptrA = (size_t) STARPU_VECTOR_GET_PTR(buffers[0]); /* FPGA */
  100. size_t ptrB = (size_t) STARPU_VECTOR_GET_PTR(buffers[1]); /* FPGA */
  101. int32_t *ptrC = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[2]);
  102. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  103. max_engine_t *engine = starpu_fpga_get_local_engine();;
  104. printf("T3 with %zu %zu %p\n", ptrA, ptrB, ptrC);
  105. /* C = A+B */
  106. max_actions_t *acts = max_actions_init(maxfile, NULL);
  107. max_ignore_scalar(acts, "Task1", "run_cycle_count");
  108. max_ignore_scalar(acts, "Task2", "run_cycle_count");
  109. max_set_ticks(acts, "Task3", size);
  110. max_ignore_stream(acts, "inAT1");
  111. max_ignore_stream(acts, "inBT1");
  112. max_ignore_memctl(acts, "MemoryControllerPro0", "outCT1");
  113. max_ignore_memctl(acts, "MemoryControllerPro0", "inAT2");
  114. max_ignore_memctl(acts, "MemoryControllerPro0", "inBT2");
  115. max_ignore_memctl(acts, "MemoryControllerPro0", "outCT2");
  116. max_memctl_linear(acts, "MemoryControllerPro0", "inAT3", ptrA, size * sizeof(uint32_t));
  117. max_memctl_linear(acts, "MemoryControllerPro0", "inBT3", ptrB, size * sizeof(uint32_t));
  118. max_queue_output(acts, "outCT3", ptrC, size * sizeof(uint32_t));
  119. max_run(engine, acts);
  120. max_actions_free(acts);
  121. printf("T3 finished\n");
  122. }
  123. static struct starpu_codelet cl3 =
  124. {
  125. .fpga_funcs = {fpga_impl3},
  126. .nbuffers = 3,
  127. .modes = {STARPU_R, STARPU_R, STARPU_W},
  128. .specific_nodes = 1,
  129. .nodes = {STARPU_SPECIFIC_NODE_LOCAL, STARPU_SPECIFIC_NODE_LOCAL, STARPU_SPECIFIC_NODE_CPU},
  130. };
  131. int main(int argc, char **argv)
  132. {
  133. struct starpu_conf conf;
  134. starpu_data_handle_t handle_a, handle_b, handle_ct1, handle_ct2, handle_c;
  135. int ret;
  136. maxfile = MyTasks_init();
  137. struct starpu_max_load load[2];
  138. load[0].file = maxfile;
  139. load[0].engine_id_pattern = "*";
  140. load[1].file = NULL;
  141. load[1].engine_id_pattern = NULL;
  142. starpu_conf_init(&conf);
  143. conf.sched_policy_name = "eager";
  144. conf.calibrate = 0;
  145. conf.fpga_load = load;
  146. ret = starpu_initialize(&conf, &argc, &argv);
  147. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  148. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  149. /* Enable profiling */
  150. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  151. int32_t a[SIZE];
  152. int32_t b[SIZE];
  153. int32_t c[SIZE];
  154. int i;
  155. for(i = 0; i < SIZE; ++i)
  156. {
  157. a[i] = random() % 100;
  158. b[i] = random() % 100;
  159. }
  160. starpu_vector_data_register(&handle_a, STARPU_MAIN_RAM, (uintptr_t) &a, SIZE, sizeof(a[0]));
  161. starpu_vector_data_register(&handle_b, STARPU_MAIN_RAM, (uintptr_t) &b, SIZE, sizeof(b[0]));
  162. starpu_vector_data_register(&handle_ct1, -1, 0, SIZE, sizeof(c[0]));
  163. starpu_vector_data_register(&handle_ct2, -1, 0, SIZE, sizeof(c[0]));
  164. starpu_vector_data_register(&handle_c, STARPU_MAIN_RAM, (uintptr_t) &c, SIZE, sizeof(c[0]));
  165. ret = starpu_task_insert(&cl1, STARPU_R, handle_a, STARPU_R, handle_b, STARPU_W, handle_ct1, 0);
  166. fprintf(stderr,"task submitted %d\n", ret);
  167. ret = starpu_task_insert(&cl2, STARPU_R, handle_ct1, STARPU_R, handle_ct1, STARPU_W, handle_ct2, 0);
  168. fprintf(stderr,"task submitted %d\n", ret);
  169. ret = starpu_task_insert(&cl3, STARPU_R, handle_ct2, STARPU_R, handle_ct2, STARPU_W, handle_c, 0);
  170. fprintf(stderr,"task submitted %d\n", ret);
  171. starpu_data_unregister(handle_a);
  172. starpu_data_unregister(handle_b);
  173. starpu_data_unregister(handle_c);
  174. ret = EXIT_SUCCESS;
  175. for (i = 0; i < SIZE; ++i)
  176. {
  177. int ct1 = a[i] + b[i];
  178. int ct2 = ct1 * ct1;
  179. int ct3 = ct2 + ct2;
  180. if (c[i] != ct3)
  181. ret = EXIT_FAILURE;
  182. if (i < 10)
  183. {
  184. printf("%d == %d\n", c[i], ct3);
  185. if (c[i] != ct3)
  186. printf("OOOPS\n");
  187. }
  188. }
  189. starpu_shutdown();
  190. if (ret == EXIT_SUCCESS)
  191. printf("OK!\n");
  192. return ret;
  193. }