GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
MpiCollectives.h
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) gpridge developers
3
4
#ifndef GPR_LINALG_MPI_COLLECTIVES_H_
5
#define GPR_LINALG_MPI_COLLECTIVES_H_
6
7
#include <algorithm>
8
#include <bit>
9
#include <cstddef>
10
#include <cstdint>
11
#include <limits>
12
#include <stdexcept>
13
#include <vector>
14
15
#include "
CompensatedSum.h
"
16
#include "
ExactSum.h
"
17
#include "
MpiContext.h
"
18
19
namespace
gpr::linalg
{
20
24
inline
void
replicate_owned_values
(
double
* data, std::size_t count,
25
const
MpiContext
& context)
26
{
27
if
(count == 0 || context.
nprocs
== 1)
return
;
28
#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
29
constexpr
std::size_t panel_size = 131072;
30
std::vector<std::uint64_t> bits(std::min(count, panel_size));
31
for
(std::size_t offset = 0; offset < count; offset += bits.size()) {
32
const
auto
length = std::min(bits.size(), count - offset);
33
for
(std::size_t i = 0; i < length; ++i)
34
bits[i] = std::bit_cast<std::uint64_t>(data[offset + i]);
35
MPI_Allreduce(MPI_IN_PLACE, bits.data(),
static_cast<
int
>
(length),
36
MPI_UINT64_T, MPI_BOR, context.
comm
);
37
for
(std::size_t i = 0; i < length; ++i)
38
data[offset + i] = std::bit_cast<double>(bits[i]);
39
}
40
#else
41
(void)data;
42
throw
std::logic_error(
"distributed replication requires MPI"
);
43
#endif
44
}
45
49
inline
void
disjoint_sum_bcast
(
double
* data,
int
count,
50
const
MpiContext
& context)
51
{
52
if
(count <= 0 || context.
nprocs
== 1)
return
;
53
#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
54
MPI_Allreduce(MPI_IN_PLACE, data, count, MPI_DOUBLE, MPI_SUM, context.
comm
);
55
#else
56
(void)data;
57
throw
std::logic_error(
"distributed reduction requires MPI"
);
58
#endif
59
}
60
63
inline
void
ordered_sum_bcast
(
double
* data,
int
count,
64
const
MpiContext
& context)
65
{
66
if
(count <= 0 || context.
nprocs
== 1)
return
;
67
#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
68
69
if
(count <= 0)
return
;
70
71
if
(context.
nprocs
== 1)
return
;
72
73
#ifdef GPR_NATIVE_REDUCTIONS
74
MPI_Allreduce(MPI_IN_PLACE, data, count, MPI_DOUBLE, MPI_SUM, context.
comm
);
75
#else
76
std::vector<double> gathered;
77
if
((context.
rank
== 0))
78
gathered.resize(
static_cast<
size_t
>
(count) *
79
static_cast<
size_t
>
(context.
nprocs
));
80
81
MPI_Gather(data, count, MPI_DOUBLE,
82
(context.
rank
== 0) ? gathered.data() :
nullptr
, count,
83
MPI_DOUBLE, 0, context.
comm
);
84
85
if
((context.
rank
== 0)) {
86
for
(
int
i = 0; i < count; ++i) {
87
long
double
accum = 0.0L;
88
for
(
int
rank = 0; rank < context.
nprocs
; ++rank) {
89
accum +=
static_cast<
long
double
>
(
90
gathered[
static_cast<
size_t
>
(rank) * count + i]);
91
}
92
data[i] =
static_cast<
double
>
(accum);
93
}
94
}
95
96
MPI_Bcast(data, count, MPI_DOUBLE, 0, context.
comm
);
97
#endif
98
99
#else
100
(void)data;
101
throw
std::logic_error(
"distributed reduction requires MPI"
);
102
#endif
103
}
104
107
inline
void
compensated_sum_bcast
(
CompensatedSum
* data,
int
count,
108
const
MpiContext
& context)
109
{
110
if
(count <= 0 || context.
nprocs
== 1)
return
;
111
if
(count > std::numeric_limits<int>::max() / 2)
112
throw
std::length_error(
"compensated reduction exceeds MPI count"
);
113
#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
114
std::vector<double> parts(2 *
static_cast<
std::size_t
>
(count));
115
for
(
int
i = 0; i < count; ++i) {
116
parts[i] = data[i].
sum
;
117
parts[count + i] = data[i].
corr
;
118
}
119
std::vector<double> gathered;
120
if
(context.
rank
== 0)
121
gathered.resize(parts.size() *
122
static_cast<
std::size_t
>
(context.
nprocs
));
123
MPI_Gather(parts.data(),
static_cast<
int
>
(parts.size()), MPI_DOUBLE,
124
context.
rank
== 0 ? gathered.data() :
nullptr
,
125
static_cast<
int
>
(parts.size()), MPI_DOUBLE, 0, context.
comm
);
126
if
(context.
rank
== 0) {
127
for
(
int
i = 0; i < count; ++i) {
128
CompensatedSum
combined;
129
for
(
int
rank = 0; rank < context.
nprocs
; ++rank) {
130
const
std::size_t offset =
131
static_cast<
std::size_t
>
(rank) * parts.size();
132
combined.
add
(gathered[offset + i]);
133
combined.
add
(gathered[offset + count + i]);
134
}
135
parts[i] = combined.
sum
;
136
parts[count + i] = combined.
corr
;
137
}
138
}
139
MPI_Bcast(parts.data(),
static_cast<
int
>
(parts.size()), MPI_DOUBLE, 0,
140
context.
comm
);
141
for
(
int
i = 0; i < count; ++i) {
142
data[i].
sum
= parts[i];
143
data[i].
corr
= parts[count + i];
144
}
145
#else
146
(void)data;
147
throw
std::logic_error(
"distributed reduction requires MPI"
);
148
#endif
149
}
150
155
inline
void
exact_sum_bcast
(
ExactSum
* sums,
int
count,
156
const
MpiContext
& context)
157
{
158
if
(count <= 0 || context.
nprocs
== 1)
return
;
159
#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
160
constexpr
int
kDigits =
ExactSum::kLimbs
;
161
std::vector<std::int64_t> digits(
static_cast<
std::size_t
>
(count) * kDigits);
162
std::vector<double> nonfinite(
static_cast<
std::size_t
>
(count));
163
for
(
int
i = 0; i < count; ++i) {
164
sums[i].
normalize
();
165
std::copy_n(sums[i].limbs(), kDigits,
166
digits.data() +
static_cast<
std::size_t
>
(i) * kDigits);
167
nonfinite[
static_cast<
std::size_t
>
(i)] = sums[i].nonfinite();
168
}
169
MPI_Allreduce(MPI_IN_PLACE, digits.data(),
static_cast<
int
>
(digits.size()),
170
MPI_INT64_T, MPI_SUM, context.
comm
);
171
MPI_Allreduce(MPI_IN_PLACE, nonfinite.data(), count, MPI_DOUBLE, MPI_SUM,
172
context.
comm
);
173
for
(
int
i = 0; i < count; ++i) {
174
sums[i].
assignLimbs
(digits.data() +
175
static_cast<
std::size_t
>
(i) * kDigits);
176
sums[i].
setNonfinite
(nonfinite[
static_cast<
std::size_t
>
(i)]);
177
}
178
#else
179
(void)sums;
180
throw
std::logic_error(
"distributed reduction requires MPI"
);
181
#endif
182
}
183
184
}
// namespace gpr::linalg
185
186
#endif
CompensatedSum.h
ExactSum.h
Exact floating-point accumulator (Kulisch long accumulator).
MpiContext.h
Tiny value type describing the MPI dispatch context for a probe loop.
gpr::linalg::ExactSum
Definition
ExactSum.h:34
gpr::linalg::ExactSum::assignLimbs
void assignLimbs(const std::int64_t *digits) noexcept
Definition
ExactSum.h:138
gpr::linalg::ExactSum::setNonfinite
void setNonfinite(double value) noexcept
Definition
ExactSum.h:184
gpr::linalg::ExactSum::kLimbs
static constexpr int kLimbs
Definition
ExactSum.h:41
gpr::linalg::ExactSum::normalize
void normalize() noexcept
Definition
ExactSum.h:159
gpr::linalg
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Definition
ConditionedKernel.h:14
gpr::linalg::disjoint_sum_bcast
void disjoint_sum_bcast(double *data, int count, const MpiContext &context)
Definition
MpiCollectives.h:49
gpr::linalg::compensated_sum_bcast
void compensated_sum_bcast(CompensatedSum *data, int count, const MpiContext &context)
Definition
MpiCollectives.h:107
gpr::linalg::ordered_sum_bcast
void ordered_sum_bcast(double *data, int count, const MpiContext &context)
Definition
MpiCollectives.h:63
gpr::linalg::replicate_owned_values
void replicate_owned_values(double *data, std::size_t count, const MpiContext &context)
Definition
MpiCollectives.h:24
gpr::linalg::exact_sum_bcast
void exact_sum_bcast(ExactSum *sums, int count, const MpiContext &context)
Definition
MpiCollectives.h:155
gpr::linalg::CompensatedSum
Definition
CompensatedSum.h:85
gpr::linalg::CompensatedSum::add
void add(double value)
Definition
CompensatedSum.h:89
gpr::linalg::CompensatedSum::corr
double corr
Definition
CompensatedSum.h:87
gpr::linalg::CompensatedSum::sum
double sum
Definition
CompensatedSum.h:86
gpr::linalg::MpiContext
Definition
MpiContext.h:59
gpr::linalg::MpiContext::rank
int rank
Definition
MpiContext.h:60
gpr::linalg::MpiContext::comm
MpiCommType comm
Definition
MpiContext.h:62
gpr::linalg::MpiContext::nprocs
int nprocs
Definition
MpiContext.h:61
gpr
linalg
MpiCollectives.h
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf