data_locality.c 5.5 KB

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