1#ifndef AMREX_FFT_R2C_H_
2#define AMREX_FFT_R2C_H_
3#include <AMReX_Config.H>
21template <
typename T>
class OpenBCSolver;
22template <
typename T>
class Poisson;
23template <
typename T>
class PoissonHybrid;
46template <
typename T = Real, FFT::Direction D = FFT::Direction::both,
bool C = false>
51 using MF = std::conditional_t
52 <
C,
cMF, std::conditional_t<std::is_same_v<T,Real>,
56 template <
typename U>
friend class Poisson;
76 explicit R2C (std::array<int,AMREX_SPACEDIM>
const& domain_size,
110 std::array<int,AMREX_SPACEDIM>
const& local_size);
125 std::pair<std::array<int,AMREX_SPACEDIM>,std::array<int,AMREX_SPACEDIM>>
157 std::array<int,AMREX_SPACEDIM>
const& local_size);
177 std::pair<std::array<int,AMREX_SPACEDIM>,std::array<int,AMREX_SPACEDIM>>
209 template <
typename F, Direction DIR=D>
212 int incomp = 0,
int outcomp = 0)
216 "FFT::R2C::forwardThenBackward(post_forward) currently supports only !twod_mode");
233 template <Direction DIR=D>
248 template <Direction DIR=D>
250 void forward (
MF const& inmf,
cMF& outmf,
int incomp = 0,
int outcomp = 0);
268 template <
typename RT,
typename CT, Direction DIR=D,
bool CP=C>
270 && (((
sizeof(RT)*2 ==
sizeof(CT)) && (!CP))
271 || ((
sizeof(RT) ==
sizeof(CT)) && CP))
283 template <Direction DIR=D>
298 template <Direction DIR=D>
300 void backward (
cMF const& inmf,
MF& outmf,
int incomp = 0,
int outcomp = 0);
318 template <
typename CT,
typename RT, Direction DIR=D,
bool CP=C>
320 && (((
sizeof(RT)*2 ==
sizeof(CT)) && (!CP))
321 || ((
sizeof(RT) ==
sizeof(CT)) && CP))
344 template <Direction DIR=D>
365 template <
typename F>
368 template <
typename F>
378 void prepare_openbc ();
384 void backward_doit (
cMF const& inmf,
MF& outmf,
387 int incomp = 0,
int outcomp = 0);
389 std::pair<Plan<T>,
Plan<T>> make_c2c_plans (
cMF& inout,
int ndims)
const;
391 [[nodiscard]]
MF& make_m_rx_alias (
IndexType const& ix_type);
396 static Box make_domain_x (
Box const& domain)
411 static Box make_domain_y (
Box const& domain)
416 domain.length(2)-1)),
421 domain.length(2)-1)),
426 static Box make_domain_z (
Box const& domain)
431 domain.length(1)-1)),
436 domain.length(1)-1)),
441 static std::pair<BoxArray,DistributionMapping>
442 make_layout_from_local_domain (std::array<int,AMREX_SPACEDIM>
const& local_start,
443 std::array<int,AMREX_SPACEDIM>
const& local_size);
445 template <
typename FA,
typename RT>
446 std::pair<std::unique_ptr<char,DataDeleter>,std::size_t>
447 install_raw_ptr (FA& fa, RT
const* p);
449 Plan<T> m_fft_fwd_x{};
450 Plan<T> m_fft_bwd_x{};
451 Plan<T> m_fft_fwd_y{};
452 Plan<T> m_fft_bwd_y{};
453 Plan<T> m_fft_fwd_z{};
454 Plan<T> m_fft_bwd_z{};
455 Plan<T> m_fft_fwd_x_half{};
456 Plan<T> m_fft_bwd_x_half{};
461 std::unique_ptr<MultiBlockCommMetaData> m_cmd_x2y;
462 std::unique_ptr<MultiBlockCommMetaData> m_cmd_y2x;
463 std::unique_ptr<MultiBlockCommMetaData> m_cmd_y2z;
464 std::unique_ptr<MultiBlockCommMetaData> m_cmd_z2y;
465 std::unique_ptr<MultiBlockCommMetaData> m_cmd_x2z;
466 std::unique_ptr<MultiBlockCommMetaData> m_cmd_z2x;
467 std::unique_ptr<MultiBlockCommMetaData> m_cmd_x2z_half;
468 std::unique_ptr<MultiBlockCommMetaData> m_cmd_z2x_half;
473 RotateFwd m_dtos_x2z{};
474 RotateBwd m_dtos_z2x{};
482 mutable cMF m_raw_cmf;
484 std::map<IntVect,MF> m_rx_aliases;
486 std::unique_ptr<char,DataDeleter> m_data_1;
487 std::unique_ptr<char,DataDeleter> m_data_2;
490 Box m_spectral_domain_x;
491 Box m_spectral_domain_y;
492 Box m_spectral_domain_z;
494 std::unique_ptr<R2C<T,D,C>> m_r2c_sub;
495 detail::SubHelper m_sub_helper;
499 bool m_do_alld_fft =
false;
500 bool m_slab_decomp =
false;
501 bool m_openbc_half =
false;
504template <
typename T, Direction D,
bool C>
506 : m_real_domain(domain),
507 m_spectral_domain_x(make_domain_x(domain)),
508#if (AMREX_SPACEDIM >= 2)
509 m_spectral_domain_y(make_domain_y(domain)),
510#if (AMREX_SPACEDIM == 3)
511 m_spectral_domain_z(make_domain_z(domain)),
514 m_sub_helper(domain),
519 static_assert(std::is_same_v<float,T> || std::is_same_v<double,T>);
522#if (AMREX_SPACEDIM == 2)
527 int(domain.
length(1) > 1) +
528 int(domain.
length(2) > 1)) >= 2);
533 Box subbox = m_sub_helper.make_box(m_real_domain);
534 if (subbox.
size() != m_real_domain.
size()) {
535 m_r2c_sub = std::make_unique<R2C<T,D,C>>(subbox, m_info);
543#if (AMREX_SPACEDIM == 3)
548 int shortside = m_real_domain.
shortside();
559 m_slab_decomp =
true;
561 m_slab_decomp =
true;
572 m_rx.define(bax, dmx, ncomp, 0,
MFInfo().SetAlloc(
false));
576 for (
auto & b : bl) {
578 b.setBig(0, m_spectral_domain_x.
bigEnd(0));
581 m_cx.
define(cbax, dmx, ncomp, 0,
MFInfo().SetAlloc(
false));
593#if (AMREX_SPACEDIM >= 2)
595 if ((m_real_domain.
length(1) > 1) && !m_slab_decomp && !m_info.
oned_mode)
599 if (cbay.size() == dmx.size()) {
602 cdmy = detail::make_iota_distromap(cbay.size());
604 m_cy.
define(cbay, cdmy, ncomp, 0,
MFInfo().SetAlloc(
false));
608#if (AMREX_SPACEDIM == 3)
610 m_real_domain.
length(1) > 1 &&
611 m_real_domain.
length(2) > 1)
614 {
false,
true,
true},
true);
616 if (cbaz.size() == dmx.size()) {
618 }
else if (cbaz.size() == cdmy.
size()) {
621 cdmz = detail::make_iota_distromap(cbaz.size());
623 m_cz.
define(cbaz, cdmz, ncomp, 0,
MFInfo().SetAlloc(
false));
629 m_data_1 = detail::make_mfs_share(m_rx, m_cx);
630 m_data_2 = detail::make_mfs_share(m_cz, m_cz);
632 m_data_1 = detail::make_mfs_share(m_rx, m_cz);
633 m_data_2 = detail::make_mfs_share(m_cy, m_cy);
635 if (myproc < m_cx.
size()) {
638 m_cx.
setFab(myproc, FAB(box, ncomp, m_rx[myproc].dataPtr()));
643 m_data_1 = detail::make_mfs_share(m_rx, m_cz);
644 m_data_2 = detail::make_mfs_share(m_cx, m_cx);
646 m_data_1 = detail::make_mfs_share(m_rx, m_cy);
647 m_data_2 = detail::make_mfs_share(m_cx, m_cz);
655#if (AMREX_SPACEDIM >= 2)
656 if (! m_cy.
empty()) {
658 m_cmd_x2y = std::make_unique<MultiBlockCommMetaData>
659 (m_cy, m_spectral_domain_y, m_cx,
IntVect(0), m_dtos_x2y);
660 m_cmd_y2x = std::make_unique<MultiBlockCommMetaData>
661 (m_cx, m_spectral_domain_x, m_cy,
IntVect(0), m_dtos_y2x);
664#if (AMREX_SPACEDIM == 3)
665 if (! m_cz.
empty() ) {
668 m_cmd_x2z = std::make_unique<MultiBlockCommMetaData>
669 (m_cz, m_spectral_domain_z, m_cx,
IntVect(0), m_dtos_x2z);
670 m_cmd_z2x = std::make_unique<MultiBlockCommMetaData>
671 (m_cx, m_spectral_domain_x, m_cz,
IntVect(0), m_dtos_z2x);
674 m_cmd_y2z = std::make_unique<MultiBlockCommMetaData>
675 (m_cz, m_spectral_domain_z, m_cy,
IntVect(0), m_dtos_y2z);
676 m_cmd_z2y = std::make_unique<MultiBlockCommMetaData>
677 (m_cy, m_spectral_domain_y, m_cz,
IntVect(0), m_dtos_z2y);
686 if (myproc < m_rx.size())
689 int ndims = m_slab_decomp ? 2 : 1;
690 std::tie(m_fft_fwd_x, m_fft_bwd_x) = make_c2c_plans(m_cx, ndims);
692 Box const& box = m_rx.box(myproc);
693 auto* pr = m_rx[myproc].dataPtr();
696 m_fft_fwd_x.template init_r2c<Direction::forward>(box, pr, pc, m_slab_decomp, ncomp);
697 m_fft_bwd_x = m_fft_fwd_x;
700 m_fft_fwd_x.template init_r2c<Direction::forward>(box, pr, pc, m_slab_decomp, ncomp);
703 m_fft_bwd_x.template init_r2c<Direction::backward>(box, pr, pc, m_slab_decomp, ncomp);
709#if (AMREX_SPACEDIM >= 2)
710 if (! m_cy.
empty()) {
711 std::tie(m_fft_fwd_y, m_fft_bwd_y) = make_c2c_plans(m_cy,1);
714#if (AMREX_SPACEDIM == 3)
715 if (! m_cz.
empty()) {
716 std::tie(m_fft_fwd_z, m_fft_bwd_z) = make_c2c_plans(m_cz,1);
723 m_data_1 = detail::make_mfs_share(m_rx, m_cx);
724 std::tie(m_fft_fwd_x, m_fft_bwd_x) = make_c2c_plans(m_cx,AMREX_SPACEDIM);
726 m_data_1 = detail::make_mfs_share(m_rx, m_rx);
727 m_data_2 = detail::make_mfs_share(m_cx, m_cx);
729 auto const& len = m_real_domain.
length();
730 auto* pr = (
void*)m_rx[0].dataPtr();
731 auto* pc = (
void*)m_cx[0].dataPtr();
733 m_fft_fwd_x.template init_r2c<Direction::forward>(len, pr, pc,
false, ncomp);
734 m_fft_bwd_x = m_fft_fwd_x;
737 m_fft_fwd_x.template init_r2c<Direction::forward>(len, pr, pc,
false, ncomp);
740 m_fft_bwd_x.template init_r2c<Direction::backward>(len, pr, pc,
false, ncomp);
747template <
typename T, Direction D,
bool C>
752template <
typename T, Direction D,
bool C>
755 if (m_fft_bwd_x.plan != m_fft_fwd_x.plan) {
756 m_fft_bwd_x.destroy();
758 if (m_fft_bwd_y.plan != m_fft_fwd_y.plan) {
759 m_fft_bwd_y.destroy();
761 if (m_fft_bwd_z.plan != m_fft_fwd_z.plan) {
762 m_fft_bwd_z.destroy();
764 m_fft_fwd_x.destroy();
765 m_fft_fwd_y.destroy();
766 m_fft_fwd_z.destroy();
767 if (m_fft_bwd_x_half.plan != m_fft_fwd_x_half.plan) {
768 m_fft_bwd_x_half.destroy();
770 m_fft_fwd_x_half.destroy();
773template <
typename T, Direction D,
bool C>
774std::pair<BoxArray,DistributionMapping>
776 std::array<int,AMREX_SPACEDIM>
const& local_size)
780 Box bx(lo, lo+len-1);
787 pmap.reserve(allboxes.size());
788 for (
int i = 0; i < allboxes.size(); ++i) {
789 if (allboxes[i].ok()) {
793 std::erase_if(allboxes, [=] (
Box const& b) {
return b.isEmpty(); });
794 BoxList bl(std::move(allboxes));
795 return std::make_pair(BoxArray(std::move(bl)), DistributionMapping(std::move(pmap)));
797 return std::make_pair(BoxArray(bx), DistributionMapping(Vector<int>({0})));
801template <
typename T, Direction D,
bool C>
803 std::array<int,AMREX_SPACEDIM>
const& local_size)
805 auto const& [ba, dm] = make_layout_from_local_domain(local_start, local_size);
806 m_raw_mf =
MF(ba, dm, m_info.batch_size, 0,
MFInfo().SetAlloc(
false));
809template <
typename T, Direction D,
bool C>
810std::pair<std::array<int,AMREX_SPACEDIM>,std::array<int,AMREX_SPACEDIM>>
813 auto const ncomp = m_info.batch_size;
815#if (AMREX_SPACEDIM > 1)
817 m_raw_mf =
MF(m_sub_helper.inverse_boxarray(m_r2c_sub->m_rx.boxArray()),
818 m_r2c_sub->m_rx.DistributionMap(), ncomp, 0,
823 m_raw_mf =
MF(m_rx.boxArray(), m_rx.DistributionMap(), ncomp, 0,
828 if (myproc < m_raw_mf.size()) {
829 Box const& box = m_raw_mf.box(myproc);
830 return std::make_pair(box.
smallEnd().toArray(),
833 return std::make_pair(std::array<int,AMREX_SPACEDIM>{
AMREX_D_DECL(0,0,0)},
838template <
typename T, Direction D,
bool C>
840 std::array<int,AMREX_SPACEDIM>
const& local_size)
842 auto const& [ba, dm] = make_layout_from_local_domain(local_start, local_size);
843 m_raw_cmf =
cMF(ba, dm, m_info.batch_size, 0,
MFInfo().SetAlloc(
false));
846template <
typename T, Direction D,
bool C>
847std::pair<std::array<int,AMREX_SPACEDIM>,std::array<int,AMREX_SPACEDIM>>
850 auto const ncomp = m_info.batch_size;
851 auto const& [ba, dm] = getSpectralDataLayout();
856 if (myproc < m_raw_cmf.size()) {
857 Box const& box = m_raw_cmf.box(myproc);
858 return std::make_pair(box.
smallEnd().toArray(), box.
length().toArray());
860 return std::make_pair(std::array<int,AMREX_SPACEDIM>{
AMREX_D_DECL(0,0,0)},
865template <
typename T, Direction D,
bool C>
868 if (
C || m_r2c_sub) {
amrex::Abort(
"R2C: OpenBC not supported with reduced dimensions or complex inputs"); }
870#if (AMREX_SPACEDIM == 3)
871 if (m_do_alld_fft) {
return; }
873 auto const ncomp = m_info.batch_size;
875 if (m_slab_decomp && ! m_fft_fwd_x_half.defined) {
876 auto* fab = detail::get_fab(m_rx);
878 Box bottom_half = m_real_domain;
879 bottom_half.
growHi(2,-m_real_domain.length(2)/2);
880 Box box = fab->box() & bottom_half;
882 auto* pr = fab->dataPtr();
884 detail::get_fab(m_cx)->dataPtr();
886 m_fft_fwd_x_half.template init_r2c<Direction::forward>
887 (box, pr, pc, m_slab_decomp, ncomp);
888 m_fft_bwd_x_half = m_fft_fwd_x_half;
891 m_fft_fwd_x_half.template init_r2c<Direction::forward>
892 (box, pr, pc, m_slab_decomp, ncomp);
895 m_fft_bwd_x_half.template init_r2c<Direction::backward>
896 (box, pr, pc, m_slab_decomp, ncomp);
903 if (m_cmd_x2z && ! m_cmd_x2z_half) {
904 Box bottom_half = m_spectral_domain_z;
907 bottom_half.
growHi(0,-m_spectral_domain_z.length(0)/2);
908 m_cmd_x2z_half = std::make_unique<MultiBlockCommMetaData>
909 (m_cz, bottom_half, m_cx,
IntVect(0), m_dtos_x2z);
912 if (m_cmd_z2x && ! m_cmd_z2x_half) {
913 Box bottom_half = m_spectral_domain_x;
914 bottom_half.
growHi(2,-m_spectral_domain_x.length(2)/2);
915 m_cmd_z2x_half = std::make_unique<MultiBlockCommMetaData>
916 (m_cx, bottom_half, m_cz,
IntVect(0), m_dtos_z2x);
921template <
typename T, Direction D,
bool C>
923R2C<T,D,C>::make_m_rx_alias (
IndexType const& ix_type)
925 auto const key = ix_type.ixType();
926 auto it = m_rx_aliases.find(key);
927 if (it != m_rx_aliases.end()) {
932 for (
auto b : m_rx.
boxArray().boxList()) {
937 MF alias(BoxArray(std::move(bl)), m_rx.DistributionMap(), m_rx.nComp(),
938 m_rx.nGrowVect(), MFInfo{}.SetAlloc(
false));
940 using FAB =
typename MF::fab_type;
941 for (MFIter mfi(alias, MFItInfo().DisableDeviceSync()); mfi.isValid(); ++mfi) {
942 alias.setFab(mfi, FAB(mfi.fabbox(), m_rx.nComp(), m_rx[mfi.index()].dataPtr()));
945 auto [alias_it, inserted] = m_rx_aliases.emplace(key, std::move(alias));
947 return alias_it->second;
950template <
typename T, Direction D,
bool C>
952R2C<T,D,C>::make_m_rx_alias_periodicity (
IndexType const& ix_type,
953 Periodicity
const& period)
const
955 IntVect per = period.intVect();
956 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
957 if (ix_type.nodeCentered(idim) && per[idim] == 0) {
958 per[idim] = m_real_domain.length(idim);
961 return Periodicity(per);
964template <
typename T, Direction D,
bool C>
965template <Direction DIR>
971 auto const ncomp = m_info.batch_size;
974 if (m_sub_helper.ghost_safe(inmf.nGrowVect())) {
975 m_r2c_sub->forward(m_sub_helper.make_alias_mf(inmf), incomp);
977 MF tmp(inmf.boxArray(), inmf.DistributionMap(), ncomp, 0);
978 tmp.LocalCopy(inmf, incomp, 0, ncomp,
IntVect(0));
979 m_r2c_sub->forward(m_sub_helper.make_alias_mf(tmp),0);
984 if (&m_rx != &inmf) {
985 if (m_rx.boxArray().ixType() == inmf.boxArray().ixType()) {
986 m_rx.ParallelCopy(inmf, incomp, 0, ncomp);
988 MF& rx_alias = make_m_rx_alias(inmf.boxArray().ixType());
989 rx_alias.ParallelCopy(inmf, incomp, 0, ncomp);
995 m_fft_fwd_x.template compute_c2c<Direction::forward>();
997 m_fft_fwd_x.template compute_r2c<Direction::forward>();
1002 auto& fft_x = m_openbc_half ? m_fft_fwd_x_half : m_fft_fwd_x;
1004 fft_x.template compute_c2c<Direction::forward>();
1006 fft_x.template compute_r2c<Direction::forward>();
1010 ParallelCopy(m_cy, m_cx, *m_cmd_x2y, 0, 0, ncomp, m_dtos_x2y);
1012 m_fft_fwd_y.template compute_c2c<Direction::forward>();
1015 ParallelCopy(m_cz, m_cy, *m_cmd_y2z, 0, 0, ncomp, m_dtos_y2z);
1017#if (AMREX_SPACEDIM == 3)
1018 else if ( m_cmd_x2z) {
1019 if (m_openbc_half) {
1023 {components, m_dtos_x2z};
1024 auto handler = ParallelCopy_nowait(m_cz, m_cx, *m_cmd_x2z_half, packing);
1026 Box upper_half = m_spectral_domain_z;
1029 upper_half.
growLo (0,-m_spectral_domain_z.length(0)/2);
1030 m_cz.setVal(0, upper_half, 0, ncomp);
1032 ParallelCopy_finish(m_cz, std::move(handler), *m_cmd_x2z_half, packing);
1034 ParallelCopy(m_cz, m_cx, *m_cmd_x2z, 0, 0, ncomp, m_dtos_x2z);
1038 m_fft_fwd_z.template compute_c2c<Direction::forward>();
1041template <
typename T, Direction D,
bool C>
1042template <
typename FA,
typename RT>
1043std::pair<std::unique_ptr<char,DataDeleter>,std::size_t>
1048 using FAB =
typename FA::FABType::value_type;
1049 using T_FAB =
typename FAB::value_type;
1050 static_assert(
sizeof(T_FAB) ==
sizeof(RT));
1052 auto const ncomp = m_info.batch_size;
1053 auto const& ia = fa.IndexArray();
1055 T_FAB*
pp =
nullptr;
1058 if ( ! ia.empty() ) {
1060 Box const& box = fa.fabbox(K);
1064 sz =
sizeof(T_FAB) * box.
numPts() * ncomp;
1067 fa.setFab(K, FAB(box,ncomp,
pp));
1071 return std::make_pair(std::unique_ptr<char,DataDeleter>{},std::size_t(0));
1073 return std::make_pair(std::unique_ptr<char,DataDeleter>
1079template <
typename T, Direction D,
bool C>
1080template <
typename RT,
typename CT, Direction DIR,
bool CP>
1082 && (((
sizeof(RT)*2 ==
sizeof(CT)) && (!CP))
1083 || ((
sizeof(RT) ==
sizeof(CT)) && CP))
1086 auto [rdata, rsz] = install_raw_ptr(m_raw_mf, in);
1087 auto [cdata, csz] = install_raw_ptr(m_raw_cmf, out);
1102template <
typename T, Direction D,
bool C>
1103template <Direction DIR>
1110template <
typename T, Direction D,
bool C>
1116 auto const ncomp = m_info.batch_size;
1119 if (m_sub_helper.ghost_safe(outmf.nGrowVect())) {
1120 MF submf = m_sub_helper.make_alias_mf(outmf);
1121 IntVect const& subngout = m_sub_helper.make_iv(ngout);
1122 Periodicity const& subperiod = m_sub_helper.make_periodicity(period);
1123 m_r2c_sub->backward_doit(submf, subngout, subperiod, outcomp);
1125 MF tmp(outmf.boxArray(), outmf.DistributionMap(), ncomp,
1126 m_sub_helper.make_safe_ghost(outmf.nGrowVect()));
1127 this->backward_doit(tmp, ngout, period, 0);
1128 outmf.LocalCopy(tmp, 0, outcomp, ncomp, tmp.nGrowVect());
1133 if (m_do_alld_fft) {
1135 m_fft_bwd_x.template compute_c2c<Direction::backward>();
1137 m_fft_bwd_x.template compute_r2c<Direction::backward>();
1140 if (m_rx.boxArray().ixType() == outmf.boxArray().ixType()) {
1141 outmf.ParallelCopy(m_rx, 0, outcomp, ncomp,
IntVect(0),
1142 dst_nghost, period);
1144 MF& rx_alias = make_m_rx_alias(outmf.boxArray().ixType());
1145 auto const copy_period = make_m_rx_alias_periodicity(outmf.boxArray().ixType(), period);
1146 outmf.ParallelCopy(rx_alias, 0, outcomp, ncomp,
IntVect(0),
1147 dst_nghost, copy_period);
1152 m_fft_bwd_z.template compute_c2c<Direction::backward>();
1154 ParallelCopy(m_cy, m_cz, *m_cmd_z2y, 0, 0, ncomp, m_dtos_z2y);
1156#if (AMREX_SPACEDIM == 3)
1157 else if ( m_cmd_z2x) {
1158 auto const& cmd = m_openbc_half ? m_cmd_z2x_half : m_cmd_z2x;
1159 ParallelCopy(m_cx, m_cz, *cmd, 0, 0, ncomp, m_dtos_z2x);
1163 m_fft_bwd_y.template compute_c2c<Direction::backward>();
1165 ParallelCopy(m_cx, m_cy, *m_cmd_y2x, 0, 0, ncomp, m_dtos_y2x);
1168 auto& fft_x = m_openbc_half ? m_fft_bwd_x_half : m_fft_bwd_x;
1170 fft_x.template compute_c2c<Direction::backward>();
1172 fft_x.template compute_r2c<Direction::backward>();
1175 if (m_rx.boxArray().ixType() == outmf.boxArray().ixType()) {
1176 outmf.ParallelCopy(m_rx, 0, outcomp, ncomp,
IntVect(0),
1177 dst_nghost, period);
1179 MF& rx_alias = make_m_rx_alias(outmf.boxArray().ixType());
1180 auto const copy_period = make_m_rx_alias_periodicity(outmf.boxArray().ixType(), period);
1181 outmf.ParallelCopy(rx_alias, 0, outcomp, ncomp,
IntVect(0),
1182 dst_nghost, copy_period);
1186template <
typename T, Direction D,
bool C>
1187template <
typename CT,
typename RT, Direction DIR,
bool CP>
1189 && (((
sizeof(RT)*2 ==
sizeof(CT)) && (!CP))
1190 || ((
sizeof(RT) ==
sizeof(CT)) && CP))
1193 auto [rdata, rsz] = install_raw_ptr(m_raw_mf, out);
1194 auto [cdata, csz] = install_raw_ptr(m_raw_cmf, in);
1209template <
typename T, Direction D,
bool C>
1216 auto* fab = detail::get_fab(inout);
1217 if (!fab) {
return {fwd, bwd};}
1219 Box const& box = fab->box();
1222 auto const ncomp = m_info.batch_size;
1224#ifdef AMREX_USE_SYCL
1225 fwd.template init_c2c<Direction::forward>(box, pio, ncomp, ndims);
1229 fwd.template init_c2c<Direction::forward>(box, pio, ncomp, ndims);
1232 bwd.template init_c2c<Direction::backward>(box, pio, ncomp, ndims);
1239namespace fft_detail {
1246 template <
typename F,
typename T>
1248 auto call_post_forward (
F const& f,
int i,
int j,
int k, Array4<T>
const& a,
1249 int ii,
int jj,
int kk,
int)
1250 noexcept ->
decltype(void(f(0,0,0,a(0,0,0))))
1252 for (
int n = 0; n < a.nComp(); ++n) {
1253 f(i,j,k,a(ii,jj,kk,n));
1257 template <
typename F,
typename T>
1259 auto call_post_forward (
F const& f,
int i,
int j,
int k, Array4<T>
const& a,
1260 int ii,
int jj,
int kk,
long)
1261 noexcept ->
decltype(void(f(0,0,0,a.cellData(0,0,0))))
1263 f(i,j,k,a.cellData(ii,jj,kk));
1267template <
typename T, Direction D,
bool C>
1268template <
typename F>
1271 if (m_info.twod_mode) {
1273#if (AMREX_SPACEDIM > 1)
1274 }
else if (m_r2c_sub) {
1276#if (AMREX_SPACEDIM == 2)
1278 m_r2c_sub->post_forward_doit_1
1280 ->
decltype(
void(post_forward(0,0,0,
static_cast<decltype(sp)
>(sp))))
1282 post_forward(0, i, 0,
static_cast<decltype(sp)
>(sp));
1285 if (m_real_domain.length(0) == 1 && m_real_domain.length(1) == 1) {
1287 m_r2c_sub->post_forward_doit_1
1289 ->
decltype(void(post_forward(0,0,0,
static_cast<decltype(sp)
>(sp))))
1291 post_forward(0, 0, i,
static_cast<decltype(sp)
>(sp));
1293 }
else if (m_real_domain.length(0) == 1 && m_real_domain.length(2) == 1) {
1295 m_r2c_sub->post_forward_doit_1
1297 ->
decltype(void(post_forward(0,0,0,
static_cast<decltype(sp)
>(sp))))
1299 post_forward(0, i, 0,
static_cast<decltype(sp)
>(sp));
1301 }
else if (m_real_domain.length(0) == 1) {
1303 m_r2c_sub->post_forward_doit_1
1305 ->
decltype(void(post_forward(0,0,0,
static_cast<decltype(sp)
>(sp))))
1307 post_forward(0, i, j,
static_cast<decltype(sp)
>(sp));
1309 }
else if (m_real_domain.length(1) == 1) {
1311 m_r2c_sub->post_forward_doit_1
1313 ->
decltype(void(post_forward(0,0,0,
static_cast<decltype(sp)
>(sp))))
1315 post_forward(i, 0, j,
static_cast<decltype(sp)
>(sp));
1318 amrex::Abort(
"R2c::post_forward_doit_0: how did this happen?");
1323 this->post_forward_doit_1(post_forward);
1327template <
typename T, Direction D,
bool C>
1328template <
typename F>
1331 if (m_info.twod_mode) {
1333 }
else if (m_r2c_sub) {
1334 amrex::Abort(
"R2C::post_forward_doit_1: How did this happen?");
1336 if ( ! m_cz.empty()) {
1337 auto* spectral_fab = detail::get_fab(m_cz);
1339 auto const& a = spectral_fab->array();
1343 fft_detail::call_post_forward(post_forward,
1344 jx,ky,iz,a,iz,jx,ky,0);
1347 }
else if ( ! m_cy.empty()) {
1348 auto* spectral_fab = detail::get_fab(m_cy);
1350 auto const& a = spectral_fab->array();
1354 fft_detail::call_post_forward(post_forward,
1355 jx,iy,k,a,iy,jx,k,0);
1359 auto* spectral_fab = detail::get_fab(m_cx);
1361 auto const& a = spectral_fab->array();
1365 fft_detail::call_post_forward(post_forward,
1373template <
typename T, Direction D,
bool C>
1376#if (AMREX_SPACEDIM > 1)
1378 if (m_r2c_sub) {
return m_r2c_sub->scalingFactor(); }
1381#if (AMREX_SPACEDIM == 3)
1383 if (m_info.oned_mode) {
1384 return T(1)/T(
Long(m_real_domain.length(0)));
1385 }
else if (m_info.twod_mode) {
1386 return T(1)/T(
Long(m_real_domain.length(0)) *
1387 Long(m_real_domain.length(1)));
1389#elif (AMREX_SPACEDIM == 2)
1390 if (m_info.oned_mode) {
1391 return T(1)/T(
Long(m_real_domain.length(0)));
1395 return T(1)/T(m_real_domain.numPts());
1399template <
typename T, Direction D,
bool C>
1400template <Direction DIR>
1405#if (AMREX_SPACEDIM > 1)
1408 return std::make_pair(cmf, m_sub_helper.inverse_order(order));
1411 if (!m_cz.empty()) {
1413 }
else if (!m_cy.empty()) {
1420template <
typename T, Direction D,
bool C>
1421template <Direction DIR>
1427 auto const ncomp = m_info.batch_size;
1431 bool inmf_safe = m_sub_helper.ghost_safe(inmf.nGrowVect());
1432 MF inmf_sub, inmf_tmp;
1435 inmf_sub = m_sub_helper.make_alias_mf(inmf);
1436 incomp_sub = incomp;
1438 inmf_tmp.define(inmf.boxArray(), inmf.DistributionMap(), ncomp, 0);
1439 inmf_tmp.LocalCopy(inmf, incomp, 0, ncomp,
IntVect(0));
1440 inmf_sub = m_sub_helper.make_alias_mf(inmf_tmp);
1444 bool outmf_safe = m_sub_helper.ghost_safe(outmf.
nGrowVect());
1445 cMF outmf_sub, outmf_tmp;
1448 outmf_sub = m_sub_helper.make_alias_mf(outmf);
1449 outcomp_sub = outcomp;
1452 outmf_sub = m_sub_helper.make_alias_mf(outmf_tmp);
1456 m_r2c_sub->forward(inmf_sub, outmf_sub, incomp_sub, outcomp_sub);
1465 if (!m_cz.empty()) {
1468 (outmf, m_spectral_domain_x, m_cz,
IntVect(0), dtos);
1469 ParallelCopy(outmf, m_cz, cmd, 0, outcomp, ncomp, dtos);
1470 }
else if (!m_cy.empty()) {
1472 (outmf, m_spectral_domain_x, m_cy,
IntVect(0), m_dtos_y2x);
1473 ParallelCopy(outmf, m_cy, cmd, 0, outcomp, ncomp, m_dtos_y2x);
1480template <
typename T, Direction D,
bool C>
1481template <Direction DIR>
1488template <
typename T, Direction D,
bool C>
1490 Periodicity const& period,
int incomp,
int outcomp)
1494 auto const ncomp = m_info.batch_size;
1498 bool inmf_safe = m_sub_helper.ghost_safe(inmf.nGrowVect());
1499 cMF inmf_sub, inmf_tmp;
1502 inmf_sub = m_sub_helper.make_alias_mf(inmf);
1503 incomp_sub = incomp;
1505 inmf_tmp.define(inmf.boxArray(), inmf.DistributionMap(), ncomp, 0);
1506 inmf_tmp.LocalCopy(inmf, incomp, 0, ncomp,
IntVect(0));
1507 inmf_sub = m_sub_helper.make_alias_mf(inmf_tmp);
1511 bool outmf_safe = m_sub_helper.ghost_safe(outmf.nGrowVect());
1512 MF outmf_sub, outmf_tmp;
1515 outmf_sub = m_sub_helper.make_alias_mf(outmf);
1516 outcomp_sub = outcomp;
1518 IntVect const& ngtmp = m_sub_helper.make_safe_ghost(outmf.nGrowVect());
1519 outmf_tmp.define(outmf.boxArray(), outmf.DistributionMap(), ncomp, ngtmp);
1520 outmf_sub = m_sub_helper.make_alias_mf(outmf_tmp);
1524 IntVect const& subngout = m_sub_helper.make_iv(ngout);
1525 Periodicity
const& subperiod = m_sub_helper.make_periodicity(period);
1526 m_r2c_sub->backward_doit(inmf_sub, outmf_sub, subngout, subperiod, incomp_sub, outcomp_sub);
1529 outmf.LocalCopy(outmf_tmp, 0, outcomp, ncomp, outmf_tmp.nGrowVect());
1534 if (!m_cz.empty()) {
1536 MultiBlockCommMetaData cmd
1537 (m_cz, m_spectral_domain_z, inmf,
IntVect(0), dtos);
1539 }
else if (!m_cy.empty()) {
1540 MultiBlockCommMetaData cmd
1541 (m_cy, m_spectral_domain_y, inmf,
IntVect(0), m_dtos_x2y);
1542 ParallelCopy(m_cy, inmf, cmd, incomp, 0, ncomp, m_dtos_x2y);
1544 m_cx.ParallelCopy(inmf, incomp, 0, ncomp);
1546 backward_doit(outmf, ngout, period, outcomp);
1550template <
typename T, Direction D,
bool C>
1551std::pair<BoxArray,DistributionMapping>
1554#if (AMREX_SPACEDIM > 1)
1556 auto const& [ba, dm] = m_r2c_sub->getSpectralDataLayout();
1557 return std::make_pair(m_sub_helper.inverse_boxarray(ba), dm);
1561#if (AMREX_SPACEDIM == 3)
1562 if (!m_cz.empty()) {
1563 BoxList bl = m_cz.boxArray().boxList();
1564 for (
auto& b : bl) {
1565 auto lo = b.smallEnd();
1566 auto hi = b.bigEnd();
1567 std::swap(lo[0], lo[1]);
1568 std::swap(lo[1], lo[2]);
1569 std::swap(hi[0], hi[1]);
1570 std::swap(hi[1], hi[2]);
1574 return std::make_pair(
BoxArray(std::move(bl)), m_cz.DistributionMap());
1577#if (AMREX_SPACEDIM >= 2)
1578 if (!m_cy.empty()) {
1579 BoxList bl = m_cy.boxArray().boxList();
1580 for (
auto& b : bl) {
1581 auto lo = b.smallEnd();
1582 auto hi = b.bigEnd();
1583 std::swap(lo[0], lo[1]);
1584 std::swap(hi[0], hi[1]);
1588 return std::make_pair(
BoxArray(std::move(bl)), m_cy.DistributionMap());
1592 return std::make_pair(m_cx.boxArray(), m_cx.DistributionMap());
1597template <
typename T = Real, FFT::Direction D = FFT::Direction::both>
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:562
#define AMREX_ALWAYS_ASSERT_WITH_MESSAGE(EX, MSG)
Definition AMReX_BLassert.H:49
#define AMREX_ALWAYS_ASSERT(EX)
Definition AMReX_BLassert.H:50
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:124
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
amrex::ParmParse pp
Input file parser instance for the given namespace.
Definition AMReX_HypreIJIface.cpp:15
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
virtual void * alloc(std::size_t sz)=0
Allocate sz bytes from this arena.
Reference-counted collection of Boxes.
Definition AMReX_BoxArray.H:681
A list of Boxes sharing a common IndexType.
Definition AMReX_BoxList.H:109
BoxList & shift(int dir, int nzones)
Apply Box::shift(dir, nzones) to every Box in the list.
Definition AMReX_BoxList.cpp:576
__host__ __device__ const IntVectND< dim > & bigEnd() const &noexcept
Return the inclusive upper bound of the box.
Definition AMReX_Box.H:136
__host__ __device__ Long numPts() const noexcept
Return the number of points contained in the BoxND.
Definition AMReX_Box.H:385
__host__ __device__ IntVectND< dim > length() const noexcept
Return the length of the BoxND.
Definition AMReX_Box.H:167
__host__ __device__ int shortside(int &dir) const noexcept
Return length of shortest side. dir is modified to give direction with shortest side: 0....
Definition AMReX_Box.H:466
__host__ __device__ IntVectND< dim > size() const noexcept
Return the length of the BoxND.
Definition AMReX_Box.H:160
__host__ __device__ BoxND & growLo(int idir, int n_cell=1) noexcept
Grow on the low end by n_cell cells in direction idir (negative shrinks).
Definition AMReX_Box.H:690
__host__ __device__ IndexTypeND< dim > ixType() const noexcept
Return the indexing type.
Definition AMReX_Box.H:148
__host__ __device__ BoxND & growHi(int idir, int n_cell=1) noexcept
Grow on the high end by n_cell cells in direction idir (negative shrinks).
Definition AMReX_Box.H:702
__host__ __device__ const IntVectND< dim > & smallEnd() const &noexcept
Return the inclusive lower bound of the box.
Definition AMReX_Box.H:124
Calculates the distribution of FABs to MPI processes.
Definition AMReX_DistributionMapping.H:51
Long size() const noexcept
Length of the underlying processor map.
Definition AMReX_DistributionMapping.H:169
Convolution-based solver for open boundary conditions using Green's functions.
Definition AMReX_FFT_OpenBCSolver.H:26
3D Poisson solver for periodic, Dirichlet & Neumann boundaries in the first two dimensions,...
Definition AMReX_FFT_Poisson.H:164
Poisson solver for periodic, Dirichlet & Neumann boundaries using FFT.
Definition AMReX_FFT_Poisson.H:32
Parallel Discrete Fourier Transform.
Definition AMReX_FFT_R2C.H:48
std::conditional_t< C, cMF, std::conditional_t< std::is_same_v< T, Real >, MultiFab, FabArray< BaseFab< T > > > > MF
Definition AMReX_FFT_R2C.H:53
R2C & operator=(R2C const &)=delete
~R2C()
Definition AMReX_FFT_R2C.H:753
void setLocalDomain(std::array< int, 3 > const &local_start, std::array< int, 3 > const &local_size)
Set local domain.
Definition AMReX_FFT_R2C.H:802
R2C(Box const &domain, Info const &info=Info{})
Constructor.
Definition AMReX_FFT_R2C.H:505
void post_forward_doit_1(F const &post_forward)
CUDA-visible helper that redistributes and applies post_forward for the batched layout.
Definition AMReX_FFT_R2C.H:1329
T scalingFactor() const
Scaling factor. If the data goes through forward and then backward, the result multiplied by the scal...
Definition AMReX_FFT_R2C.H:1374
std::pair< cMF *, IntVect > getSpectralData() const
Get the internal spectral data.
void post_forward_doit_0(F const &post_forward)
CUDA-visible hook that walks internal spectral data and applies post_forward.
Definition AMReX_FFT_R2C.H:1269
std::pair< std::array< int, 3 >, std::array< int, 3 > > getLocalDomain() const
Get local domain.
Definition AMReX_FFT_R2C.H:811
std::pair< BoxArray, DistributionMapping > getSpectralDataLayout() const
Get BoxArray and DistributionMapping for spectral data.
Definition AMReX_FFT_R2C.H:1552
void forwardThenBackward(MF const &inmf, MF &outmf, F const &post_forward, int incomp=0, int outcomp=0)
Forward and then backward transform.
Definition AMReX_FFT_R2C.H:211
R2C(std::array< int, 3 > const &domain_size, Info const &info=Info{})
Constructor.
Definition AMReX_FFT_R2C.H:748
FabArray< BaseFab< GpuComplex< T > > > cMF
Definition AMReX_FFT_R2C.H:50
std::pair< std::array< int, 3 >, std::array< int, 3 > > getLocalSpectralDomain() const
Get local spectral domain.
Definition AMReX_FFT_R2C.H:848
void setLocalSpectralDomain(std::array< int, 3 > const &local_start, std::array< int, 3 > const &local_size)
Set local spectral domain.
Definition AMReX_FFT_R2C.H:839
IntVect nGrowVect() const noexcept
Definition AMReX_FabArrayBase.H:85
int size() const noexcept
Return the number of FABs in the FabArray.
Definition AMReX_FabArrayBase.H:115
const DistributionMapping & DistributionMap() const noexcept
Return constant reference to associated DistributionMapping.
Definition AMReX_FabArrayBase.H:135
bool empty() const noexcept
Definition AMReX_FabArrayBase.H:94
Box fabbox(int K) const noexcept
Return the Kth FABs Box in the FabArray. That is, the region the Kth fab is actually defined on.
Definition AMReX_FabArrayBase.cpp:217
const BoxArray & boxArray() const noexcept
Return a constant reference to the BoxArray that defines the valid region associated with this FabArr...
Definition AMReX_FabArrayBase.H:100
void setFab(int boxno, std::unique_ptr< FAB > elem)
Explicitly set the Kth FAB in the FabArray to point to elem.
Definition AMReX_FabArray.H:2673
void ParallelCopy(const FabArray< FAB > &src, const Periodicity &period=Periodicity::NonPeriodic(), CpOp op=FabArrayBase::COPY)
Definition AMReX_FabArray.H:971
typename std::conditional_t< IsBaseFab< BaseFab< GpuComplex< T > > >::value, BaseFab< GpuComplex< T > >, FABType >::value_type value_type
Definition AMReX_FabArray.H:367
void define(const BoxArray &bxs, const DistributionMapping &dm, int nvar, int ngrow, const MFInfo &info=MFInfo(), const FabFactory< FAB > &factory=DefaultFabFactory< FAB >())
Define this FabArray identically to that performed by the constructor having an analogous function si...
Definition AMReX_FabArray.H:2489
void LocalCopy(FabArray< SFAB > const &src, int scomp, int dcomp, int ncomp, IntVect const &nghost)
Perform local copy of FabArray data.
Definition AMReX_FabArray.H:2265
A collection (stored as an array) of FArrayBox objects.
Definition AMReX_MultiFab.H:40
This provides length of period for periodic domains. 0 means it is not periodic in that direction....
Definition AMReX_Periodicity.H:17
static const Periodicity & NonPeriodic() noexcept
Definition AMReX_Periodicity.cpp:52
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:29
amrex_long Long
Definition AMReX_INT.H:30
void ParallelForOMP(T n, L const &f) noexcept
Performance-portable kernel launch function with optional OpenMP threading.
Definition AMReX_GpuLaunch.H:328
bool is_aligned(const void *p, std::size_t alignment) noexcept
Definition AMReX_Arena.H:45
Arena * The_Arena()
Definition AMReX_Arena.cpp:815
int NProcs() noexcept
Definition AMReX_ParallelDescriptor.H:255
__host__ __device__ constexpr T elemwiseMin(T const &a, T const &b) noexcept
Definition AMReX_Algorithm.H:73
Definition AMReX_FFT_Helper.H:53
void dtod_memcpy_async(void *p_d_dst, const void *p_d_src, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:449
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:310
MPI_Comm CommunicatorSub() noexcept
sub-communicator for current frame
Definition AMReX_ParallelContext.H:70
int MyProcSub() noexcept
my sub-rank in current frame
Definition AMReX_ParallelContext.H:76
int local_to_global_rank(int rank) noexcept
translate between local rank and global rank
Definition AMReX_ParallelContext.H:98
int NProcsSub() noexcept
number of ranks in current frame
Definition AMReX_ParallelContext.H:74
__host__ __device__ void ignore_unused(const Ts &...)
No-op helper that marks variables as intentionally unused.
Definition AMReX.H:259
BoxArray const & boxArray(FabArrayBase const &fa)
Convenience wrapper that forwards to fa.boxArray().
Definition AMReX_FabArrayBase.cpp:2870
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:35
IndexTypeND< 3 > IndexType
IndexType is an alias for amrex::IndexTypeND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:41
void ParallelCopy(MF &dst, MF const &src, int scomp, int dcomp, int ncomp, IntVect const &ng_src=IntVect(0), IntVect const &ng_dst=IntVect(0), Periodicity const &period=Periodicity::NonPeriodic())
dst = src w/ MPI communication
Definition AMReX_FabArrayUtility.H:2251
BoxArray decompose(Box const &domain, int nboxes, Array< bool, 3 > const &decomp, bool no_overlap)
Decompose domain box into BoxArray.
Definition AMReX_BoxArray.cpp:1961
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:38
void Abort(const std::string &msg)
Print a fatal-error message to stderr and abort execution.
Definition AMReX.cpp:242
Definition AMReX_FFT_Helper.H:83
bool twod_mode
Definition AMReX_FFT_Helper.H:94
bool oned_mode
Definition AMReX_FFT_Helper.H:103
int batch_size
Batched FFT size. Only support in R2C, not R2X.
Definition AMReX_FFT_Helper.H:106
DomainStrategy domain_strategy
Domain composition strategy.
Definition AMReX_FFT_Helper.H:85
int nprocs
Max number of processes to use.
Definition AMReX_FFT_Helper.H:109
int pencil_threshold
Definition AMReX_FFT_Helper.H:89
Definition AMReX_FFT_Helper.H:360
std::conditional_t< std::is_same_v< float, T >, cuComplex, cuDoubleComplex > VendorComplex
Definition AMReX_FFT_Helper.H:364
FabArray memory allocation information.
Definition AMReX_FabArray.H:73
MFInfo & SetAlloc(bool a) noexcept
Control whether FAB storage is allocated when the FabArray is defined.
Definition AMReX_FabArray.H:81
This class specializes behaviour on local copies and unpacking receive buffers.
Definition AMReX_NonLocalBC.H:642
Contains information about which components take part of the data transaction.
Definition AMReX_NonLocalBC.H:550
int n_components
Definition AMReX_NonLocalBC.H:553
Communication datatype (note: this structure also works without MPI)
Definition AMReX_ccse-mpi.H:78