dummy_sched_with_ctx.c 5.5 KB

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