46 template<
class T,
class S>
53 MPI_Comm_size(comm, &size); MPI_Comm_rank(comm, &rank);
55 size_t numsend = 0, numrecv = 0;
56 for(
int i=0; i<size; i++) {
57 if(grph.
scnt[i]) numsend++;
58 if(grph.
rcnt[i]) numrecv++;
64 size_t sidx = 0, ridx = 0;
65 for(
int i=0; i<size; i++) {
69 MPI_Isend(send.
data() + grph.
sdsp[i], grph.
scnt[i]*
sizeof(S), MPI_BYTE, i,
SF_MPITAG, comm, sreq.data()+sidx++);
72 if( (ridx != numrecv) || (sidx != numsend) )
73 fprintf(stderr,
"Rank %d: MPI_Exchange error! \n", rank);
75 MPI_Waitall(ridx, rreq.
data(), stat.
data());
76 MPI_Waitall(sidx, sreq.data(), stat.
data());
95 MPI_Comm_rank(comm, &rank); MPI_Comm_size(comm, &size);
98 size_t lsize = vec.
size();
100 MPI_Allgather(&lsize,
sizeof(
size_t), MPI_BYTE, vec_sizes.
data(),
sizeof(size_t), MPI_BYTE, comm);
102 int fallback_rank = 0;
103 while(fallback_rank < size && vec_sizes[fallback_rank] == 0) fallback_rank++;
105 assert(vec_sizes[fallback_rank]);
107 T fallback_value = T();
108 if(rank == fallback_rank)
109 fallback_value = vec[0];
111 MPI_Bcast(&fallback_value,
sizeof(T), MPI_BYTE, fallback_rank, comm);
113 return fallback_value;
129 MPI_Comm_size(comm, &size);
131 int zero_size_err = 0;
132 if(vec.
size() == 0) zero_size_err++;
133 MPI_Allreduce(MPI_IN_PLACE, &zero_size_err, 1, MPI_INT, MPI_SUM, comm);
141 T lmin = vec.
size() ? *std::min_element(vec.
begin(), vec.
end()) : fb;
142 MPI_Allgather(&lmin,
sizeof(T), MPI_BYTE, mins.
data(),
sizeof(T), MPI_BYTE, comm);
143 T gmin = *std::min_element(mins.
begin(), mins.
end());
159 MPI_Comm_size(comm, &size);
161 int zero_size_err = 0;
162 if(vec.
size() == 0) zero_size_err++;
163 MPI_Allreduce(MPI_IN_PLACE, &zero_size_err, 1, MPI_INT, MPI_SUM, comm);
171 T lmax = vec.
size() ? *std::max_element(vec.
begin(), vec.
end()) : fb;
172 MPI_Allgather(&lmax,
sizeof(T), MPI_BYTE, max.
data(),
sizeof(T), MPI_BYTE, comm);
173 T gmax = *std::max_element(max.
begin(), max.
end());
189 MPI_Comm_size(comm, &size);
192 MPI_Allgather(&val,
sizeof(T), MPI_BYTE, max.
data(),
sizeof(T), MPI_BYTE, comm);
193 T gmax = *std::max_element(max.
begin(), max.
end());
200 template<
class T>
inline 204 MPI_Comm_size(comm, &size);
207 MPI_Allgather(&count,
sizeof(T), MPI_BYTE, cnt.
data(),
sizeof(T), MPI_BYTE, comm);
211 template<
class T>
inline 214 T li = 0, end = layout.
size() - 1;
216 while(li < end && !(layout[li] <= idx && layout[li+1] > idx))
224 template<
class T>
inline 227 int vecsize = vec.
size()*
sizeof(T);
233 MPI_Allgatherv(vec.
data(), vecsize, MPI_BYTE, out.
data(), cnt.
data(), dsp.
data(), MPI_BYTE, comm);
236 template<
class T>
inline
T global_min(const vector< T > &vec, MPI_Comm comm)
Compute the global minimum of a distributed vector.
The vector class and related algorithms.
T index_in_layout(const T idx, const vector< T > &layout)
void layout_from_count(const T count, vector< T > &layout, MPI_Comm comm)
void MPI_Exchange(commgraph< T > &grph, vector< S > &send, vector< S > &recv, MPI_Comm comm)
Exchange data in parallel over MPI.
constexpr T max(T a, T b)
T parallel_fallback_value(const vector< T > &vec, MPI_Comm comm)
Get a fallback value for operations on parallel vectors if the local vector is of 0 size...
#define SF_MPITAG
the MPI tag when communicating
void count(const vector< T > &data, vector< S > &cnt)
Count number of occurrences of indices.
vector< T > rdsp
Displacements w.r.t. rcnt.
T * data()
Pointer to the vector's start.
const T * end() const
Pointer to the vector's end.
The class holds the communication graph for a MPI_Exchange() call.
void cnt_from_dsp(const vector< T > &dsp, vector< T > &cnt)
Compute counts from displacements.
void make_global(const vector< T > &vec, vector< T > &out, MPI_Comm comm)
make a parallel vector global
T sum(const vector< T > &vec)
Compute sum of a vector's entries.
T global_max(const vector< T > &vec, MPI_Comm comm)
Compute the global maximum of a distributed vector.
size_t size() const
The current size of the vector.
vector< T > scnt
Number of elements sent to each rank.
vector< T > sdsp
Displacements w.r.t. scnt.
const T * begin() const
Pointer to the vector's start.
vector< T > rcnt
Number of elements received from each rank.
void resize(size_t n)
Resize a vector.
void dsp_from_cnt(const vector< T > &cnt, vector< T > &dsp)
Compute displacements from counts.