locality.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. };
  55. int main(int argc, char *argv[])
  56. {
  57. int ret;
  58. starpu_data_handle_t A[N];
  59. starpu_data_handle_t B[N];
  60. unsigned i, loop, finished;
  61. ret = starpu_initialize(NULL, &argc, &argv);
  62. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  63. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  64. /* Get most parallelism by using an arbiter */
  65. starpu_arbiter_t arbiter = starpu_arbiter_create();
  66. for (i = 0; i < N; i++)
  67. {
  68. starpu_void_data_register(&A[i]);
  69. starpu_void_data_register(&B[i]);
  70. starpu_data_assign_arbiter(A[i], arbiter);
  71. }
  72. for (loop = 0; loop < ITER; loop++)
  73. {
  74. for (i = 1; i < N-1; i++)
  75. {
  76. starpu_task_insert(&cl,
  77. STARPU_RW, B[i],
  78. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY, A[i-1],
  79. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY, A[i],
  80. STARPU_RW | STARPU_COMMUTE | STARPU_LOCALITY, A[i+1],
  81. STARPU_VALUE, &loop, sizeof(loop),
  82. STARPU_VALUE, &i, sizeof(i),
  83. 0);
  84. }
  85. }
  86. starpu_task_wait_for_all();
  87. for (loop = 0; loop < ITER; loop++)
  88. {
  89. for (i = 1; i < N-1; i++)
  90. {
  91. printf("%d ", task_worker[i][loop]);
  92. }
  93. printf("\n");
  94. }
  95. loop = 0;
  96. do {
  97. finished = 1;
  98. for (i = 0; i < starpu_worker_get_count(); i++)
  99. {
  100. if (loop < worker_ntask[i])
  101. {
  102. printf("%02d ", worker_task[i][loop]);
  103. finished = 0;
  104. }
  105. else
  106. printf(" ");
  107. }
  108. loop++;
  109. printf("\n");
  110. } while (!finished && loop < 100);
  111. starpu_shutdown();
  112. return EXIT_SUCCESS;
  113. }