dummy_sched.c 5.4 KB

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