OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
FieldArray.h
Go to the documentation of this file.
1//
2// Class FieldArray
3// Storage for one component of a 3D field map.
4//
5// Holds the samples either in its own heap buffer (the historical behaviour) or as a
6// read-only view of the HDF5 file mapped into memory. The mapped form exists because
7// every MPI rank otherwise allocates a private copy of every map: two maps of ~5 GB
8// each cost ~11.5 GB per rank, which caps a job by memory long before it runs out of
9// cores. Mapping the file instead lets the page cache hold one physical copy per node,
10// shared by every rank on it, with no MPI involvement.
11//
12// The mapped form is only possible when the dataset is stored contiguously, unfiltered,
13// as little-endian float64, at a byte offset the host can align - i.e. when the bytes on
14// disk are already exactly what we would have read into memory. tryMap() checks all of
15// that and returns false if anything does not hold, in which case the caller reads the
16// data the way it always did.
17//
18// Copyright (c) 2026, Paul Scherrer Institut, Villigen PSI, Switzerland
19// All rights reserved.
20//
21// This file is part of OPAL.
22//
23// OPAL is free software: you can redistribute it and/or modify
24// it under the terms of the GNU General Public License as published by
25// the Free Software Foundation, either version 3 of the License, or
26// (at your option) any later version.
27//
28// You should have received a copy of the GNU General Public License
29// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
30//
31#ifndef CLASSIC_FIELDARRAY_H
32#define CLASSIC_FIELDARRAY_H
33
34#include <cstddef>
35#include <string>
36#include <vector>
37
39public:
40 FieldArray() = default;
42
43 FieldArray(const FieldArray&) = delete;
44 FieldArray& operator=(const FieldArray&) = delete;
45
47 std::size_t size() const { return size_m; }
48 bool empty() const { return size_m == 0; }
49
52 double operator[](std::size_t i) const { return data_m[i]; }
53
54 const double* data() const { return data_m; }
55
59 bool anyNonZero(std::size_t begin, std::size_t end) const;
60
63 bool isMapped() const { return mapping_m != nullptr; }
64
67 void allocate(std::size_t n);
68
71 double* mutableData() { return owned_m.empty() ? nullptr : owned_m.data(); }
72
77 bool tryMap(const std::string& filename,
78 const std::string& dataset,
79 std::size_t expectedSamples,
80 std::string& why);
81
83 void reset();
84
85private:
86 const double* data_m = nullptr;
87 std::size_t size_m = 0;
88 std::vector<double> owned_m;
89 void* mapping_m = nullptr;
90 std::size_t mapBytes_m = 0;
91};
92
93#endif
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)
double operator[](std::size_t i) const
Definition FieldArray.h:52
bool isMapped() const
Definition FieldArray.h:63
const double * data_m
what operator[] reads; into owned_m or the mapping
Definition FieldArray.h:86
bool empty() const
Definition FieldArray.h:48
std::size_t size() const
Number of samples. Zero for an array that has not been filled.
Definition FieldArray.h:47
FieldArray()=default
std::vector< double > owned_m
used when not mapped
Definition FieldArray.h:88
const double * data() const
Definition FieldArray.h:54
void allocate(std::size_t n)
FieldArray(const FieldArray &)=delete
std::size_t mapBytes_m
length passed to mmap, for munmap
Definition FieldArray.h:90
FieldArray & operator=(const FieldArray &)=delete
void reset()
Release the buffer or the mapping.
bool anyNonZero(std::size_t begin, std::size_t end) const
double * mutableData()
Definition FieldArray.h:71
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