utils.cu

This page documents the functions defined in utils.cu. utils.cu contains utility functions used throughout the GPUmonty codebase.

Functions

Functions

__device__ double interp_scalar(cudaTextureObject_t var, const int mmenemonics, const int i, const int j, const int k, const double del[4])

Interpolates a scalar field using hardware-accelerated CUDA Texture Objects.

This function retrieves the value of a physical variable at a sub-grid location. Unlike interp_scalar_pointer, which performs a manual 8-point trilinear interpolation in software, this version uses the GPU’s dedicated texture mapping units (TMUs) to accelerate the lookups.

Note

To handle 4D data (3 spatial dimensions + multiple physical variables), the spatial index ‘k’ and the variable index ‘mmenemonics’ are flattened into the texture’s X-axis.

Note

By fetching two discrete points, k * NPRIM and (k+1) * NPRIM, and blending them with del[3], we ensure we are only interpolating the same variable across two different spatial slices.

Parameters:
  • var – The CUDA 3D texture object containing the fluid data.

  • mmenemonics – The index of the specific variable (e.g., RHO, UU).

  • i, j, k – Integer base indices for the grid cell.

  • del[4] – Array of fractional offsets [0, 1] for the sub-grid position at (i,j,k).

Returns:

The interpolated value of the scalar field.

__device__ int findPhotonIndex(const unsigned long long *cumulativeArray, int arraySize, unsigned long long photon_index)

Maps a global photon ID to a specific grid cell or source index using binary search.

This function performs a search over a prefix cumulative array to determine which “bucket” a specific superphoton belongs to. This is used to identify at which spatial cell a superphoton was emitted from based on its unique global ID.

Parameters:
  • cumulativeArray – Pointer to the prefix sum array (e.g., cumulative photon counts per cell).

  • arraySize – The number of elements in the array.

  • photon_index – The unique ID of the photon being processed.

Returns:

The index of the cell or source responsible for this photon.

__device__ double interp_scalar_pointer(const double *var, const int mmenemonics, const int i, const int j, const int k, const double coeff[8])

Performs trilinear interpolation of a scalar field at a sub-grid location.

This function calculates the value of a specific fluid variable at an arbitrary position within a 3D grid cell. It uses eight precomputed weights (coefficients) and the values at the eight surrounding grid nodes.

Note

The function assumes periodicity in the third dimension ( \(k\)). If the index \(k\) is at the grid boundary ( \(d\_N3 - 1\)), it wraps around to index 0.

Parameters:
  • var – Pointer to the flattened 3D array containing the fluid data.

  • mmenemonics – The index (mnemonic) of the specific scalar variable to interpolate.

  • i – Base index in the first dimension ( \(N_1\)).

  • j – Base index in the second dimension ( \(N_2\)).

  • k – Base index in the third dimension ( \(N_3\)).

  • coeff – An array of 8 weights representing the volumetric contribution of each corner.

Returns:

The interpolated scalar value at the target location.

__host__ __device__ inline int NPRIM_INDEX3D(int v, int i, int j, int k)

Maps a 4D index (Variable + 3D Space) to a 1D linear memory offset.

  • This macro implements a “Variable-Major” or “Structure of Arrays” (SoA) layout.

Memory Layout Order: [Variable \(v\)][Dimension \(i\)][Dimension \(j\)][Dimension \(k\)]

  • \(k\) is the fastest-varying index (unit stride).

  • \(v\) is the slowest-varying index.

    \( \text{Index} = v \times (N_1 \times N_2 \times N_3) + i \times (N_2 \times N_3) + j \times N_3 + k \)

Parameters:
  • v – The index of the physical variable (mnemonic).

  • i, j, k – The 3D spatial coordinates.

Returns:

The 1D linear index in the primitive variable array.

__host__ __device__ inline int SPATIAL_INDEX2D(int i, int j)

Maps 2D spatial coordinates (i, j) to a 1D linear memory offset.

This function performs a standard row-major (or height-major) flattening for 2D grids.

Memory Layout Order: [Dimension \(i\)][Dimension \(j\)]

\( \text{Index} = j + (N_2 \times i) \)

Parameters:
  • i – The radial spatial index.

  • j – The angular spatial index.

Returns:

The 1D linear integer offset for the given coordinates.

__host__ __device__ inline int SPATIAL_INDEX3D(int i, int j, int k)

Maps 3D spatial coordinates (i, j, k) to a 1D linear memory offset.

This function flattens a 3D volume into a 1D array.

Memory Layout Order: [Dimension \(i\)][Dimension \(j\)][Dimension \(k\)]

\( \text{Index} = k + N_3 \times (j + N_2 \times i) \)

Parameters:
  • i – The radial spatial index (typically the radial or X dimension).

  • j – The polar spatial index (typically the polar or Y dimension).

  • k – The azimuthal spatial index (typically the azimuthal or Z dimension).

Returns:

The 1D linear integer offset for the given 3D coordinates.