dummy_sched_with_ctx.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. /*
  17. * This is an example of an application-defined scheduler run inside a
  18. * scheduling context.
  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. #ifdef STARPU_QUICK_CHECK
  26. #define NTASKS 320
  27. #elif !defined(STARPU_LONG_CHECK)
  28. #define NTASKS 3200
  29. #else
  30. #define NTASKS 32000
  31. #endif
  32. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  33. struct dummy_sched_data
  34. {
  35. struct starpu_task_list sched_list;
  36. starpu_pthread_mutex_t policy_mutex;
  37. };
  38. static void init_dummy_sched(unsigned sched_ctx_id)
  39. {
  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. starpu_sched_ctx_set_policy_data(sched_ctx_id, (void*)data);
  44. STARPU_PTHREAD_MUTEX_INIT(&data->policy_mutex, NULL);
  45. FPRINTF(stderr, "Initialising Dummy scheduler\n");
  46. }
  47. static void deinit_dummy_sched(unsigned sched_ctx_id)
  48. {
  49. struct dummy_sched_data *data = (struct dummy_sched_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  50. STARPU_ASSERT(starpu_task_list_empty(&data->sched_list));
  51. STARPU_PTHREAD_MUTEX_DESTROY(&data->policy_mutex);
  52. free(data);
  53. FPRINTF(stderr, "Destroying Dummy scheduler\n");
  54. }
  55. static int push_task_dummy(struct starpu_task *task)
  56. {
  57. unsigned sched_ctx_id = task->sched_ctx;
  58. struct dummy_sched_data *data = (struct dummy_sched_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  59. /* NB: In this simplistic strategy, we assume that the context in which
  60. we push task has at least one worker*/
  61. /* lock all workers when pushing tasks on a list where all
  62. of them would pop for tasks */
  63. STARPU_PTHREAD_MUTEX_LOCK(&data->policy_mutex);
  64. starpu_task_list_push_front(&data->sched_list, task);
  65. starpu_push_task_end(task);
  66. STARPU_PTHREAD_MUTEX_UNLOCK(&data->policy_mutex);
  67. /*if there are no tasks block */
  68. /* wake people waiting for a task */
  69. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  70. struct starpu_sched_ctx_iterator it;
  71. workers->init_iterator(workers, &it);
  72. while(workers->has_next(workers, &it))
  73. {
  74. unsigned worker;
  75. worker = workers->get_next(workers, &it);
  76. starpu_pthread_mutex_t *sched_mutex;
  77. starpu_pthread_cond_t *sched_cond;
  78. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  79. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  80. STARPU_PTHREAD_COND_SIGNAL(sched_cond);
  81. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  82. }
  83. return 0;
  84. }
  85. /* The mutex associated to the calling worker is already taken by StarPU */
  86. static struct starpu_task *pop_task_dummy(unsigned sched_ctx_id)
  87. {
  88. /* NB: In this simplistic strategy, we assume that all workers are able
  89. * to execute all tasks, otherwise, it would have been necessary to go
  90. * through the entire list until we find a task that is executable from
  91. * the calling worker. So we just take the head of the list and give it
  92. * to the worker. */
  93. struct dummy_sched_data *data = (struct dummy_sched_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  94. #ifdef STARPU_NON_BLOCKING_DRIVERS
  95. if (starpu_task_list_empty(&data->sched_list))
  96. return NULL;
  97. #endif
  98. STARPU_PTHREAD_MUTEX_LOCK(&data->policy_mutex);
  99. struct starpu_task *task = NULL;
  100. if (!starpu_task_list_empty(&data->sched_list))
  101. task = starpu_task_list_pop_back(&data->sched_list);
  102. STARPU_PTHREAD_MUTEX_UNLOCK(&data->policy_mutex);
  103. return task;
  104. }
  105. static struct starpu_sched_policy dummy_sched_policy =
  106. {
  107. .init_sched = init_dummy_sched,
  108. .add_workers = NULL,
  109. .remove_workers = NULL,
  110. .deinit_sched = deinit_dummy_sched,
  111. .push_task = push_task_dummy,
  112. .pop_task = pop_task_dummy,
  113. .post_exec_hook = NULL,
  114. .pop_every_task = NULL,
  115. .policy_name = "dummy",
  116. .policy_description = "dummy scheduling strategy",
  117. .worker_type = STARPU_WORKER_LIST,
  118. };
  119. void dummy_func(void *descr[], void *arg)
  120. {
  121. (void)descr;
  122. (void)arg;
  123. }
  124. static struct starpu_codelet dummy_codelet =
  125. {
  126. .cpu_funcs = {dummy_func},
  127. .cpu_funcs_name = {"dummy_func"},
  128. .cuda_funcs = {dummy_func},
  129. .opencl_funcs = {dummy_func},
  130. .model = NULL,
  131. .nbuffers = 0,
  132. .name = "dummy",
  133. };
  134. int main(void)
  135. {
  136. int ntasks = NTASKS;
  137. int ret;
  138. /* struct starpu_conf conf; */
  139. /* starpu_conf_init(&conf); */
  140. /* conf.sched_policy = &dummy_sched_policy, */
  141. ret = starpu_init(NULL);
  142. if (ret == -ENODEV)
  143. return 77;
  144. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  145. unsigned sched_ctx = starpu_sched_ctx_create(NULL, -1, "dummy", STARPU_SCHED_CTX_POLICY_STRUCT, &dummy_sched_policy, 0);
  146. #ifdef STARPU_QUICK_CHECK
  147. ntasks /= 100;
  148. #endif
  149. starpu_sched_ctx_set_context(&sched_ctx);
  150. int i;
  151. for (i = 0; i < ntasks; i++)
  152. {
  153. struct starpu_task *task = starpu_task_create();
  154. task->cl = &dummy_codelet;
  155. task->cl_arg = NULL;
  156. ret = starpu_task_submit(task);
  157. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  158. }
  159. starpu_task_wait_for_all();
  160. starpu_shutdown();
  161. return 0;
  162. }