Skip to content

Commit

Permalink
Minor refactoring in muscl.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkestene committed Apr 28, 2024
1 parent 386b693 commit d994069
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 591 deletions.
229 changes: 68 additions & 161 deletions src/muscl/HydroRunFunctors2D.h

Large diffs are not rendered by default.

222 changes: 69 additions & 153 deletions src/muscl/HydroRunFunctors3D.h

Large diffs are not rendered by default.

147 changes: 43 additions & 104 deletions src/muscl/MHDRunFunctors2D.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#ifndef MHD_RUN_FUNCTORS_2D_H_
#define MHD_RUN_FUNCTORS_2D_H_

#include <limits> // for std::numeric_limits
#ifdef __CUDA_ARCH__
# include <math_constants.h>
#endif // __CUDA_ARCH__

#include "shared/kokkos_shared.h"
#include "MHDBaseFunctor2D.h"
#include "shared/RiemannSolvers_MHD.h"
Expand All @@ -32,42 +27,29 @@ class ComputeDtFunctor2D_MHD : public MHDBaseFunctor2D

// static method which does it all: create and execute functor
static void
apply(HydroParams params, DataArray2d Udata, int nbCells, real_t & invDt)
apply(HydroParams params, DataArray2d Udata, real_t & invDt)
{
ComputeDtFunctor2D_MHD functor(params, Udata);
Kokkos::parallel_reduce(nbCells, functor, invDt);
Kokkos::Max<real_t> reducer(invDt);
Kokkos::parallel_reduce(
"ComputeDtFunctor2D_MHD",
Kokkos::MDRangePolicy<Kokkos::Rank<2>>({ 0, 0 }, { params.isize, params.jsize }),
functor,
reducer);
}

// Tell each thread how to initialize its reduction result.
KOKKOS_INLINE_FUNCTION
void
init(real_t & dst) const
{
// The identity under max is -Inf.
// Kokkos does not come with a portable way to access
// floating-point Inf and NaN.
#ifdef __CUDA_ARCH__
dst = -CUDART_INF;
#else
dst = std::numeric_limits<real_t>::min();
#endif // __CUDA_ARCH__
} // init

/* this is a reduce (max) functor */
KOKKOS_INLINE_FUNCTION
void
operator()(const int & index, real_t & invDt) const
operator()(const int & i, const int & j, real_t & invDt) const
{
const int isize = params.isize;
const int jsize = params.jsize;
const int ghostWidth = params.ghostWidth;
const real_t dx = params.dx;
const real_t dy = params.dy;

int i, j;
index2coord(index, i, j, isize, jsize);

if (j >= ghostWidth && j < jsize - ghostWidth && i >= ghostWidth && i < isize - ghostWidth)
if (j >= ghostWidth and j < jsize - ghostWidth and i >= ghostWidth and i < isize - ghostWidth)
{

MHDState qLoc; // primitive variables in current cell
Expand All @@ -94,27 +76,6 @@ class ComputeDtFunctor2D_MHD : public MHDBaseFunctor2D

} // operator ()


// "Join" intermediate results from different threads.
// This should normally implement the same reduction
// operation as operator() above. Note that both input
// arguments MUST be declared volatile.
KOKKOS_INLINE_FUNCTION
#if KOKKOS_VERSION_MAJOR > 3
void
join(real_t & dst, const real_t & src) const
#else
void
join(volatile real_t & dst, const volatile real_t & src) const
#endif
{
// max reduce
if (dst < src)
{
dst = src;
}
} // join

DataArray2d Qdata;

}; // ComputeDtFunctor2D_MHD
Expand All @@ -133,27 +94,27 @@ class ConvertToPrimitivesFunctor2D_MHD : public MHDBaseFunctor2D

// static method which does it all: create and execute functor
static void
apply(HydroParams params, DataArray2d Udata, DataArray2d Qdata, int nbCells)
apply(HydroParams params, DataArray2d Udata, DataArray2d Qdata)
{
ConvertToPrimitivesFunctor2D_MHD functor(params, Udata, Qdata);
Kokkos::parallel_for(nbCells, functor);
Kokkos::parallel_for(
"ConvertToPrimitivesFunctor2D_MHD",
Kokkos::MDRangePolicy<Kokkos::Rank<2>>({ 0, 0 }, { params.isize, params.jsize }),
functor);
}

KOKKOS_INLINE_FUNCTION
void
operator()(const int & index) const
operator()(const int & i, const int & j) const
{
const int isize = params.isize;
const int jsize = params.jsize;
// const int ghostWidth = params.ghostWidth;

int i, j;
index2coord(index, i, j, isize, jsize);

// magnetic field in neighbor cells
real_t magFieldNeighbors[3];

if (j >= 0 && j < jsize - 1 && i >= 0 && i < isize - 1)
if (j >= 0 and j < jsize - 1 and i >= 0 and i < isize - 1)
{

MHDState uLoc; // conservative variables in current cell
Expand Down Expand Up @@ -231,26 +192,23 @@ class ComputeFluxesAndStoreFunctor2D_MHD : public MHDBaseFunctor2D
DataArray2d Flux_x,
DataArray2d Flux_y,
real_t dtdx,
real_t dtdy,
int nbCells)
real_t dtdy)
{
ComputeFluxesAndStoreFunctor2D_MHD functor(
params, Qm_x, Qm_y, Qp_x, Qp_y, Flux_x, Flux_y, dtdx, dtdy);
Kokkos::parallel_for(nbCells, functor);
Kokkos::parallel_for(
Kokkos::MDRangePolicy<Kokkos::Rank<2>>({ 0, 0 }, { params.isize, params.jsize }), functor);
}

KOKKOS_INLINE_FUNCTION
void
operator()(const int & index) const
operator()(const int & i, const int & j) const
{
const int isize = params.isize;
const int jsize = params.jsize;
const int ghostWidth = params.ghostWidth;

int i, j;
index2coord(index, i, j, isize, jsize);

if (j >= ghostWidth && j < jsize - ghostWidth + 1 && i >= ghostWidth &&
if (j >= ghostWidth and j < jsize - ghostWidth + 1 and i >= ghostWidth and
i < isize - ghostWidth + 1)
{

Expand Down Expand Up @@ -327,26 +285,23 @@ class ComputeEmfAndStoreFunctor2D : public MHDBaseFunctor2D
DataArray2d QEdge_LB,
DataArrayScalar Emf,
real_t dtdx,
real_t dtdy,
int nbCells)
real_t dtdy)
{
ComputeEmfAndStoreFunctor2D functor(
params, QEdge_RT, QEdge_RB, QEdge_LT, QEdge_LB, Emf, dtdx, dtdy);
Kokkos::parallel_for(nbCells, functor);
Kokkos::parallel_for(
Kokkos::MDRangePolicy<Kokkos::Rank<2>>({ 0, 0 }, { params.isize, params.jsize }), functor);
}

KOKKOS_INLINE_FUNCTION
void
operator()(const int & index) const
operator()(const int & i, const int & j) const
{
const int isize = params.isize;
const int jsize = params.jsize;
const int ghostWidth = params.ghostWidth;

int i, j;
index2coord(index, i, j, isize, jsize);

if (j >= ghostWidth && j < jsize - ghostWidth + 1 && i >= ghostWidth &&
if (j >= ghostWidth and j < jsize - ghostWidth + 1 and i >= ghostWidth and
i < isize - ghostWidth + 1)
{

Expand Down Expand Up @@ -424,8 +379,7 @@ class ComputeTraceFunctor2D_MHD : public MHDBaseFunctor2D
DataArray2d QEdge_LT,
DataArray2d QEdge_LB,
real_t dtdx,
real_t dtdy,
int nbCells)
real_t dtdy)
{
ComputeTraceFunctor2D_MHD functor(params,
Udata,
Expand All @@ -440,21 +394,19 @@ class ComputeTraceFunctor2D_MHD : public MHDBaseFunctor2D
QEdge_LB,
dtdx,
dtdy);
Kokkos::parallel_for(nbCells, functor);
Kokkos::parallel_for(
Kokkos::MDRangePolicy<Kokkos::Rank<2>>({ 0, 0 }, { params.isize, params.jsize }), functor);
}

KOKKOS_INLINE_FUNCTION
void
operator()(const int & index) const
operator()(const int & i, const int & j) const
{
const int isize = params.isize;
const int jsize = params.jsize;
const int ghostWidth = params.ghostWidth;

int i, j;
index2coord(index, i, j, isize, jsize);

if (j >= ghostWidth - 2 && j < jsize - ghostWidth + 1 && i >= ghostWidth - 2 &&
if (j >= ghostWidth - 2 and j < jsize - ghostWidth + 1 and i >= ghostWidth - 2 and
i < isize - ghostWidth + 1)
{

Expand Down Expand Up @@ -537,25 +489,22 @@ class UpdateFunctor2D_MHD : public MHDBaseFunctor2D
DataArray2d FluxData_x,
DataArray2d FluxData_y,
real_t dtdx,
real_t dtdy,
int nbCells)
real_t dtdy)
{
UpdateFunctor2D_MHD functor(params, Udata, FluxData_x, FluxData_y, dtdx, dtdy);
Kokkos::parallel_for(nbCells, functor);
Kokkos::parallel_for(
Kokkos::MDRangePolicy<Kokkos::Rank<2>>({ 0, 0 }, { params.isize, params.jsize }), functor);
}

KOKKOS_INLINE_FUNCTION
void
operator()(const int & index) const
operator()(const int & i, const int & j) const
{
const int isize = params.isize;
const int jsize = params.jsize;
const int ghostWidth = params.ghostWidth;

int i, j;
index2coord(index, i, j, isize, jsize);

if (j >= ghostWidth && j < jsize - ghostWidth && i >= ghostWidth && i < isize - ghostWidth)
if (j >= ghostWidth and j < jsize - ghostWidth and i >= ghostWidth and i < isize - ghostWidth)
{

MHDState udata;
Expand Down Expand Up @@ -638,29 +587,22 @@ class UpdateEmfFunctor2D : public MHDBaseFunctor2D

// static method which does it all: create and execute functor
static void
apply(HydroParams params,
DataArray2d Udata,
DataArrayScalar Emf,
real_t dtdx,
real_t dtdy,
int nbCells)
apply(HydroParams params, DataArray2d Udata, DataArrayScalar Emf, real_t dtdx, real_t dtdy)
{
UpdateEmfFunctor2D functor(params, Udata, Emf, dtdx, dtdy);
Kokkos::parallel_for(nbCells, functor);
Kokkos::parallel_for(
Kokkos::MDRangePolicy<Kokkos::Rank<2>>({ 0, 0 }, { params.isize, params.jsize }), functor);
}

KOKKOS_INLINE_FUNCTION
void
operator()(const int & index) const
operator()(const int & i, const int & j) const
{
const int isize = params.isize;
const int jsize = params.jsize;
const int ghostWidth = params.ghostWidth;

int i, j;
index2coord(index, i, j, isize, jsize);

if (j >= ghostWidth && j < jsize - ghostWidth /*+1*/ && i >= ghostWidth &&
if (j >= ghostWidth and j < jsize - ghostWidth /*+1*/ and i >= ghostWidth and
i < isize - ghostWidth /*+1*/)
{

Expand Down Expand Up @@ -705,16 +647,13 @@ class ComputeTraceAndFluxes_Functor2D_MHD : public MHDBaseFunctor2D

KOKKOS_INLINE_FUNCTION
void
operator()(const int & index) const
operator()(const int & i, const int & j) const
{
const int isize = params.isize;
const int jsize = params.jsize;
const int ghostWidth = params.ghostWidth;

int i, j;
index2coord(index, i, j, isize, jsize);

if (j >= ghostWidth && j <= jsize - ghostWidth && i >= ghostWidth && i <= isize - ghostWidth)
if (j >= ghostWidth and j <= jsize - ghostWidth and i >= ghostWidth and i <= isize - ghostWidth)
{

// local primitive variables
Expand Down
Loading

0 comments on commit d994069

Please sign in to comment.