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_rx.nComp(), 0,
MFInfo().SetAlloc(
false));
809template <
typename T, Direction D,
bool C>
810std::pair<std::array<int,AMREX_SPACEDIM>,std::array<int,AMREX_SPACEDIM>>
813 m_raw_mf =
MF(m_rx.boxArray(), m_rx.DistributionMap(), m_rx.nComp(), 0,
817 if (myproc < m_rx.size()) {
818 Box const& box = m_rx.box(myproc);
819 return std::make_pair(box.
smallEnd().toArray(),
822 return std::make_pair(std::array<int,AMREX_SPACEDIM>{
AMREX_D_DECL(0,0,0)},
827template <
typename T, Direction D,
bool C>
829 std::array<int,AMREX_SPACEDIM>
const& local_size)
831 auto const& [ba, dm] = make_layout_from_local_domain(local_start, local_size);
832 m_raw_cmf =
cMF(ba, dm, m_rx.nComp(), 0,
MFInfo().SetAlloc(
false));
835template <
typename T, Direction D,
bool C>
836std::pair<std::array<int,AMREX_SPACEDIM>,std::array<int,AMREX_SPACEDIM>>
839 auto const ncomp = m_info.batch_size;
840 auto const& [ba, dm] = getSpectralDataLayout();
845 if (myproc < m_raw_cmf.size()) {
846 Box const& box = m_raw_cmf.box(myproc);
847 return std::make_pair(box.
smallEnd().toArray(), box.
length().toArray());
849 return std::make_pair(std::array<int,AMREX_SPACEDIM>{
AMREX_D_DECL(0,0,0)},
854template <
typename T, Direction D,
bool C>
857 if (
C || m_r2c_sub) {
amrex::Abort(
"R2C: OpenBC not supported with reduced dimensions or complex inputs"); }
859#if (AMREX_SPACEDIM == 3)
860 if (m_do_alld_fft) {
return; }
862 auto const ncomp = m_info.batch_size;
864 if (m_slab_decomp && ! m_fft_fwd_x_half.defined) {
865 auto* fab = detail::get_fab(m_rx);
867 Box bottom_half = m_real_domain;
868 bottom_half.
growHi(2,-m_real_domain.length(2)/2);
869 Box box = fab->box() & bottom_half;
871 auto* pr = fab->dataPtr();
873 detail::get_fab(m_cx)->dataPtr();
875 m_fft_fwd_x_half.template init_r2c<Direction::forward>
876 (box, pr, pc, m_slab_decomp, ncomp);
877 m_fft_bwd_x_half = m_fft_fwd_x_half;
880 m_fft_fwd_x_half.template init_r2c<Direction::forward>
881 (box, pr, pc, m_slab_decomp, ncomp);
884 m_fft_bwd_x_half.template init_r2c<Direction::backward>
885 (box, pr, pc, m_slab_decomp, ncomp);
892 if (m_cmd_x2z && ! m_cmd_x2z_half) {
893 Box bottom_half = m_spectral_domain_z;
896 bottom_half.
growHi(0,-m_spectral_domain_z.length(0)/2);
897 m_cmd_x2z_half = std::make_unique<MultiBlockCommMetaData>
898 (m_cz, bottom_half, m_cx,
IntVect(0), m_dtos_x2z);
901 if (m_cmd_z2x && ! m_cmd_z2x_half) {
902 Box bottom_half = m_spectral_domain_x;
903 bottom_half.
growHi(2,-m_spectral_domain_x.length(2)/2);
904 m_cmd_z2x_half = std::make_unique<MultiBlockCommMetaData>
905 (m_cx, bottom_half, m_cz,
IntVect(0), m_dtos_z2x);
910template <
typename T, Direction D,
bool C>
912R2C<T,D,C>::make_m_rx_alias (
IndexType const& ix_type)
914 auto const key = ix_type.ixType();
915 auto it = m_rx_aliases.find(key);
916 if (it != m_rx_aliases.end()) {
921 for (
auto b : m_rx.
boxArray().boxList()) {
926 MF alias(BoxArray(std::move(bl)), m_rx.DistributionMap(), m_rx.nComp(),
927 m_rx.nGrowVect(), MFInfo{}.SetAlloc(
false));
929 using FAB =
typename MF::fab_type;
930 for (MFIter mfi(alias, MFItInfo().DisableDeviceSync()); mfi.isValid(); ++mfi) {
931 alias.setFab(mfi, FAB(mfi.fabbox(), m_rx.nComp(), m_rx[mfi.index()].dataPtr()));
934 auto [alias_it, inserted] = m_rx_aliases.emplace(key, std::move(alias));
936 return alias_it->second;
939template <
typename T, Direction D,
bool C>
941R2C<T,D,C>::make_m_rx_alias_periodicity (
IndexType const& ix_type,
942 Periodicity
const& period)
const
944 IntVect per = period.intVect();
945 for (
int idim = 0; idim < AMREX_SPACEDIM; ++idim) {
946 if (ix_type.nodeCentered(idim) && per[idim] == 0) {
947 per[idim] = m_real_domain.length(idim);
950 return Periodicity(per);
953template <
typename T, Direction D,
bool C>
954template <Direction DIR>
960 auto const ncomp = m_info.batch_size;
963 if (m_sub_helper.ghost_safe(inmf.nGrowVect())) {
964 m_r2c_sub->forward(m_sub_helper.make_alias_mf(inmf), incomp);
966 MF tmp(inmf.boxArray(), inmf.DistributionMap(), ncomp, 0);
967 tmp.LocalCopy(inmf, incomp, 0, ncomp,
IntVect(0));
968 m_r2c_sub->forward(m_sub_helper.make_alias_mf(tmp),0);
973 if (&m_rx != &inmf) {
974 if (m_rx.boxArray().ixType() == inmf.boxArray().ixType()) {
975 m_rx.ParallelCopy(inmf, incomp, 0, ncomp);
977 MF& rx_alias = make_m_rx_alias(inmf.boxArray().ixType());
978 rx_alias.ParallelCopy(inmf, incomp, 0, ncomp);
984 m_fft_fwd_x.template compute_c2c<Direction::forward>();
986 m_fft_fwd_x.template compute_r2c<Direction::forward>();
991 auto& fft_x = m_openbc_half ? m_fft_fwd_x_half : m_fft_fwd_x;
993 fft_x.template compute_c2c<Direction::forward>();
995 fft_x.template compute_r2c<Direction::forward>();
999 ParallelCopy(m_cy, m_cx, *m_cmd_x2y, 0, 0, ncomp, m_dtos_x2y);
1001 m_fft_fwd_y.template compute_c2c<Direction::forward>();
1004 ParallelCopy(m_cz, m_cy, *m_cmd_y2z, 0, 0, ncomp, m_dtos_y2z);
1006#if (AMREX_SPACEDIM == 3)
1007 else if ( m_cmd_x2z) {
1008 if (m_openbc_half) {
1012 {components, m_dtos_x2z};
1013 auto handler = ParallelCopy_nowait(m_cz, m_cx, *m_cmd_x2z_half, packing);
1015 Box upper_half = m_spectral_domain_z;
1018 upper_half.
growLo (0,-m_spectral_domain_z.length(0)/2);
1019 m_cz.setVal(0, upper_half, 0, ncomp);
1021 ParallelCopy_finish(m_cz, std::move(handler), *m_cmd_x2z_half, packing);
1023 ParallelCopy(m_cz, m_cx, *m_cmd_x2z, 0, 0, ncomp, m_dtos_x2z);
1027 m_fft_fwd_z.template compute_c2c<Direction::forward>();
1030template <
typename T, Direction D,
bool C>
1031template <
typename FA,
typename RT>
1032std::pair<std::unique_ptr<char,DataDeleter>,std::size_t>
1037 using FAB =
typename FA::FABType::value_type;
1038 using T_FAB =
typename FAB::value_type;
1039 static_assert(
sizeof(T_FAB) ==
sizeof(RT));
1041 auto const ncomp = m_info.batch_size;
1042 auto const& ia = fa.IndexArray();
1044 T_FAB*
pp =
nullptr;
1047 if ( ! ia.empty() ) {
1049 Box const& box = fa.fabbox(K);
1053 sz =
sizeof(T_FAB) * box.
numPts() * ncomp;
1056 fa.setFab(K, FAB(box,ncomp,
pp));
1060 return std::make_pair(std::unique_ptr<char,DataDeleter>{},std::size_t(0));
1062 return std::make_pair(std::unique_ptr<char,DataDeleter>
1068template <
typename T, Direction D,
bool C>
1069template <
typename RT,
typename CT, Direction DIR,
bool CP>
1071 && (((
sizeof(RT)*2 ==
sizeof(CT)) && (!CP))
1072 || ((
sizeof(RT) ==
sizeof(CT)) && CP))
1075 auto [rdata, rsz] = install_raw_ptr(m_raw_mf, in);
1076 auto [cdata, csz] = install_raw_ptr(m_raw_cmf, out);
1091template <
typename T, Direction D,
bool C>
1092template <Direction DIR>
1099template <
typename T, Direction D,
bool C>
1105 auto const ncomp = m_info.batch_size;
1108 if (m_sub_helper.ghost_safe(outmf.nGrowVect())) {
1109 MF submf = m_sub_helper.make_alias_mf(outmf);
1110 IntVect const& subngout = m_sub_helper.make_iv(ngout);
1111 Periodicity const& subperiod = m_sub_helper.make_periodicity(period);
1112 m_r2c_sub->backward_doit(submf, subngout, subperiod, outcomp);
1114 MF tmp(outmf.boxArray(), outmf.DistributionMap(), ncomp,
1115 m_sub_helper.make_safe_ghost(outmf.nGrowVect()));
1116 this->backward_doit(tmp, ngout, period, 0);
1117 outmf.LocalCopy(tmp, 0, outcomp, ncomp, tmp.nGrowVect());
1122 if (m_do_alld_fft) {
1124 m_fft_bwd_x.template compute_c2c<Direction::backward>();
1126 m_fft_bwd_x.template compute_r2c<Direction::backward>();
1129 if (m_rx.boxArray().ixType() == outmf.boxArray().ixType()) {
1130 outmf.ParallelCopy(m_rx, 0, outcomp, ncomp,
IntVect(0),
1131 dst_nghost, period);
1133 MF& rx_alias = make_m_rx_alias(outmf.boxArray().ixType());
1134 auto const copy_period = make_m_rx_alias_periodicity(outmf.boxArray().ixType(), period);
1135 outmf.ParallelCopy(rx_alias, 0, outcomp, ncomp,
IntVect(0),
1136 dst_nghost, copy_period);
1141 m_fft_bwd_z.template compute_c2c<Direction::backward>();
1143 ParallelCopy(m_cy, m_cz, *m_cmd_z2y, 0, 0, ncomp, m_dtos_z2y);
1145#if (AMREX_SPACEDIM == 3)
1146 else if ( m_cmd_z2x) {
1147 auto const& cmd = m_openbc_half ? m_cmd_z2x_half : m_cmd_z2x;
1148 ParallelCopy(m_cx, m_cz, *cmd, 0, 0, ncomp, m_dtos_z2x);
1152 m_fft_bwd_y.template compute_c2c<Direction::backward>();
1154 ParallelCopy(m_cx, m_cy, *m_cmd_y2x, 0, 0, ncomp, m_dtos_y2x);
1157 auto& fft_x = m_openbc_half ? m_fft_bwd_x_half : m_fft_bwd_x;
1159 fft_x.template compute_c2c<Direction::backward>();
1161 fft_x.template compute_r2c<Direction::backward>();
1164 if (m_rx.boxArray().ixType() == outmf.boxArray().ixType()) {
1165 outmf.ParallelCopy(m_rx, 0, outcomp, ncomp,
IntVect(0),
1166 dst_nghost, period);
1168 MF& rx_alias = make_m_rx_alias(outmf.boxArray().ixType());
1169 auto const copy_period = make_m_rx_alias_periodicity(outmf.boxArray().ixType(), period);
1170 outmf.ParallelCopy(rx_alias, 0, outcomp, ncomp,
IntVect(0),
1171 dst_nghost, copy_period);
1175template <
typename T, Direction D,
bool C>
1176template <
typename CT,
typename RT, Direction DIR,
bool CP>
1178 && (((
sizeof(RT)*2 ==
sizeof(CT)) && (!CP))
1179 || ((
sizeof(RT) ==
sizeof(CT)) && CP))
1182 auto [rdata, rsz] = install_raw_ptr(m_raw_mf, out);
1183 auto [cdata, csz] = install_raw_ptr(m_raw_cmf, in);
1198template <
typename T, Direction D,
bool C>
1205 auto* fab = detail::get_fab(inout);
1206 if (!fab) {
return {fwd, bwd};}
1208 Box const& box = fab->box();
1211 auto const ncomp = m_info.batch_size;
1213#ifdef AMREX_USE_SYCL
1214 fwd.template init_c2c<Direction::forward>(box, pio, ncomp, ndims);
1218 fwd.template init_c2c<Direction::forward>(box, pio, ncomp, ndims);
1221 bwd.template init_c2c<Direction::backward>(box, pio, ncomp, ndims);
1228namespace fft_detail {
1235 template <
typename F,
typename T>
1237 auto call_post_forward (
F const& f,
int i,
int j,
int k, Array4<T>
const& a,
1238 int ii,
int jj,
int kk,
int)
1239 noexcept ->
decltype(void(f(0,0,0,a(0,0,0))))
1241 for (
int n = 0; n < a.nComp(); ++n) {
1242 f(i,j,k,a(ii,jj,kk,n));
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,
long)
1250 noexcept ->
decltype(void(f(0,0,0,a.cellData(0,0,0))))
1252 f(i,j,k,a.cellData(ii,jj,kk));
1256template <
typename T, Direction D,
bool C>
1257template <
typename F>
1260 if (m_info.twod_mode) {
1262#if (AMREX_SPACEDIM > 1)
1263 }
else if (m_r2c_sub) {
1265#if (AMREX_SPACEDIM == 2)
1267 m_r2c_sub->post_forward_doit_1
1269 ->
decltype(
void(post_forward(0,0,0,
static_cast<decltype(sp)
>(sp))))
1271 post_forward(0, i, 0,
static_cast<decltype(sp)
>(sp));
1274 if (m_real_domain.length(0) == 1 && m_real_domain.length(1) == 1) {
1276 m_r2c_sub->post_forward_doit_1
1278 ->
decltype(void(post_forward(0,0,0,
static_cast<decltype(sp)
>(sp))))
1280 post_forward(0, 0, i,
static_cast<decltype(sp)
>(sp));
1282 }
else if (m_real_domain.length(0) == 1 && m_real_domain.length(2) == 1) {
1284 m_r2c_sub->post_forward_doit_1
1286 ->
decltype(void(post_forward(0,0,0,
static_cast<decltype(sp)
>(sp))))
1288 post_forward(0, i, 0,
static_cast<decltype(sp)
>(sp));
1290 }
else if (m_real_domain.length(0) == 1) {
1292 m_r2c_sub->post_forward_doit_1
1294 ->
decltype(void(post_forward(0,0,0,
static_cast<decltype(sp)
>(sp))))
1296 post_forward(0, i, j,
static_cast<decltype(sp)
>(sp));
1298 }
else if (m_real_domain.length(1) == 1) {
1300 m_r2c_sub->post_forward_doit_1
1302 ->
decltype(void(post_forward(0,0,0,
static_cast<decltype(sp)
>(sp))))
1304 post_forward(i, 0, j,
static_cast<decltype(sp)
>(sp));
1307 amrex::Abort(
"R2c::post_forward_doit_0: how did this happen?");
1312 this->post_forward_doit_1(post_forward);
1316template <
typename T, Direction D,
bool C>
1317template <
typename F>
1320 if (m_info.twod_mode) {
1322 }
else if (m_r2c_sub) {
1323 amrex::Abort(
"R2C::post_forward_doit_1: How did this happen?");
1325 if ( ! m_cz.empty()) {
1326 auto* spectral_fab = detail::get_fab(m_cz);
1328 auto const& a = spectral_fab->array();
1332 fft_detail::call_post_forward(post_forward,
1333 jx,ky,iz,a,iz,jx,ky,0);
1336 }
else if ( ! m_cy.empty()) {
1337 auto* spectral_fab = detail::get_fab(m_cy);
1339 auto const& a = spectral_fab->array();
1343 fft_detail::call_post_forward(post_forward,
1344 jx,iy,k,a,iy,jx,k,0);
1348 auto* spectral_fab = detail::get_fab(m_cx);
1350 auto const& a = spectral_fab->array();
1354 fft_detail::call_post_forward(post_forward,
1362template <
typename T, Direction D,
bool C>
1365#if (AMREX_SPACEDIM == 3)
1366 if (m_info.oned_mode && !m_info.twod_mode) {
1367 return T(1)/T(
Long(m_real_domain.length(0)));
1368 }
else if (m_info.twod_mode) {
1369 return T(1)/T(
Long(m_real_domain.length(0)) *
1370 Long(m_real_domain.length(1)));
1372#elif (AMREX_SPACEDIM == 2)
1373 if (m_info.oned_mode) {
1374 return T(1)/T(
Long(m_real_domain.length(0)));
1378 return T(1)/T(m_real_domain.numPts());
1382template <
typename T, Direction D,
bool C>
1383template <Direction DIR>
1388#if (AMREX_SPACEDIM > 1)
1391 return std::make_pair(cmf, m_sub_helper.inverse_order(order));
1394 if (!m_cz.empty()) {
1396 }
else if (!m_cy.empty()) {
1403template <
typename T, Direction D,
bool C>
1404template <Direction DIR>
1410 auto const ncomp = m_info.batch_size;
1414 bool inmf_safe = m_sub_helper.ghost_safe(inmf.nGrowVect());
1415 MF inmf_sub, inmf_tmp;
1418 inmf_sub = m_sub_helper.make_alias_mf(inmf);
1419 incomp_sub = incomp;
1421 inmf_tmp.define(inmf.boxArray(), inmf.DistributionMap(), ncomp, 0);
1422 inmf_tmp.LocalCopy(inmf, incomp, 0, ncomp,
IntVect(0));
1423 inmf_sub = m_sub_helper.make_alias_mf(inmf_tmp);
1427 bool outmf_safe = m_sub_helper.ghost_safe(outmf.
nGrowVect());
1428 cMF outmf_sub, outmf_tmp;
1431 outmf_sub = m_sub_helper.make_alias_mf(outmf);
1432 outcomp_sub = outcomp;
1435 outmf_sub = m_sub_helper.make_alias_mf(outmf_tmp);
1439 m_r2c_sub->forward(inmf_sub, outmf_sub, incomp_sub, outcomp_sub);
1448 if (!m_cz.empty()) {
1451 (outmf, m_spectral_domain_x, m_cz,
IntVect(0), dtos);
1452 ParallelCopy(outmf, m_cz, cmd, 0, outcomp, ncomp, dtos);
1453 }
else if (!m_cy.empty()) {
1455 (outmf, m_spectral_domain_x, m_cy,
IntVect(0), m_dtos_y2x);
1456 ParallelCopy(outmf, m_cy, cmd, 0, outcomp, ncomp, m_dtos_y2x);
1463template <
typename T, Direction D,
bool C>
1464template <Direction DIR>
1471template <
typename T, Direction D,
bool C>
1473 Periodicity const& period,
int incomp,
int outcomp)
1477 auto const ncomp = m_info.batch_size;
1481 bool inmf_safe = m_sub_helper.ghost_safe(inmf.nGrowVect());
1482 cMF inmf_sub, inmf_tmp;
1485 inmf_sub = m_sub_helper.make_alias_mf(inmf);
1486 incomp_sub = incomp;
1488 inmf_tmp.define(inmf.boxArray(), inmf.DistributionMap(), ncomp, 0);
1489 inmf_tmp.LocalCopy(inmf, incomp, 0, ncomp,
IntVect(0));
1490 inmf_sub = m_sub_helper.make_alias_mf(inmf_tmp);
1494 bool outmf_safe = m_sub_helper.ghost_safe(outmf.nGrowVect());
1495 MF outmf_sub, outmf_tmp;
1498 outmf_sub = m_sub_helper.make_alias_mf(outmf);
1499 outcomp_sub = outcomp;
1501 IntVect const& ngtmp = m_sub_helper.make_safe_ghost(outmf.nGrowVect());
1502 outmf_tmp.define(outmf.boxArray(), outmf.DistributionMap(), ncomp, ngtmp);
1503 outmf_sub = m_sub_helper.make_alias_mf(outmf_tmp);
1507 IntVect const& subngout = m_sub_helper.make_iv(ngout);
1508 Periodicity
const& subperiod = m_sub_helper.make_periodicity(period);
1509 m_r2c_sub->backward_doit(inmf_sub, outmf_sub, subngout, subperiod, incomp_sub, outcomp_sub);
1512 outmf.LocalCopy(outmf_tmp, 0, outcomp, ncomp, outmf_tmp.nGrowVect());
1517 if (!m_cz.empty()) {
1519 MultiBlockCommMetaData cmd
1520 (m_cz, m_spectral_domain_z, inmf,
IntVect(0), dtos);
1522 }
else if (!m_cy.empty()) {
1523 MultiBlockCommMetaData cmd
1524 (m_cy, m_spectral_domain_y, inmf,
IntVect(0), m_dtos_x2y);
1525 ParallelCopy(m_cy, inmf, cmd, incomp, 0, ncomp, m_dtos_x2y);
1527 m_cx.ParallelCopy(inmf, incomp, 0, ncomp);
1529 backward_doit(outmf, ngout, period, outcomp);
1533template <
typename T, Direction D,
bool C>
1534std::pair<BoxArray,DistributionMapping>
1537#if (AMREX_SPACEDIM > 1)
1539 auto const& [ba, dm] = m_r2c_sub->getSpectralDataLayout();
1540 return std::make_pair(m_sub_helper.inverse_boxarray(ba), dm);
1544#if (AMREX_SPACEDIM == 3)
1545 if (!m_cz.empty()) {
1546 BoxList bl = m_cz.boxArray().boxList();
1547 for (
auto& b : bl) {
1548 auto lo = b.smallEnd();
1549 auto hi = b.bigEnd();
1550 std::swap(lo[0], lo[1]);
1551 std::swap(lo[1], lo[2]);
1552 std::swap(hi[0], hi[1]);
1553 std::swap(hi[1], hi[2]);
1557 return std::make_pair(
BoxArray(std::move(bl)), m_cz.DistributionMap());
1560#if (AMREX_SPACEDIM >= 2)
1561 if (!m_cy.empty()) {
1562 BoxList bl = m_cy.boxArray().boxList();
1563 for (
auto& b : bl) {
1564 auto lo = b.smallEnd();
1565 auto hi = b.bigEnd();
1566 std::swap(lo[0], lo[1]);
1567 std::swap(hi[0], hi[1]);
1571 return std::make_pair(
BoxArray(std::move(bl)), m_cy.DistributionMap());
1575 return std::make_pair(m_cx.boxArray(), m_cx.DistributionMap());
1580template <
typename T = Real, FFT::Direction D = FFT::Direction::both>
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:551
#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:676
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:563
__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:1318
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:1363
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:1258
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:1535
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:837
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:828
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:2642
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:2458
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:2234
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:820
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:95
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:2862
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:2235
BoxArray decompose(Box const &domain, int nboxes, Array< bool, 3 > const &decomp, bool no_overlap)
Decompose domain box into BoxArray.
Definition AMReX_BoxArray.cpp:1943
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