data_locality.c 5.2 KB

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