25#ifndef CPU_SIMD_COLUMN_H
26#define CPU_SIMD_COLUMN_H
36#include <asm/unistd_64.h>
56 std::unique_ptr<T[]> _data;
63 _data(new T[count] {})
72 _data(rhs._data.release())
77 _data(new T[_count] {})
79 std::copy(rhs._data.get(),
80 rhs._data.get() + _count,
88 std::unique_ptr<T[]>
data = std::unique_ptr<T[]>(
new T[count] { });
89 std::copy(_data.get(), _data.get() + std::min(_count, count),
data.get());
104 std::size_t
size()
const override
126 bool*
getBlock(std::size_t iBlock, std::size_t& sizeBlock,
bool loadingMode)
override;
136 const std::size_t page_size = sysconf(_SC_PAGESIZE);
137 const std::size_t size = ((count *
sizeof(T) - 1) / page_size + 1) * page_size;
138 const std::size_t volume = size /
sizeof(T);
140 if (size % page_size != 0) {
141 throw std::invalid_argument(
"Buffer size must be multiple of PAGE_SIZE");
156 const std::ptrdiff_t _count;
157 const std::size_t _size;
161 std::uint8_t* _buffer;
166 std::ptrdiff_t _shift;
173 _size(_count * sizeof(T)),
176 #ifdef __NR_memfd_create
179 _shm_file = syscall(__NR_memfd_create,
"openlb", MFD_CLOEXEC);
181 std::string shm_path =
"/openlb_block_XXXXXX";
182 const int shm_name = mkstemp(
const_cast<char*
>(shm_path.data()));
183 if (shm_name != -1) {
184 throw std::runtime_error(
"Could not generate unique shared memory object name");
187 _shm_file = shm_open(shm_path.c_str(), O_CREAT | O_RDWR | O_EXCL | O_CLOEXEC, S_IRUSR | S_IWUSR);
188 shm_unlink(shm_path.c_str());
190 if (_shm_file == -1) {
191 throw std::runtime_error(
"Failed to create shared memory object");
195 if (ftruncate(_shm_file, _size) == -1) {
196 throw std::runtime_error(
"Failed to resize shared memory object");
200 _buffer =
static_cast<std::uint8_t*
>(
201 mmap(NULL, 2 * _size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
204 mmap(_buffer, _size,
PROT_RW, MAP_SHARED | MAP_FIXED, _shm_file, 0);
205 mmap(_buffer + _size, _size,
PROT_RW, MAP_SHARED | MAP_FIXED, _shm_file, 0);
208 _base =
reinterpret_cast<T*
>(_buffer);
214 munmap(_buffer, 2 * _size);
228 std::size_t
size()
const override
241 if (_shift >= _count) {
244 else if (_shift < 0) {
252 throw std::logic_error(
"Cyclic column can not be resized");
262 bool*
getBlock(std::size_t iBlock, std::size_t& sizeBlock,
bool loadingMode)
override;
296 return indices.
size() *
sizeof(T);
301 std::uint8_t* buffer)
const
304 T* target =
reinterpret_cast<T*
>(buffer);
307 for (std::size_t i=0;
317 *(target++) = _column[indices[i]];
320 std::uint8_t* target = buffer;
321 for (
CellID index : indices) {
322 std::memcpy(target,
reinterpret_cast<const void*
>(&_column[index]),
sizeof(T));
326 return indices.
size() *
sizeof(T);
331 const std::uint8_t* buffer)
334 const T* source =
reinterpret_cast<const T*
>(buffer);
337 for (std::size_t i=0;
347 _column[indices[i]] = *(source++);
350 const std::uint8_t* source = buffer;
351 for (
CellID index : indices) {
352 std::memcpy(
reinterpret_cast<void*
>(&_column[index]), source,
sizeof(T));
356 return indices.
size() *
sizeof(T);
ConcreteCommunicatable(cpu::simd::CyclicColumn< T > &column)
std::size_t serialize(ConstSpan< CellID > indices, std::uint8_t *buffer) const
Serialize data at locations indices to buffer
std::size_t size(ConstSpan< CellID > indices) const
Get serialized size for data at locations indices
std::size_t deserialize(ConstSpan< CellID > indices, const std::uint8_t *buffer)
Deserialize data at locations indices to buffer
Base class for serializable objects of constant size. For dynamic size use BufferSerializable.
Plain column for SIMD CPU targets.
Column(const Column< T > &rhs)
Column(std::size_t count)
Column(Column< T > &&rhs)
void resize(std::size_t count)
virtual ~Column()=default
std::size_t getSerializableSize() const override
Binary size for the serializer.
bool * getBlock(std::size_t iBlock, std::size_t &sizeBlock, bool loadingMode) override
Return a pointer to the memory of the current block and its size for the serializable interface.
std::size_t getNblock() const override
Number of data blocks for the serializable interface.
void setProcessingContext(ProcessingContext)
std::size_t size() const override
T & operator[](std::size_t i) override
const T & operator[](std::size_t i) const override
Virtual memory based cyclic column for usage in ColumnVector.
void rotate(std::ptrdiff_t offset)
const T & operator[](std::size_t i) const override
void setProcessingContext(ProcessingContext)
std::size_t size() const override
CyclicColumn(std::size_t count)
T & operator[](std::size_t i) override
void resize(std::size_t count)
std::size_t getSerializableSize() const override
Binary size for the serializer.
bool * getBlock(std::size_t iBlock, std::size_t &sizeBlock, bool loadingMode) override
Return a pointer to the memory of the current block and its size for the serializable interface.
std::size_t getNblock() const override
Number of data blocks for the serializable interface.
std::size_t getPageAlignedCount(std::size_t count)
void store(T *target, Pack< T > value)
Top level namespace for all of OpenLB.
std::uint32_t CellID
Type for sequential block-local cell indices.
ProcessingContext
OpenLB processing contexts.
Platform
OpenLB execution targets.
@ CPU_SIMD
Basic scalar CPU.
Abstract declarator of Column-like storage.
Abstract declarator of cyclic Column-like storage.
Specializable declarator for concrete implementations of abstract storage types.