execute_on_a_specific_worker.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2013 Inria
  4. * Copyright (C) 2010-2013,2015,2017 CNRS
  5. * Copyright (C) 2009-2010,2012-2016 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <starpu.h>
  22. #include <stdlib.h>
  23. #include "../helper.h"
  24. #include <common/thread.h>
  25. /*
  26. * Test binding tasks on specific workers
  27. */
  28. #ifdef STARPU_QUICK_CHECK
  29. #define N 10
  30. #elif !defined(STARPU_LONG_CHECK)
  31. #define N 100
  32. #else
  33. #define N 1000
  34. #endif
  35. #define VECTORSIZE 1024
  36. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  37. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  38. static unsigned finished = 0;
  39. static unsigned cnt;
  40. starpu_data_handle_t v_handle;
  41. static unsigned *v;
  42. static void callback(void *arg)
  43. {
  44. (void)arg;
  45. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  46. ANNOTATE_HAPPENS_BEFORE(&cnt);
  47. if (res == 0)
  48. {
  49. ANNOTATE_HAPPENS_AFTER(&cnt);
  50. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  51. finished = 1;
  52. STARPU_PTHREAD_COND_SIGNAL(&cond);
  53. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  54. }
  55. }
  56. void codelet_null(void *descr[], void *_args)
  57. {
  58. (void)descr;
  59. (void)_args;
  60. // int id = starpu_worker_get_id();
  61. // FPRINTF(stderr, "worker #%d\n", id);
  62. }
  63. static struct starpu_codelet cl_r =
  64. {
  65. .cpu_funcs = {codelet_null},
  66. .cuda_funcs = {codelet_null},
  67. .opencl_funcs = {codelet_null},
  68. .cpu_funcs_name = {"codelet_null"},
  69. .nbuffers = 1,
  70. .modes = {STARPU_R}
  71. };
  72. static struct starpu_codelet cl_w =
  73. {
  74. .cpu_funcs = {codelet_null},
  75. .cuda_funcs = {codelet_null},
  76. .opencl_funcs = {codelet_null},
  77. .cpu_funcs_name = {"codelet_null"},
  78. .nbuffers = 1,
  79. .modes = {STARPU_W}
  80. };
  81. static struct starpu_codelet cl_rw =
  82. {
  83. .cpu_funcs = {codelet_null},
  84. .cuda_funcs = {codelet_null},
  85. .opencl_funcs = {codelet_null},
  86. .cpu_funcs_name = {"codelet_null"},
  87. .nbuffers = 1,
  88. .modes = {STARPU_RW}
  89. };
  90. static struct starpu_codelet *select_codelet_with_random_mode(void)
  91. {
  92. int r = rand();
  93. switch (r % 3)
  94. {
  95. case 0:
  96. return &cl_r;
  97. case 1:
  98. return &cl_w;
  99. case 2:
  100. return &cl_rw;
  101. };
  102. return &cl_rw;
  103. }
  104. int main(int argc, char **argv)
  105. {
  106. int ret;
  107. ret = starpu_initialize(NULL, &argc, &argv);
  108. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  109. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  110. ret = starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  112. starpu_vector_data_register(&v_handle, STARPU_MAIN_RAM, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  113. unsigned nworker = starpu_worker_get_count();
  114. cnt = nworker*N;
  115. unsigned iter, worker;
  116. for (iter = 0; iter < N; iter++)
  117. {
  118. for (worker = 0; worker < nworker; worker++)
  119. {
  120. /* execute a task on that worker */
  121. struct starpu_task *task = starpu_task_create();
  122. task->handles[0] = v_handle;
  123. task->cl = select_codelet_with_random_mode();
  124. task->callback_func = callback;
  125. task->callback_arg = NULL;
  126. task->execute_on_a_specific_worker = 1;
  127. task->workerid = worker;
  128. ret = starpu_task_submit(task);
  129. if (ret == -ENODEV) goto enodev;
  130. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  131. }
  132. }
  133. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  134. while (!finished)
  135. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  136. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  137. starpu_data_unregister(v_handle);
  138. starpu_free(v);
  139. starpu_shutdown();
  140. return EXIT_SUCCESS;
  141. enodev:
  142. starpu_data_unregister(v_handle);
  143. starpu_free(v);
  144. starpu_shutdown();
  145. fprintf(stderr, "WARNING: No one can execute this task\n");
  146. /* yes, we do not perform the computation but we did detect that no one
  147. * could perform the kernel, so this is not an error from StarPU */
  148. return STARPU_TEST_SKIPPED;
  149. }