locality.c 4.0 KB

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