dummy_sched.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. #include <sys/time.h>
  17. #include <pthread.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <starpu.h>
  21. #define NTASKS 32000
  22. struct task_list_node {
  23. struct starpu_task *task;
  24. struct task_list_node *prev;
  25. struct task_list_node *next;
  26. };
  27. struct task_list {
  28. struct task_list_node *head;
  29. struct task_list_node *tail;
  30. };
  31. struct task_list sched_list;
  32. static pthread_cond_t sched_cond;
  33. static pthread_mutex_t sched_mutex;
  34. static void init_dummy_sched(struct starpu_machine_topology_s *topology,
  35. struct starpu_sched_policy_s *policy)
  36. {
  37. /* Create a linked-list of tasks and a condition variable to protect it */
  38. sched_list.head = NULL;
  39. sched_list.tail = NULL;
  40. pthread_mutex_init(&sched_mutex, NULL);
  41. pthread_cond_init(&sched_cond, NULL);
  42. unsigned workerid;
  43. for (workerid = 0; workerid < topology->nworkers; workerid++)
  44. starpu_worker_set_sched_condition(workerid, &sched_cond, &sched_mutex);
  45. }
  46. static void deinit_dummy_sched(struct starpu_machine_topology_s *topology,
  47. struct starpu_sched_policy_s *policy)
  48. {
  49. STARPU_ASSERT(sched_list.head == NULL);
  50. STARPU_ASSERT(sched_list.tail == NULL);
  51. pthread_cond_destroy(&sched_cond);
  52. pthread_mutex_destroy(&sched_mutex);
  53. }
  54. static int push_task_dummy(struct starpu_task *task)
  55. {
  56. pthread_mutex_lock(&sched_mutex);
  57. if (!sched_list.head)
  58. {
  59. /* This is the first element in the queue */
  60. sched_list.head = malloc(sizeof(struct task_list_node));
  61. sched_list.head->task = task;
  62. sched_list.head->prev = NULL;
  63. sched_list.head->next = NULL;
  64. sched_list.tail = sched_list.head;
  65. }
  66. else {
  67. struct task_list_node *node = malloc(sizeof(struct task_list_node));
  68. node->task = task;
  69. sched_list.tail->next = node;
  70. node->prev = sched_list.tail;
  71. node->next = NULL;
  72. sched_list.tail = node;
  73. }
  74. pthread_cond_signal(&sched_cond);
  75. pthread_mutex_unlock(&sched_mutex);
  76. }
  77. /* The mutex associated to the calling worker is already taken by StarPU */
  78. static struct starpu_task *pop_task_dummy(void)
  79. {
  80. struct starpu_task *task = NULL;
  81. struct task_list_node *link, *second;
  82. if (!sched_list.head)
  83. return NULL;
  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. link = sched_list.head;
  90. task = link->task;
  91. second = link->next;
  92. if (second)
  93. {
  94. second->prev = NULL;
  95. sched_list.head = second;
  96. }
  97. else {
  98. sched_list.head = NULL;
  99. sched_list.tail = NULL;
  100. }
  101. return task;
  102. }
  103. static struct starpu_sched_policy_s dummy_sched_policy = {
  104. .init_sched = init_dummy_sched,
  105. .deinit_sched = deinit_dummy_sched,
  106. .push_task = push_task_dummy,
  107. .pop_task = pop_task_dummy,
  108. .pop_every_task = NULL,
  109. .policy_name = "dummy",
  110. .policy_description = "dummy scheduling strategy"
  111. };
  112. static struct starpu_conf conf = {
  113. .sched_policy_name = NULL,
  114. .sched_policy = &dummy_sched_policy,
  115. .ncpus = -1,
  116. .ncuda = -1,
  117. .nopencl = -1,
  118. .nspus = -1,
  119. .use_explicit_workers_bindid = 0,
  120. .use_explicit_workers_cuda_gpuid = 0,
  121. .use_explicit_workers_opencl_gpuid = 0,
  122. .calibrate = 0
  123. };
  124. static void dummy_func(void *descr[] __attribute__ ((unused)), void *arg __attribute__ ((unused)))
  125. {
  126. }
  127. static starpu_codelet dummy_codelet =
  128. {
  129. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  130. .cpu_func = dummy_func,
  131. .cuda_func = dummy_func,
  132. .opencl_func = dummy_func,
  133. .model = NULL,
  134. .nbuffers = 0
  135. };
  136. int main(int argc, char **argv)
  137. {
  138. starpu_init(&conf);
  139. unsigned i;
  140. for (i = 0; i < NTASKS; i++)
  141. {
  142. struct starpu_task *task = starpu_task_create();
  143. task->cl = &dummy_codelet;
  144. task->cl_arg = NULL;
  145. starpu_task_submit(task);
  146. }
  147. starpu_task_wait_for_all();
  148. starpu_shutdown();
  149. return 0;
  150. }