6#include "plumbing/lattice.h"
10class partitions_struct {
12 unsigned _number, _mylattice;
18 unsigned mylattice() {
26extern partitions_struct partitions;
51int get_next_msg_tag();
65MPI_Datatype get_MPI_number_type(
size_t &size,
bool with_int =
false) {
67 if (std::is_same<hila::arithmetic_type<T>,
int>::value) {
69 return with_int ? MPI_2INT : MPI_INT;
70 }
else if (std::is_same<hila::arithmetic_type<T>,
unsigned>::value) {
71 size =
sizeof(unsigned);
72 return with_int ? MPI_2INT : MPI_UNSIGNED;
73 }
else if (std::is_same<hila::arithmetic_type<T>,
long>::value) {
75 return with_int ? MPI_LONG_INT : MPI_LONG;
76 }
else if (std::is_same<hila::arithmetic_type<T>, int64_t>::value) {
77 size =
sizeof(int64_t);
78 return with_int ? MPI_LONG_INT : MPI_INT64_T;
79 }
else if (std::is_same<hila::arithmetic_type<T>, uint64_t>::value) {
80 size =
sizeof(uint64_t);
81 return with_int ? MPI_LONG_INT : MPI_UINT64_T;
82 }
else if (std::is_same<hila::arithmetic_type<T>,
float>::value) {
84 return with_int ? MPI_FLOAT_INT : MPI_FLOAT;
85 }
else if (std::is_same<hila::arithmetic_type<T>,
double>::value) {
86 size =
sizeof(double);
87 return with_int ? MPI_DOUBLE_INT : MPI_DOUBLE;
88 }
else if (std::is_same<hila::arithmetic_type<T>,
long double>::value) {
89 size =
sizeof(
long double);
90 return with_int ? MPI_LONG_DOUBLE_INT : MPI_LONG_DOUBLE;
99MPI_Datatype get_MPI_number_type() {
101 return get_MPI_number_type<T>(s);
112MPI_Datatype get_MPI_complex_type(
size_t &siz) {
113 if constexpr (std::is_same<T, Complex<double>>::value) {
115 return MPI_C_DOUBLE_COMPLEX;
116 }
else if constexpr (std::is_same<T, Complex<float>>::value) {
118 return MPI_C_FLOAT_COMPLEX;
120 static_assert(
sizeof(T) > 0,
121 "get_MPI_complex_type<T>() called without T being a complex type");
153 static_assert(std::is_trivial<T>::value,
"broadcast(var) must use trivial type");
154 if (hila::check_input)
159 broadcast_timer.start();
160 MPI_Bcast(&var,
sizeof(T), MPI_BYTE, rank, lattice.mpi_comm_lat);
161 broadcast_timer.stop();
176 static_assert(std::is_trivial<T>::value,
"broadcast(std::vector<T>) must have trivial T");
178 if (hila::check_input)
181 broadcast_timer.start();
183 int size = list.size();
184 MPI_Bcast(&size,
sizeof(
int), MPI_BYTE, rank, lattice.mpi_comm_lat);
190 MPI_Bcast((
void *)list.data(),
sizeof(T) * size, MPI_BYTE, rank, lattice.mpi_comm_lat);
192 broadcast_timer.stop();
196template <
typename T,
int n>
199 static_assert(std::is_trivial<T>::value,
"broadcast(std::array<T>) must have trivial T");
201 if (hila::check_input)
204 broadcast_timer.start();
207 MPI_Bcast((
void *)arr.data(),
sizeof(T) * n, MPI_BYTE, rank, lattice.mpi_comm_lat);
209 broadcast_timer.stop();
220 static_assert(
sizeof(T) > 0 &&
221 "Do not use pointers to broadcast()-function. Use 'broadcast_array(T* arr, "
222 "int size)' to broadcast an array");
231 if (hila::check_input)
234 broadcast_timer.start();
235 MPI_Bcast((
void *)var,
sizeof(T) * n, MPI_BYTE, rank, lattice.mpi_comm_lat);
236 broadcast_timer.stop();
240void broadcast(std::string &r,
int rank = 0);
241void broadcast(std::vector<std::string> &l,
int rank = 0);
244template <
typename T,
typename U>
247 if (hila::check_input)
262void send_to(
int to_rank,
const T &data) {
263 if (hila::check_input)
267 MPI_Send(&data,
sizeof(T), MPI_BYTE, to_rank,
hila::myrank(), lattice.mpi_comm_lat);
272void receive_from(
int from_rank, T &data) {
273 if (hila::check_input)
277 MPI_Recv(&data,
sizeof(T), MPI_BYTE, from_rank, from_rank, lattice.mpi_comm_lat,
283void send_to(
int to_rank,
const std::vector<T> &data) {
284 if (hila::check_input)
288 size_t s = data.size();
289 MPI_Send(&s,
sizeof(
size_t), MPI_BYTE, to_rank,
hila::myrank(), lattice.mpi_comm_lat);
291 MPI_Send(data.data(),
sizeof(T) * s, MPI_BYTE, to_rank,
hila::myrank(), lattice.mpi_comm_lat);
296void receive_from(
int from_rank, std::vector<T> &data) {
297 if (hila::check_input)
302 MPI_Recv(&s,
sizeof(
size_t), MPI_BYTE, from_rank, from_rank, lattice.mpi_comm_lat,
306 MPI_Recv(data.data(),
sizeof(T) * s, MPI_BYTE, from_rank, from_rank, lattice.mpi_comm_lat,
318 if (hila::check_input)
321 std::vector<T> recv_data(send_count);
323 dtype = get_MPI_number_type<T>();
325 reduction_timer.start();
327 MPI_Allreduce((
void *)value, (
void *)recv_data.data(),
328 send_count * (
sizeof(T) /
sizeof(hila::arithmetic_type<T>)), dtype, MPI_SUM,
329 lattice.mpi_comm_lat);
330 for (
int i = 0; i < send_count; i++)
331 value[i] = recv_data[i];
333 MPI_Reduce((
void *)value, (
void *)recv_data.data(),
334 send_count * (
sizeof(T) /
sizeof(hila::arithmetic_type<T>)), dtype, MPI_SUM, 0,
335 lattice.mpi_comm_lat);
337 for (
int i = 0; i < send_count; i++)
338 value[i] = recv_data[i];
340 reduction_timer.stop();
356void reduce_node_product(T *send_data,
int send_count,
bool allreduce =
true) {
357 std::vector<T> recv_data(send_count);
360 if (hila::check_input)
363 dtype = get_MPI_number_type<T>();
365 reduction_timer.start();
367 MPI_Allreduce((
void *)send_data, (
void *)recv_data.data(), send_count, dtype, MPI_PROD,
368 lattice.mpi_comm_lat);
369 for (
int i = 0; i < send_count; i++)
370 send_data[i] = recv_data[i];
372 MPI_Reduce((
void *)send_data, (
void *)recv_data.data(), send_count, dtype, MPI_PROD, 0,
373 lattice.mpi_comm_lat);
375 for (
int i = 0; i < send_count; i++)
376 send_data[i] = recv_data[i];
378 reduction_timer.stop();
382T reduce_node_product(T &var,
bool allreduce =
true) {
383 reduce_node_product(&var, 1, allreduce);
394void hila_reduce_double_setup(
double *d,
int n);
395void hila_reduce_float_setup(
float *d,
int n);
396void hila_reduce_sums();
400void hila_reduce_sum_setup(T *value) {
402 using b_t = hila::arithmetic_type<T>;
403 if (std::is_same<b_t, double>::value) {
404 hila_reduce_double_setup((
double *)value,
sizeof(T) /
sizeof(
double));
405 }
else if (std::is_same<b_t, float>::value) {
406 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