locality.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2016-2020 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. /*
  17. * This is a dumb sample of stencil application
  18. *
  19. * Dumb domain split in N pieces:
  20. *
  21. * 0 | 1 | ... | N-1
  22. *
  23. * for each simulation iteration, a task works on some adjacent pieces
  24. *
  25. * Locality is thus set on the central piece.
  26. */
  27. #include <starpu.h>
  28. #include "../helper.h"
  29. #define N 50
  30. #define ITER 50
  31. int task_worker[N][ITER];
  32. int worker_task[STARPU_NMAXWORKERS][N*ITER];
  33. unsigned worker_ntask[STARPU_NMAXWORKERS];
  34. void cpu_f(void *descr[], void *_args)
  35. {
  36. (void)descr;
  37. unsigned i, loop, worker = starpu_worker_get_id();
  38. enum starpu_worker_archtype worker_type = starpu_worker_get_type(worker);
  39. starpu_codelet_unpack_args(_args, &loop, &i);
  40. task_worker[i][loop] = worker;
  41. worker_task[worker][worker_ntask[worker]++] = i;
  42. if (worker_type == STARPU_CPU_WORKER)
  43. starpu_sleep(0.001);
  44. else
  45. starpu_sleep(0.0001);
  46. }
  47. double cost_function(struct starpu_task *t, struct starpu_perfmodel_arch *a, unsigned i)
  48. {
  49. (void) t; (void) i;
  50. STARPU_ASSERT(a->ndevices == 1);
  51. if (a->devices[0].type == STARPU_CPU_WORKER)
  52. {
  53. STARPU_ASSERT(a->devices[0].ncores == 1);
  54. return 1000;
  55. }
  56. else
  57. return 100;
  58. }
  59. static struct starpu_perfmodel perf_model =
  60. {
  61. .type = STARPU_PER_ARCH,
  62. .arch_cost_function = cost_function,
  63. };
  64. static struct starpu_codelet cl =
  65. {
  66. .cpu_funcs = { cpu_f },
  67. .cpu_funcs_name = { "cpu_f" },
  68. .cuda_funcs = { cpu_f },
  69. .opencl_funcs = { cpu_f },
  70. .nbuffers = 4,
  71. .modes =
  72. {
  73. STARPU_RW,
  74. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY,
  75. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY,
  76. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY,
  77. },
  78. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  79. .model = &perf_model,
  80. };
  81. int main(int argc, char *argv[])
  82. {
  83. int ret;
  84. starpu_data_handle_t A[N];
  85. starpu_data_handle_t B[N];
  86. unsigned i, loop, finished;
  87. ret = starpu_initialize(NULL, &argc, &argv);
  88. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  89. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  90. /* Get most parallelism by using an arbiter */
  91. starpu_arbiter_t arbiter = starpu_arbiter_create();
  92. for (i = 0; i < N; i++)
  93. {
  94. starpu_void_data_register(&A[i]);
  95. starpu_void_data_register(&B[i]);
  96. starpu_data_assign_arbiter(A[i], arbiter);
  97. }
  98. for (loop = 0; loop < ITER; loop++)
  99. {
  100. for (i = 1; i < N-1; i++)
  101. {
  102. starpu_task_insert(&cl,
  103. STARPU_RW, B[i],
  104. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY, A[i-1],
  105. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY, A[i],
  106. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY, A[i+1],
  107. STARPU_VALUE, &loop, sizeof(loop),
  108. STARPU_VALUE, &i, sizeof(i),
  109. 0);
  110. }
  111. }
  112. starpu_task_wait_for_all();
  113. for (i = 0; i < N; i++)
  114. {
  115. starpu_data_unregister(A[i]);
  116. starpu_data_unregister(B[i]);
  117. }
  118. starpu_arbiter_destroy(arbiter);
  119. printf("worker where each domain piece was computed, over time\n");
  120. for (loop = 0; loop < ITER; loop++)
  121. {
  122. for (i = 1; i < N-1; i++)
  123. {
  124. printf("%02d ", task_worker[i][loop]);
  125. }
  126. printf("\n");
  127. }
  128. printf("\n");
  129. printf("domain piece that each worker has computed, over time\n");
  130. loop = 0;
  131. do
  132. {
  133. finished = 1;
  134. for (i = 0; i < starpu_worker_get_count(); i++)
  135. {
  136. if (loop < worker_ntask[i])
  137. {
  138. printf("%02d ", worker_task[i][loop]);
  139. finished = 0;
  140. }
  141. else
  142. printf(" ");
  143. }
  144. loop++;
  145. printf("\n");
  146. }
  147. while (!finished && loop < 100);
  148. starpu_shutdown();
  149. return EXIT_SUCCESS;
  150. }