insert_task_sent_cache.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016 CNRS
  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. /* Dummy cost function for simgrid */
  32. static double cost_function(struct starpu_task *task STARPU_ATTRIBUTE_UNUSED, unsigned nimpl STARPU_ATTRIBUTE_UNUSED)
  33. {
  34. return 0.000001;
  35. }
  36. static struct starpu_perfmodel dumb_model =
  37. {
  38. .type = STARPU_COMMON,
  39. .cost_function = cost_function
  40. };
  41. struct starpu_codelet mycodelet =
  42. {
  43. .cpu_funcs = {func_cpu},
  44. .nbuffers = 2,
  45. .modes = {STARPU_RW, STARPU_R},
  46. .model = &dumb_model
  47. };
  48. #define N 1000
  49. /* Returns the MPI node number where data indexes index is */
  50. int my_distrib(int x)
  51. {
  52. return x;
  53. }
  54. void test_cache(int rank, char *enabled, size_t *comm_amount)
  55. {
  56. int i;
  57. int ret;
  58. unsigned **v;
  59. starpu_data_handle_t data_handles[2];
  60. setenv("STARPU_MPI_CACHE", enabled, 1);
  61. ret = starpu_init(NULL);
  62. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  63. ret = starpu_mpi_init(NULL, NULL, 0);
  64. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  65. v = malloc(2 * sizeof(unsigned *));
  66. for(i = 0; i < 2; i++)
  67. {
  68. int j;
  69. v[i] = malloc(N * sizeof(unsigned));
  70. for(j=0 ; j<N ; j++)
  71. {
  72. v[i][j] = 12;
  73. }
  74. }
  75. for(i = 0; i < 2; i++)
  76. {
  77. int mpi_rank = my_distrib(i);
  78. if (mpi_rank == rank)
  79. {
  80. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  81. starpu_vector_data_register(&data_handles[i], STARPU_MAIN_RAM, (uintptr_t)&(v[i]), N, sizeof(unsigned));
  82. }
  83. else
  84. {
  85. /* I don't own that index, but will need it for my computations */
  86. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  87. starpu_vector_data_register(&data_handles[i], -1, (uintptr_t)NULL, N, sizeof(unsigned));
  88. }
  89. starpu_mpi_data_register(data_handles[i], i, mpi_rank);
  90. }
  91. for(i = 0; i < 5; i++)
  92. {
  93. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[0], STARPU_R, data_handles[1], 0);
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  95. }
  96. for(i = 0; i < 5; i++)
  97. {
  98. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[1], STARPU_R, data_handles[0], 0);
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  100. }
  101. for(i = 0; i < 5; i++)
  102. {
  103. starpu_mpi_cache_flush(MPI_COMM_WORLD, data_handles[0]);
  104. }
  105. for(i = 0; i < 5; i++)
  106. {
  107. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[1], STARPU_R, data_handles[0], 0);
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  109. }
  110. starpu_task_wait_for_all();
  111. for(i = 0; i < 2; i++)
  112. {
  113. starpu_data_unregister(data_handles[i]);
  114. free(v[i]);
  115. }
  116. free(v);
  117. starpu_mpi_comm_amounts_retrieve(comm_amount);
  118. starpu_mpi_shutdown();
  119. starpu_shutdown();
  120. }
  121. int main(int argc, char **argv)
  122. {
  123. int dst, rank, size;
  124. int result=0;
  125. size_t *comm_amount_with_cache;
  126. size_t *comm_amount_without_cache;
  127. MPI_Init(&argc, &argv);
  128. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  129. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  130. setenv("STARPU_COMM_STATS", "1", 1);
  131. comm_amount_with_cache = malloc(size * sizeof(size_t));
  132. comm_amount_without_cache = malloc(size * sizeof(size_t));
  133. test_cache(rank, "0", comm_amount_with_cache);
  134. test_cache(rank, "1", comm_amount_without_cache);
  135. if (rank == 0 || rank == 1)
  136. {
  137. dst = (rank == 0) ? 1 : 0;
  138. result = (comm_amount_with_cache[dst] == comm_amount_without_cache[dst] * 5);
  139. FPRINTF_MPI(stderr, "Communication cache mechanism is %sworking\n", result?"":"NOT ");
  140. }
  141. else
  142. {
  143. result = 1;
  144. }
  145. free(comm_amount_without_cache);
  146. free(comm_amount_with_cache);
  147. MPI_Finalize();
  148. return !result;
  149. }
  150. #endif