insert_task_recv_cache.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2020 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. #include <starpu.h>
  17. #include <starpu_mpi.h>
  18. #include <math.h>
  19. #include "helper.h"
  20. #if !defined(STARPU_HAVE_SETENV)
  21. #warning setenv is not defined. Skipping test
  22. int main(void)
  23. {
  24. return STARPU_TEST_SKIPPED;
  25. }
  26. #else
  27. void func_cpu(void *descr[], void *_args)
  28. {
  29. (void)descr;
  30. (void)_args;
  31. }
  32. struct starpu_codelet mycodelet =
  33. {
  34. .cpu_funcs = {func_cpu},
  35. .nbuffers = 2,
  36. .modes = {STARPU_RW, STARPU_R},
  37. .model = &starpu_perfmodel_nop,
  38. };
  39. #define N 1000
  40. /* Returns the MPI node number where data indexes index is */
  41. int my_distrib(int x)
  42. {
  43. return x;
  44. }
  45. void test_cache(int rank, char *enabled, size_t *comm_amount)
  46. {
  47. int i;
  48. int ret;
  49. unsigned *v[2];
  50. starpu_data_handle_t data_handles[2];
  51. FPRINTF_MPI(stderr, "Testing with STARPU_MPI_CACHE=%s\n", enabled);
  52. setenv("STARPU_MPI_CACHE", enabled, 1);
  53. ret = starpu_mpi_init_conf(NULL, NULL, 0, MPI_COMM_WORLD, NULL);
  54. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  55. for(i = 0; i < 2; i++)
  56. {
  57. int j;
  58. v[i] = calloc(N, sizeof(unsigned));
  59. for(j=0 ; j<N ; j++)
  60. {
  61. v[i][j] = 12;
  62. }
  63. }
  64. for(i = 0; i < 2; i++)
  65. {
  66. int mpi_rank = my_distrib(i);
  67. if (mpi_rank == rank)
  68. {
  69. starpu_vector_data_register(&data_handles[i], STARPU_MAIN_RAM, (uintptr_t)v[i], N, sizeof(unsigned));
  70. }
  71. else
  72. {
  73. /* I don't own this index, but will need it for my computations */
  74. starpu_vector_data_register(&data_handles[i], -1, (uintptr_t)NULL, N, sizeof(unsigned));
  75. }
  76. starpu_mpi_data_register(data_handles[i], i, mpi_rank);
  77. }
  78. // We call starpu_mpi_task_insert twice, when the cache is enabled, the 1st time puts the
  79. // data in the cache, the 2nd time allows to check the data is not sent again
  80. for(i = 0; i < 2; i++)
  81. {
  82. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[0], STARPU_R, data_handles[1], 0);
  83. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  84. }
  85. // Flush the cache for data_handles[1] which has been sent from node1 to node0
  86. starpu_mpi_cache_flush(MPI_COMM_WORLD, data_handles[1]);
  87. // Check again
  88. for(i = 0; i < 2; i++)
  89. {
  90. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[0], STARPU_R, data_handles[1], 0);
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  92. }
  93. starpu_task_wait_for_all();
  94. for(i = 0; i < 2; i++)
  95. {
  96. starpu_data_unregister(data_handles[i]);
  97. free(v[i]);
  98. }
  99. starpu_mpi_comm_amounts_retrieve(comm_amount);
  100. starpu_mpi_shutdown();
  101. }
  102. int main(int argc, char **argv)
  103. {
  104. int rank, size;
  105. int result=0;
  106. size_t *comm_amount_with_cache;
  107. size_t *comm_amount_without_cache;
  108. MPI_INIT_THREAD_real(&argc, &argv, MPI_THREAD_SERIALIZED);
  109. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  110. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  111. setenv("STARPU_COMM_STATS", "1", 1);
  112. setenv("STARPU_MPI_CACHE_STATS", "1", 1);
  113. comm_amount_with_cache = malloc(size * sizeof(size_t));
  114. comm_amount_without_cache = malloc(size * sizeof(size_t));
  115. test_cache(rank, "0", comm_amount_with_cache);
  116. test_cache(rank, "1", comm_amount_without_cache);
  117. if (rank == 1)
  118. {
  119. result = (comm_amount_with_cache[0] == comm_amount_without_cache[0] * 2);
  120. FPRINTF_MPI(stderr, "Communication cache mechanism is %sworking (with cache: %ld) (without cache: %ld)\n", result?"":"NOT ", (long)comm_amount_with_cache[0], (long)comm_amount_without_cache[0]);
  121. }
  122. else
  123. {
  124. result = 1;
  125. }
  126. free(comm_amount_without_cache);
  127. free(comm_amount_with_cache);
  128. MPI_Finalize();
  129. return !result;
  130. }
  131. #endif