Skip to content

Commit

Permalink
parallel for outside for cuda
Browse files Browse the repository at this point in the history
  • Loading branch information
RevathiJambunathan committed Apr 22, 2024
1 parent 505546e commit 8c137ca
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 59 deletions.
4 changes: 4 additions & 0 deletions Source/WarpX.H
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,10 @@ public:
// for cuda
void BuildBufferMasksInBox ( amrex::Box tbx, amrex::IArrayBox &buffer_mask,
const amrex::IArrayBox &guard_mask, int ng );

void SetWeightsInGatherBuffer(const amrex::Box tbx, amrex::Array4<amrex::Real> wtmsk,
const amrex::Array4<int const> gmsk, const amrex::Array4<int const> bmsk,
const int ngbuffer, const bool do_interpolate,amrex::Real tanh_midpoint);
#ifdef AMREX_USE_EB
amrex::EBFArrayBoxFactory const& fieldEBFactory (int lev) const noexcept {
return static_cast<amrex::EBFArrayBoxFactory const&>(*m_field_factory[lev]);
Expand Down
150 changes: 91 additions & 59 deletions Source/WarpX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3149,71 +3149,103 @@ WarpX::BuildBufferMasks ()
auto const& gmsk = tmp[mfi].const_array();
auto const& bmsk = (*bmasks)[mfi].array();
auto const& wtmsk = (*weight_gbuffer)[mfi].array();
amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) {
wtmsk(i,j,k) = 0._rt;
if (bmsk(i,j,k) == 0 && do_interpolate) {
if(gmsk(i,j,k)==0) {
wtmsk(i,j,k) = 0.;
return;
}
for (int ii = i-1; ii >= i-ngbuffer; --ii) {
if (gmsk(ii,j,k)==0) {
amrex::Real arg = (static_cast<amrex::Real>(i-ii)-ngbuffer*tanh_midpoint)
/ ((1.-tanh_midpoint)*(ngbuffer/3.));
wtmsk(i,j,k) = std::tanh(arg)*0.5 + 0.5;
amrex::Print() << " i edge wt is " << wtmsk(i,j,k) << "\n";
return;
}
}
for (int ii = i+1; ii <= i+ngbuffer; ++ii) {
if (gmsk(ii,j,k)==0) {
amrex::Real arg = (static_cast<amrex::Real>(ii-i)-ngbuffer*tanh_midpoint)
/ ((1.-tanh_midpoint)*(ngbuffer/3.));
wtmsk(i,j,k) = std::tanh(arg)*0.5+0.5;
amrex::Print() << " wt is " << wtmsk(i,j,k) << "\n";
return;
}
}
//for (int jj = j-1; jj >= j-ngbuffer; --jj) {
// if (gmsk(i,jj,k)==0) {
// amrex::Real arg = (static_cast<amrex::Real>(j-jj)-ngbuffer*tanh_midpoint)
// / ((1.-tanh_midpoint)*(ngbuffer/3.));
// wtmsk(i,j,k) = std::tanh(arg)*0.5 + 0.5;
// return;
// }
//}
//for (int jj = j+1; jj <= j+ngbuffer; ++jj) {
// if (gmsk(i,jj,k)==0) {
// amrex::Real arg = (static_cast<amrex::Real>(jj - j)-ngbuffer*tanh_midpoint)
// / ((1.-tanh_midpoint)*(ngbuffer/3.));
// wtmsk(i,j,k) = std::tanh(arg)*0.5+0.5;
// return;
// }
//}
//for (int kk = k-1; kk >= k-ngbuffer; --kk) {
// if (gmsk(i,j,kk)==0) {
// amrex::Real arg = (static_cast<amrex::Real>(k-kk)-ngbuffer*tanh_midpoint)
// / ((1.-tanh_midpoint)*(ngbuffer/3.));
// wtmsk(i,j,k) = std::tanh(arg)*0.5+0.5;
// return;
// }
//}
//for (int kk = k+1; kk <= k+ngbuffer; ++kk) {
// if (gmsk(i,j,kk)==0) {
// amrex::Real arg = (static_cast<amrex::Real>(kk-k)-ngbuffer*tanh_midpoint)
// / ((1.-tanh_midpoint)*(ngbuffer/3.));
// wtmsk(i,j,k) = std::tanh(arg)*0.5+0.5;
// return;
// }
//}
}
});
SetWeightsInGatherBuffer(tbx, wtmsk, gmsk, bmsk, ngbuffer, do_interpolate, tanh_midpoint);
//amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) {
// wtmsk(i,j,k) = 0._rt;
// if (bmsk(i,j,k) == 0 && do_interpolate) {
// if(gmsk(i,j,k)==0) {
// wtmsk(i,j,k) = 0.;
// return;
// }
// for (int ii = i-1; ii >= i-ngbuffer; --ii) {
// if (gmsk(ii,j,k)==0) {
// amrex::Real arg = (static_cast<amrex::Real>(i-ii)-ngbuffer*tanh_midpoint)
// / ((1.-tanh_midpoint)*(ngbuffer/3.));
// wtmsk(i,j,k) = std::tanh(arg)*0.5 + 0.5;
// amrex::Print() << " i edge wt is " << wtmsk(i,j,k) << "\n";
// return;
// }
// }
// for (int ii = i+1; ii <= i+ngbuffer; ++ii) {
// if (gmsk(ii,j,k)==0) {
// amrex::Real arg = (static_cast<amrex::Real>(ii-i)-ngbuffer*tanh_midpoint)
// / ((1.-tanh_midpoint)*(ngbuffer/3.));
// wtmsk(i,j,k) = std::tanh(arg)*0.5+0.5;
// amrex::Print() << " wt is " << wtmsk(i,j,k) << "\n";
// return;
// }
// }
// //for (int jj = j-1; jj >= j-ngbuffer; --jj) {
// // if (gmsk(i,jj,k)==0) {
// // amrex::Real arg = (static_cast<amrex::Real>(j-jj)-ngbuffer*tanh_midpoint)
// // / ((1.-tanh_midpoint)*(ngbuffer/3.));
// // wtmsk(i,j,k) = std::tanh(arg)*0.5 + 0.5;
// // return;
// // }
// //}
// //for (int jj = j+1; jj <= j+ngbuffer; ++jj) {
// // if (gmsk(i,jj,k)==0) {
// // amrex::Real arg = (static_cast<amrex::Real>(jj - j)-ngbuffer*tanh_midpoint)
// // / ((1.-tanh_midpoint)*(ngbuffer/3.));
// // wtmsk(i,j,k) = std::tanh(arg)*0.5+0.5;
// // return;
// // }
// //}
// //for (int kk = k-1; kk >= k-ngbuffer; --kk) {
// // if (gmsk(i,j,kk)==0) {
// // amrex::Real arg = (static_cast<amrex::Real>(k-kk)-ngbuffer*tanh_midpoint)
// // / ((1.-tanh_midpoint)*(ngbuffer/3.));
// // wtmsk(i,j,k) = std::tanh(arg)*0.5+0.5;
// // return;
// // }
// //}
// //for (int kk = k+1; kk <= k+ngbuffer; ++kk) {
// // if (gmsk(i,j,kk)==0) {
// // amrex::Real arg = (static_cast<amrex::Real>(kk-k)-ngbuffer*tanh_midpoint)
// // / ((1.-tanh_midpoint)*(ngbuffer/3.));
// // wtmsk(i,j,k) = std::tanh(arg)*0.5+0.5;
// // return;
// // }
// //}
// }
//});
}
}
}
}
}

void
WarpX::SetWeightsInGatherBuffer(const amrex::Box tbx, amrex::Array4<amrex::Real> wtmsk,
const amrex::Array4<int const> gmsk, const amrex::Array4<int const> bmsk, const int ngbuffer, const bool do_interpolate, amrex::Real tanh_midpoint)
{
amrex::ParallelFor(tbx, [=] AMREX_GPU_DEVICE(int i, int j, int k) {
wtmsk(i,j,k) = 0._rt;
if (bmsk(i,j,k) == 0 && do_interpolate) {
if(gmsk(i,j,k)==0) {
wtmsk(i,j,k) = 0.;
return;
}
for (int ii = i-1; ii >= i-ngbuffer; --ii) {
if (gmsk(ii,j,k)==0) {
amrex::Real arg = (static_cast<amrex::Real>(i-ii)-ngbuffer*tanh_midpoint)
/ ((1.-tanh_midpoint)*(ngbuffer/3.));
wtmsk(i,j,k) = std::tanh(arg)*0.5 + 0.5;
return;
}
}
for (int ii = i+1; ii <= i+ngbuffer; ++ii) {
if (gmsk(ii,j,k)==0) {
amrex::Real arg = (static_cast<amrex::Real>(ii-i)-ngbuffer*tanh_midpoint)
/ ((1.-tanh_midpoint)*(ngbuffer/3.));
wtmsk(i,j,k) = std::tanh(arg)*0.5+0.5;
return;
}
}
}
});
}

/**
* \brief Build buffer mask within given FArrayBox
*
Expand Down

0 comments on commit 8c137ca

Please sign in to comment.