sync_and_notify_data.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <starpu.h>
  19. #define N 100
  20. #define K 256
  21. /*
  22. * In this test, we maintain a vector v = (a,b,c).
  23. *
  24. * Each iteration consists of:
  25. * - increment a n times
  26. * - sync v in ram
  27. * - incrementer b
  28. * - notify the modification of v
  29. * - incrementer c n times
  30. * - sync v
  31. *
  32. * At the end, we have to make sure that if we did k iterations,
  33. * v == (kn, k, kn)
  34. */
  35. #ifdef USE_CUDA
  36. void cuda_codelet_incA(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args);
  37. void cuda_codelet_incC(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args);
  38. #endif
  39. starpu_data_handle v_handle;
  40. unsigned v[3] = {0, 0, 0};
  41. void core_codelet_incA(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args)
  42. {
  43. unsigned *val = (unsigned *)buffers[0].vector.ptr;
  44. val[0]++;
  45. }
  46. void core_codelet_incC(starpu_data_interface_t *buffers, __attribute__ ((unused)) void *_args)
  47. {
  48. unsigned *val = (unsigned *)buffers[0].vector.ptr;
  49. val[2]++;
  50. }
  51. int main(int argc, char **argv)
  52. {
  53. starpu_init(NULL);
  54. starpu_monitor_vector_data(&v_handle, 0, (uintptr_t)v, 3, sizeof(unsigned));
  55. unsigned iter;
  56. for (iter = 0; iter < K; iter++)
  57. {
  58. unsigned ind;
  59. for (ind = 0; ind < N; ind++)
  60. {
  61. /* increment a = v[0] */
  62. starpu_codelet cl_inc_a = {
  63. .where = CORE|CUBLAS,
  64. .core_func = core_codelet_incA,
  65. #ifdef USE_CUDA
  66. .cublas_func = cuda_codelet_incA,
  67. #endif
  68. .nbuffers = 1
  69. };
  70. struct starpu_task *task = starpu_task_create();
  71. task->cl = &cl_inc_a;
  72. task->buffers[0].state = v_handle;
  73. task->buffers[0].mode = RW;
  74. task->synchronous = 1;
  75. starpu_submit_task(task);
  76. }
  77. /* synchronize v in RAM */
  78. starpu_sync_data_with_mem(v_handle);
  79. /* increment b */
  80. v[1]++;
  81. /* inform StarPU that v was modified by the apps */
  82. starpu_notify_data_modification(v_handle, 0);
  83. for (ind = 0; ind < N; ind++)
  84. {
  85. /* increment c = v[2] */
  86. starpu_codelet cl_inc_c = {
  87. .where = CORE|CUBLAS,
  88. .core_func = core_codelet_incC,
  89. #ifdef USE_CUDA
  90. .cublas_func = cuda_codelet_incC,
  91. #endif
  92. .nbuffers = 1
  93. };
  94. struct starpu_task *task = starpu_task_create();
  95. task->cl = &cl_inc_c;
  96. task->buffers[0].state = v_handle;
  97. task->buffers[0].mode = RW;
  98. task->synchronous = 1;
  99. starpu_submit_task(task);
  100. }
  101. }
  102. starpu_sync_data_with_mem(v_handle);
  103. fprintf(stderr, "V = { %d, %d, %d }\n", v[0], v[1], v[2]);
  104. starpu_shutdown();
  105. if ((v[0] != N*K) || (v[1] != K) || (v[2] != N*K))
  106. return -1;
  107. return 0;
  108. }