Skip to content

Commit

Permalink
Add Tests/OpenMP/atomicAdd (AMReX-Codes#3805)
Browse files Browse the repository at this point in the history
Co-authored-by: Axel Huebl <[email protected]>
  • Loading branch information
WeiqunZhang and ax3l authored Mar 15, 2024
1 parent bde2963 commit 3fe7aad
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ else()
list(APPEND AMREX_TESTS_SUBDIRS GPU)
endif ()

if (AMReX_GPU_BACKEND STREQUAL NONE)
list(APPEND AMREX_TESTS_SUBDIRS OpenMP)
endif ()

list(TRANSFORM AMREX_TESTS_SUBDIRS PREPEND "${CMAKE_CURRENT_LIST_DIR}/")

#
Expand Down
13 changes: 13 additions & 0 deletions Tests/OpenMP/atomicAdd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
if (NOT AMReX_GPU_BACKEND STREQUAL NONE)
return()
endif ()

foreach(D IN LISTS AMReX_SPACEDIM)
set(_sources main.cpp)
set(_input_files)

setup_test(${D} _sources _input_files)

unset(_sources)
unset(_input_files)
endforeach()
15 changes: 15 additions & 0 deletions Tests/OpenMP/atomicAdd/GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
AMREX_HOME = ../../..

DEBUG = FALSE
DIM = 3
COMP = gcc

USE_MPI = FALSE
USE_OMP = 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
1 change: 1 addition & 0 deletions Tests/OpenMP/atomicAdd/Make.package
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CEXE_sources += main.cpp
67 changes: 67 additions & 0 deletions Tests/OpenMP/atomicAdd/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <AMReX.H>
#include <AMReX_MultiFab.H>
#include <AMReX_OpenMP.H>
#include <AMReX_Print.H>

using namespace amrex;

void test_atomicAdd (MultiFab& mf)
{

#ifdef AMREX_USE_OMP
#pragma omp parallel
#endif
{
FArrayBox tmp;
for (MFIter mfi(mf,true); mfi.isValid(); ++mfi) {
Box const& tbx = mfi.growntilebox();
tmp.resize(tbx);
tmp.template setVal<RunOn::Host>(0.2);
mf[mfi].template atomicAdd<RunOn::Host>(tmp, tbx, tbx, 0, 0, 1);
}
}
}

void test_lockAdd (MultiFab& mf)
{

#ifdef AMREX_USE_OMP
#pragma omp parallel
#endif
{
FArrayBox tmp;
for (MFIter mfi(mf,true); mfi.isValid(); ++mfi) {
Box const& tbx = mfi.growntilebox();
tmp.resize(tbx);
tmp.template setVal<RunOn::Host>(0.2);
mf[mfi].template lockAdd<RunOn::Host>(tmp, tbx, tbx, 0, 0, 1);
}
}
}

int main (int argc, char* argv[])
{
amrex::Initialize(argc, argv);
{
BoxArray ba(Box(IntVect(0),IntVect(127)));
ba.maxSize(32);
DistributionMapping dm(ba);

MultiFab mf(ba,dm,1,2);
mf.setVal(0.0);

test_atomicAdd(mf);
double t = amrex::second();
test_atomicAdd(mf);
double t_aa = amrex::second() - t;

test_lockAdd(mf);
t = amrex::second();
test_lockAdd(mf);
double t_la = amrex::second() - t;

amrex::Print() << " atomicAdd time is " << t_aa << "\n"
<< " lockAdd time is " << t_la << "\n";
}
amrex::Finalize();
}

0 comments on commit 3fe7aad

Please sign in to comment.