Skip to content

Commit

Permalink
add ports-of-call
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahm-LANL committed Feb 18, 2021
1 parent 4faf017 commit 57b25ec
Show file tree
Hide file tree
Showing 3 changed files with 680 additions and 0 deletions.
72 changes: 72 additions & 0 deletions ports-of-call/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Ports of call

Contains portability utilities used in Spiner. This functionality is
header-only.

We define a few portability macros which are useful:

1. `PORTABLE_FUNCTION`: decorators necessary for compiling a kernel function
2. `PORTABLE_INLINE_FUNCTION`: ditto, but for when functions ought to be inlined
3. `_WITH_KOKKOS_`: Defined if Kokkos is enabled.
4. `_WITH_CUDA_`: Defined when Cuda is enabled
5. `Real`: a typedef to double (default) or float (if you define
`SINGLE_PRECISION_ENABLED`)
6. `PORTABLE_MALLOC()`, `PORTABLE_FREE()`: A macro or wrapper for
kokkos_malloc or cudaMalloc, or raw malloc.

At compile time, you define `PORTABILITY_STRATEGY_{KOKKOS,CUDA,NONE}` (if you don't
define it, it defaults to NONE).

There are to be two headers in this folder, for different use cases.

### portability.hpp

- Provides certain macros for decorating functions, lambdas, etc. Also
provides device malloc/device free operations. This is the principle
path for turning on/off Kokkos.
- Provides loop abstractions that can be leveraged by the code.

### portable_array.hpp:

Provides an implementation of a multidimensional array with round
parentheses access. Could be a Kokkos view or something that is not
portable

## Copyright

© (or copyright) 2019. Triad National Security, LLC. All rights
reserved. This program was produced under U.S. Government contract
89233218CNA000001 for Los Alamos National Laboratory (LANL), which is
operated by Triad National Security, LLC for the U.S. Department of
Energy/National Nuclear Security Administration. All rights in the
program are reserved by Triad National Security, LLC, and the
U.S. Department of Energy/National Nuclear Security
Administration. The Government is granted for itself and others acting
on its behalf a nonexclusive, paid-up, irrevocable worldwide license
in this material to reproduce, prepare derivative works, distribute
copies to the public, perform publicly and display publicly, and to
permit others to do so.

This program is open source under the BSD-3 License. Redistribution
and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
137 changes: 137 additions & 0 deletions ports-of-call/portability.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#ifndef _PORTABILITY_HPP_
#define _PORTABILITY_HPP_

#include <string>

#ifdef PORTABILITY_STRATEGY_KOKKOS
#include "Kokkos_Core.hpp"
#define PORTABLE_FUNCTION KOKKOS_FUNCTION
#define PORTABLE_INLINE_FUNCTION KOKKOS_INLINE_FUNCTION
#define PORTABLE_LAMBDA KOKKOS_LAMBDA
#define _WITH_KOKKOS_
// The following is a malloc on default memory space
#define PORTABLE_MALLOC(size) Kokkos::kokkos_malloc<>(size)
#define PORTABLE_FREE(ptr) Kokkos::kokkos_free<>(ptr)
// Do we want to include additional terms here (for memory spaces, etc.)?
#else
#ifdef PORTABILITY_STRATEGY_CUDA
#include "cuda.h"
#define PORTABLE_FUNCTION __host__ __device__
#define PORTABLE_INLINE_FUNCTION __host__ __device__ inline
#define PORTABLE_LAMBDA [=] __host__ __device__
void * PORTABLE_MALLOC(size_t size){
void * devPtr
cudaError_t e = cudaMalloc(devPtr, size);
return devPtr;
}
void PORTABLE_FREE(void * ptr){
cudaError_t e = cudaFree(ptr);
}
#define _WITH_CUDA_
// It is worth noting here that we will not define
// _WITH_CUDA_ when we are doing KOKKOS (even with the
// CUDA backend) Rely on KOKKOS_HAVE_CUDA in that case
#else
#define PORTABLE_FUNCTION
#define PORTABLE_INLINE_FUNCTION inline
#define PORTABLE_LAMBDA [=]
#define PORTABLE_MALLOC(size) malloc(size)
#define PORTABLE_FREE(ptr) free(ptr)
#endif
#endif

#ifndef SINGLE_PRECISION_ENABLED
#define SINGLE_PRECISION_ENABLED 0
#endif

#if SINGLE_PRECISION_ENABLED
typedef float Real;
#else
typedef double Real;
#endif

template<typename Function>
void portableFor(const char* name,
int start, int stop,
Function function) {
#ifdef PORTABILITY_STRATEGY_KOKKOS
using policy = Kokkos::RangePolicy<>;
Kokkos::parallel_for(name,
policy(start,stop),
function);
#else
for (int i = start; i < stop; i++) {
function(i);
}
#endif
}

template <typename Function>
void portableFor(const char* name,
int starty, int stopy,
int startx, int stopx,
Function function) {
#ifdef PORTABILITY_STRATEGY_KOKKOS
using Policy2D = Kokkos::MDRangePolicy<Kokkos::Rank<2>>;
Kokkos::parallel_for(name,
Policy2D({starty,startx},
{stopy,stopx}),
function);
#else
for (int iy = starty; iy < stopy; iy++) {
for (int ix = startx; ix < stopx; ix++) {
function(iy,ix);
}
}
#endif
}

template <typename Function>
void portableFor(const char* name,
int startz, int stopz,
int starty, int stopy,
int startx, int stopx,
Function function) {
#ifdef PORTABILITY_STRATEGY_KOKKOS
using Policy3D = Kokkos::MDRangePolicy<Kokkos::Rank<3>>;
Kokkos::parallel_for(name,
Policy3D({startz,starty,startx},
{stopz,stopy,stopx}),
function);
#else
for (int iz = startz; iz < stopz; iz++) {
for (int iy = starty; iy < stopy; iy++) {
for (int ix = startx; ix < stopx; ix++) {
function(iz,iy,ix);
}
}
}
#endif
}

template <typename Function, typename T>
void portableReduce(const char* name,
int startz, int stopz,
int starty, int stopy,
int startx, int stopx,
Function function,
T& reduced) {
#ifdef PORTABILITY_STRATEGY_KOKKOS
using Policy3D = Kokkos::MDRangePolicy<Kokkos::Rank<3>>;
Kokkos::parallel_reduce(name,
Policy3D({startz,starty,startx},
{stopz,stopy,stopx}),
function,
reduced);
#else
for (int iz = startz; iz < stopz; iz++) {
for (int iy = starty; iy < stopy; iy++) {
for (int ix = startx; ix < stopx; ix++) {
function(iz,iy,ix, reduced);
}
}
}
#endif
}

#endif // PORTABILITY_HPP
Loading

0 comments on commit 57b25ec

Please sign in to comment.