sync_and_notify_data.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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 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 USE_CUDA
  40. void cuda_codelet_incA(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args);
  41. void cuda_codelet_incC(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args);
  42. #endif
  43. #define VECTORSIZE 16
  44. starpu_data_handle v_handle;
  45. static unsigned v[VECTORSIZE] __attribute__((aligned(128))) = {0, 0, 0, 0};
  46. void core_codelet_incA(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args)
  47. {
  48. unsigned *val = (unsigned *)buffers[0].vector.ptr;
  49. val[0]++;
  50. }
  51. void core_codelet_incC(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args)
  52. {
  53. unsigned *val = (unsigned *)buffers[0].vector.ptr;
  54. val[2]++;
  55. }
  56. int main(int argc, char **argv)
  57. {
  58. starpu_init(NULL);
  59. #ifdef USE_GORDON
  60. unsigned elf_id = gordon_register_elf_plugin("./microbenchs/sync_and_notify_data_gordon_kernels.spuelf");
  61. gordon_load_plugin_on_all_spu(elf_id);
  62. unsigned kernel_incA_id = gordon_register_kernel(elf_id, "incA");
  63. gordon_load_kernel_on_all_spu(kernel_incA_id);
  64. unsigned kernel_incC_id = gordon_register_kernel(elf_id, "incC");
  65. gordon_load_kernel_on_all_spu(kernel_incC_id);
  66. fprintf(stderr, "kernel incA %d incC %d elf %d\n", kernel_incA_id, kernel_incC_id, elf_id);
  67. #endif
  68. starpu_register_vector_data(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  69. unsigned iter;
  70. for (iter = 0; iter < K; iter++)
  71. {
  72. int ret;
  73. unsigned ind;
  74. for (ind = 0; ind < N; ind++)
  75. {
  76. /* increment a = v[0] */
  77. starpu_codelet cl_inc_a = {
  78. .where = CORE|CUDA|GORDON,
  79. .core_func = core_codelet_incA,
  80. #ifdef USE_CUDA
  81. .cuda_func = cuda_codelet_incA,
  82. #endif
  83. #ifdef USE_GORDON
  84. .gordon_func = kernel_incA_id,
  85. #endif
  86. .nbuffers = 1
  87. };
  88. struct starpu_task *task = starpu_task_create();
  89. task->cl = &cl_inc_a;
  90. task->buffers[0].handle = v_handle;
  91. task->buffers[0].mode = STARPU_RW;
  92. task->synchronous = 1;
  93. ret = starpu_submit_task(task);
  94. if (ret == -ENODEV)
  95. goto enodev;
  96. }
  97. /* synchronize v in RAM */
  98. starpu_sync_data_with_mem(v_handle, STARPU_RW);
  99. /* increment b */
  100. v[1]++;
  101. starpu_release_data_from_mem(v_handle);
  102. for (ind = 0; ind < N; ind++)
  103. {
  104. /* increment c = v[2] */
  105. starpu_codelet cl_inc_c = {
  106. .where = CORE|CUDA|GORDON,
  107. .core_func = core_codelet_incC,
  108. #ifdef USE_CUDA
  109. .cuda_func = cuda_codelet_incC,
  110. #endif
  111. #ifdef USE_GORDON
  112. .gordon_func = kernel_incC_id,
  113. #endif
  114. .nbuffers = 1
  115. };
  116. struct starpu_task *task = starpu_task_create();
  117. task->cl = &cl_inc_c;
  118. task->buffers[0].handle = v_handle;
  119. task->buffers[0].mode = STARPU_RW;
  120. task->synchronous = 1;
  121. ret = starpu_submit_task(task);
  122. if (ret == -ENODEV)
  123. goto enodev;
  124. }
  125. }
  126. starpu_sync_data_with_mem(v_handle, STARPU_RW);
  127. fprintf(stderr, "V = { %d, %d, %d }\n", v[0], v[1], v[2]);
  128. starpu_release_data_from_mem(v_handle);
  129. starpu_shutdown();
  130. if ((v[0] != N*K) || (v[1] != K) || (v[2] != N*K))
  131. return -1;
  132. return 0;
  133. enodev:
  134. fprintf(stderr, "WARNING: No one can execute this task\n");
  135. /* yes, we do not perform the computation but we did detect that no one
  136. * could perform the kernel, so this is not an error from StarPU */
  137. return 0;
  138. }