sync_and_notify_data_implicit.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. //#define N 1
  26. //#define K 1
  27. /*
  28. * In this test, we maintain a vector v = (a,b,c).
  29. *
  30. * Each iteration consists of:
  31. * - increment a n times
  32. * - sync v in ram
  33. * - incrementer b
  34. * - notify the modification of v
  35. * - incrementer c n times
  36. * - sync v
  37. *
  38. * At the end, we have to make sure that if we did k iterations,
  39. * v == (kn, k, kn)
  40. */
  41. #ifdef STARPU_USE_CUDA
  42. void cuda_codelet_incA(void *descr[], __attribute__ ((unused)) void *_args);
  43. void cuda_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args);
  44. #endif
  45. #ifdef STARPU_USE_OPENCL
  46. #include <starpu_opencl.h>
  47. void opencl_codelet_incA(void *descr[], __attribute__ ((unused)) void *_args);
  48. void opencl_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args);
  49. struct starpu_opencl_program opencl_code;
  50. #endif
  51. #define VECTORSIZE 16
  52. starpu_data_handle v_handle;
  53. static unsigned v[VECTORSIZE] __attribute__((aligned(128))) = {0, 0, 0, 0};
  54. void cpu_codelet_incA(void *descr[], __attribute__ ((unused)) void *_args)
  55. {
  56. unsigned *val = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  57. val[0]++;
  58. }
  59. void cpu_codelet_incC(void *descr[], __attribute__ ((unused)) void *_args)
  60. {
  61. unsigned *val = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  62. val[2]++;
  63. }
  64. /* increment a = v[0] */
  65. static starpu_codelet cl_inc_a = {
  66. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_GORDON,
  67. .cpu_func = cpu_codelet_incA,
  68. #ifdef STARPU_USE_CUDA
  69. .cuda_func = cuda_codelet_incA,
  70. #endif
  71. #ifdef STARPU_USE_OPENCL
  72. .opencl_func = opencl_codelet_incA,
  73. #endif
  74. #ifdef STARPU_USE_GORDON
  75. .gordon_func = kernel_incA_id,
  76. #endif
  77. .nbuffers = 1
  78. };
  79. /* increment c = v[2] */
  80. starpu_codelet cl_inc_c = {
  81. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_GORDON,
  82. .cpu_func = cpu_codelet_incC,
  83. #ifdef STARPU_USE_CUDA
  84. .cuda_func = cuda_codelet_incC,
  85. #endif
  86. #ifdef STARPU_USE_OPENCL
  87. .opencl_func = opencl_codelet_incC,
  88. #endif
  89. #ifdef STARPU_USE_GORDON
  90. .gordon_func = kernel_incC_id,
  91. #endif
  92. .nbuffers = 1
  93. };
  94. int main(int argc, char **argv)
  95. {
  96. starpu_init(NULL);
  97. #ifdef STARPU_USE_GORDON
  98. unsigned elf_id = gordon_register_elf_plugin("./datawizard/sync_and_notify_data_gordon_kernels.spuelf");
  99. gordon_load_plugin_on_all_spu(elf_id);
  100. unsigned kernel_incA_id = gordon_register_kernel(elf_id, "incA");
  101. gordon_load_kernel_on_all_spu(kernel_incA_id);
  102. unsigned kernel_incC_id = gordon_register_kernel(elf_id, "incC");
  103. gordon_load_kernel_on_all_spu(kernel_incC_id);
  104. fprintf(stderr, "kernel incA %d incC %d elf %d\n", kernel_incA_id, kernel_incC_id, elf_id);
  105. #endif
  106. #ifdef STARPU_USE_OPENCL
  107. starpu_opencl_load_opencl_from_file("tests/datawizard/sync_and_notify_data_opencl_codelet.cl", &opencl_code);
  108. #endif
  109. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  110. unsigned iter;
  111. for (iter = 0; iter < K; iter++)
  112. {
  113. int ret;
  114. unsigned ind;
  115. for (ind = 0; ind < N; ind++)
  116. {
  117. struct starpu_task *task = starpu_task_create();
  118. task->cl = &cl_inc_a;
  119. task->buffers[0].handle = v_handle;
  120. task->buffers[0].mode = STARPU_RW;
  121. ret = starpu_task_submit(task);
  122. if (ret == -ENODEV)
  123. goto enodev;
  124. }
  125. /* synchronize v in RAM */
  126. starpu_data_acquire(v_handle, STARPU_RW);
  127. /* increment b */
  128. v[1]++;
  129. starpu_data_release(v_handle);
  130. for (ind = 0; ind < N; ind++)
  131. {
  132. struct starpu_task *task = starpu_task_create();
  133. task->cl = &cl_inc_c;
  134. task->buffers[0].handle = v_handle;
  135. task->buffers[0].mode = STARPU_RW;
  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. }