dummy_sched.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010-2012 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 <pthread.h>
  18. #include <starpu.h>
  19. #define NTASKS 32000
  20. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  21. typedef struct dummy_sched_data {
  22. struct starpu_task_list sched_list;
  23. pthread_mutex_t sched_mutex;
  24. pthread_cond_t sched_cond;
  25. } dummy_sched_data;
  26. static void init_dummy_sched_for_workers(unsigned sched_ctx_id, int *workerids, unsigned nnew_workers)
  27. {
  28. struct dummy_sched_data *data = (struct dummy_sched_data*)starpu_get_sched_ctx_policy_data(sched_ctx_id);
  29. unsigned i;
  30. int workerid;
  31. for(i = 0; i < nnew_workers; i++)
  32. {
  33. workerid = workerids[i];
  34. starpu_worker_set_sched_condition(sched_ctx_id, workerid, &data->sched_mutex, &data->sched_cond);
  35. }
  36. }
  37. static void init_dummy_sched(unsigned sched_ctx_id)
  38. {
  39. unsigned nworkers_ctx = starpu_get_nworkers_of_ctx(sched_ctx_id);
  40. struct dummy_sched_data *data = (struct dummy_sched_data*)malloc(sizeof(struct dummy_sched_data));
  41. /* Create a linked-list of tasks and a condition variable to protect it */
  42. starpu_task_list_init(&data->sched_list);
  43. pthread_mutex_init(&data->sched_mutex, NULL);
  44. pthread_cond_init(&data->sched_cond, NULL);
  45. starpu_set_sched_ctx_policy_data(sched_ctx_id, (void*)data);
  46. int *workerids = starpu_get_workers_of_ctx(sched_ctx_id);
  47. int workerid;
  48. unsigned workerid_ctx;
  49. for (workerid_ctx = 0; workerid_ctx < nworkers_ctx; workerid_ctx++)
  50. {
  51. workerid = workerids[workerid_ctx];
  52. starpu_worker_set_sched_condition(sched_ctx_id, workerid, &data->sched_mutex, &data->sched_cond);
  53. }
  54. FPRINTF(stderr, "Initialising Dummy scheduler\n");
  55. }
  56. static void deinit_dummy_sched(unsigned sched_ctx_id)
  57. {
  58. struct dummy_sched_data *data = (struct dummy_sched_data*)starpu_get_sched_ctx_policy_data(sched_ctx_id);
  59. STARPU_ASSERT(starpu_task_list_empty(&data->sched_list));
  60. unsigned nworkers_ctx = starpu_get_nworkers_of_ctx(sched_ctx_id);
  61. int *workerids = starpu_get_workers_of_ctx(sched_ctx_id);
  62. int workerid;
  63. unsigned workerid_ctx;
  64. for (workerid_ctx = 0; workerid_ctx < nworkers_ctx; workerid_ctx++)
  65. {
  66. workerid = workerids[workerid_ctx];
  67. starpu_worker_set_sched_condition(sched_ctx_id, workerid, NULL, NULL);
  68. }
  69. pthread_cond_destroy(&data->sched_cond);
  70. pthread_mutex_destroy(&data->sched_mutex);
  71. free(data);
  72. FPRINTF(stderr, "Destroying Dummy scheduler\n");
  73. }
  74. static int push_task_dummy(struct starpu_task *task, unsigned sched_ctx_id)
  75. {
  76. struct dummy_sched_data *data = (struct dummy_sched_data*)starpu_get_sched_ctx_policy_data(sched_ctx_id);
  77. pthread_mutex_lock(&data->sched_mutex);
  78. starpu_task_list_push_front(&data->sched_list, task);
  79. pthread_cond_signal(&data->sched_cond);
  80. pthread_mutex_unlock(&data->sched_mutex);
  81. return 0;
  82. }
  83. /* The mutex associated to the calling worker is already taken by StarPU */
  84. static struct starpu_task *pop_task_dummy(unsigned sched_ctx_id)
  85. {
  86. /* NB: In this simplistic strategy, we assume that all workers are able
  87. * to execute all tasks, otherwise, it would have been necessary to go
  88. * through the entire list until we find a task that is executable from
  89. * the calling worker. So we just take the head of the list and give it
  90. * to the worker. */
  91. struct dummy_sched_data *data = (struct dummy_sched_data*)starpu_get_sched_ctx_policy_data(sched_ctx_id);
  92. return starpu_task_list_pop_back(&data->sched_list);
  93. }
  94. static struct starpu_sched_policy dummy_sched_policy =
  95. {
  96. .init_sched = init_dummy_sched,
  97. .init_sched_for_workers = init_dummy_sched_for_workers,
  98. .deinit_sched = deinit_dummy_sched,
  99. .push_task = push_task_dummy,
  100. .pop_task = pop_task_dummy,
  101. .post_exec_hook = NULL,
  102. .pop_every_task = NULL,
  103. .policy_name = "dummy",
  104. .policy_description = "dummy scheduling strategy"
  105. };
  106. static struct starpu_conf conf =
  107. {
  108. .sched_policy_name = NULL,
  109. .sched_policy = &dummy_sched_policy,
  110. .ncpus = -1,
  111. .ncuda = -1,
  112. .nopencl = -1,
  113. .nspus = -1,
  114. .use_explicit_workers_bindid = 0,
  115. .use_explicit_workers_cuda_gpuid = 0,
  116. .use_explicit_workers_opencl_gpuid = 0,
  117. .calibrate = 0
  118. };
  119. static void dummy_func(void *descr[] __attribute__ ((unused)), void *arg __attribute__ ((unused)))
  120. {
  121. }
  122. static struct starpu_codelet dummy_codelet =
  123. {
  124. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  125. .cpu_funcs = {dummy_func, NULL},
  126. .cuda_funcs = {dummy_func, NULL},
  127. .opencl_funcs = {dummy_func, NULL},
  128. .model = NULL,
  129. .nbuffers = 0
  130. };
  131. int main(int argc, char **argv)
  132. {
  133. int ntasks = NTASKS;
  134. int ret;
  135. ret = starpu_init(&conf);
  136. if (ret == -ENODEV)
  137. return 77;
  138. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  139. #ifdef STARPU_SLOW_MACHINE
  140. ntasks /= 100;
  141. #endif
  142. unsigned i;
  143. for (i = 0; i < ntasks; i++)
  144. {
  145. struct starpu_task *task = starpu_task_create();
  146. task->cl = &dummy_codelet;
  147. task->cl_arg = NULL;
  148. ret = starpu_task_submit(task);
  149. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  150. }
  151. starpu_task_wait_for_all();
  152. starpu_shutdown();
  153. return 0;
  154. }