Skip to content

File ffttopology.h

File List > code_source > templat > include > TempLat > fft > ffttopology.h

Go to the documentation of this file

#ifndef TEMPLAT_FFT_FFTTOPOLOGY_H
#define TEMPLAT_FFT_FFTTOPOLOGY_H

/* This file is part of TempLat, available at https://cosmolattice.github.io/templat .
   Copyright 2021-2026 The TempLat authors, see AUTHORS.md.
   Released under the MIT license, see LICENSE.md. */

// File info: Main contributor(s): Franz R. Sattler, Year: 2026

#include "TempLat/fft/fftdecomposition.h"
#include "TempLat/parallel/mpi/comm/mpicommreference.h"
#include "TempLat/util/exception.h"

#include <array>
#include <cstddef>
#include <vector>

namespace TempLat
{
  MakeException(FFTTopologyException);

  template <size_t NDim> struct FFTTopology {
    MPICommReference comm;
    std::array<int, NDim> dims = {};
    std::array<int, NDim> coords = {};
    int nDimsToSplit = 0;

    FFTDecomposition<NDim> decomposition() const { return FFTDecomposition<NDim>{nDimsToSplit, dims}; }

    std::vector<int> dimsVector() const { return std::vector<int>(dims.begin(), dims.end()); }

    void checkInvariants(int commSize) const
    {
      long product = 1;
      for (size_t i = 0; i < NDim; ++i) {
        if (dims[i] < 1)
          throw FFTTopologyException("FFT backend reported a non-positive grid extent ", dims[i], " at dimension ", i,
                                     ".");
        if (coords[i] < 0 || coords[i] >= dims[i])
          throw FFTTopologyException("FFT backend reported coordinate ", coords[i], " at dimension ", i,
                                     ", outside the grid extent ", dims[i], ".");
        product *= dims[i];
      }

      if (product != commSize)
        throw FFTTopologyException("FFT backend grid has ", product, " cells but the communicator has ", commSize,
                                   " ranks. The grid must tile the communicator exactly.");

      // The last axis must stay local. Three places depend on this and none of them can detect a
      // violation on their own:
      //   - ParafaftMemoryLayout applies the r2c padding to the last axis only;
      //   - ParaFaFT pins real_global_start_[D-1] = 0;
      //   - FileSaverHDF5 writes the last axis as one contiguous rod with offset 0, so a split
      //     there corrupts output files with no error at all.
      if (dims[NDim - 1] != 1)
        throw FFTTopologyException(
            "FFT backend wants to distribute the last lattice dimension (grid extent ", dims[NDim - 1],
            "), which TempLat does not support. The r2c padding in ParafaftMemoryLayout, ParaFaFT's "
            "real_global_start_[D-1] = 0, and the contiguous last-axis write in FileSaverHDF5 all assume it "
            "stays local. Fix all three before allowing this.");
    }
  };
} // namespace TempLat

#endif