dummy_sched.c 5.3 KB

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