forked from AMReX-Codes/amrex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GPU Device Variable on Intel GPUs (AMReX-Codes#4056)
This adds GPU device variable support on Intel GPUs using Intel oneAPI compiler's experimental feature. To make the user interface consistent, we have add a macro AMREX_DEVICE_GLOBAL_VARIABLE. For example, the user can define a device variable as follows for all GPUs and CPUs. AMREX_DEVICE_GLOBAL_VARIABLE(amrex::Real, my_dg1); // amrex::Real my_dg1; AMREX_DEVICE_GLOBAL_VARIABLE(amrex::Real, 4, my_dg2); // amrex::Real my_dg2[4]; Below are their declarations. extern AMREX_DEVICE_GLOBAL_VARIABLE(amrex::Real, my_dg1); extern AMREX_DEVICE_GLOBAL_VARIABLE(amrex::Real, 4, my_dg2); GPU and CPU kernels can use the global variables if they see the declarations. We have also added two functions from copying data from and to device global variables. //! Copy `nbytes` bytes from host to device global variable. `offset` is the //! offset in bytes from the start of the device global variable. template <typename T> void memcpy_from_host_to_device_global_async (T& dg, const void* src, std::size_t nbytes, std::size_t offset = 0) //! Copy `nbytes` bytes from device global variable to host. `offset` is the //! offset in bytes from the start of the device global variable. template <typename T> void memcpy_from_device_global_to_host_async (void* dst, T const& dg, std::size_t nbytes, std::size_t offset = 0)
- Loading branch information
1 parent
e792933
commit 5dedac0
Showing
14 changed files
with
229 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
if (( (AMReX_GPU_BACKEND STREQUAL "CUDA") OR | ||
(AMReX_GPU_BACKEND STREQUAL "HIP" ) ) AND | ||
(NOT AMReX_GPU_RDC)) | ||
return() | ||
endif () | ||
|
||
foreach(D IN LISTS AMReX_SPACEDIM) | ||
set(_sources main.cpp global_vars.cpp init.cpp work.cpp) | ||
set(_input_files) | ||
|
||
setup_test(${D} _sources _input_files) | ||
|
||
unset(_sources) | ||
unset(_input_files) | ||
endforeach() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
AMREX_HOME ?= ../../ | ||
|
||
DEBUG = FALSE | ||
|
||
DIM = 3 | ||
|
||
COMP = gcc | ||
|
||
USE_CUDA = TRUE | ||
USE_HIP = FALSE | ||
USE_SYCL = FALSE | ||
|
||
USE_MPI = FALSE | ||
USE_OMP = FALSE | ||
|
||
BL_NO_FORT = TRUE | ||
|
||
include $(AMREX_HOME)/Tools/GNUMake/Make.defs | ||
|
||
include ./Make.package | ||
include $(AMREX_HOME)/Src/Base/Make.package | ||
|
||
include $(AMREX_HOME)/Tools/GNUMake/Make.rules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
CEXE_sources += main.cpp init.cpp work.cpp global_vars.cpp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
#include <AMReX_Gpu.H> | ||
#include <AMReX_INT.H> | ||
|
||
extern AMREX_DEVICE_GLOBAL_VARIABLE(amrex::Long, dg_x); | ||
extern AMREX_DEVICE_GLOBAL_VARIABLE(amrex::Long, 4, dg_y); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
#include "global_vars.H" | ||
|
||
// definitions of global variables | ||
|
||
AMREX_DEVICE_GLOBAL_VARIABLE(amrex::Long, dg_x); | ||
AMREX_DEVICE_GLOBAL_VARIABLE(amrex::Long, 4, dg_y); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
#include "global_vars.H" | ||
|
||
void init () | ||
{ | ||
amrex::ParallelFor(1, [=] AMREX_GPU_DEVICE (int) | ||
{ | ||
dg_x = 1; | ||
for (int n = 0; n < 4; ++n) { | ||
dg_y[n] = 100 + n; | ||
} | ||
}); | ||
|
||
amrex::Gpu::streamSynchronize(); | ||
} | ||
|
||
void init2 () | ||
{ | ||
amrex::Gpu::PinnedVector<amrex::Long> pv{2,200,201,202,203}; | ||
amrex::Gpu::memcpy_from_host_to_device_global_async | ||
(dg_x, pv.data(), sizeof(amrex::Long)); | ||
amrex::Gpu::memcpy_from_host_to_device_global_async | ||
(dg_y, pv.data()+1, sizeof(amrex::Long)); | ||
amrex::Gpu::memcpy_from_host_to_device_global_async | ||
(dg_y, pv.data()+2, sizeof(amrex::Long)*3, sizeof(amrex::Long)); | ||
amrex::Gpu::streamSynchronize(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <AMReX.H> | ||
#include <AMReX_Print.H> | ||
|
||
void init(); | ||
void work(); | ||
void init2(); | ||
void work2(); | ||
|
||
int main (int argc, char* argv[]) | ||
{ | ||
amrex::Initialize(argc,argv); | ||
{ | ||
init(); | ||
work(); | ||
|
||
init2(); | ||
work2(); | ||
} | ||
amrex::Finalize(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
#include "global_vars.H" | ||
|
||
void work () | ||
{ | ||
amrex::Gpu::PinnedVector<amrex::Long> pv; | ||
pv.resize(5,0); | ||
auto* p = pv.data(); | ||
amrex::ParallelFor(1, [=] AMREX_GPU_DEVICE (int) | ||
{ | ||
p[0] = dg_x; | ||
for (int n = 0; n < 4; ++n) { | ||
p[1+n] = dg_y[n]; | ||
} | ||
}); | ||
amrex::Gpu::streamSynchronize(); | ||
AMREX_ALWAYS_ASSERT(pv[0] == 1 && | ||
pv[1] == 100 && | ||
pv[2] == 101 && | ||
pv[3] == 102 && | ||
pv[4] == 103); | ||
} | ||
|
||
void work2 () | ||
{ | ||
amrex::Gpu::PinnedVector<amrex::Long> pv; | ||
pv.resize(5,0); | ||
amrex::Gpu::memcpy_from_device_global_to_host_async | ||
(pv.data(), dg_x, sizeof(amrex::Long)); | ||
amrex::Gpu::memcpy_from_device_global_to_host_async | ||
(pv.data()+1, dg_y, sizeof(amrex::Long)); | ||
amrex::Gpu::memcpy_from_device_global_to_host_async | ||
(pv.data()+2, dg_y, sizeof(amrex::Long)*3, sizeof(amrex::Long)); | ||
amrex::Gpu::streamSynchronize(); | ||
AMREX_ALWAYS_ASSERT(pv[0] == 2 && | ||
pv[1] == 200 && | ||
pv[2] == 201 && | ||
pv[3] == 202 && | ||
pv[4] == 203); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters