starpu_mpi_early_data.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2014 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <stdlib.h>
  18. #include <starpu_mpi.h>
  19. #include <starpu_mpi_early_data.h>
  20. #include <starpu_mpi_private.h>
  21. #include <common/uthash.h>
  22. struct _starpu_mpi_early_data_handle_hashlist
  23. {
  24. struct _starpu_mpi_early_data_handle_list *list;
  25. UT_hash_handle hh;
  26. struct _starpu_mpi_node_tag node_tag;
  27. };
  28. /** stores data which have been received by MPI but have not been requested by the application */
  29. static starpu_pthread_mutex_t _starpu_mpi_early_data_handle_mutex;
  30. static struct _starpu_mpi_early_data_handle_hashlist *_starpu_mpi_early_data_handle_hashmap = NULL;
  31. static int _starpu_mpi_early_data_handle_hashmap_count = 0;
  32. void _starpu_mpi_early_data_init(void)
  33. {
  34. _starpu_mpi_early_data_handle_hashmap = NULL;
  35. _starpu_mpi_early_data_handle_hashmap_count = 0;
  36. STARPU_PTHREAD_MUTEX_INIT(&_starpu_mpi_early_data_handle_mutex, NULL);
  37. }
  38. void _starpu_mpi_early_data_check_termination(void)
  39. {
  40. STARPU_ASSERT_MSG(_starpu_mpi_early_data_handle_hashmap_count == 0, "Number of unexpected received messages left is not zero, did you forget to post a receive corresponding to a send?");
  41. }
  42. void _starpu_mpi_early_data_free(void)
  43. {
  44. struct _starpu_mpi_early_data_handle_hashlist *current, *tmp;
  45. HASH_ITER(hh, _starpu_mpi_early_data_handle_hashmap, current, tmp)
  46. {
  47. _starpu_mpi_early_data_handle_list_delete(current->list);
  48. HASH_DEL(_starpu_mpi_early_data_handle_hashmap, current);
  49. free(current);
  50. }
  51. STARPU_PTHREAD_MUTEX_DESTROY(&_starpu_mpi_early_data_handle_mutex);
  52. }
  53. struct _starpu_mpi_early_data_handle *_starpu_mpi_early_data_create(struct _starpu_mpi_envelope *envelope, int source, MPI_Comm comm)
  54. {
  55. struct _starpu_mpi_early_data_handle* early_data_handle = calloc(1, sizeof(struct _starpu_mpi_early_data_handle));
  56. STARPU_ASSERT(early_data_handle);
  57. STARPU_PTHREAD_MUTEX_INIT(&early_data_handle->req_mutex, NULL);
  58. STARPU_PTHREAD_COND_INIT(&early_data_handle->req_cond, NULL);
  59. early_data_handle->env = envelope;
  60. early_data_handle->node_tag.comm = comm;
  61. early_data_handle->node_tag.rank = source;
  62. early_data_handle->node_tag.data_tag = envelope->data_tag;
  63. return early_data_handle;
  64. }
  65. #ifdef STARPU_VERBOSE
  66. static void _starpu_mpi_early_data_handle_display_hash(struct _starpu_mpi_node_tag *node_tag)
  67. {
  68. struct _starpu_mpi_early_data_handle_hashlist *hashlist;
  69. HASH_FIND(hh, _starpu_mpi_early_data_handle_hashmap, node_tag, sizeof(struct _starpu_mpi_node_tag), hashlist);
  70. if (hashlist == NULL)
  71. {
  72. _STARPU_MPI_DEBUG(60, "Hashlist for comm %p source %d and tag %d does not exist\n", node_tag->comm, node_tag->rank, node_tag->data_tag);
  73. }
  74. else if (_starpu_mpi_early_data_handle_list_empty(hashlist->list))
  75. {
  76. _STARPU_MPI_DEBUG(60, "Hashlist for comm %p source %d and tag %d is empty\n", node_tag->comm, node_tag->rank, node_tag->data_tag);
  77. }
  78. else
  79. {
  80. struct _starpu_mpi_early_data_handle *cur;
  81. for (cur = _starpu_mpi_early_data_handle_list_begin(hashlist->list) ;
  82. cur != _starpu_mpi_early_data_handle_list_end(hashlist->list);
  83. cur = _starpu_mpi_early_data_handle_list_next(cur))
  84. {
  85. _STARPU_MPI_DEBUG(60, "Element for comm %p source %d and tag %d: %p\n", node_tag->comm, node_tag->rank, node_tag->data_tag, cur);
  86. }
  87. }
  88. }
  89. #endif
  90. static
  91. struct _starpu_mpi_early_data_handle *_starpu_mpi_early_data_pop(struct _starpu_mpi_node_tag *node_tag, int delete)
  92. {
  93. struct _starpu_mpi_early_data_handle_hashlist *hashlist;
  94. struct _starpu_mpi_early_data_handle *early_data_handle;
  95. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_mpi_early_data_handle_mutex);
  96. _STARPU_MPI_DEBUG(60, "Looking for early_data_handle with comm %p source %d tag %d\n", node_tag->comm, node_tag->rank, node_tag->data_tag);
  97. HASH_FIND(hh, _starpu_mpi_early_data_handle_hashmap, node_tag, sizeof(struct _starpu_mpi_node_tag), hashlist);
  98. if (hashlist == NULL)
  99. {
  100. early_data_handle = NULL;
  101. }
  102. else
  103. {
  104. if (_starpu_mpi_early_data_handle_list_empty(hashlist->list))
  105. {
  106. early_data_handle = NULL;
  107. }
  108. else
  109. {
  110. if (delete == 1)
  111. {
  112. early_data_handle = _starpu_mpi_early_data_handle_list_pop_front(hashlist->list);
  113. }
  114. else
  115. {
  116. early_data_handle = _starpu_mpi_early_data_handle_list_front(hashlist->list);
  117. }
  118. }
  119. }
  120. _STARPU_MPI_DEBUG(60, "Found early_data_handle %p with comm %p source %d tag %d\n", early_data_handle, node_tag->comm, node_tag->rank, node_tag->data_tag);
  121. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_early_data_handle_mutex);
  122. return early_data_handle;
  123. }
  124. struct _starpu_mpi_early_data_handle *_starpu_mpi_early_data_find(struct _starpu_mpi_node_tag *node_tag)
  125. {
  126. return _starpu_mpi_early_data_pop(node_tag, 0);
  127. }
  128. void _starpu_mpi_early_data_add(struct _starpu_mpi_early_data_handle *early_data_handle)
  129. {
  130. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_mpi_early_data_handle_mutex);
  131. _STARPU_MPI_DEBUG(60, "Trying to add early_data_handle %p with comm %p source %d tag %d\n", early_data_handle, early_data_handle->node_tag.comm,
  132. early_data_handle->node_tag.rank, early_data_handle->node_tag.data_tag);
  133. struct _starpu_mpi_early_data_handle_hashlist *hashlist;
  134. HASH_FIND(hh, _starpu_mpi_early_data_handle_hashmap, &early_data_handle->node_tag, sizeof(struct _starpu_mpi_node_tag), hashlist);
  135. if (hashlist == NULL)
  136. {
  137. hashlist = malloc(sizeof(struct _starpu_mpi_early_data_handle_hashlist));
  138. hashlist->list = _starpu_mpi_early_data_handle_list_new();
  139. hashlist->node_tag = early_data_handle->node_tag;
  140. HASH_ADD(hh, _starpu_mpi_early_data_handle_hashmap, node_tag, sizeof(hashlist->node_tag), hashlist);
  141. }
  142. _starpu_mpi_early_data_handle_list_push_back(hashlist->list, early_data_handle);
  143. _starpu_mpi_early_data_handle_hashmap_count ++;
  144. #ifdef STARPU_VERBOSE
  145. _starpu_mpi_early_data_handle_display_hash(&hashlist->node_tag);
  146. #endif
  147. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_early_data_handle_mutex);
  148. }
  149. void _starpu_mpi_early_data_delete(struct _starpu_mpi_early_data_handle *early_data_handle)
  150. {
  151. _STARPU_MPI_DEBUG(60, "Trying to delete early_data_handle %p with comm %p source %d tag %d\n", early_data_handle, early_data_handle->node_tag.comm,
  152. early_data_handle->node_tag.rank, early_data_handle->node_tag.data_tag);
  153. struct _starpu_mpi_early_data_handle *found = _starpu_mpi_early_data_pop(&early_data_handle->node_tag, 1);
  154. STARPU_ASSERT_MSG(found == early_data_handle,
  155. "[_starpu_mpi_early_data_delete][error] early_data_handle %p with comm %p source %d tag %d is NOT available\n",
  156. early_data_handle, early_data_handle->node_tag.comm, early_data_handle->node_tag.rank, early_data_handle->node_tag.data_tag);
  157. _starpu_mpi_early_data_handle_hashmap_count --;
  158. #ifdef STARPU_VERBOSE
  159. _starpu_mpi_early_data_handle_display_hash(&early_data_handle->node_tag);
  160. #endif
  161. }