insert_task_cache.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012 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 <starpu.h>
  17. #include <starpu_mpi.h>
  18. #include <math.h>
  19. #include "helper.h"
  20. #if !defined(STARPU_HAVE_UNSETENV)
  21. #warning unsetenv is not defined. Skipping test
  22. int main(int argc, char **argv)
  23. {
  24. return STARPU_TEST_SKIPPED;
  25. }
  26. #else
  27. void func_cpu(__attribute__ ((unused)) void *descr[], __attribute__ ((unused)) void *_args)
  28. {
  29. }
  30. struct starpu_codelet mycodelet =
  31. {
  32. .cpu_funcs = {func_cpu, NULL},
  33. .nbuffers = 2,
  34. .modes = {STARPU_RW, STARPU_R}
  35. };
  36. #define N 1000
  37. /* Returns the MPI node number where data indexes index is */
  38. int my_distrib(int x)
  39. {
  40. return x;
  41. }
  42. void test_cache(int rank, int size, int enabled, size_t *comm_amount)
  43. {
  44. int i;
  45. int ret;
  46. unsigned v[2][N];
  47. starpu_data_handle_t data_handles[2];
  48. char *string;
  49. string = malloc(50);
  50. sprintf(string, "STARPU_MPI_CACHE=%d", enabled);
  51. putenv(string);
  52. ret = starpu_init(NULL);
  53. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  54. ret = starpu_mpi_init(NULL, NULL, 0);
  55. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  56. for(i = 0; i < 2; i++)
  57. {
  58. int mpi_rank = my_distrib(i);
  59. if (mpi_rank == rank)
  60. {
  61. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  62. starpu_vector_data_register(&data_handles[i], 0, (uintptr_t)&(v[i]), N, sizeof(unsigned));
  63. }
  64. else
  65. {
  66. /* I don't own that index, but will need it for my computations */
  67. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  68. starpu_vector_data_register(&data_handles[i], -1, (uintptr_t)NULL, N, sizeof(unsigned));
  69. }
  70. starpu_data_set_rank(data_handles[i], mpi_rank);
  71. starpu_data_set_tag(data_handles[i], i);
  72. }
  73. for(i = 0; i < 5; i++)
  74. {
  75. ret = starpu_mpi_insert_task(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[0], STARPU_R, data_handles[1], 0);
  76. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_insert_task");
  77. }
  78. for(i = 0; i < 5; i++)
  79. {
  80. ret = starpu_mpi_insert_task(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[1], STARPU_R, data_handles[0], 0);
  81. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_insert_task");
  82. }
  83. starpu_task_wait_for_all();
  84. for(i = 0; i < 2; i++)
  85. {
  86. starpu_data_unregister(data_handles[i]);
  87. }
  88. starpu_mpi_comm_amounts_retrieve(comm_amount);
  89. starpu_mpi_shutdown();
  90. starpu_shutdown();
  91. free(string);
  92. }
  93. int main(int argc, char **argv)
  94. {
  95. int dst, rank, size;
  96. int result=0;
  97. size_t *comm_amount_with_cache;
  98. size_t *comm_amount_without_cache;
  99. char *string;
  100. MPI_Init(&argc, &argv);
  101. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  102. MPI_Comm_size(MPI_COMM_WORLD, &size);
  103. string = malloc(50);
  104. sprintf(string, "STARPU_COMM_STATS=1");
  105. putenv(string);
  106. comm_amount_with_cache = malloc(size * sizeof(size_t));
  107. comm_amount_without_cache = malloc(size * sizeof(size_t));
  108. test_cache(rank, size, 0, comm_amount_with_cache);
  109. test_cache(rank, size, 1, comm_amount_without_cache);
  110. if (rank == 0 || rank == 1)
  111. {
  112. dst = (rank == 0) ? 1 : 0;
  113. result = (comm_amount_with_cache[dst] == comm_amount_without_cache[dst] * 5);
  114. fprintf(stderr, "Communication cache mechanism is %sworking\n", result?"":"NOT ");
  115. }
  116. else
  117. result = 1;
  118. free(comm_amount_without_cache);
  119. free(comm_amount_with_cache);
  120. free(string);
  121. MPI_Finalize();
  122. return !result;
  123. }
  124. #endif