data_locality.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-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. #include <starpu.h>
  17. #include <starpu_scheduler.h>
  18. #include "../helper.h"
  19. /*
  20. * Check that scheduling policies tend to put tasks on the worker which has a
  21. * copy of the data
  22. */
  23. #define NTASKS 8
  24. /*
  25. * It is very inefficient to keep moving data between memory nodes. This
  26. * test makes sure the scheduler will take account of the data locality
  27. * when scheduling tasks.
  28. *
  29. * Applies to : dmda, pheft.
  30. */
  31. void dummy(void *buffers[], void *args)
  32. {
  33. (void) buffers;
  34. (void) args;
  35. }
  36. /*
  37. * Dummy cost function, used to make sure the scheduler does schedule the
  38. * task, instead of getting rid of it as soon as possible because it doesn't
  39. * know its expected length.
  40. */
  41. static double
  42. cost_function(struct starpu_task *task, unsigned nimpl)
  43. {
  44. (void) task;
  45. (void) nimpl;
  46. return 1.0;
  47. }
  48. static struct starpu_perfmodel model =
  49. {
  50. .type = STARPU_COMMON,
  51. .cost_function = cost_function
  52. };
  53. static struct starpu_codelet cl =
  54. {
  55. .cpu_funcs = { dummy },
  56. .cuda_funcs = { dummy },
  57. .opencl_funcs = { dummy },
  58. .modes = { STARPU_RW },
  59. .model = &model,
  60. .nbuffers = 1
  61. };
  62. static int var = 42;
  63. static starpu_data_handle_t rw_handle;
  64. static void
  65. init_data(void)
  66. {
  67. starpu_variable_data_register(&rw_handle, STARPU_MAIN_RAM, (uintptr_t) &var,
  68. sizeof(var));
  69. }
  70. static void
  71. free_data(void)
  72. {
  73. starpu_data_unregister(rw_handle);
  74. }
  75. static int
  76. run(struct starpu_sched_policy *policy)
  77. {
  78. int ret;
  79. struct starpu_conf conf;
  80. starpu_conf_init(&conf);
  81. conf.sched_policy = policy;
  82. ret = starpu_init(&conf);
  83. if (ret == -ENODEV)
  84. {
  85. FPRINTF(stderr, "No device found\n");
  86. return -ENODEV;
  87. }
  88. if (starpu_cpu_worker_get_count() == 0 || (starpu_cuda_worker_get_count() == 0 && starpu_opencl_worker_get_count() == 0))
  89. goto enodev;
  90. starpu_profiling_status_set(1);
  91. init_data();
  92. /* Send the handle to a GPU. */
  93. cl.where = STARPU_CUDA | STARPU_OPENCL;
  94. struct starpu_task *tasks[NTASKS];
  95. tasks[0] = starpu_task_create();
  96. tasks[0]->cl = &cl;
  97. tasks[0]->synchronous = 1;
  98. tasks[0]->handles[0] = rw_handle;
  99. tasks[0]->destroy = 0;
  100. ret = starpu_task_submit(tasks[0]);
  101. if (ret == -ENODEV)
  102. goto enodev;
  103. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  104. /* Now, run multiple tasks using this handle. */
  105. cl.where |= STARPU_CPU;
  106. int i;
  107. for (i = 1; i < NTASKS; i++)
  108. {
  109. tasks[i] = starpu_task_create();
  110. tasks[i]->cl = &cl;
  111. tasks[i]->handles[0] = rw_handle;
  112. tasks[i]->destroy = 0;
  113. ret = starpu_task_submit(tasks[i]);
  114. if (ret == -ENODEV)
  115. goto enodev;
  116. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  117. }
  118. starpu_task_wait_for_all();
  119. /* All tasks should have been executed on the same GPU. */
  120. ret = 0;
  121. int workerid = tasks[0]->profiling_info->workerid;
  122. for (i = 0; i < NTASKS; i++)
  123. {
  124. if (tasks[i]->profiling_info->workerid != workerid)
  125. {
  126. FPRINTF(stderr, "Error for task %d. Worker id %d different from expected worker id %d\n", i, tasks[i]->profiling_info->workerid, workerid);
  127. ret = 1;
  128. break;
  129. }
  130. starpu_task_destroy(tasks[i]);
  131. }
  132. /* Clean everything up. */
  133. for (; i < NTASKS; i++)
  134. starpu_task_destroy(tasks[i]);
  135. free_data();
  136. starpu_shutdown();
  137. return ret;
  138. enodev:
  139. FPRINTF(stderr, "No device found\n");
  140. starpu_shutdown();
  141. return -ENODEV;
  142. }
  143. /* XXX: Does this test apply to other schedulers ? */
  144. //extern struct starpu_sched_policy _starpu_sched_ws_policy;
  145. //extern struct starpu_sched_policy _starpu_sched_prio_policy;
  146. //extern struct starpu_sched_policy _starpu_sched_random_policy;
  147. //extern struct starpu_sched_policy _starpu_sched_dm_policy;
  148. extern struct starpu_sched_policy _starpu_sched_dmda_policy;
  149. //extern struct starpu_sched_policy _starpu_sched_dmda_ready_policy;
  150. //extern struct starpu_sched_policy _starpu_sched_dmda_sorted_policy;
  151. //extern struct starpu_sched_policy _starpu_sched_eager_policy;
  152. extern struct starpu_sched_policy _starpu_sched_parallel_heft_policy;
  153. //extern struct starpu_sched_policy _starpu_sched_peager_policy;
  154. static struct starpu_sched_policy *policies[] =
  155. {
  156. //&_starpu_sched_ws_policy,
  157. //&_starpu_sched_prio_policy,
  158. //&_starpu_sched_dm_policy,
  159. &_starpu_sched_dmda_policy,
  160. //&_starpu_sched_dmda_ready_policy,
  161. //&_starpu_sched_dmda_sorted_policy,
  162. //&_starpu_sched_random_policy,
  163. //&_starpu_sched_eager_policy,
  164. &_starpu_sched_parallel_heft_policy,
  165. //&_starpu_sched_peager_policy
  166. };
  167. int main(void)
  168. {
  169. int i;
  170. int n_policies = sizeof(policies)/sizeof(policies[0]);
  171. int global_ret = 0;
  172. char *sched = getenv("STARPU_SCHED");
  173. for (i = 0; i < n_policies; ++i)
  174. {
  175. struct starpu_sched_policy *policy = policies[i];
  176. if (sched && strcmp(sched, policy->policy_name))
  177. /* Testing another specific scheduler, no need to run this */
  178. continue;
  179. FPRINTF(stdout, "Running with policy %s.\n", policy->policy_name);
  180. int ret = run(policy);
  181. if (ret == -ENODEV && global_ret == 0)
  182. global_ret = STARPU_TEST_SKIPPED;
  183. if (ret == 1 && global_ret == 0)
  184. global_ret = ret;
  185. }
  186. return global_ret;
  187. }