OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
FieldArray.cpp
Go to the documentation of this file.
1//
2// Class FieldArray
3// See FieldArray.h.
4//
5// Copyright (c) 2026, Paul Scherrer Institut, Villigen PSI, Switzerland
6// All rights reserved.
7//
8// This file is part of OPAL.
9//
10// OPAL is free software: you can redistribute it and/or modify
11// it under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// You should have received a copy of the GNU General Public License
16// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17//
18
19#include "Fields/FieldArray.h"
20
21#include <hdf5.h>
22
23#include <fcntl.h>
24#include <sys/mman.h>
25#include <unistd.h>
26
27#include <algorithm>
28#include <cstdint>
29
30namespace {
31 // The access pattern during tracking is an 8-corner gather per particle, i.e.
32 // effectively random over a multi-GB region, so tell the kernel not to bother with
33 // readahead.
34 constexpr int trackingAdvice = MADV_RANDOM;
35}
36
37void FieldArray::allocate(std::size_t n) {
38 reset();
39 owned_m.assign(n, 0.0);
40 data_m = owned_m.data();
41 size_m = n;
42}
43
45 if (mapping_m != nullptr) {
46 munmap(mapping_m, mapBytes_m);
47 mapping_m = nullptr;
48 mapBytes_m = 0;
49 }
50 std::vector<double>().swap(owned_m);
51 data_m = nullptr;
52 size_m = 0;
53}
54
55bool FieldArray::tryMap(const std::string& filename,
56 const std::string& dataset,
57 std::size_t expectedSamples,
58 std::string& why) {
59 // Everything here is best effort: any failure means "read it the old way", never
60 // an exception. HDF5's own error stack is silenced so a missing dataset does not
61 // print a wall of text on a path we expect to fail sometimes.
62 H5E_auto2_t oldFunc = nullptr;
63 void* oldData = nullptr;
64 H5Eget_auto2(H5E_DEFAULT, &oldFunc, &oldData);
65 H5Eset_auto2(H5E_DEFAULT, nullptr, nullptr);
66 struct ErrRestore {
67 H5E_auto2_t f; void* d;
68 ~ErrRestore() { H5Eset_auto2(H5E_DEFAULT, f, d); }
69 } errRestore{oldFunc, oldData};
70
71 // Serial (POSIX) VFD on purpose: we only want metadata, every rank asks
72 // independently, and this must not be collective over the tracking communicator.
73 hid_t file = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
74 if (file < 0) { why = "cannot open serially"; return false; }
75
76 hid_t dset = H5Dopen2(file, dataset.c_str(), H5P_DEFAULT);
77 if (dset < 0) { H5Fclose(file); why = "no dataset " + dataset; return false; }
78
79 bool ok = false;
80 haddr_t offset = HADDR_UNDEF;
81 do {
82 hid_t dcpl = H5Dget_create_plist(dset);
83 if (dcpl < 0) { why = "no create plist"; break; }
84 const H5D_layout_t layout = H5Pget_layout(dcpl);
85 const int nfilters = H5Pget_nfilters(dcpl);
86 H5Pclose(dcpl);
87 if (layout != H5D_CONTIGUOUS) { why = "not contiguous"; break; }
88 if (nfilters != 0) { why = "filtered"; break; }
89
90 // The in-file representation must be bit-identical to a double in memory,
91 // otherwise H5Dread's type conversion is doing real work and we cannot skip it.
92 hid_t dtype = H5Dget_type(dset);
93 const htri_t sameType = H5Tequal(dtype, H5T_IEEE_F64LE);
94 H5Tclose(dtype);
95 if (sameType <= 0) { why = "not little-endian float64"; break; }
96
97 if (H5Dget_storage_size(dset) != expectedSamples * sizeof(double)) {
98 why = "storage size mismatch"; break;
99 }
100
101 offset = H5Dget_offset(dset);
102 if (offset == HADDR_UNDEF) { why = "no file offset"; break; }
103 // mmap can start at any page, but the samples themselves must land on a
104 // natural double boundary. HDF5 does not promise that.
105 if (offset % sizeof(double) != 0) { why = "offset not 8-byte aligned"; break; }
106 ok = true;
107 } while (false);
108
109 H5Dclose(dset);
110 H5Fclose(file);
111 if (!ok) return false;
112
113 const int fd = open(filename.c_str(), O_RDONLY);
114 if (fd < 0) { why = "cannot open for mapping"; return false; }
115
116 const std::size_t pageSize = static_cast<std::size_t>(sysconf(_SC_PAGESIZE));
117 const off_t base = static_cast<off_t>((offset / pageSize) * pageSize);
118 const std::size_t delta = static_cast<std::size_t>(offset - static_cast<haddr_t>(base));
119 const std::size_t bytes = expectedSamples * sizeof(double) + delta;
120
121 // MAP_SHARED so every process on the node faults the same page-cache pages; MAP_PRIVATE
122 // would be copy-on-write and defeat the entire purpose. PROT_READ keeps it read-only,
123 // so a stray write is a fault rather than silent corruption of the file.
124 void* p = mmap(nullptr, bytes, PROT_READ, MAP_SHARED, fd, base);
125 close(fd); // the mapping keeps its own reference to the file
126 if (p == MAP_FAILED) { why = "mmap failed"; return false; }
127
128 madvise(p, bytes, trackingAdvice);
129
130 reset();
131 mapping_m = p;
132 mapBytes_m = bytes;
133 data_m = reinterpret_cast<const double*>(static_cast<const char*>(p) + delta);
134 size_m = expectedSamples;
135 return true;
136}
137
138bool FieldArray::anyNonZero(std::size_t begin, std::size_t end) const {
139 end = std::min(end, size_m);
140 if (begin >= end) {
141 return false;
142 }
143
144 void* adviseBase = nullptr;
145 std::size_t adviseBytes = 0;
146 if (mapping_m != nullptr) {
147 // madvise wants a page-aligned start; rounding down stays inside the mapping,
148 // whose base is page aligned.
149 const std::uintptr_t pageMask = static_cast<std::uintptr_t>(sysconf(_SC_PAGESIZE)) - 1;
150 const std::uintptr_t lo = reinterpret_cast<std::uintptr_t>(data_m + begin) & ~pageMask;
151 const std::uintptr_t hi = reinterpret_cast<std::uintptr_t>(data_m + end);
152 adviseBase = reinterpret_cast<void*>(lo);
153 adviseBytes = hi - lo;
154 madvise(adviseBase, adviseBytes, MADV_SEQUENTIAL);
155 }
156
157 // A block at a time, so the inner loop has no early exit and vectorises.
158 // x != 0.0 is also true for NaN, which must not be mistaken for an empty field.
159 constexpr std::size_t block = 4096;
160 bool found = false;
161 for (std::size_t i = begin; i < end && !found; i += block) {
162 const std::size_t stop = std::min(i + block, end);
163 for (std::size_t j = i; j < stop; ++j) {
164 found |= (data_m[j] != 0.0);
165 }
166 }
167
168 if (adviseBase != nullptr) {
169 madvise(adviseBase, adviseBytes, trackingAdvice);
170 }
171 return found;
172}
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
bool tryMap(const std::string &filename, const std::string &dataset, std::size_t expectedSamples, std::string &why)
const double * data_m
what operator[] reads; into owned_m or the mapping
Definition FieldArray.h:86
std::vector< double > owned_m
used when not mapped
Definition FieldArray.h:88
void allocate(std::size_t n)
std::size_t mapBytes_m
length passed to mmap, for munmap
Definition FieldArray.h:90
void reset()
Release the buffer or the mapping.
bool anyNonZero(std::size_t begin, std::size_t end) const
std::size_t size_m
samples, not bytes
Definition FieldArray.h:87
void * mapping_m
page-aligned mmap base, null when not mapped
Definition FieldArray.h:89