sync_and_notify_data_implicit.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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 <stdio.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <starpu.h>
  21. #ifdef STARPU_USE_GORDON
  22. #include <gordon.h>
  23. #endif
  24. #include "../common/helper.h"
  25. #define N 100
  26. #define K 256
  27. //#define N 1
  28. //#define K 1
  29. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  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 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. unsigned *val = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  60. val[0]++;
  61. }
  62. void cpu_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args)
  63. {
  64. unsigned *val = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  65. val[2]++;
  66. }
  67. /* increment a = v[0] */
  68. static starpu_codelet cl_inc_a = {
  69. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_GORDON,
  70. .cpu_func = cpu_codelet_incA,
  71. #ifdef STARPU_USE_CUDA
  72. .cuda_func = cuda_codelet_incA,
  73. #endif
  74. #ifdef STARPU_USE_OPENCL
  75. .opencl_func = opencl_codelet_incA,
  76. #endif
  77. #ifdef STARPU_USE_GORDON
  78. .gordon_func = kernel_incA_id,
  79. #endif
  80. .nbuffers = 1
  81. };
  82. /* increment c = v[2] */
  83. starpu_codelet cl_inc_c = {
  84. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_GORDON,
  85. .cpu_func = cpu_codelet_incC,
  86. #ifdef STARPU_USE_CUDA
  87. .cuda_func = cuda_codelet_incC,
  88. #endif
  89. #ifdef STARPU_USE_OPENCL
  90. .opencl_func = opencl_codelet_incC,
  91. #endif
  92. #ifdef STARPU_USE_GORDON
  93. .gordon_func = kernel_incC_id,
  94. #endif
  95. .nbuffers = 1
  96. };
  97. int main(int argc, char **argv)
  98. {
  99. int ret;
  100. ret = starpu_init(NULL);
  101. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  102. #ifdef STARPU_USE_GORDON
  103. unsigned elf_id = gordon_register_elf_plugin("./datawizard/sync_and_notify_data_gordon_kernels.spuelf");
  104. gordon_load_plugin_on_all_spu(elf_id);
  105. unsigned kernel_incA_id = gordon_register_kernel(elf_id, "incA");
  106. gordon_load_kernel_on_all_spu(kernel_incA_id);
  107. unsigned kernel_incC_id = gordon_register_kernel(elf_id, "incC");
  108. gordon_load_kernel_on_all_spu(kernel_incC_id);
  109. FPRINTF(stderr, "kernel incA %d incC %d elf %d\n", kernel_incA_id, kernel_incC_id, elf_id);
  110. #endif
  111. #ifdef STARPU_USE_OPENCL
  112. starpu_opencl_load_opencl_from_file("tests/datawizard/sync_and_notify_data_opencl_codelet.cl", &opencl_code, NULL);
  113. #endif
  114. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  115. unsigned iter;
  116. for (iter = 0; iter < K; iter++)
  117. {
  118. int ret;
  119. unsigned ind;
  120. for (ind = 0; ind < N; ind++)
  121. {
  122. struct starpu_task *task = starpu_task_create();
  123. task->cl = &cl_inc_a;
  124. task->buffers[0].handle = v_handle;
  125. task->buffers[0].mode = STARPU_RW;
  126. ret = starpu_task_submit(task);
  127. if (ret == -ENODEV) goto enodev;
  128. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  129. }
  130. /* synchronize v in RAM */
  131. ret = starpu_data_acquire(v_handle, STARPU_RW);
  132. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  133. /* increment b */
  134. v[1]++;
  135. starpu_data_release(v_handle);
  136. for (ind = 0; ind < N; ind++)
  137. {
  138. struct starpu_task *task = starpu_task_create();
  139. task->cl = &cl_inc_c;
  140. task->buffers[0].handle = v_handle;
  141. task->buffers[0].mode = STARPU_RW;
  142. ret = starpu_task_submit(task);
  143. if (ret == -ENODEV) goto enodev;
  144. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  145. }
  146. }
  147. ret = starpu_data_acquire(v_handle, STARPU_RW);
  148. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  149. FPRINTF(stderr, "V = {%u, %u, %u}\n", v[0], v[1], v[2]);
  150. starpu_data_release(v_handle);
  151. starpu_data_unregister(v_handle);
  152. starpu_shutdown();
  153. if ((v[0] != N*K) || (v[1] != K) || (v[2] != N*K))
  154. return -1;
  155. return 0;
  156. enodev:
  157. starpu_data_unregister(v_handle);
  158. starpu_shutdown();
  159. fprintf(stderr, "WARNING: No one can execute this task\n");
  160. /* yes, we do not perform the computation but we did detect that no one
  161. * could perform the kernel, so this is not an error from StarPU */
  162. return 77;
  163. }