sync_and_notify_data.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2012 inria
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <config.h>
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <errno.h>
  22. #include <starpu.h>
  23. #ifdef STARPU_USE_GORDON
  24. #include <gordon.h>
  25. #endif
  26. #include "../helper.h"
  27. #define N_DEF 100
  28. #define K_DEF 256
  29. static int n=N_DEF;
  30. static int k=K_DEF;
  31. /*
  32. * In this test, we maintain a vector v = (a,b,c).
  33. *
  34. * Each iteration consists of:
  35. * - increment a n times
  36. * - sync v in ram
  37. * - incrementer b
  38. * - notify the modification of v
  39. * - incrementer c n times
  40. * - sync v
  41. *
  42. * At the end, we have to make sure that if we did k iterations,
  43. * v == (kn, k, kn)
  44. */
  45. #ifdef STARPU_USE_CUDA
  46. void cuda_codelet_incA(void *descr[], __attribute__ ((unused)) void *_args);
  47. void cuda_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args);
  48. #endif
  49. #ifdef STARPU_USE_OPENCL
  50. #include <starpu_opencl.h>
  51. void opencl_codelet_incA(void *descr[], __attribute__ ((unused)) void *_args);
  52. void opencl_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args);
  53. struct starpu_opencl_program opencl_code;
  54. #endif
  55. #define VECTORSIZE 16
  56. starpu_data_handle_t v_handle;
  57. static unsigned v[VECTORSIZE] __attribute__((aligned(128))) = {0, 0, 0, 0};
  58. void cpu_codelet_incA(void *descr[], __attribute__ ((unused)) void *_args)
  59. {
  60. STARPU_SKIP_IF_VALGRIND;
  61. unsigned *val = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  62. val[0]++;
  63. }
  64. void cpu_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args)
  65. {
  66. STARPU_SKIP_IF_VALGRIND;
  67. unsigned *val = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  68. val[2]++;
  69. }
  70. int main(int argc, char **argv)
  71. {
  72. int ret;
  73. #ifdef STARPU_SLOW_MACHINE
  74. n /= 10;
  75. k /= 8;
  76. #endif
  77. ret = starpu_init(NULL);
  78. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  79. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  80. #ifdef STARPU_USE_GORDON
  81. unsigned elf_id = gordon_register_elf_plugin("./datawizard/sync_and_notify_data_gordon_kernels.spuelf");
  82. gordon_load_plugin_on_all_spu(elf_id);
  83. unsigned kernel_incA_id = gordon_register_kernel(elf_id, "incA");
  84. gordon_load_kernel_on_all_spu(kernel_incA_id);
  85. unsigned kernel_incC_id = gordon_register_kernel(elf_id, "incC");
  86. gordon_load_kernel_on_all_spu(kernel_incC_id);
  87. FPRINTF(stderr, "kernel incA %d incC %d elf %d\n", kernel_incA_id, kernel_incC_id, elf_id);
  88. #endif
  89. #ifdef STARPU_USE_OPENCL
  90. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/sync_and_notify_data_opencl_codelet.cl", &opencl_code, NULL);
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  92. #endif
  93. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  94. unsigned iter;
  95. for (iter = 0; iter < k; iter++)
  96. {
  97. int ret;
  98. unsigned ind;
  99. for (ind = 0; ind < n; ind++)
  100. {
  101. /* increment a = v[0] */
  102. struct starpu_codelet cl_inc_a =
  103. {
  104. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_GORDON,
  105. .cpu_funcs = {cpu_codelet_incA, NULL},
  106. #ifdef STARPU_USE_CUDA
  107. .cuda_funcs = {cuda_codelet_incA, NULL},
  108. #endif
  109. #ifdef STARPU_USE_OPENCL
  110. .opencl_funcs = {opencl_codelet_incA, NULL},
  111. #endif
  112. #ifdef STARPU_USE_GORDON
  113. .gordon_func = kernel_incA_id,
  114. #endif
  115. .nbuffers = 1,
  116. .modes = {STARPU_RW}
  117. };
  118. struct starpu_task *task = starpu_task_create();
  119. task->cl = &cl_inc_a;
  120. task->handles[0] = v_handle;
  121. task->synchronous = 1;
  122. ret = starpu_task_submit(task);
  123. if (ret == -ENODEV) goto enodev;
  124. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  125. }
  126. /* synchronize v in RAM */
  127. ret = starpu_data_acquire(v_handle, STARPU_RW);
  128. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  129. /* increment b */
  130. v[1]++;
  131. starpu_data_release(v_handle);
  132. for (ind = 0; ind < n; ind++)
  133. {
  134. /* increment c = v[2] */
  135. struct starpu_codelet cl_inc_c =
  136. {
  137. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_GORDON,
  138. .cpu_funcs = {cpu_codelet_incC, NULL},
  139. #ifdef STARPU_USE_CUDA
  140. .cuda_funcs = {cuda_codelet_incC, NULL},
  141. #endif
  142. #ifdef STARPU_USE_OPENCL
  143. .opencl_funcs = {opencl_codelet_incC, NULL},
  144. #endif
  145. #ifdef STARPU_USE_GORDON
  146. .gordon_func = kernel_incC_id,
  147. #endif
  148. .nbuffers = 1,
  149. .modes = {STARPU_RW}
  150. };
  151. struct starpu_task *task = starpu_task_create();
  152. task->cl = &cl_inc_c;
  153. task->handles[0] = v_handle;
  154. task->synchronous = 1;
  155. ret = starpu_task_submit(task);
  156. if (ret == -ENODEV) goto enodev;
  157. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  158. }
  159. }
  160. ret = starpu_data_acquire(v_handle, STARPU_RW);
  161. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  162. FPRINTF(stderr, "V = {%u, %u, %u}\n", v[0], v[1], v[2]);
  163. starpu_data_release(v_handle);
  164. starpu_data_unregister(v_handle);
  165. starpu_shutdown();
  166. if ((v[0] != n*k) || (v[1] != k) || (v[2] != n*k))
  167. {
  168. FPRINTF(stderr, "Incorrect result\n");
  169. return EXIT_FAILURE;
  170. }
  171. return EXIT_SUCCESS;
  172. enodev:
  173. starpu_data_unregister(v_handle);
  174. starpu_shutdown();
  175. fprintf(stderr, "WARNING: No one can execute this task\n");
  176. /* yes, we do not perform the computation but we did detect that no one
  177. * could perform the kernel, so this is not an error from StarPU */
  178. return STARPU_TEST_SKIPPED;
  179. }