Skip to content

Commit

Permalink
start gpu
Browse files Browse the repository at this point in the history
  • Loading branch information
WeiqunZhang committed Sep 20, 2024
1 parent 6ddc592 commit 96e1d8b
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 4 deletions.
54 changes: 54 additions & 0 deletions Src/AMReX_Arena.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,70 @@
#include "AMReX_Arena.H"
#include "AMReX_Gpu.H"

namespace amrex
{

void* allocate_host (std::size_t sz)
{
#if defined(AMREX_USE_CUDA)
void* p;
cudaHostAlloc(&p, sz, cudaHostAllocMapped);
return p;
#elif defined(AMREX_USE_HIP)
void* p;
hipHostAlloc(&p, sz, hipHostAllocMapped | hipHostMallocNonCoherent);
return p;
#elif defined(AMREX_USE_SYCL)
return sycl::malloc_host(...);
#else
return std::malloc(sz);
#endif
}

void free_host (void* pt)
{
#if defined(AMREX_USE_CUDA)
cudaFreeHost(pt);
#elif defined(AMREX_USE_HIP)
hipHostFree(pt);
#elif defined(AMREX_USE_SYCL)
sycl::free(...);
#else
std::free(pt);
#endif
}

#ifdef AMREX_USE_GPU

void* allocate_device (std::size_t sz)
{
void* p;
#if defined(AMREX_USE_CUDA)
cudaMalloc(&p, sz);
#elif defined(AMREX_USE_HIP)
hipMalloc(&p, sz);
#elif defined(AMREX_USE_SYCL)
p = sycl::malloc_device(...);
#else
static_assert(false);
#endif
return p;
}

void free_device (void* pt)
{
#if defined(AMREX_USE_CUDA)
cudaFree(pt);
#elif defined(AMREX_USE_HIP)
hipFree(pt);
#elif defined(AMREX_USE_SYCL)
sycl::free(...);
#else
static_assert(false);
#endif

}

#endif

}
2 changes: 1 addition & 1 deletion Src/AMReX_BLassert.H
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void Assert (const char* EX, const char* file, int line, const char* msg)
{
AMREX_IF_ON_HOST((Assert_host(EX,file,line,msg);))
AMREX_IF_ON_DEVICE((amrex::ignore_unused(EX,file,line,msg);))
AMREX_IF_ON_DEVICE((assert(0)));
AMREX_IF_ON_DEVICE((assert(0);))
}

}
Expand Down
19 changes: 16 additions & 3 deletions Src/AMReX_Gpu.H
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#ifndef AMREX_GPU_H_
#define AMREX_GPU_H_

#include <cstdlib>

#if defined(AMREX_USE_GPU) && !defined(AMREX_USE_SYCL)

#if defined(AMREX_USE_HIP)
Expand Down Expand Up @@ -70,12 +73,22 @@
AMREX_WRONG_NUM_ARGS)(__VA_ARGS__)

#ifdef AMREX_USE_GPU
namespace amrex::Gpu {
namespace amrex {

#if defined(AMREX_USE_CUDA)
using gpuStream_t = cudaStream_t;
#elif defined(AMREX_USE_HIP)
using gpuStream_t = hipStream_t;
#elif defined(AMREX_USE_SYCL)
using gpuStream_t = sycl::queue*;
#endif

// add init(); // to get stream I am supposed to use
namespace Gpu {

void htod_memcpy (....); // todo: need to synchronize too, use default stream
void setStream (gpuStream_t a_stream);

void htod_memcpy (void* p_d, void const* p_h, std::size_t sz);
}
}
#endif

Expand Down
34 changes: 34 additions & 0 deletions Src/AMReX_Gpu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "AMReX_Gpu.H"

#ifdef AMREX_USE_GPU

namespace {
#ifdef AMREX_USE_SYCL
amrex::gpuStream_t gpu_stream = nullptr;
#else
amrex::gpuStream_t gpu_stream = 0;
#endif
}

namespace amrex::Gpu {

void setStream (gpuStream_t a_stream)
{
gpu_stream = a_stream;
}

void htod_memcpy (void* p_d, void const* p_h, std::size_t sz)
{
#if defined(AMREX_USE_CUDA)
cudaMemcpyAsync(p_d, p_h, sz, cudaMemcpyHostToDevice, gpu_stream);
cudaStreamSynchronize(gpu_stream);
#elif defined(AMREX_USE_HIP)
#elif defined(AMREX_USE_SYCL)
#else
static_assert(false);
#endif
}

}

#endif
1 change: 1 addition & 0 deletions Src/Make.package
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CEXE_headers += AMReX_Arena.H \

CEXE_sources += AMReX_Arena.cpp \
AMReX_BLassert.cpp \
AMReX_Gpu.cpp \
AMReX_Parser.cpp \
AMReX_Parser_Exe.cpp \
amrex_parser.lex.cpp \
Expand Down
16 changes: 16 additions & 0 deletions Tests/GPU/GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
AMREX_HOME := ../..

DEBUG = FALSE

DIM = 3

COMP = gcc

USE_CUDA = TRUE
USE_HIP = FALSE
USE_SYCL = FALSE

include $(AMREX_HOME)/Tools/GNUMake/Make.defs
include ./Make.package
include $(AMREX_HOME)/Src/Make.package
include $(AMREX_HOME)/Tools/GNUMake/Make.rules
1 change: 1 addition & 0 deletions Tests/GPU/Make.package
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CEXE_sources += main.cpp
10 changes: 10 additions & 0 deletions Tests/GPU/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "AMReX_Parser.H"
#include <iostream>

using namespace amrex;


int main (int argc, char* argv[])
{
amrex::ignore_unused(argc, argv);
}
3 changes: 3 additions & 0 deletions Tools/GNUMake/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,9 @@ else ifeq ($(USE_CUDA),TRUE)
SYSTEM_INCLUDE_LOCATIONS += $(MAKE_CUDA_PATH)/include
endif

LINKFLAGS = $(NVCC_FLAGS) $(NVCC_ARCH_LINK_FLAGS) $(CXXFLAGS_FROM_HOST)
AMREX_LINKER = nvcc

ifeq ($(nvcc_forward_unknowns),0)

comm := ,
Expand Down

0 comments on commit 96e1d8b

Please sign in to comment.