starpu_mpi_early_request.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2014, 2016-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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_private.h>
  20. #include <starpu_mpi_early_request.h>
  21. #include <common/uthash.h>
  22. /** stores application requests for which data have not been received yet */
  23. struct _starpu_mpi_early_request_hashlist
  24. {
  25. struct _starpu_mpi_req_list list;
  26. UT_hash_handle hh;
  27. struct _starpu_mpi_node_tag node_tag;
  28. };
  29. static starpu_pthread_mutex_t _starpu_mpi_early_request_mutex;
  30. struct _starpu_mpi_early_request_hashlist *_starpu_mpi_early_request_hash;
  31. int _starpu_mpi_early_request_hash_count;
  32. void _starpu_mpi_early_request_init()
  33. {
  34. _starpu_mpi_early_request_hash = NULL;
  35. _starpu_mpi_early_request_hash_count = 0;
  36. STARPU_PTHREAD_MUTEX_INIT(&_starpu_mpi_early_request_mutex, NULL);
  37. }
  38. void _starpu_mpi_early_request_shutdown()
  39. {
  40. struct _starpu_mpi_early_request_hashlist *entry, *tmp;
  41. HASH_ITER(hh, _starpu_mpi_early_request_hash, entry, tmp)
  42. {
  43. STARPU_ASSERT(_starpu_mpi_req_list_empty(&entry->list));
  44. HASH_DEL(_starpu_mpi_early_request_hash, entry);
  45. free(entry);
  46. }
  47. STARPU_PTHREAD_MUTEX_DESTROY(&_starpu_mpi_early_request_mutex);
  48. }
  49. int _starpu_mpi_early_request_count()
  50. {
  51. return _starpu_mpi_early_request_hash_count;
  52. }
  53. void _starpu_mpi_early_request_check_termination()
  54. {
  55. STARPU_ASSERT_MSG(_starpu_mpi_early_request_count() == 0, "Number of early requests left is not zero");
  56. }
  57. struct _starpu_mpi_req* _starpu_mpi_early_request_dequeue(starpu_mpi_tag_t data_tag, int source, MPI_Comm comm)
  58. {
  59. struct _starpu_mpi_node_tag node_tag;
  60. struct _starpu_mpi_req *found;
  61. struct _starpu_mpi_early_request_hashlist *hashlist;
  62. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_mpi_early_request_mutex);
  63. memset(&node_tag, 0, sizeof(struct _starpu_mpi_node_tag));
  64. node_tag.comm = comm;
  65. node_tag.rank = source;
  66. node_tag.data_tag = data_tag;
  67. _STARPU_MPI_DEBUG(100, "Looking for early_request with comm %ld source %d tag %ld\n", (long int)node_tag.comm, node_tag.rank, node_tag.data_tag);
  68. HASH_FIND(hh, _starpu_mpi_early_request_hash, &node_tag, sizeof(struct _starpu_mpi_node_tag), hashlist);
  69. if (hashlist == NULL)
  70. {
  71. found = NULL;
  72. }
  73. else
  74. {
  75. if (_starpu_mpi_req_list_empty(&hashlist->list))
  76. {
  77. found = NULL;
  78. }
  79. else
  80. {
  81. found = _starpu_mpi_req_list_pop_front(&hashlist->list);
  82. _starpu_mpi_early_request_hash_count --;
  83. }
  84. }
  85. _STARPU_MPI_DEBUG(100, "Found early_request %p with comm %ld source %d tag %ld\n", found, (long int)node_tag.comm, node_tag.rank, node_tag.data_tag);
  86. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_early_request_mutex);
  87. return found;
  88. }
  89. void _starpu_mpi_early_request_enqueue(struct _starpu_mpi_req *req)
  90. {
  91. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_mpi_early_request_mutex);
  92. _STARPU_MPI_DEBUG(100, "Adding request %p with comm %ld source %d tag %ld in the application request hashmap\n", req, (long int)req->node_tag.comm, req->node_tag.rank, req->node_tag.data_tag);
  93. struct _starpu_mpi_early_request_hashlist *hashlist;
  94. HASH_FIND(hh, _starpu_mpi_early_request_hash, &req->node_tag, sizeof(struct _starpu_mpi_node_tag), hashlist);
  95. if (hashlist == NULL)
  96. {
  97. _STARPU_MPI_MALLOC(hashlist, sizeof(struct _starpu_mpi_early_request_hashlist));
  98. _starpu_mpi_req_list_init(&hashlist->list);
  99. hashlist->node_tag = req->node_tag;
  100. HASH_ADD(hh, _starpu_mpi_early_request_hash, node_tag, sizeof(hashlist->node_tag), hashlist);
  101. }
  102. _starpu_mpi_req_list_push_back(&hashlist->list, req);
  103. _starpu_mpi_early_request_hash_count ++;
  104. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_early_request_mutex);
  105. }