dummy_sched.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2015 Université de Bordeaux
  4. * Copyright (C) 2010-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. /*
  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. #define NTASKS 32000
  26. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  27. struct dummy_sched_data
  28. {
  29. struct starpu_task_list sched_list;
  30. starpu_pthread_mutex_t policy_mutex;
  31. };
  32. static void init_dummy_sched(unsigned sched_ctx_id)
  33. {
  34. starpu_sched_ctx_create_worker_collection(sched_ctx_id, STARPU_WORKER_LIST);
  35. struct dummy_sched_data *data = (struct dummy_sched_data*)malloc(sizeof(struct dummy_sched_data));
  36. /* Create a linked-list of tasks and a condition variable to protect it */
  37. starpu_task_list_init(&data->sched_list);
  38. starpu_sched_ctx_set_policy_data(sched_ctx_id, (void*)data);
  39. starpu_pthread_mutex_init(&data->policy_mutex, NULL);
  40. FPRINTF(stderr, "Initialising Dummy scheduler\n");
  41. }
  42. static void deinit_dummy_sched(unsigned sched_ctx_id)
  43. {
  44. struct dummy_sched_data *data = (struct dummy_sched_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  45. STARPU_ASSERT(starpu_task_list_empty(&data->sched_list));
  46. starpu_sched_ctx_delete_worker_collection(sched_ctx_id);
  47. starpu_pthread_mutex_destroy(&data->policy_mutex);
  48. free(data);
  49. FPRINTF(stderr, "Destroying Dummy scheduler\n");
  50. }
  51. static int push_task_dummy(struct starpu_task *task)
  52. {
  53. unsigned sched_ctx_id = task->sched_ctx;
  54. struct dummy_sched_data *data = (struct dummy_sched_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  55. /* NB: In this simplistic strategy, we assume that the context in which
  56. we push task has at least one worker*/
  57. /* lock all workers when pushing tasks on a list where all
  58. of them would pop for tasks */
  59. starpu_pthread_mutex_lock(&data->policy_mutex);
  60. starpu_task_list_push_front(&data->sched_list, task);
  61. starpu_push_task_end(task);
  62. starpu_pthread_mutex_unlock(&data->policy_mutex);
  63. /*if there are no tasks block */
  64. /* wake people waiting for a task */
  65. unsigned worker = 0;
  66. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  67. struct starpu_sched_ctx_iterator it;
  68. workers->init_iterator(workers, &it);
  69. while(workers->has_next(workers, &it))
  70. {
  71. worker = workers->get_next(workers, &it);
  72. starpu_pthread_mutex_t *sched_mutex;
  73. starpu_pthread_cond_t *sched_cond;
  74. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  75. starpu_pthread_mutex_lock(sched_mutex);
  76. starpu_pthread_cond_signal(sched_cond);
  77. starpu_pthread_mutex_unlock(sched_mutex);
  78. }
  79. return 0;
  80. }
  81. /* The mutex associated to the calling worker is already taken by StarPU */
  82. static struct starpu_task *pop_task_dummy(unsigned sched_ctx_id)
  83. {
  84. /* NB: In this simplistic strategy, we assume that all workers are able
  85. * to execute all tasks, otherwise, it would have been necessary to go
  86. * through the entire list until we find a task that is executable from
  87. * the calling worker. So we just take the head of the list and give it
  88. * to the worker. */
  89. struct dummy_sched_data *data = (struct dummy_sched_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  90. starpu_pthread_mutex_lock(&data->policy_mutex);
  91. struct starpu_task *task = starpu_task_list_pop_back(&data->sched_list);
  92. starpu_pthread_mutex_unlock(&data->policy_mutex);
  93. return task;
  94. }
  95. static struct starpu_sched_policy dummy_sched_policy =
  96. {
  97. .init_sched = init_dummy_sched,
  98. .add_workers = NULL,
  99. .remove_workers = NULL,
  100. .deinit_sched = deinit_dummy_sched,
  101. .push_task = push_task_dummy,
  102. .pop_task = pop_task_dummy,
  103. .post_exec_hook = NULL,
  104. .pop_every_task = NULL,
  105. .policy_name = "dummy",
  106. .policy_description = "dummy scheduling strategy"
  107. };
  108. static void dummy_func(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg STARPU_ATTRIBUTE_UNUSED)
  109. {
  110. }
  111. static struct starpu_codelet dummy_codelet =
  112. {
  113. .cpu_funcs = {dummy_func},
  114. .cuda_funcs = {dummy_func},
  115. .opencl_funcs = {dummy_func},
  116. .model = NULL,
  117. .nbuffers = 0,
  118. .name = "dummy",
  119. };
  120. int main(int argc, char **argv)
  121. {
  122. int ntasks = NTASKS;
  123. int ret;
  124. struct starpu_conf conf;
  125. starpu_conf_init(&conf);
  126. conf.sched_policy = &dummy_sched_policy,
  127. ret = starpu_init(&conf);
  128. if (ret == -ENODEV)
  129. return 77;
  130. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  131. #ifdef STARPU_QUICK_CHECK
  132. ntasks /= 100;
  133. #endif
  134. int i;
  135. for (i = 0; i < ntasks; i++)
  136. {
  137. struct starpu_task *task = starpu_task_create();
  138. task->cl = &dummy_codelet;
  139. task->cl_arg = NULL;
  140. ret = starpu_task_submit(task);
  141. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  142. }
  143. starpu_task_wait_for_all();
  144. starpu_shutdown();
  145. return 0;
  146. }