locality.c 4.0 KB

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