6#include "plumbing/lattice.h"
10class partitions_struct {
12 unsigned _number, _mylattice;
17 unsigned number()
const {
21 void set_number(
const unsigned u) {
25 unsigned mylattice()
const {
29 void set_mylattice(
const unsigned l) {
37 void set_sync(
bool s) {
42extern partitions_struct partitions;
67int get_next_msg_tag();
81MPI_Datatype get_MPI_number_type(
size_t &size,
bool with_int =
false) {
83 if (std::is_same<hila::arithmetic_type<T>,
int>::value) {
85 return with_int ? MPI_2INT : MPI_INT;
86 }
else if (std::is_same<hila::arithmetic_type<T>,
unsigned>::value) {
87 size =
sizeof(unsigned);
88 return with_int ? MPI_2INT : MPI_UNSIGNED;
89 }
else if (std::is_same<hila::arithmetic_type<T>,
long>::value) {
91 return with_int ? MPI_LONG_INT : MPI_LONG;
92 }
else if (std::is_same<hila::arithmetic_type<T>, int64_t>::value) {
93 size =
sizeof(int64_t);
94 return with_int ? MPI_LONG_INT : MPI_INT64_T;
95 }
else if (std::is_same<hila::arithmetic_type<T>, uint64_t>::value) {
96 size =
sizeof(uint64_t);
97 return with_int ? MPI_LONG_INT : MPI_UINT64_T;
98 }
else if (std::is_same<hila::arithmetic_type<T>,
float>::value) {
100 return with_int ? MPI_FLOAT_INT : MPI_FLOAT;
101 }
else if (std::is_same<hila::arithmetic_type<T>,
double>::value) {
102 size =
sizeof(double);
103 return with_int ? MPI_DOUBLE_INT : MPI_DOUBLE;
104 }
else if (std::is_same<hila::arithmetic_type<T>,
long double>::value) {
105 size =
sizeof(
long double);
106 return with_int ? MPI_LONG_DOUBLE_INT : MPI_LONG_DOUBLE;
115MPI_Datatype get_MPI_number_type() {
117 return get_MPI_number_type<T>(s);
128MPI_Datatype get_MPI_complex_type(
size_t &siz) {
129 if constexpr (std::is_same<T, Complex<double>>::value) {
131 return MPI_C_DOUBLE_COMPLEX;
132 }
else if constexpr (std::is_same<T, Complex<float>>::value) {
134 return MPI_C_FLOAT_COMPLEX;
136 static_assert(
sizeof(T) > 0,
137 "get_MPI_complex_type<T>() called without T being a complex type");
169 static_assert(std::is_trivial<T>::value,
"broadcast(var) must use trivial type");
170 if (hila::check_input)
175 broadcast_timer.start();
176 MPI_Bcast(&var,
sizeof(T), MPI_BYTE, rank, lattice.mpi_comm_lat);
177 broadcast_timer.stop();
192 static_assert(std::is_trivial<T>::value,
"broadcast(std::vector<T>) must have trivial T");
194 if (hila::check_input)
197 broadcast_timer.start();
199 int size = list.size();
200 MPI_Bcast(&size,
sizeof(
int), MPI_BYTE, rank, lattice.mpi_comm_lat);
206 MPI_Bcast((
void *)list.data(),
sizeof(T) * size, MPI_BYTE, rank, lattice.mpi_comm_lat);
208 broadcast_timer.stop();
212template <
typename T,
int n>
215 static_assert(std::is_trivial<T>::value,
"broadcast(std::array<T>) must have trivial T");
217 if (hila::check_input)
220 broadcast_timer.start();
223 MPI_Bcast((
void *)arr.data(),
sizeof(T) * n, MPI_BYTE, rank, lattice.mpi_comm_lat);
225 broadcast_timer.stop();
236 static_assert(
sizeof(T) > 0 &&
237 "Do not use pointers to broadcast()-function. Use 'broadcast_array(T* arr, "
238 "int size)' to broadcast an array");
247 if (hila::check_input)
250 broadcast_timer.start();
251 MPI_Bcast((
void *)var,
sizeof(T) * n, MPI_BYTE, rank, lattice.mpi_comm_lat);
252 broadcast_timer.stop();
256void broadcast(std::string &r,
int rank = 0);
257void broadcast(std::vector<std::string> &l,
int rank = 0);
260template <
typename T,
typename U>
263 if (hila::check_input)
278void send_to(
int to_rank,
const T &data) {
279 if (hila::check_input)
283 MPI_Send(&data,
sizeof(T), MPI_BYTE, to_rank,
hila::myrank(), lattice.mpi_comm_lat);
288void receive_from(
int from_rank, T &data) {
289 if (hila::check_input)
293 MPI_Recv(&data,
sizeof(T), MPI_BYTE, from_rank, from_rank, lattice.mpi_comm_lat,
299void send_to(
int to_rank,
const std::vector<T> &data) {
300 if (hila::check_input)
304 size_t s = data.size();
305 MPI_Send(&s,
sizeof(
size_t), MPI_BYTE, to_rank,
hila::myrank(), lattice.mpi_comm_lat);
307 MPI_Send(data.data(),
sizeof(T) * s, MPI_BYTE, to_rank,
hila::myrank(), lattice.mpi_comm_lat);
312void receive_from(
int from_rank, std::vector<T> &data) {
313 if (hila::check_input)
318 MPI_Recv(&s,
sizeof(
size_t), MPI_BYTE, from_rank, from_rank, lattice.mpi_comm_lat,
322 MPI_Recv(data.data(),
sizeof(T) * s, MPI_BYTE, from_rank, from_rank, lattice.mpi_comm_lat,
334 if (hila::check_input)
337 std::vector<T> recv_data(send_count);
339 dtype = get_MPI_number_type<T>();
341 reduction_timer.start();
343 MPI_Allreduce((
void *)value, (
void *)recv_data.data(),
344 send_count * (
sizeof(T) /
sizeof(hila::arithmetic_type<T>)), dtype, MPI_SUM,
345 lattice.mpi_comm_lat);
346 for (
int i = 0; i < send_count; i++)
347 value[i] = recv_data[i];
349 MPI_Reduce((
void *)value, (
void *)recv_data.data(),
350 send_count * (
sizeof(T) /
sizeof(hila::arithmetic_type<T>)), dtype, MPI_SUM, 0,
351 lattice.mpi_comm_lat);
353 for (
int i = 0; i < send_count; i++)
354 value[i] = recv_data[i];
356 reduction_timer.stop();
372void reduce_node_product(T *send_data,
int send_count,
bool allreduce =
true) {
373 std::vector<T> recv_data(send_count);
376 if (hila::check_input)
379 dtype = get_MPI_number_type<T>();
381 reduction_timer.start();
383 MPI_Allreduce((
void *)send_data, (
void *)recv_data.data(), send_count, dtype, MPI_PROD,
384 lattice.mpi_comm_lat);
385 for (
int i = 0; i < send_count; i++)
386 send_data[i] = recv_data[i];
388 MPI_Reduce((
void *)send_data, (
void *)recv_data.data(), send_count, dtype, MPI_PROD, 0,
389 lattice.mpi_comm_lat);
391 for (
int i = 0; i < send_count; i++)
392 send_data[i] = recv_data[i];
394 reduction_timer.stop();
398T reduce_node_product(T &var,
bool allreduce =
true) {
399 reduce_node_product(&var, 1, allreduce);
410void hila_reduce_double_setup(
double *d,
int n);
411void hila_reduce_float_setup(
float *d,
int n);
412void hila_reduce_sums();
416void hila_reduce_sum_setup(T *value) {
418 using b_t = hila::arithmetic_type<T>;
419 if (std::is_same<b_t, double>::value) {
420 hila_reduce_double_setup((
double *)value,
sizeof(T) /
sizeof(
double));
421 }
else if (std::is_same<b_t, float>::value) {
422 hila_reduce_float_setup((
float *)value,
sizeof(T) /
sizeof(
float));
This file defines all includes for HILA.
Implement hila::swap for gauge fields.
void broadcast_array(T *var, int n, int rank=0)
Broadcast for arrays where size must be known and same for all nodes.
int myrank()
rank of this node
int number_of_nodes()
how many nodes there are
void set_allreduce(bool on=true)
set allreduce on (default) or off on the next reduction
void reduce_node_sum(T *value, int send_count, bool allreduce=true)
Reduce an array across nodes.
T broadcast(T &var, int rank=0)
Broadcast the value of var to all MPI ranks from rank (default=0).
void broadcast2(T &t, U &u, int rank=0)
and broadcast with two values