@@ -71,34 +71,35 @@ void Comm_Trans<Tkey,Tvalue,Tdatas_isend,Tdatas_recv>::communicate(
7171 std::vector<std::future<void >> futures_isend (comm_size);
7272 std::vector<std::future<void >> futures_recv (comm_size);
7373 std::atomic_flag lock_set_value = ATOMIC_FLAG_INIT;
74+ std::atomic<std::size_t > memory_max_isend (0 );
75+ std::atomic<std::size_t > memory_max_recv (0 );
7476
7577 std::future<void > future_post_process = std::async (std::launch::async,
7678 &Comm_Trans::post_process, this ,
7779 std::ref (requests_isend), std::ref (strs_isend), std::ref (futures_isend), std::ref (futures_recv));
7880
7981 while (future_post_process.wait_for (std::chrono::seconds (0 )) != std::future_status::ready)
8082 {
81- if (rank_isend_tmp < this ->comm_size ) // && enough_memory())
82- {
83- const int rank_isend = (rank_isend_tmp + this ->rank_mine ) % this ->comm_size ;
84- futures_isend[rank_isend] = std::async (std::launch::async,
85- &Comm_Trans::isend_data, this ,
86- rank_isend, std::cref (datas_isend), std::ref (strs_isend[rank_isend]), std::ref (requests_isend[rank_isend]));
87- ++rank_isend_tmp;
88- }
89-
90-
9183 int flag_iprobe=0 ;
9284 MPI_Status status_recv;
9385 MPI_Message message_recv;
9486 MPI_CHECK (MPI_Improbe (MPI_ANY_SOURCE, Comm_Trans::tag_data, this ->mpi_comm , &flag_iprobe, &message_recv, &status_recv));
95- if (flag_iprobe && rank_recv_working!=status_recv.MPI_SOURCE )
87+ if (flag_iprobe && rank_recv_working!=status_recv.MPI_SOURCE && memory_enough (memory_max_recv) )
9688 {
9789 futures_recv[status_recv.MPI_SOURCE ] = std::async (std::launch::async,
9890 &Comm_Trans::recv_data, this ,
99- std::ref (datas_recv), status_recv, message_recv, std::ref (lock_set_value));
91+ std::ref (datas_recv), status_recv, message_recv, std::ref (lock_set_value), std::ref (memory_max_recv) );
10092 rank_recv_working = status_recv.MPI_SOURCE ;
10193 }
94+
95+ if (rank_isend_tmp < this ->comm_size && memory_enough (memory_max_isend))
96+ {
97+ const int rank_isend = (rank_isend_tmp + this ->rank_mine ) % this ->comm_size ;
98+ futures_isend[rank_isend] = std::async (std::launch::async,
99+ &Comm_Trans::isend_data, this ,
100+ rank_isend, std::cref (datas_isend), std::ref (strs_isend[rank_isend]), std::ref (requests_isend[rank_isend]), std::ref (memory_max_isend));
101+ ++rank_isend_tmp;
102+ }
102103 }
103104 future_post_process.get ();
104105}
@@ -109,7 +110,8 @@ void Comm_Trans<Tkey,Tvalue,Tdatas_isend,Tdatas_recv>::isend_data(
109110 const int rank_isend,
110111 const Tdatas_isend &datas_isend,
111112 std::string &str_isend,
112- MPI_Request &request_isend) const
113+ MPI_Request &request_isend,
114+ std::atomic<std::size_t > &memory_max_isend) const
113115{
114116 std::stringstream ss_isend;
115117 {
@@ -130,6 +132,7 @@ void Comm_Trans<Tkey,Tvalue,Tdatas_isend,Tdatas_recv>::isend_data(
130132 oar (size_item);
131133 } // end cereal::BinaryOutputArchive
132134 str_isend = std::move (ss_isend.str ());
135+ memory_max_isend.store ( std::max (str_isend.size ()*sizeof (char ), memory_max_isend.load ()) );
133136#if MPI_VERSION>=4
134137 MPI_CHECK (MPI_Isend_c (str_isend.c_str (), str_isend.size (), MPI_CHAR, rank_isend, Comm_Trans::tag_data, this ->mpi_comm , &request_isend));
135138#else
@@ -144,7 +147,8 @@ void Comm_Trans<Tkey,Tvalue,Tdatas_isend,Tdatas_recv>::recv_data (
144147 Tdatas_recv &datas_recv,
145148 const MPI_Status status_recv,
146149 MPI_Message message_recv,
147- std::atomic_flag &lock_set_value)
150+ std::atomic_flag &lock_set_value,
151+ std::atomic<std::size_t > &memory_max_recv)
148152{
149153#if MPI_VERSION>=4
150154 MPI_Count size_mpi; MPI_CHECK ( MPI_Get_count_c (&status_recv, MPI_CHAR, &size_mpi) );
@@ -158,6 +162,7 @@ void Comm_Trans<Tkey,Tvalue,Tdatas_isend,Tdatas_recv>::recv_data (
158162
159163 std::stringstream ss_recv;
160164 ss_recv.rdbuf ()->pubsetbuf (buffer_recv.data (), size_mpi);
165+ memory_max_recv.store ( std::max (buffer_recv.size ()*sizeof (char ), memory_max_recv.load ()) );
161166
162167 {
163168 cereal::BinaryInputArchive iar (ss_recv);
0 commit comments