execute_on_a_specific_worker.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  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. #ifdef STARPU_QUICK_CHECK
  25. #define N 100
  26. #else
  27. #define N 1000
  28. #endif
  29. #define VECTORSIZE 1024
  30. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  31. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  32. static unsigned finished = 0;
  33. static unsigned cnt;
  34. starpu_data_handle_t v_handle;
  35. static unsigned *v;
  36. static void callback(void *arg)
  37. {
  38. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  39. if (res == 0)
  40. {
  41. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  42. finished = 1;
  43. STARPU_PTHREAD_COND_SIGNAL(&cond);
  44. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  45. }
  46. }
  47. void codelet_null(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  48. {
  49. // int id = starpu_worker_get_id();
  50. // FPRINTF(stderr, "worker #%d\n", id);
  51. }
  52. static struct starpu_codelet cl_r =
  53. {
  54. .cpu_funcs = {codelet_null, NULL},
  55. .cuda_funcs = {codelet_null, NULL},
  56. .opencl_funcs = {codelet_null, NULL},
  57. .cpu_funcs_name = {"codelet_null", NULL},
  58. .nbuffers = 1,
  59. .modes = {STARPU_R}
  60. };
  61. static struct starpu_codelet cl_w =
  62. {
  63. .cpu_funcs = {codelet_null, NULL},
  64. .cuda_funcs = {codelet_null, NULL},
  65. .opencl_funcs = {codelet_null, NULL},
  66. .cpu_funcs_name = {"codelet_null", NULL},
  67. .nbuffers = 1,
  68. .modes = {STARPU_W}
  69. };
  70. static struct starpu_codelet cl_rw =
  71. {
  72. .cpu_funcs = {codelet_null, NULL},
  73. .cuda_funcs = {codelet_null, NULL},
  74. .opencl_funcs = {codelet_null, NULL},
  75. .cpu_funcs_name = {"codelet_null", NULL},
  76. .nbuffers = 1,
  77. .modes = {STARPU_RW}
  78. };
  79. static struct starpu_codelet *select_codelet_with_random_mode(void)
  80. {
  81. int r = rand();
  82. switch (r % 3)
  83. {
  84. case 0:
  85. return &cl_r;
  86. case 1:
  87. return &cl_w;
  88. case 2:
  89. return &cl_rw;
  90. };
  91. return &cl_rw;
  92. }
  93. int main(int argc, char **argv)
  94. {
  95. int ret;
  96. ret = starpu_initialize(NULL, &argc, &argv);
  97. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  99. ret = starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  100. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  101. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  102. unsigned nworker = starpu_worker_get_count();
  103. cnt = nworker*N;
  104. unsigned iter, worker;
  105. for (iter = 0; iter < N; iter++)
  106. {
  107. for (worker = 0; worker < nworker; worker++)
  108. {
  109. /* execute a task on that worker */
  110. struct starpu_task *task = starpu_task_create();
  111. task->handles[0] = v_handle;
  112. task->cl = select_codelet_with_random_mode();
  113. task->callback_func = callback;
  114. task->callback_arg = NULL;
  115. task->execute_on_a_specific_worker = 1;
  116. task->workerid = worker;
  117. ret = starpu_task_submit(task);
  118. if (ret == -ENODEV) goto enodev;
  119. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  120. }
  121. }
  122. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  123. while (!finished)
  124. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  125. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  126. starpu_data_unregister(v_handle);
  127. starpu_free(v);
  128. starpu_shutdown();
  129. return EXIT_SUCCESS;
  130. enodev:
  131. starpu_data_unregister(v_handle);
  132. starpu_free(v);
  133. starpu_shutdown();
  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 STARPU_TEST_SKIPPED;
  138. }