insert_task_sent_cache.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2018 CNRS
  4. * Copyright (C) 2013 Inria
  5. * Copyright (C) 2013-2015,2017 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <starpu_mpi.h>
  20. #include <math.h>
  21. #include "helper.h"
  22. #if !defined(STARPU_HAVE_SETENV)
  23. #warning setenv is not defined. Skipping test
  24. int main(void)
  25. {
  26. return STARPU_TEST_SKIPPED;
  27. }
  28. #else
  29. void func_cpu(void *descr[], void *_args)
  30. {
  31. (void)descr;
  32. (void)_args;
  33. }
  34. struct starpu_codelet mycodelet =
  35. {
  36. .cpu_funcs = {func_cpu},
  37. .nbuffers = 2,
  38. .modes = {STARPU_RW, STARPU_R},
  39. .model = &starpu_perfmodel_nop,
  40. };
  41. #define N 1000
  42. /* Returns the MPI node number where data indexes index is */
  43. int my_distrib(int x)
  44. {
  45. return x;
  46. }
  47. void test_cache(int rank, char *enabled, size_t *comm_amount)
  48. {
  49. int i;
  50. int ret;
  51. unsigned *v[2];
  52. starpu_data_handle_t data_handles[2];
  53. setenv("STARPU_MPI_CACHE", enabled, 1);
  54. ret = starpu_init(NULL);
  55. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  56. ret = starpu_mpi_init(NULL, NULL, 0);
  57. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  58. for(i = 0; i < 2; i++)
  59. {
  60. int j;
  61. v[i] = malloc(N * sizeof(unsigned));
  62. for(j=0 ; j<N ; j++)
  63. {
  64. v[i][j] = 12;
  65. }
  66. }
  67. for(i = 0; i < 2; i++)
  68. {
  69. int mpi_rank = my_distrib(i);
  70. if (mpi_rank == rank)
  71. {
  72. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  73. starpu_vector_data_register(&data_handles[i], STARPU_MAIN_RAM, (uintptr_t)v[i], N, sizeof(unsigned));
  74. }
  75. else
  76. {
  77. /* I don't own this index, but will need it for my computations */
  78. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  79. starpu_vector_data_register(&data_handles[i], -1, (uintptr_t)NULL, N, sizeof(unsigned));
  80. }
  81. starpu_mpi_data_register(data_handles[i], i, mpi_rank);
  82. }
  83. for(i = 0; i < 5; i++)
  84. {
  85. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[0], STARPU_R, data_handles[1], 0);
  86. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  87. }
  88. for(i = 0; i < 5; i++)
  89. {
  90. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[1], STARPU_R, data_handles[0], 0);
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  92. }
  93. for(i = 0; i < 5; i++)
  94. {
  95. starpu_mpi_cache_flush(MPI_COMM_WORLD, data_handles[0]);
  96. }
  97. for(i = 0; i < 5; i++)
  98. {
  99. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[1], STARPU_R, data_handles[0], 0);
  100. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  101. }
  102. starpu_task_wait_for_all();
  103. for(i = 0; i < 2; i++)
  104. {
  105. starpu_data_unregister(data_handles[i]);
  106. free(v[i]);
  107. }
  108. starpu_mpi_comm_amounts_retrieve(comm_amount);
  109. starpu_mpi_shutdown();
  110. starpu_shutdown();
  111. }
  112. int main(int argc, char **argv)
  113. {
  114. int rank, size;
  115. int result=0;
  116. size_t *comm_amount_with_cache;
  117. size_t *comm_amount_without_cache;
  118. MPI_INIT_THREAD_real(&argc, &argv, MPI_THREAD_SERIALIZED);
  119. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  120. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  121. setenv("STARPU_COMM_STATS", "1", 1);
  122. comm_amount_with_cache = malloc(size * sizeof(size_t));
  123. comm_amount_without_cache = malloc(size * sizeof(size_t));
  124. test_cache(rank, "0", comm_amount_with_cache);
  125. test_cache(rank, "1", comm_amount_without_cache);
  126. if (rank == 0 || rank == 1)
  127. {
  128. int dst = (rank == 0) ? 1 : 0;
  129. result = (comm_amount_with_cache[dst] == comm_amount_without_cache[dst] * 5);
  130. FPRINTF_MPI(stderr, "Communication cache mechanism is %sworking\n", result?"":"NOT ");
  131. }
  132. else
  133. {
  134. result = 1;
  135. }
  136. free(comm_amount_without_cache);
  137. free(comm_amount_with_cache);
  138. MPI_Finalize();
  139. return !result;
  140. }
  141. #endif