31 #ifndef __TASMANIAN_ADDONS_MPIGRIDSCATTER_HPP
32 #define __TASMANIAN_ADDONS_MPIGRIDSCATTER_HPP
56 #ifdef Tasmanian_ENABLE_MPI
76 setg(&*data.begin(), data.data(), &*data.end());
87 inline int getMPIRank(MPI_Comm comm){
int rank; MPI_Comm_rank(comm, &rank);
return rank; }
126 template<
bool binary = TasGr
id::mode_binary>
128 std::stringstream ss;
129 grid.
write(ss, binary);
130 while(ss.str().size() % 16 != 0) ss <<
" ";
131 return MPI_Send(ss.str().c_str(), (
int) (ss.str().size() / 16), MPI_LONG_DOUBLE, destination, tag, comm);
154 template<
bool binary = TasGr
id::mode_binary>
156 MPI_Status internal_status;
157 if (status == MPI_STATUS_IGNORE) status = &internal_status;
160 MPI_Probe(source, tag, comm, status);
161 MPI_Get_count(status, MPI_LONG_DOUBLE, &short_data_size);
165 std::vector<char> buff(data_size);
166 auto result = MPI_Recv(buff.data(), (
int) (data_size / 16), MPI_LONG_DOUBLE, source, tag, comm, status);
169 std::istream is(&data_buffer);
170 grid.
read(is, binary);
210 template<
bool binary = TasGr
id::mode_binary>
214 std::stringstream ss;
215 grid.
write(ss, binary);
217 while(ss.str().size() % 16 != 0) ss <<
" ";
219 unsigned long long data_size = (
unsigned long long) ss.str().size();
220 auto result = MPI_Bcast(&data_size, 1, MPI_UNSIGNED_LONG_LONG, me, comm);
221 if (result != MPI_SUCCESS)
return result;
223 return MPI_Bcast(
const_cast<char*
>(ss.str().c_str()), (
int) (data_size / 16), MPI_LONG_DOUBLE, me, comm);
225 unsigned long long data_size;
227 auto result = MPI_Bcast(&data_size, 1, MPI_UNSIGNED_LONG_LONG, root, comm);
228 if (result != MPI_SUCCESS)
return result;
230 std::vector<char> buff((
size_t) data_size);
231 result = MPI_Bcast(buff.data(), (
int) (buff.size() / 16), MPI_LONG_DOUBLE, root, comm);
234 std::istream is(&data_buffer);
235 grid.
read(is, binary);
275 template<
bool binary = TasGr
id::mode_binary>
280 int num_ranks; MPI_Comm_size(comm, &num_ranks);
281 int num_effective_ranks = std::min(num_ranks, source.
getNumOutputs());
287 auto offset = [&](
int rank)->
int{
return rank * stride + std::min(rank, extras); };
289 for(
int rank=0; rank<num_effective_ranks; rank++){
291 destination =
copyGrid(source, offset(rank), offset(rank+1));
293 auto result = MPIGridSend<binary>(
copyGrid(source, offset(rank), offset(rank+1)) , rank, tag, comm);
294 if (result != MPI_SUCCESS)
return result;
297 for(
int rank=num_effective_ranks; rank<num_ranks; rank++){
301 auto result = MPIGridSend<binary>(
makeEmpty() , rank, tag, comm);
302 if (result != MPI_SUCCESS)
return result;
307 return MPIGridRecv<binary>(destination, root, tag, comm);
The master-class that represents an instance of a Tasmanian sparse grid.
Definition: TasmanianSparseGrid.hpp:293
int getNumOutputs() const
Return the outputs of the grid, i.e., number of model outputs.
Definition: TasmanianSparseGrid.hpp:644
void write(const char *filename, bool binary=mode_binary) const
Write the grid to the given filename using either binary or ASCII format.
void read(const char *filename)
Read the grid from the given filename, automatically detect the format.
Coverts a vector to basic stream-buffer.
Definition: tsgMPIScatterGrid.hpp:72
VectorToStreamBuffer(std::vector< char > &data)
Make a stream-buffer from the data vector.
Definition: tsgMPIScatterGrid.hpp:75
int MPIGridScatterOutputs(TasmanianSparseGrid const &source, TasmanianSparseGrid &destination, int root, int tag, MPI_Comm comm)
Split the grid across the comm where each rank receives an equal portion of the total outputs.
Definition: tsgMPIScatterGrid.hpp:276
int MPIGridRecv(TasmanianSparseGrid &grid, int source, int tag, MPI_Comm comm, MPI_Status *status=MPI_STATUS_IGNORE)
Receive a grid from another process in the MPI comm.
Definition: tsgMPIScatterGrid.hpp:155
int MPIGridSend(TasmanianSparseGrid const &grid, int destination, int tag, MPI_Comm comm)
Send a grid to another process in the MPI comm.
Definition: tsgMPIScatterGrid.hpp:127
int getMPIRank(MPI_Comm comm)
Utility to return the rank within the given comm.
Definition: tsgMPIScatterGrid.hpp:87
int MPIGridBcast(TasmanianSparseGrid &grid, int root, MPI_Comm comm)
Broadcast a grid to all processes in an MPI comm.
Definition: tsgMPIScatterGrid.hpp:211
TasmanianSparseGrid makeEmpty()
Returns an empty sparse grid.
Definition: TasmanianSparseGrid.hpp:2255
TasmanianSparseGrid copyGrid(TasmanianSparseGrid const &source, int outputs_begin=0, int outputs_end=-1)
Returns a grid that is a copy of the source.
Definition: TasmanianSparseGrid.hpp:2367
size_t size_mult(IntA a, IntB b)
Converts two integer-like variables to size_t and returns the product..
Definition: tsgUtils.hpp:82
Encapsulates the Tasmanian Sparse Grid module.
Definition: TasmanianSparseGrid.hpp:68
Common includes and methods for all addons.