From 3c2eadfde21745eae87c7d13ebaacb1732a8619d Mon Sep 17 00:00:00 2001 From: WeiqunZhang Date: Tue, 1 Sep 2020 14:11:35 -0700 Subject: Remove ManagedVector from spectral solver (#1270) --- .../FieldSolver/SpectralSolver/SpectralKSpace.cpp | 147 ++++++++++++--------- 1 file changed, 81 insertions(+), 66 deletions(-) (limited to 'Source/FieldSolver/SpectralSolver/SpectralKSpace.cpp') diff --git a/Source/FieldSolver/SpectralSolver/SpectralKSpace.cpp b/Source/FieldSolver/SpectralSolver/SpectralKSpace.cpp index 2c60cdc38..dd12fb2b8 100644 --- a/Source/FieldSolver/SpectralSolver/SpectralKSpace.cpp +++ b/Source/FieldSolver/SpectralSolver/SpectralKSpace.cpp @@ -75,17 +75,18 @@ SpectralKSpace::getKComponent( const DistributionMapping& dm, const int i_dim, const bool only_positive_k ) const { - // Initialize an empty ManagedVector in each box + // Initialize an empty DeviceVector in each box KVectorComponent k_comp(spectralspace_ba, dm); - // Loop over boxes and allocate the corresponding ManagedVector + // Loop over boxes and allocate the corresponding DeviceVector // for each box owned by the local MPI proc for ( MFIter mfi(spectralspace_ba, dm); mfi.isValid(); ++mfi ){ Box bx = spectralspace_ba[mfi]; - ManagedVector& k = k_comp[mfi]; + DeviceVector& k = k_comp[mfi]; // Allocate k to the right size int N = bx.length( i_dim ); k.resize( N ); + Real* pk = k.data(); // Fill the k vector IntVect fft_size = realspace_ba[mfi].length(); @@ -97,21 +98,24 @@ SpectralKSpace::getKComponent( const DistributionMapping& dm, if (only_positive_k){ // Fill the full axis with positive k values // (typically: first axis, in a real-to-complex FFT) - for (int i=0; i& k = k_vec[i_dim][mfi]; - ManagedVector& shift = shift_factor[mfi]; + const DeviceVector& k = k_vec[i_dim][mfi]; + DeviceVector& shift = shift_factor[mfi]; // Allocate shift coefficients - shift.resize( k.size() ); + const int N = k.size(); + shift.resize(N); + Real const* pk = k.data(); + Complex* pshift = shift.data(); // Fill the shift coefficients Real sign = 0; @@ -149,11 +156,10 @@ SpectralKSpace::getSpectralShiftFactor( const DistributionMapping& dm, case ShiftType::TransformToCellCentered: sign = 1.; } const Complex I{0,1}; - int i = 0; - for (auto const& kv : k){ - shift[i] = exp( I*sign*kv*0.5_rt*dx[i_dim]); - i++; - } + amrex::ParallelFor(N, [=] AMREX_GPU_DEVICE (int i) noexcept + { + pshift[i] = amrex::exp( I*sign*pk[i]*0.5_rt*dx[i_dim]); + }); } return shift_factor; } @@ -177,72 +183,81 @@ SpectralKSpace::getModifiedKComponent( const DistributionMapping& dm, const int n_order, const bool nodal ) const { - // Initialize an empty ManagedVector in each box + // Initialize an empty DeviceVector in each box KVectorComponent modified_k_comp(spectralspace_ba, dm); if (n_order == -1) { // Infinite-order case for ( MFIter mfi(spectralspace_ba, dm); mfi.isValid(); ++mfi ){ - const ManagedVector& k = k_vec[i_dim][mfi]; - ManagedVector& modified_k = modified_k_comp[mfi]; + const DeviceVector& k = k_vec[i_dim][mfi]; + DeviceVector& modified_k = modified_k_comp[mfi]; // Allocate modified_k to the same size as k + const int N = k.size(); modified_k.resize( k.size() ); // Fill the modified k vector - int i = 0; - for (auto const& kv : k){ - modified_k[i] = k[i]; // infinite-order case. - i++; - } + Gpu::copyAsync(Gpu::deviceToDevice, k.begin(), k.end(), modified_k.begin()); } } else { // Compute real-space stencil coefficients - Vector stencil_coef = getFonbergStencilCoefficients(n_order, nodal); + Vector h_stencil_coef = getFonbergStencilCoefficients(n_order, nodal); + DeviceVector d_stencil_coef(h_stencil_coef.size()); + Gpu::copyAsync(Gpu::hostToDevice, h_stencil_coef.begin(), h_stencil_coef.end(), + d_stencil_coef.begin()); + Gpu::synchronize(); + const int nstencil = d_stencil_coef.size(); + Real const* p_stencil_coef = d_stencil_coef.data(); - // Loop over boxes and allocate the corresponding ManagedVector + // Loop over boxes and allocate the corresponding DeviceVector // for each box owned by the local MPI proc for ( MFIter mfi(spectralspace_ba, dm); mfi.isValid(); ++mfi ){ Real delta_x = dx[i_dim]; - const ManagedVector& k = k_vec[i_dim][mfi]; - ManagedVector& modified_k = modified_k_comp[mfi]; + const DeviceVector& k = k_vec[i_dim][mfi]; + DeviceVector& modified_k = modified_k_comp[mfi]; // Allocate modified_k to the same size as k - modified_k.resize( k.size() ); + const int N = k.size(); + modified_k.resize(N); + Real const* p_k = k.data(); + Real * p_modified_k = modified_k.data(); // Fill the modified k vector - int i = 0; - for (auto const& kv : k){ - modified_k[i] = 0; - for (int n=1; n