# Node-local shared memory

Read-only data used by several MPI ranks on the same node can occupy one
shared allocation. Each rank has its own address mapping and lightweight
metadata, while the array's physical storage is shared. This is useful for
lookup tables and other arrays that remain unchanged between construction
and release.

`source_base/parallel_shared_memory.h` provides `SharedMemoryContext` and
`SharedDoubleBuffer` in `Parallel_Common` for contiguous host arrays of
`double`.

## Memory model

Construct a context with the communicator whose ranks participate in building
the data. The component uses `MPI_Comm_split_type` with `MPI_COMM_TYPE_SHARED`
to form shared-memory groups. Each group has an independent allocation; the
component does not request separate groups for NUMA domains.

All group members participate in `MPI_Win_allocate_shared`. Group rank zero
requests the array storage, and the other ranks request zero bytes. Each rank
obtains a local pointer through `MPI_Win_shared_query`. Pointer values may
differ between processes even though they refer to the same storage.

## Construction and access

`build(count, layout, initialize)` is collective over the supplied communicator:

1. Check that the element count and layout identifier agree within each group.
2. Invoke `initialize(double*)` synchronously on group rank zero to fill the
   allocated array.
3. Publish the writes using window synchronization and a group barrier.
4. Return `std::shared_ptr<const SharedDoubleBuffer>` on each rank.

The layout identifier is defined by the caller. Matching counts and layouts
do not check array contents; callers must also ensure that their input data
agree within each group. After `build()` returns, reads through `data()` are
ordinary host-memory accesses and require no MPI calls.

For example, given a context named `sharing`:

```cpp
const std::size_t count = 16;
const std::uint64_t layout_id = 1;
std::vector<double> private_values;
const std::function<void(double*)> initialize = [&private_values, count](double* values)
{
    if (values == nullptr)
    {
        private_values.resize(count);
        values = private_values.data();
    }
    for (std::size_t i = 0; i < count; ++i)
    {
        values[i] = static_cast<double>(i);
    }
};
const std::shared_ptr<const Parallel_Common::SharedDoubleBuffer> table =
    sharing.build(count, layout_id, initialize);
const double* values = private_values.data();
if (table)
{
    values = table->data();
}
// Use values while table or private_values still owns the storage.
```

A default-constructed context uses private storage and also works in a serial
build. Default contexts, singleton groups, empty arrays and incompatible layouts
call the initializer with `nullptr` on each affected rank and return an empty
owner. The initializer supplies private storage in that case, as shown above.

The writable pointer is valid for initialization only and must not be retained
for later writes. Published owners expose `const double*`. This is a C++ access
contract, not operating-system page protection. To change the contents, build
a new buffer and release the old owners after their readers have finished.

## Ownership and release

Keep an owner alive for every pointer or view into an array. Reference counting
is local to each process. Releasing the last local owner invokes the buffer's
destructor, which ends the window access epoch, collectively frees the MPI
window, and releases the group communicator.

All participants must call `build()` in the same order and release their final
owners in the same order within each group. Release all buffers before MPI
finalization, after local readers have finished. The context does not own the
supplied communicator; keep that communicator valid for calls to `build()`.

MPI allocation errors and exceptions from a shared initializer terminate the
calculation so that other ranks do not wait indefinitely for publication.
Exceptions from a private initializer propagate to its caller.
