sync_and_notify_data.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 <stdio.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #ifdef STARPU_USE_GORDON
  21. #include <gordon.h>
  22. #endif
  23. #define N 100
  24. #define K 256
  25. /*
  26. * In this test, we maintain a vector v = (a,b,c).
  27. *
  28. * Each iteration consists of:
  29. * - increment a n times
  30. * - sync v in ram
  31. * - incrementer b
  32. * - notify the modification of v
  33. * - incrementer c n times
  34. * - sync v
  35. *
  36. * At the end, we have to make sure that if we did k iterations,
  37. * v == (kn, k, kn)
  38. */
  39. #ifdef STARPU_USE_CUDA
  40. void cuda_codelet_incA(void *descr[], __attribute__ ((unused)) void *_args);
  41. void cuda_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args);
  42. #endif
  43. #ifdef STARPU_USE_OPENCL
  44. #include <starpu_opencl.h>
  45. void opencl_codelet_incA(void *descr[], __attribute__ ((unused)) void *_args);
  46. void opencl_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args);
  47. struct starpu_opencl_program opencl_code;
  48. #endif
  49. #define VECTORSIZE 16
  50. starpu_data_handle v_handle;
  51. static unsigned v[VECTORSIZE] __attribute__((aligned(128))) = {0, 0, 0, 0};
  52. void cpu_codelet_incA(void *descr[], __attribute__ ((unused)) void *_args)
  53. {
  54. unsigned *val = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  55. val[0]++;
  56. }
  57. void cpu_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args)
  58. {
  59. unsigned *val = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  60. val[2]++;
  61. }
  62. int main(int argc, char **argv)
  63. {
  64. starpu_init(NULL);
  65. #ifdef STARPU_USE_GORDON
  66. unsigned elf_id = gordon_register_elf_plugin("./datawizard/sync_and_notify_data_gordon_kernels.spuelf");
  67. gordon_load_plugin_on_all_spu(elf_id);
  68. unsigned kernel_incA_id = gordon_register_kernel(elf_id, "incA");
  69. gordon_load_kernel_on_all_spu(kernel_incA_id);
  70. unsigned kernel_incC_id = gordon_register_kernel(elf_id, "incC");
  71. gordon_load_kernel_on_all_spu(kernel_incC_id);
  72. fprintf(stderr, "kernel incA %d incC %d elf %d\n", kernel_incA_id, kernel_incC_id, elf_id);
  73. #endif
  74. #ifdef STARPU_USE_OPENCL
  75. starpu_opencl_load_opencl_from_file("tests/datawizard/sync_and_notify_data_opencl_codelet.cl", &opencl_code);
  76. #endif
  77. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  78. unsigned iter;
  79. for (iter = 0; iter < K; iter++)
  80. {
  81. int ret;
  82. unsigned ind;
  83. for (ind = 0; ind < N; ind++)
  84. {
  85. /* increment a = v[0] */
  86. starpu_codelet cl_inc_a = {
  87. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_GORDON,
  88. .cpu_func = cpu_codelet_incA,
  89. #ifdef STARPU_USE_CUDA
  90. .cuda_func = cuda_codelet_incA,
  91. #endif
  92. #ifdef STARPU_USE_OPENCL
  93. .opencl_func = opencl_codelet_incA,
  94. #endif
  95. #ifdef STARPU_USE_GORDON
  96. .gordon_func = kernel_incA_id,
  97. #endif
  98. .nbuffers = 1
  99. };
  100. struct starpu_task *task = starpu_task_create();
  101. task->cl = &cl_inc_a;
  102. task->buffers[0].handle = v_handle;
  103. task->buffers[0].mode = STARPU_RW;
  104. task->synchronous = 1;
  105. ret = starpu_task_submit(task);
  106. if (ret == -ENODEV)
  107. goto enodev;
  108. }
  109. /* synchronize v in RAM */
  110. starpu_data_acquire(v_handle, STARPU_RW);
  111. /* increment b */
  112. v[1]++;
  113. starpu_data_release(v_handle);
  114. for (ind = 0; ind < N; ind++)
  115. {
  116. /* increment c = v[2] */
  117. starpu_codelet cl_inc_c = {
  118. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_GORDON,
  119. .cpu_func = cpu_codelet_incC,
  120. #ifdef STARPU_USE_CUDA
  121. .cuda_func = cuda_codelet_incC,
  122. #endif
  123. #ifdef STARPU_USE_OPENCL
  124. .opencl_func = opencl_codelet_incC,
  125. #endif
  126. #ifdef STARPU_USE_GORDON
  127. .gordon_func = kernel_incC_id,
  128. #endif
  129. .nbuffers = 1
  130. };
  131. struct starpu_task *task = starpu_task_create();
  132. task->cl = &cl_inc_c;
  133. task->buffers[0].handle = v_handle;
  134. task->buffers[0].mode = STARPU_RW;
  135. task->synchronous = 1;
  136. ret = starpu_task_submit(task);
  137. if (ret == -ENODEV)
  138. goto enodev;
  139. }
  140. }
  141. starpu_data_acquire(v_handle, STARPU_RW);
  142. fprintf(stderr, "V = { %d, %d, %d }\n", v[0], v[1], v[2]);
  143. starpu_data_release(v_handle);
  144. starpu_shutdown();
  145. if ((v[0] != N*K) || (v[1] != K) || (v[2] != N*K))
  146. return -1;
  147. return 0;
  148. enodev:
  149. fprintf(stderr, "WARNING: No one can execute this task\n");
  150. /* yes, we do not perform the computation but we did detect that no one
  151. * could perform the kernel, so this is not an error from StarPU */
  152. return 0;
  153. }