insert_task_recv_cache.c 3.9 KB

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