sync_and_notify_data_implicit.c 5.6 KB

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