Loading...
Searching...
No Matches
Lp64Limits.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
22
23#ifndef GPR_LINALG_LP64_LIMITS_H_
24#define GPR_LINALG_LP64_LIMITS_H_
25
26#include <algorithm>
27#include <atomic>
28#include <cstdint>
29#include <limits>
30
31namespace gpr {
32namespace linalg {
33
35inline constexpr std::int64_t kLp64IndexLimit =
36 std::numeric_limits<std::int32_t>::max();
37
48
55constexpr std::int64_t blockCyclicLocalExtent(std::int64_t n, std::int64_t nb,
56 std::int64_t iproc,
57 std::int64_t isrc,
58 std::int64_t nprocs) noexcept
59{
60 if (n <= 0 || nb <= 0 || nprocs <= 0) return 0;
61 const std::int64_t distance = ((iproc - isrc) % nprocs + nprocs) % nprocs;
62 const std::int64_t blocks = n / nb;
63 std::int64_t extent = (blocks / nprocs) * nb;
64 const std::int64_t extra = blocks % nprocs;
65 if (distance < extra)
66 extent += nb;
67 else if (distance == extra)
68 extent += n % nb;
69 return extent;
70}
71
84constexpr std::int64_t largestLocalEntries(std::int64_t m, std::int64_t n,
85 std::int64_t mb, std::int64_t nb,
86 std::int64_t rsrc, std::int64_t csrc,
87 std::int64_t nprow,
88 std::int64_t npcol) noexcept
89{
90 const std::int64_t rows = blockCyclicLocalExtent(m, mb, rsrc, rsrc, nprow);
91 const std::int64_t cols = blockCyclicLocalExtent(n, nb, csrc, csrc, npcol);
92 return std::max<std::int64_t>(1, rows) * cols;
93}
94
98constexpr std::int64_t largestLocalEntries(const int* desc, int nprow,
99 int npcol) noexcept
100{
101 const std::int64_t by_layout = largestLocalEntries(
102 desc[kDescM], desc[kDescN], desc[kDescMb], desc[kDescNb],
103 desc[kDescRsrc], desc[kDescCsrc], nprow, npcol);
104 const std::int64_t cols = blockCyclicLocalExtent(
105 desc[kDescN], desc[kDescNb], desc[kDescCsrc], desc[kDescCsrc], npcol);
106 return std::max(by_layout,
107 static_cast<std::int64_t>(desc[kDescLld]) * cols);
108}
109
114inline std::atomic<std::int64_t>& lp64IndexLimitStore() noexcept
115{
116 static std::atomic<std::int64_t> limit{kLp64IndexLimit};
117 return limit;
118}
119
120inline std::int64_t lp64IndexLimit() noexcept
121{
122 return lp64IndexLimitStore().load(std::memory_order_relaxed);
123}
124
127public:
128 explicit ScopedLp64IndexLimit(std::int64_t limit) noexcept
129 : previous_(lp64IndexLimitStore().exchange(limit))
130 {
131 }
133 {
134 lp64IndexLimitStore().store(previous_);
135 }
138
139private:
140 std::int64_t previous_;
141};
142
151inline bool exceedsLp64(const int* desc, int nprow, int npcol) noexcept
152{
153 return largestLocalEntries(desc, nprow, npcol) > lp64IndexLimit();
154}
155
157inline bool exceedsLp64(std::int64_t count) noexcept
158{
159 return count > lp64IndexLimit();
160}
161
162} // namespace linalg
163} // namespace gpr
164
165#endif // GPR_LINALG_LP64_LIMITS_H_
ScopedLp64IndexLimit(const ScopedLp64IndexLimit &)=delete
ScopedLp64IndexLimit(std::int64_t limit) noexcept
Definition Lp64Limits.h:128
ScopedLp64IndexLimit & operator=(const ScopedLp64IndexLimit &)=delete
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
std::int64_t lp64IndexLimit() noexcept
Definition Lp64Limits.h:120
std::atomic< std::int64_t > & lp64IndexLimitStore() noexcept
Definition Lp64Limits.h:114
Lp64DescriptorField
Offsets of the ScaLAPACK array descriptor fields (DTYPE_ .. LLD_ - 1).
Definition Lp64Limits.h:39
constexpr std::int64_t largestLocalEntries(std::int64_t m, std::int64_t n, std::int64_t mb, std::int64_t nb, std::int64_t rsrc, std::int64_t csrc, std::int64_t nprow, std::int64_t npcol) noexcept
Definition Lp64Limits.h:84
bool exceedsLp64(const int *desc, int nprow, int npcol) noexcept
Definition Lp64Limits.h:151
constexpr std::int64_t blockCyclicLocalExtent(std::int64_t n, std::int64_t nb, std::int64_t iproc, std::int64_t isrc, std::int64_t nprocs) noexcept
Definition Lp64Limits.h:55
constexpr std::int64_t kLp64IndexLimit
Largest element offset an LP64 ScaLAPACK routine forms without overflow.
Definition Lp64Limits.h:35
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16