execute_on_a_specific_worker.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU 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. * StarPU 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. #include <stdlib.h>
  21. #include "../helper.h"
  22. #include <common/thread.h>
  23. /*
  24. * Test binding tasks on specific workers
  25. */
  26. #ifdef STARPU_QUICK_CHECK
  27. #define N 10
  28. #elif !defined(STARPU_LONG_CHECK)
  29. #define N 100
  30. #else
  31. #define N 1000
  32. #endif
  33. #define VECTORSIZE 1024
  34. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  35. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  36. static unsigned finished = 0;
  37. static unsigned cnt;
  38. starpu_data_handle_t v_handle;
  39. static unsigned *v;
  40. static void callback(void *arg)
  41. {
  42. (void)arg;
  43. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  44. ANNOTATE_HAPPENS_BEFORE(&cnt);
  45. if (res == 0)
  46. {
  47. ANNOTATE_HAPPENS_AFTER(&cnt);
  48. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  49. finished = 1;
  50. STARPU_PTHREAD_COND_SIGNAL(&cond);
  51. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  52. }
  53. }
  54. void codelet_null(void *descr[], void *_args)
  55. {
  56. (void)descr;
  57. (void)_args;
  58. // int id = starpu_worker_get_id();
  59. // FPRINTF(stderr, "worker #%d\n", id);
  60. }
  61. static struct starpu_codelet cl_r =
  62. {
  63. .cpu_funcs = {codelet_null},
  64. .cuda_funcs = {codelet_null},
  65. .opencl_funcs = {codelet_null},
  66. .cpu_funcs_name = {"codelet_null"},
  67. .nbuffers = 1,
  68. .modes = {STARPU_R}
  69. };
  70. static struct starpu_codelet cl_w =
  71. {
  72. .cpu_funcs = {codelet_null},
  73. .cuda_funcs = {codelet_null},
  74. .opencl_funcs = {codelet_null},
  75. .cpu_funcs_name = {"codelet_null"},
  76. .nbuffers = 1,
  77. .modes = {STARPU_W}
  78. };
  79. static struct starpu_codelet cl_rw =
  80. {
  81. .cpu_funcs = {codelet_null},
  82. .cuda_funcs = {codelet_null},
  83. .opencl_funcs = {codelet_null},
  84. .cpu_funcs_name = {"codelet_null"},
  85. .nbuffers = 1,
  86. .modes = {STARPU_RW}
  87. };
  88. static struct starpu_codelet *select_codelet_with_random_mode(void)
  89. {
  90. int r = rand();
  91. switch (r % 3)
  92. {
  93. case 0:
  94. return &cl_r;
  95. case 1:
  96. return &cl_w;
  97. case 2:
  98. return &cl_rw;
  99. };
  100. return &cl_rw;
  101. }
  102. int main(int argc, char **argv)
  103. {
  104. int ret;
  105. ret = starpu_initialize(NULL, &argc, &argv);
  106. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  107. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  108. ret = starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  109. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  110. starpu_vector_data_register(&v_handle, STARPU_MAIN_RAM, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  111. unsigned nworker = starpu_worker_get_count();
  112. cnt = nworker*N;
  113. unsigned iter, worker;
  114. for (iter = 0; iter < N; iter++)
  115. {
  116. for (worker = 0; worker < nworker; worker++)
  117. {
  118. /* execute a task on that worker */
  119. struct starpu_task *task = starpu_task_create();
  120. task->handles[0] = v_handle;
  121. task->cl = select_codelet_with_random_mode();
  122. task->callback_func = callback;
  123. task->callback_arg = NULL;
  124. task->execute_on_a_specific_worker = 1;
  125. task->workerid = worker;
  126. ret = starpu_task_submit(task);
  127. if (ret == -ENODEV) goto enodev;
  128. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  129. }
  130. }
  131. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  132. while (!finished)
  133. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  134. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  135. starpu_data_unregister(v_handle);
  136. starpu_free_noflag(v, VECTORSIZE*sizeof(unsigned));
  137. starpu_shutdown();
  138. return EXIT_SUCCESS;
  139. enodev:
  140. starpu_data_unregister(v_handle);
  141. starpu_free_noflag(v, VECTORSIZE*sizeof(unsigned));
  142. starpu_shutdown();
  143. fprintf(stderr, "WARNING: No one can execute this task\n");
  144. /* yes, we do not perform the computation but we did detect that no one
  145. * could perform the kernel, so this is not an error from StarPU */
  146. return STARPU_TEST_SKIPPED;
  147. }