locality.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2016 Université de Bordeaux
  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 adjactent 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[] STARPU_ATTRIBUTE_UNUSED, void *_args)
  35. {
  36. unsigned i, loop, worker = starpu_worker_get_id();
  37. starpu_codelet_unpack_args(_args, &loop, &i);
  38. task_worker[i][loop] = worker;
  39. worker_task[worker][worker_ntask[worker]++] = i;
  40. starpu_sleep(0.001);
  41. }
  42. static struct starpu_codelet cl =
  43. {
  44. .cpu_funcs = { cpu_f },
  45. .cpu_funcs_name = { "cpu_f" },
  46. .nbuffers = 4,
  47. .modes =
  48. {
  49. STARPU_RW,
  50. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY,
  51. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY,
  52. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY,
  53. },
  54. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  55. };
  56. int main(int argc, char *argv[])
  57. {
  58. int ret;
  59. starpu_data_handle_t A[N];
  60. starpu_data_handle_t B[N];
  61. unsigned i, loop, finished;
  62. ret = starpu_initialize(NULL, &argc, &argv);
  63. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  64. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  65. /* Get most parallelism by using an arbiter */
  66. starpu_arbiter_t arbiter = starpu_arbiter_create();
  67. for (i = 0; i < N; i++)
  68. {
  69. starpu_void_data_register(&A[i]);
  70. starpu_void_data_register(&B[i]);
  71. starpu_data_assign_arbiter(A[i], arbiter);
  72. }
  73. for (loop = 0; loop < ITER; loop++)
  74. {
  75. for (i = 1; i < N-1; i++)
  76. {
  77. starpu_task_insert(&cl,
  78. STARPU_RW, B[i],
  79. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY, A[i-1],
  80. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY, A[i],
  81. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY, A[i+1],
  82. STARPU_VALUE, &loop, sizeof(loop),
  83. STARPU_VALUE, &i, sizeof(i),
  84. 0);
  85. }
  86. }
  87. starpu_task_wait_for_all();
  88. for (i = 0; i < N; i++)
  89. {
  90. starpu_data_unregister(A[i]);
  91. starpu_data_unregister(B[i]);
  92. }
  93. starpu_arbiter_destroy(arbiter);
  94. printf("worker where each domain piece was computed, over time\n");
  95. for (loop = 0; loop < ITER; loop++)
  96. {
  97. for (i = 1; i < N-1; i++)
  98. {
  99. printf("%02d ", task_worker[i][loop]);
  100. }
  101. printf("\n");
  102. }
  103. printf("\n");
  104. printf("domain piece that each worker has computed, over time\n");
  105. loop = 0;
  106. do {
  107. finished = 1;
  108. for (i = 0; i < starpu_worker_get_count(); i++)
  109. {
  110. if (loop < worker_ntask[i])
  111. {
  112. printf("%02d ", worker_task[i][loop]);
  113. finished = 0;
  114. }
  115. else
  116. printf(" ");
  117. }
  118. loop++;
  119. printf("\n");
  120. } while (!finished && loop < 100);
  121. starpu_shutdown();
  122. return EXIT_SUCCESS;
  123. }