execute_on_a_specific_worker.c 4.1 KB

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