max_fpga_basic_static.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 basic 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. printf("Loading DFE memory.\n");
  41. /* C = A+B */
  42. MyTasks_interfaceT1(size, ptrCT1, ptrA, ptrB);
  43. printf("T1 finished\n");
  44. /* C = A*B */
  45. MyTasks_interfaceT2(size, ptrAT2, ptrBT2, ptrCT2);
  46. printf("T2 finished\n");
  47. /* C = A+B */
  48. MyTasks_interfaceT3(size, ptrAT3, ptrBT3, ptrC);
  49. printf("T3 finished\n");
  50. printf("Running DFE.\n");
  51. }
  52. static struct starpu_codelet cl =
  53. {
  54. .fpga_funcs = {fpga_impl},
  55. .nbuffers = 3,
  56. .modes = {STARPU_R, STARPU_R, STARPU_W},
  57. .specific_nodes = 1,
  58. .nodes = {STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_CPU},
  59. };
  60. void fpga_impl1(void *buffers[], void *cl_arg)
  61. {
  62. (void)cl_arg;
  63. int32_t *ptrA = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[0]);
  64. int32_t *ptrB = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[1]);
  65. size_t ptrC = (size_t) STARPU_VECTOR_GET_PTR(buffers[2]); /* FPGA */
  66. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  67. printf("T1 with %p %p %zu\n", ptrA, ptrB, ptrC);
  68. /* C = A+B */
  69. MyTasks_interfaceT1(size, ptrC, ptrA, ptrB);
  70. printf("T1 finished\n");
  71. }
  72. static struct starpu_codelet cl1 =
  73. {
  74. .fpga_funcs = {fpga_impl1},
  75. .nbuffers = 3,
  76. .modes = {STARPU_R, STARPU_R, STARPU_W},
  77. .specific_nodes = 1,
  78. .nodes = {STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_LOCAL},
  79. };
  80. void fpga_impl2(void *buffers[], void *cl_arg)
  81. {
  82. (void)cl_arg;
  83. size_t ptrA = (size_t) STARPU_VECTOR_GET_PTR(buffers[0]); /* FPGA */
  84. size_t ptrB = (size_t) STARPU_VECTOR_GET_PTR(buffers[1]); /* FPGA */
  85. size_t ptrC = (size_t) STARPU_VECTOR_GET_PTR(buffers[2]); /* FPGA */
  86. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  87. printf("T2 with %zu %zu %zu\n", ptrA, ptrB, ptrC);
  88. /* C = A*B */
  89. MyTasks_interfaceT2(size, ptrA, ptrB, ptrC);
  90. printf("T2 finished\n");
  91. }
  92. static struct starpu_codelet cl2 =
  93. {
  94. .fpga_funcs = {fpga_impl2},
  95. .nbuffers = 3,
  96. .modes = {STARPU_R, STARPU_R, STARPU_W}
  97. /* local by default */
  98. };
  99. void fpga_impl3(void *buffers[], void *cl_arg)
  100. {
  101. (void)cl_arg;
  102. size_t ptrA = (size_t) STARPU_VECTOR_GET_PTR(buffers[0]); /* FPGA */
  103. size_t ptrB = (size_t) STARPU_VECTOR_GET_PTR(buffers[1]); /* FPGA */
  104. int32_t *ptrC = (int32_t*) STARPU_VECTOR_GET_PTR(buffers[2]);
  105. int size = STARPU_VECTOR_GET_NX(buffers[0]);
  106. printf("T3 with %zu %zu %p\n", ptrA, ptrB, ptrC);
  107. /* C = A+B */
  108. MyTasks_interfaceT3(size, ptrA, ptrB, ptrC);
  109. printf("T3 finished\n");
  110. }
  111. static struct starpu_codelet cl3 =
  112. {
  113. .fpga_funcs = {fpga_impl3},
  114. .nbuffers = 3,
  115. .modes = {STARPU_R, STARPU_R, STARPU_W},
  116. .specific_nodes = 1,
  117. .nodes = {STARPU_SPECIFIC_NODE_LOCAL, STARPU_SPECIFIC_NODE_LOCAL, STARPU_SPECIFIC_NODE_CPU},
  118. };
  119. int main(int argc, char **argv)
  120. {
  121. /* Enable profiling */
  122. starpu_profiling_status_set(1);
  123. struct starpu_conf conf;
  124. starpu_data_handle_t handle_a, handle_b, handle_ct1, handle_ct2, handle_c;
  125. int ret;
  126. starpu_conf_init(&conf);
  127. conf.sched_policy_name = "eager";
  128. conf.calibrate = 0;
  129. ret = starpu_initialize(&conf, &argc, &argv);
  130. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  131. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  132. int32_t a[SIZE];
  133. int32_t b[SIZE];
  134. int32_t c[SIZE];
  135. int i;
  136. for(i = 0; i < SIZE; ++i)
  137. {
  138. a[i] = random() % 100;
  139. b[i] = random() % 100;
  140. }
  141. starpu_vector_data_register(&handle_a, STARPU_MAIN_RAM, (uintptr_t) &a, SIZE, sizeof(a[0]));
  142. starpu_vector_data_register(&handle_b, STARPU_MAIN_RAM, (uintptr_t) &b, SIZE, sizeof(b[0]));
  143. starpu_vector_data_register(&handle_ct1, -1, 0, SIZE, sizeof(c[0]));
  144. starpu_vector_data_register(&handle_ct2, -1, 0, SIZE, sizeof(c[0]));
  145. starpu_vector_data_register(&handle_c, STARPU_MAIN_RAM, (uintptr_t) &c, SIZE, sizeof(c[0]));
  146. #if 0
  147. ret = starpu_task_insert(&cl, STARPU_R, handle_a, STARPU_R, handle_b, STARPU_W, handle_c, STARPU_TASK_SYNCHRONOUS, 1, 0);
  148. fprintf(stderr,"task submitted %d\n", ret);
  149. #else
  150. ret = starpu_task_insert(&cl1, STARPU_R, handle_a, STARPU_R, handle_b, STARPU_W, handle_ct1, 0);
  151. fprintf(stderr,"task submitted %d\n", ret);
  152. ret = starpu_task_insert(&cl2, STARPU_R, handle_ct1, STARPU_R, handle_ct1, STARPU_W, handle_ct2, 0);
  153. fprintf(stderr,"task submitted %d\n", ret);
  154. ret = starpu_task_insert(&cl3, STARPU_R, handle_ct2, STARPU_R, handle_ct2, STARPU_W, handle_c, 0);
  155. fprintf(stderr,"task submitted %d\n", ret);
  156. #endif
  157. starpu_data_unregister(handle_a);
  158. starpu_data_unregister(handle_b);
  159. starpu_data_unregister(handle_c);
  160. ret = EXIT_SUCCESS;
  161. for (i = 0; i < SIZE; ++i)
  162. {
  163. int ct1 = a[i] + b[i];
  164. int ct2 = ct1 * ct1;
  165. int ct3 = ct2 + ct2;
  166. if (c[i] != ct3)
  167. ret = EXIT_FAILURE;
  168. if (i < 10)
  169. {
  170. printf("%d == %d\n", c[i], ct3);
  171. if (c[i] != ct3)
  172. printf("OOOPS\n");
  173. }
  174. }
  175. starpu_shutdown();
  176. if (ret == EXIT_SUCCESS)
  177. printf("OK!\n");
  178. return ret;
  179. }