dummy_sched.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2016 Université de Bordeaux
  4. * Copyright (C) 2010-2013, 2016 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. /*
  18. * This is an example of an application-defined scheduler.
  19. * This is a mere eager scheduler with a centralized list of tasks to schedule:
  20. * when a task becomes ready (push) it is put on the list. When a device
  21. * becomes ready (pop), a task is taken from the list.
  22. */
  23. #include <starpu.h>
  24. #include <starpu_scheduler.h>
  25. #include <config.h>
  26. #ifdef STARPU_QUICK_CHECK
  27. #define NTASKS 320
  28. #elif !defined(STARPU_LONG_CHECK)
  29. #define NTASKS 3200
  30. #else
  31. #define NTASKS 32000
  32. #endif
  33. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  34. struct dummy_sched_data
  35. {
  36. struct starpu_task_list sched_list;
  37. starpu_pthread_mutex_t policy_mutex;
  38. };
  39. static void init_dummy_sched(unsigned sched_ctx_id)
  40. {
  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. starpu_sched_ctx_set_policy_data(sched_ctx_id, (void*)data);
  45. STARPU_PTHREAD_MUTEX_INIT(&data->policy_mutex, NULL);
  46. FPRINTF(stderr, "Initialising Dummy scheduler\n");
  47. }
  48. static void deinit_dummy_sched(unsigned sched_ctx_id)
  49. {
  50. struct dummy_sched_data *data = (struct dummy_sched_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  51. STARPU_ASSERT(starpu_task_list_empty(&data->sched_list));
  52. STARPU_PTHREAD_MUTEX_DESTROY(&data->policy_mutex);
  53. free(data);
  54. FPRINTF(stderr, "Destroying Dummy scheduler\n");
  55. }
  56. static int push_task_dummy(struct starpu_task *task)
  57. {
  58. unsigned sched_ctx_id = task->sched_ctx;
  59. struct dummy_sched_data *data = (struct dummy_sched_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  60. /* NB: In this simplistic strategy, we assume that the context in which
  61. we push task has at least one worker*/
  62. /* lock all workers when pushing tasks on a list where all
  63. of them would pop for tasks */
  64. STARPU_PTHREAD_MUTEX_LOCK(&data->policy_mutex);
  65. starpu_task_list_push_front(&data->sched_list, task);
  66. starpu_push_task_end(task);
  67. STARPU_PTHREAD_MUTEX_UNLOCK(&data->policy_mutex);
  68. /*if there are no tasks block */
  69. /* wake people waiting for a task */
  70. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  71. struct starpu_sched_ctx_iterator it;
  72. workers->init_iterator(workers, &it);
  73. while(workers->has_next(workers, &it))
  74. {
  75. unsigned worker;
  76. worker = workers->get_next(workers, &it);
  77. starpu_pthread_mutex_t *sched_mutex;
  78. starpu_pthread_cond_t *sched_cond;
  79. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  80. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  81. STARPU_PTHREAD_COND_SIGNAL(sched_cond);
  82. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  83. }
  84. return 0;
  85. }
  86. /* The mutex associated to the calling worker is already taken by StarPU */
  87. static struct starpu_task *pop_task_dummy(unsigned sched_ctx_id)
  88. {
  89. /* NB: In this simplistic strategy, we assume that all workers are able
  90. * to execute all tasks, otherwise, it would have been necessary to go
  91. * through the entire list until we find a task that is executable from
  92. * the calling worker. So we just take the head of the list and give it
  93. * to the worker. */
  94. struct dummy_sched_data *data = (struct dummy_sched_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  95. STARPU_PTHREAD_MUTEX_LOCK(&data->policy_mutex);
  96. struct starpu_task *task = starpu_task_list_pop_back(&data->sched_list);
  97. STARPU_PTHREAD_MUTEX_UNLOCK(&data->policy_mutex);
  98. return task;
  99. }
  100. static struct starpu_sched_policy dummy_sched_policy =
  101. {
  102. .init_sched = init_dummy_sched,
  103. .deinit_sched = deinit_dummy_sched,
  104. .push_task = push_task_dummy,
  105. .pop_task = pop_task_dummy,
  106. .policy_name = "dummy",
  107. .policy_description = "dummy scheduling strategy",
  108. .worker_type = STARPU_WORKER_LIST,
  109. };
  110. void dummy_func(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg STARPU_ATTRIBUTE_UNUSED)
  111. {
  112. }
  113. static struct starpu_codelet dummy_codelet =
  114. {
  115. .cpu_funcs = {dummy_func},
  116. .cpu_funcs_name = {"dummy_func"},
  117. .cuda_funcs = {dummy_func},
  118. .opencl_funcs = {dummy_func},
  119. .model = NULL,
  120. .nbuffers = 0,
  121. .name = "dummy",
  122. };
  123. int main(int argc, char **argv)
  124. {
  125. int ntasks = NTASKS;
  126. int ret;
  127. struct starpu_conf conf;
  128. starpu_conf_init(&conf);
  129. conf.sched_policy = &dummy_sched_policy,
  130. ret = starpu_init(&conf);
  131. if (ret == -ENODEV)
  132. return 77;
  133. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  134. #ifdef STARPU_QUICK_CHECK
  135. ntasks /= 100;
  136. #endif
  137. int 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. ret = starpu_task_submit(task);
  144. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  145. }
  146. starpu_task_wait_for_all();
  147. starpu_shutdown();
  148. return 0;
  149. }