starpu_mpi_early_request.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_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_free()
  39. {
  40. free(_starpu_mpi_early_request_hash);
  41. STARPU_PTHREAD_MUTEX_DESTROY(&_starpu_mpi_early_request_mutex);
  42. }
  43. int _starpu_mpi_early_request_count()
  44. {
  45. return _starpu_mpi_early_request_hash_count;
  46. }
  47. void _starpu_mpi_early_request_check_termination()
  48. {
  49. STARPU_ASSERT_MSG(_starpu_mpi_early_request_count() == 0, "Number of early requests left is not zero");
  50. }
  51. struct _starpu_mpi_req* _starpu_mpi_early_request_dequeue(int data_tag, int source, MPI_Comm comm)
  52. {
  53. struct _starpu_mpi_node_tag node_tag;
  54. struct _starpu_mpi_req *found;
  55. struct _starpu_mpi_early_request_hashlist *hashlist;
  56. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_mpi_early_request_mutex);
  57. memset(&node_tag, 0, sizeof(struct _starpu_mpi_node_tag));
  58. node_tag.comm = comm;
  59. node_tag.rank = source;
  60. node_tag.data_tag = data_tag;
  61. _STARPU_MPI_DEBUG(100, "Looking for early_request with comm %p source %d tag %d\n", node_tag.comm, node_tag.rank, node_tag.data_tag);
  62. HASH_FIND(hh, _starpu_mpi_early_request_hash, &node_tag, sizeof(struct _starpu_mpi_node_tag), hashlist);
  63. if (hashlist == NULL)
  64. {
  65. found = NULL;
  66. }
  67. else
  68. {
  69. if (_starpu_mpi_req_list_empty(hashlist->list))
  70. {
  71. found = NULL;
  72. }
  73. else
  74. {
  75. found = _starpu_mpi_req_list_pop_front(hashlist->list);
  76. _starpu_mpi_early_request_hash_count --;
  77. }
  78. }
  79. _STARPU_MPI_DEBUG(100, "Found early_request %p with comm %p source %d tag %d\n", found, node_tag.comm, node_tag.rank, node_tag.data_tag);
  80. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_early_request_mutex);
  81. return found;
  82. }
  83. void _starpu_mpi_early_request_enqueue(struct _starpu_mpi_req *req)
  84. {
  85. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_mpi_early_request_mutex);
  86. _STARPU_MPI_DEBUG(100, "Adding request %p with comm %p source %d tag %d in the application request hashmap\n", req, req->node_tag.comm, req->node_tag.rank, req->node_tag.data_tag);
  87. struct _starpu_mpi_early_request_hashlist *hashlist;
  88. HASH_FIND(hh, _starpu_mpi_early_request_hash, &req->node_tag, sizeof(struct _starpu_mpi_node_tag), hashlist);
  89. if (hashlist == NULL)
  90. {
  91. hashlist = malloc(sizeof(struct _starpu_mpi_early_request_hashlist));
  92. hashlist->list = _starpu_mpi_req_list_new();
  93. hashlist->node_tag = req->node_tag;
  94. HASH_ADD(hh, _starpu_mpi_early_request_hash, node_tag, sizeof(hashlist->node_tag), hashlist);
  95. }
  96. _starpu_mpi_req_list_push_back(hashlist->list, req);
  97. _starpu_mpi_early_request_hash_count ++;
  98. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_early_request_mutex);
  99. }