1#ifndef AMREX_FFT_R2C_H_
2#define AMREX_FFT_R2C_H_
3#include <AMReX_Config.H>
20template <
typename T>
class OpenBCSolver;
21template <
typename T>
class Poisson;
22template <
typename T>
class PoissonHybrid;
45template <
typename T = Real, FFT::Direction D = FFT::Direction::both,
bool C = false>
50 using MF = std::conditional_t
51 <
C,
cMF, std::conditional_t<std::is_same_v<T,Real>,
55 template <
typename U>
friend class Poisson;
75 explicit R2C (std::array<int,AMREX_SPACEDIM>
const& domain_size,
109 std::array<int,AMREX_SPACEDIM>
const& local_size);
124 std::pair<std::array<int,AMREX_SPACEDIM>,std::array<int,AMREX_SPACEDIM>>
156 std::array<int,AMREX_SPACEDIM>
const& local_size);
176 std::pair<std::array<int,AMREX_SPACEDIM>,std::array<int,AMREX_SPACEDIM>>
201 std::enable_if_t<DIR == Direction::both, int> = 0>
203 int incomp = 0,
int outcomp = 0)
207 "FFT::R2C::forwardThenBackward(post_forward) currently supports only !twod_mode and batch_size==1");
241 void forward (
MF const& inmf,
cMF& outmf,
int incomp = 0,
int outcomp = 0);
259 template <
typename RT,
typename CT,
Direction DIR=D,
bool CP=
C,
262 && ((
sizeof(RT)*2 ==
sizeof(CT) && !CP) ||
263 (
sizeof(RT) ==
sizeof(CT) && CP)),
int> = 0>
275 template <Direction DIR=D, std::enable_if_t<DIR == Direction::both,
int> = 0>
291 void backward (
cMF const& inmf,
MF& outmf,
int incomp = 0,
int outcomp = 0);
309 template <
typename CT,
typename RT,
Direction DIR=D,
bool CP=
C,
312 && ((
sizeof(RT)*2 ==
sizeof(CT) && !CP) ||
313 (
sizeof(RT) ==
sizeof(CT) && CP)),
int> = 0>
356 template <
typename F>
359 template <
typename F>
369 void prepare_openbc ();
375 void backward_doit (
cMF const& inmf,
MF& outmf,
378 int incomp = 0,
int outcomp = 0);
380 std::pair<Plan<T>,
Plan<T>> make_c2c_plans (
cMF& inout,
int ndims)
const;
382 static Box make_domain_x (
Box const& domain)
397 static Box make_domain_y (
Box const& domain)
402 domain.length(2)-1)),
407 domain.length(2)-1)),
412 static Box make_domain_z (
Box const& domain)
417 domain.length(1)-1)),
422 domain.length(1)-1)),
427 static std::pair<BoxArray,DistributionMapping>
428 make_layout_from_local_domain (std::array<int,AMREX_SPACEDIM>
const& local_start,
429 std::array<int,AMREX_SPACEDIM>
const& local_size);
431 template <
typename FA,
typename RT>
432 std::pair<std::unique_ptr<char,DataDeleter>,std::size_t>
433 install_raw_ptr (FA& fa, RT
const* p);
435 Plan<T> m_fft_fwd_x{};
436 Plan<T> m_fft_bwd_x{};
437 Plan<T> m_fft_fwd_y{};
438 Plan<T> m_fft_bwd_y{};
439 Plan<T> m_fft_fwd_z{};
440 Plan<T> m_fft_bwd_z{};
441 Plan<T> m_fft_fwd_x_half{};
442 Plan<T> m_fft_bwd_x_half{};
447 std::unique_ptr<MultiBlockCommMetaData> m_cmd_x2y;
448 std::unique_ptr<MultiBlockCommMetaData> m_cmd_y2x;
449 std::unique_ptr<MultiBlockCommMetaData> m_cmd_y2z;
450 std::unique_ptr<MultiBlockCommMetaData> m_cmd_z2y;
451 std::unique_ptr<MultiBlockCommMetaData> m_cmd_x2z;
452 std::unique_ptr<MultiBlockCommMetaData> m_cmd_z2x;
453 std::unique_ptr<MultiBlockCommMetaData> m_cmd_x2z_half;
454 std::unique_ptr<MultiBlockCommMetaData> m_cmd_z2x_half;
459 RotateFwd m_dtos_x2z{};
460 RotateBwd m_dtos_z2x{};
468 mutable cMF m_raw_cmf;
470 std::unique_ptr<char,DataDeleter> m_data_1;
471 std::unique_ptr<char,DataDeleter> m_data_2;
474 Box m_spectral_domain_x;
475 Box m_spectral_domain_y;
476 Box m_spectral_domain_z;
478 std::unique_ptr<R2C<T,D,C>> m_r2c_sub;
479 detail::SubHelper m_sub_helper;
483 bool m_do_alld_fft =
false;
484 bool m_slab_decomp =
false;
485 bool m_openbc_half =
false;
488template <
typename T, Direction D,
bool C>
490 : m_real_domain(domain),
491 m_spectral_domain_x(make_domain_x(domain)),
492#if (AMREX_SPACEDIM >= 2)
493 m_spectral_domain_y(make_domain_y(domain)),
494#if (AMREX_SPACEDIM == 3)
495 m_spectral_domain_z(make_domain_z(domain)),
498 m_sub_helper(domain),
503 static_assert(std::is_same_v<float,T> || std::is_same_v<double,T>);
506#if (AMREX_SPACEDIM == 2)
511 int(domain.
length(1) > 1) +
512 int(domain.
length(2) > 1)) >= 2);
517 Box subbox = m_sub_helper.make_box(m_real_domain);
518 if (subbox.
size() != m_real_domain.
size()) {
519 m_r2c_sub = std::make_unique<R2C<T,D,C>>(subbox, m_info);
527#if (AMREX_SPACEDIM == 3)
532 int shortside = m_real_domain.
shortside();
543 m_slab_decomp =
true;
545 m_slab_decomp =
true;
557 m_rx.define(bax, dmx, ncomp, 0,
MFInfo().SetAlloc(
false));
561 for (
auto & b : bl) {
563 b.setBig(0, m_spectral_domain_x.
bigEnd(0));
566 m_cx.
define(cbax, dmx, ncomp, 0,
MFInfo().SetAlloc(
false));
577#if (AMREX_SPACEDIM >= 2)
579 if ((m_real_domain.
length(1) > 1) && !m_slab_decomp && !m_info.
oned_mode)
583 if (cbay.size() == dmx.size()) {
586 cdmy = detail::make_iota_distromap(cbay.size());
588 m_cy.
define(cbay, cdmy, ncomp, 0,
MFInfo().SetAlloc(
false));
592#if (AMREX_SPACEDIM == 3)
593 if (m_real_domain.
length(1) > 1 &&
597 {
false,
true,
true},
true);
599 if (cbaz.size() == dmx.size()) {
601 }
else if (cbaz.size() == cdmy.
size()) {
604 cdmz = detail::make_iota_distromap(cbaz.size());
606 m_cz.
define(cbaz, cdmz, ncomp, 0,
MFInfo().SetAlloc(
false));
612 m_data_1 = detail::make_mfs_share(m_rx, m_cx);
613 m_data_2 = detail::make_mfs_share(m_cz, m_cz);
615 m_data_1 = detail::make_mfs_share(m_rx, m_cz);
616 m_data_2 = detail::make_mfs_share(m_cy, m_cy);
618 if (myproc < m_cx.
size()) {
621 m_cx.
setFab(myproc, FAB(box, ncomp, m_rx[myproc].dataPtr()));
626 m_data_1 = detail::make_mfs_share(m_rx, m_cz);
627 m_data_2 = detail::make_mfs_share(m_cx, m_cx);
629 m_data_1 = detail::make_mfs_share(m_rx, m_cy);
630 m_data_2 = detail::make_mfs_share(m_cx, m_cz);
638#if (AMREX_SPACEDIM >= 2)
639 if (! m_cy.
empty()) {
641 m_cmd_x2y = std::make_unique<MultiBlockCommMetaData>
642 (m_cy, m_spectral_domain_y, m_cx,
IntVect(0), m_dtos_x2y);
643 m_cmd_y2x = std::make_unique<MultiBlockCommMetaData>
644 (m_cx, m_spectral_domain_x, m_cy,
IntVect(0), m_dtos_y2x);
647#if (AMREX_SPACEDIM == 3)
648 if (! m_cz.
empty() ) {
651 m_cmd_x2z = std::make_unique<MultiBlockCommMetaData>
652 (m_cz, m_spectral_domain_z, m_cx,
IntVect(0), m_dtos_x2z);
653 m_cmd_z2x = std::make_unique<MultiBlockCommMetaData>
654 (m_cx, m_spectral_domain_x, m_cz,
IntVect(0), m_dtos_z2x);
657 m_cmd_y2z = std::make_unique<MultiBlockCommMetaData>
658 (m_cz, m_spectral_domain_z, m_cy,
IntVect(0), m_dtos_y2z);
659 m_cmd_z2y = std::make_unique<MultiBlockCommMetaData>
660 (m_cy, m_spectral_domain_y, m_cz,
IntVect(0), m_dtos_z2y);
669 if (myproc < m_rx.size())
672 int ndims = m_slab_decomp ? 2 : 1;
673 std::tie(m_fft_fwd_x, m_fft_bwd_x) = make_c2c_plans(m_cx, ndims);
675 Box const& box = m_rx.box(myproc);
676 auto* pr = m_rx[myproc].dataPtr();
679 m_fft_fwd_x.template init_r2c<Direction::forward>(box, pr, pc, m_slab_decomp, ncomp);
680 m_fft_bwd_x = m_fft_fwd_x;
683 m_fft_fwd_x.template init_r2c<Direction::forward>(box, pr, pc, m_slab_decomp, ncomp);
686 m_fft_bwd_x.template init_r2c<Direction::backward>(box, pr, pc, m_slab_decomp, ncomp);
692#if (AMREX_SPACEDIM >= 2)
693 if (! m_cy.
empty()) {
694 std::tie(m_fft_fwd_y, m_fft_bwd_y) = make_c2c_plans(m_cy,1);
697#if (AMREX_SPACEDIM == 3)
698 if (! m_cz.
empty()) {
699 std::tie(m_fft_fwd_z, m_fft_bwd_z) = make_c2c_plans(m_cz,1);
706 m_data_1 = detail::make_mfs_share(m_rx, m_cx);
707 std::tie(m_fft_fwd_x, m_fft_bwd_x) = make_c2c_plans(m_cx,AMREX_SPACEDIM);
709 m_data_1 = detail::make_mfs_share(m_rx, m_rx);
710 m_data_2 = detail::make_mfs_share(m_cx, m_cx);
712 auto const& len = m_real_domain.
length();
713 auto* pr = (
void*)m_rx[0].dataPtr();
714 auto* pc = (
void*)m_cx[0].dataPtr();
716 m_fft_fwd_x.template init_r2c<Direction::forward>(len, pr, pc,
false, ncomp);
717 m_fft_bwd_x = m_fft_fwd_x;
720 m_fft_fwd_x.template init_r2c<Direction::forward>(len, pr, pc,
false, ncomp);
723 m_fft_bwd_x.template init_r2c<Direction::backward>(len, pr, pc,
false, ncomp);
730template <
typename T, Direction D,
bool C>
735template <
typename T, Direction D,
bool C>
738 if (m_fft_bwd_x.plan != m_fft_fwd_x.plan) {
739 m_fft_bwd_x.destroy();
741 if (m_fft_bwd_y.plan != m_fft_fwd_y.plan) {
742 m_fft_bwd_y.destroy();
744 if (m_fft_bwd_z.plan != m_fft_fwd_z.plan) {
745 m_fft_bwd_z.destroy();
747 m_fft_fwd_x.destroy();
748 m_fft_fwd_y.destroy();
749 m_fft_fwd_z.destroy();
750 if (m_fft_bwd_x_half.plan != m_fft_fwd_x_half.plan) {
751 m_fft_bwd_x_half.destroy();
753 m_fft_fwd_x_half.destroy();
756template <
typename T, Direction D,
bool C>
757std::pair<BoxArray,DistributionMapping>
759 std::array<int,AMREX_SPACEDIM>
const& local_size)
763 Box bx(lo, lo+len-1);
770 pmap.reserve(allboxes.size());
771 for (
int i = 0; i < allboxes.size(); ++i) {
772 if (allboxes[i].ok()) {
776 allboxes.erase(std::remove_if(allboxes.begin(), allboxes.end(),
777 [=] (
Box const& b) { return b.isEmpty(); }),
779 BoxList bl(std::move(allboxes));
780 return std::make_pair(BoxArray(std::move(bl)), DistributionMapping(std::move(pmap)));
782 return std::make_pair(BoxArray(bx), DistributionMapping(Vector<int>({0})));
786template <
typename T, Direction D,
bool C>
788 std::array<int,AMREX_SPACEDIM>
const& local_size)
790 auto const& [ba, dm] = make_layout_from_local_domain(local_start, local_size);
791 m_raw_mf =
MF(ba, dm, m_rx.nComp(), 0,
MFInfo().SetAlloc(
false));
794template <
typename T, Direction D,
bool C>
795std::pair<std::array<int,AMREX_SPACEDIM>,std::array<int,AMREX_SPACEDIM>>
798 m_raw_mf =
MF(m_rx.boxArray(), m_rx.DistributionMap(), m_rx.nComp(), 0,
802 if (myproc < m_rx.size()) {
803 Box const& box = m_rx.box(myproc);
804 return std::make_pair(box.
smallEnd().toArray(),
807 return std::make_pair(std::array<int,AMREX_SPACEDIM>{
AMREX_D_DECL(0,0,0)},
812template <
typename T, Direction D,
bool C>
814 std::array<int,AMREX_SPACEDIM>
const& local_size)
816 auto const& [ba, dm] = make_layout_from_local_domain(local_start, local_size);
817 m_raw_cmf =
cMF(ba, dm, m_rx.nComp(), 0,
MFInfo().SetAlloc(
false));
820template <
typename T, Direction D,
bool C>
821std::pair<std::array<int,AMREX_SPACEDIM>,std::array<int,AMREX_SPACEDIM>>
824 auto const ncomp = m_info.batch_size;
825 auto const& [ba, dm] = getSpectralDataLayout();
830 if (myproc < m_raw_cmf.size()) {
831 Box const& box = m_raw_cmf.box(myproc);
832 return std::make_pair(box.
smallEnd().toArray(), box.
length().toArray());
834 return std::make_pair(std::array<int,AMREX_SPACEDIM>{
AMREX_D_DECL(0,0,0)},
839template <
typename T, Direction D,
bool C>
842 if (
C || m_r2c_sub) {
amrex::Abort(
"R2C: OpenBC not supported with reduced dimensions or complex inputs"); }
844#if (AMREX_SPACEDIM == 3)
845 if (m_do_alld_fft) {
return; }
847 auto const ncomp = m_info.batch_size;
849 if (m_slab_decomp && ! m_fft_fwd_x_half.defined) {
850 auto* fab = detail::get_fab(m_rx);
852 Box bottom_half = m_real_domain;
853 bottom_half.
growHi(2,-m_real_domain.length(2)/2);
854 Box box = fab->box() & bottom_half;
856 auto* pr = fab->dataPtr();
858 detail::get_fab(m_cx)->dataPtr();
860 m_fft_fwd_x_half.template init_r2c<Direction::forward>
861 (box, pr, pc, m_slab_decomp, ncomp);
862 m_fft_bwd_x_half = m_fft_fwd_x_half;
865 m_fft_fwd_x_half.template init_r2c<Direction::forward>
866 (box, pr, pc, m_slab_decomp, ncomp);
869 m_fft_bwd_x_half.template init_r2c<Direction::backward>
870 (box, pr, pc, m_slab_decomp, ncomp);
877 if (m_cmd_x2z && ! m_cmd_x2z_half) {
878 Box bottom_half = m_spectral_domain_z;
881 bottom_half.
growHi(0,-m_spectral_domain_z.length(0)/2);
882 m_cmd_x2z_half = std::make_unique<MultiBlockCommMetaData>
883 (m_cz, bottom_half, m_cx,
IntVect(0), m_dtos_x2z);
886 if (m_cmd_z2x && ! m_cmd_z2x_half) {
887 Box bottom_half = m_spectral_domain_x;
888 bottom_half.
growHi(2,-m_spectral_domain_x.length(2)/2);
889 m_cmd_z2x_half = std::make_unique<MultiBlockCommMetaData>
890 (m_cx, bottom_half, m_cz,
IntVect(0), m_dtos_z2x);
895template <
typename T, Direction D,
bool C>
902 auto const ncomp = m_info.batch_size;
905 if (m_sub_helper.ghost_safe(inmf.nGrowVect())) {
906 m_r2c_sub->forward(m_sub_helper.make_alias_mf(inmf), incomp);
908 MF tmp(inmf.boxArray(), inmf.DistributionMap(), ncomp, 0);
909 tmp.LocalCopy(inmf, incomp, 0, ncomp,
IntVect(0));
910 m_r2c_sub->forward(m_sub_helper.make_alias_mf(tmp),0);
915 if (&m_rx != &inmf) {
916 m_rx.ParallelCopy(inmf, incomp, 0, ncomp);
921 m_fft_fwd_x.template compute_c2c<Direction::forward>();
923 m_fft_fwd_x.template compute_r2c<Direction::forward>();
928 auto& fft_x = m_openbc_half ? m_fft_fwd_x_half : m_fft_fwd_x;
930 fft_x.template compute_c2c<Direction::forward>();
932 fft_x.template compute_r2c<Direction::forward>();
936 ParallelCopy(m_cy, m_cx, *m_cmd_x2y, 0, 0, ncomp, m_dtos_x2y);
938 m_fft_fwd_y.template compute_c2c<Direction::forward>();
941 ParallelCopy(m_cz, m_cy, *m_cmd_y2z, 0, 0, ncomp, m_dtos_y2z);
943#if (AMREX_SPACEDIM == 3)
944 else if ( m_cmd_x2z) {
949 {components, m_dtos_x2z};
950 auto handler = ParallelCopy_nowait(m_cz, m_cx, *m_cmd_x2z_half, packing);
952 Box upper_half = m_spectral_domain_z;
955 upper_half.
growLo (0,-m_spectral_domain_z.length(0)/2);
956 m_cz.setVal(0, upper_half, 0, ncomp);
958 ParallelCopy_finish(m_cz, std::move(handler), *m_cmd_x2z_half, packing);
960 ParallelCopy(m_cz, m_cx, *m_cmd_x2z, 0, 0, ncomp, m_dtos_x2z);
964 m_fft_fwd_z.template compute_c2c<Direction::forward>();
967template <
typename T, Direction D,
bool C>
968template <
typename FA,
typename RT>
969std::pair<std::unique_ptr<char,DataDeleter>,std::size_t>
974 using FAB =
typename FA::FABType::value_type;
975 using T_FAB =
typename FAB::value_type;
976 static_assert(
sizeof(T_FAB) ==
sizeof(RT));
978 auto const ncomp = m_info.batch_size;
979 auto const& ia = fa.IndexArray();
984 if ( ! ia.empty() ) {
986 Box const& box = fa.fabbox(K);
990 sz =
sizeof(T_FAB) * box.
numPts() * ncomp;
993 fa.setFab(K, FAB(box,ncomp,
pp));
997 return std::make_pair(std::unique_ptr<char,DataDeleter>{},std::size_t(0));
999 return std::make_pair(std::unique_ptr<char,DataDeleter>
1005template <
typename T, Direction D,
bool C>
1006template <
typename RT,
typename CT,
Direction DIR,
bool CP,
1009 && ((
sizeof(RT)*2 ==
sizeof(CT) && !CP) ||
1010 (
sizeof(RT) ==
sizeof(CT) && CP)),
int> >
1013 auto [rdata, rsz] = install_raw_ptr(m_raw_mf, in);
1014 auto [cdata, csz] = install_raw_ptr(m_raw_cmf, out);
1029template <
typename T, Direction D,
bool C>
1030template <Direction DIR, std::enable_if_t<DIR == Direction::both,
int> >
1036template <
typename T, Direction D,
bool C>
1042 auto const ncomp = m_info.batch_size;
1045 if (m_sub_helper.ghost_safe(outmf.nGrowVect())) {
1046 MF submf = m_sub_helper.make_alias_mf(outmf);
1047 IntVect const& subngout = m_sub_helper.make_iv(ngout);
1048 Periodicity const& subperiod = m_sub_helper.make_periodicity(period);
1049 m_r2c_sub->backward_doit(submf, subngout, subperiod, outcomp);
1051 MF tmp(outmf.boxArray(), outmf.DistributionMap(), ncomp,
1052 m_sub_helper.make_safe_ghost(outmf.nGrowVect()));
1053 this->backward_doit(tmp, ngout, period, 0);
1054 outmf.LocalCopy(tmp, 0, outcomp, ncomp, tmp.nGrowVect());
1059 if (m_do_alld_fft) {
1061 m_fft_bwd_x.template compute_c2c<Direction::backward>();
1063 m_fft_bwd_x.template compute_r2c<Direction::backward>();
1065 outmf.ParallelCopy(m_rx, 0, outcomp, ncomp,
IntVect(0),
1070 m_fft_bwd_z.template compute_c2c<Direction::backward>();
1072 ParallelCopy(m_cy, m_cz, *m_cmd_z2y, 0, 0, ncomp, m_dtos_z2y);
1074#if (AMREX_SPACEDIM == 3)
1075 else if ( m_cmd_z2x) {
1076 auto const& cmd = m_openbc_half ? m_cmd_z2x_half : m_cmd_z2x;
1077 ParallelCopy(m_cx, m_cz, *cmd, 0, 0, ncomp, m_dtos_z2x);
1081 m_fft_bwd_y.template compute_c2c<Direction::backward>();
1083 ParallelCopy(m_cx, m_cy, *m_cmd_y2x, 0, 0, ncomp, m_dtos_y2x);
1086 auto& fft_x = m_openbc_half ? m_fft_bwd_x_half : m_fft_bwd_x;
1088 fft_x.template compute_c2c<Direction::backward>();
1090 fft_x.template compute_r2c<Direction::backward>();
1092 outmf.ParallelCopy(m_rx, 0, outcomp, ncomp,
IntVect(0),
1096template <
typename T, Direction D,
bool C>
1097template <
typename CT,
typename RT,
Direction DIR,
bool CP,
1100 && ((
sizeof(RT)*2 ==
sizeof(CT) && !CP) ||
1101 (
sizeof(RT) ==
sizeof(CT) && CP)),
int> >
1104 auto [rdata, rsz] = install_raw_ptr(m_raw_mf, out);
1105 auto [cdata, csz] = install_raw_ptr(m_raw_cmf, in);
1120template <
typename T, Direction D,
bool C>
1127 auto* fab = detail::get_fab(inout);
1128 if (!fab) {
return {fwd, bwd};}
1130 Box const& box = fab->box();
1133 auto const ncomp = m_info.batch_size;
1135#ifdef AMREX_USE_SYCL
1136 fwd.template init_c2c<Direction::forward>(box, pio, ncomp, ndims);
1140 fwd.template init_c2c<Direction::forward>(box, pio, ncomp, ndims);
1143 bwd.template init_c2c<Direction::backward>(box, pio, ncomp, ndims);
1150template <
typename T, Direction D,
bool C>
1151template <
typename F>
1154 if (m_info.twod_mode || m_info.batch_size > 1) {
1156#if (AMREX_SPACEDIM > 1)
1157 }
else if (m_r2c_sub) {
1159#if (AMREX_SPACEDIM == 2)
1161 m_r2c_sub->post_forward_doit_1
1164 post_forward(0, i, 0, sp);
1167 if (m_real_domain.length(0) == 1 && m_real_domain.length(1) == 1) {
1169 m_r2c_sub->post_forward_doit_1
1172 post_forward(0, 0, i, sp);
1174 }
else if (m_real_domain.length(0) == 1 && m_real_domain.length(2) == 1) {
1176 m_r2c_sub->post_forward_doit_1
1179 post_forward(0, i, 0, sp);
1181 }
else if (m_real_domain.length(0) == 1) {
1183 m_r2c_sub->post_forward_doit_1
1186 post_forward(0, i, j, sp);
1188 }
else if (m_real_domain.length(1) == 1) {
1190 m_r2c_sub->post_forward_doit_1
1193 post_forward(i, 0, j, sp);
1196 amrex::Abort(
"R2c::post_forward_doit_0: how did this happen?");
1201 this->post_forward_doit_1(post_forward);
1205template <
typename T, Direction D,
bool C>
1206template <
typename F>
1209 if (m_info.twod_mode || m_info.batch_size > 1) {
1211 }
else if (m_r2c_sub) {
1212 amrex::Abort(
"R2C::post_forward_doit_1: How did this happen?");
1214 if ( ! m_cz.empty()) {
1215 auto* spectral_fab = detail::get_fab(m_cz);
1217 auto const& a = spectral_fab->array();
1221 post_forward(jx,ky,iz,a(iz,jx,ky));
1224 }
else if ( ! m_cy.empty()) {
1225 auto* spectral_fab = detail::get_fab(m_cy);
1227 auto const& a = spectral_fab->array();
1231 post_forward(jx,iy,k,a(iy,jx,k));
1235 auto* spectral_fab = detail::get_fab(m_cx);
1237 auto const& a = spectral_fab->array();
1241 post_forward(i,j,k,a(i,j,k));
1248template <
typename T, Direction D,
bool C>
1251#if (AMREX_SPACEDIM == 3)
1252 if (m_info.twod_mode) {
1253 return T(1)/T(
Long(m_real_domain.length(0)) *
1254 Long(m_real_domain.length(1)));
1258 return T(1)/T(m_real_domain.numPts());
1262template <
typename T, Direction D,
bool C>
1265std::pair<typename R2C<T,D,C>::cMF *,
IntVect>
1268#if (AMREX_SPACEDIM > 1)
1270 auto [cmf, order] = m_r2c_sub->getSpectralData();
1271 return std::make_pair(cmf, m_sub_helper.inverse_order(order));
1274 if (!m_cz.empty()) {
1276 }
else if (!m_cy.empty()) {
1283template <
typename T, Direction D,
bool C>
1290 auto const ncomp = m_info.batch_size;
1294 bool inmf_safe = m_sub_helper.ghost_safe(inmf.nGrowVect());
1295 MF inmf_sub, inmf_tmp;
1298 inmf_sub = m_sub_helper.make_alias_mf(inmf);
1299 incomp_sub = incomp;
1301 inmf_tmp.define(inmf.boxArray(), inmf.DistributionMap(), ncomp, 0);
1302 inmf_tmp.LocalCopy(inmf, incomp, 0, ncomp,
IntVect(0));
1303 inmf_sub = m_sub_helper.make_alias_mf(inmf_tmp);
1307 bool outmf_safe = m_sub_helper.ghost_safe(outmf.
nGrowVect());
1308 cMF outmf_sub, outmf_tmp;
1311 outmf_sub = m_sub_helper.make_alias_mf(outmf);
1312 outcomp_sub = outcomp;
1315 outmf_sub = m_sub_helper.make_alias_mf(outmf_tmp);
1319 m_r2c_sub->forward(inmf_sub, outmf_sub, incomp_sub, outcomp_sub);
1328 if (!m_cz.empty()) {
1331 (outmf, m_spectral_domain_x, m_cz,
IntVect(0), dtos);
1332 ParallelCopy(outmf, m_cz, cmd, 0, outcomp, ncomp, dtos);
1333 }
else if (!m_cy.empty()) {
1335 (outmf, m_spectral_domain_x, m_cy,
IntVect(0), m_dtos_y2x);
1336 ParallelCopy(outmf, m_cy, cmd, 0, outcomp, ncomp, m_dtos_y2x);
1343template <
typename T, Direction D,
bool C>
1351template <
typename T, Direction D,
bool C>
1353 Periodicity const& period,
int incomp,
int outcomp)
1357 auto const ncomp = m_info.batch_size;
1361 bool inmf_safe = m_sub_helper.ghost_safe(inmf.nGrowVect());
1362 cMF inmf_sub, inmf_tmp;
1365 inmf_sub = m_sub_helper.make_alias_mf(inmf);
1366 incomp_sub = incomp;
1368 inmf_tmp.define(inmf.boxArray(), inmf.DistributionMap(), ncomp, 0);
1369 inmf_tmp.LocalCopy(inmf, incomp, 0, ncomp,
IntVect(0));
1370 inmf_sub = m_sub_helper.make_alias_mf(inmf_tmp);
1374 bool outmf_safe = m_sub_helper.ghost_safe(outmf.nGrowVect());
1375 MF outmf_sub, outmf_tmp;
1378 outmf_sub = m_sub_helper.make_alias_mf(outmf);
1379 outcomp_sub = outcomp;
1381 IntVect const& ngtmp = m_sub_helper.make_safe_ghost(outmf.nGrowVect());
1382 outmf_tmp.define(outmf.boxArray(), outmf.DistributionMap(), ncomp, ngtmp);
1383 outmf_sub = m_sub_helper.make_alias_mf(outmf_tmp);
1387 IntVect const& subngout = m_sub_helper.make_iv(ngout);
1388 Periodicity
const& subperiod = m_sub_helper.make_periodicity(period);
1389 m_r2c_sub->backward_doit(inmf_sub, outmf_sub, subngout, subperiod, incomp_sub, outcomp_sub);
1392 outmf.LocalCopy(outmf_tmp, 0, outcomp, ncomp, outmf_tmp.nGrowVect());
1397 if (!m_cz.empty()) {
1399 MultiBlockCommMetaData cmd
1400 (m_cz, m_spectral_domain_z, inmf,
IntVect(0), dtos);
1402 }
else if (!m_cy.empty()) {
1403 MultiBlockCommMetaData cmd
1404 (m_cy, m_spectral_domain_y, inmf,
IntVect(0), m_dtos_x2y);
1405 ParallelCopy(m_cy, inmf, cmd, incomp, 0, ncomp, m_dtos_x2y);
1407 m_cx.ParallelCopy(inmf, incomp, 0, ncomp);
1409 backward_doit(outmf, ngout, period, outcomp);
1413template <
typename T, Direction D,
bool C>
1414std::pair<BoxArray,DistributionMapping>
1417#if (AMREX_SPACEDIM > 1)
1419 auto const& [ba, dm] = m_r2c_sub->getSpectralDataLayout();
1420 return std::make_pair(m_sub_helper.inverse_boxarray(ba), dm);
1424#if (AMREX_SPACEDIM == 3)
1425 if (!m_cz.empty()) {
1426 BoxList bl = m_cz.boxArray().boxList();
1427 for (
auto& b : bl) {
1428 auto lo = b.smallEnd();
1429 auto hi = b.bigEnd();
1430 std::swap(lo[0], lo[1]);
1431 std::swap(lo[1], lo[2]);
1432 std::swap(hi[0], hi[1]);
1433 std::swap(hi[1], hi[2]);
1437 return std::make_pair(
BoxArray(std::move(bl)), m_cz.DistributionMap());
1440#if (AMREX_SPACEDIM >= 2)
1441 if (!m_cy.empty()) {
1442 BoxList bl = m_cy.boxArray().boxList();
1443 for (
auto& b : bl) {
1444 auto lo = b.smallEnd();
1445 auto hi = b.bigEnd();
1446 std::swap(lo[0], lo[1]);
1447 std::swap(hi[0], hi[1]);
1451 return std::make_pair(
BoxArray(std::move(bl)), m_cy.DistributionMap());
1455 return std::make_pair(m_cx.boxArray(), m_cx.DistributionMap());
1460template <
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_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
A collection of Boxes stored in an Array.
Definition AMReX_BoxArray.H:568
A class for managing a List of Boxes that share a common IndexType. This class implements operations ...
Definition AMReX_BoxList.H:52
BoxList & shift(int dir, int nzones)
Applies Box::shift(int,int) to each Box in the BoxList.
Definition AMReX_BoxList.cpp:565
__host__ __device__ const IntVectND< dim > & bigEnd() const &noexcept
Return the inclusive upper bound of the box.
Definition AMReX_Box.H:123
__host__ __device__ Long numPts() const noexcept
Return the number of points contained in the BoxND.
Definition AMReX_Box.H:356
__host__ __device__ IntVectND< dim > length() const noexcept
Return the length of the BoxND.
Definition AMReX_Box.H:154
__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:437
__host__ __device__ IntVectND< dim > size() const noexcept
Return the length of the BoxND.
Definition AMReX_Box.H:147
__host__ __device__ BoxND & growLo(int idir, int n_cell=1) noexcept
Grow the BoxND on the low end by n_cell cells in direction idir. NOTE: n_cell negative shrinks the Bo...
Definition AMReX_Box.H:662
__host__ __device__ IndexTypeND< dim > ixType() const noexcept
Return the indexing type.
Definition AMReX_Box.H:135
__host__ __device__ BoxND & growHi(int idir, int n_cell=1) noexcept
Grow the BoxND on the high end by n_cell cells in direction idir. NOTE: n_cell negative shrinks the B...
Definition AMReX_Box.H:673
__host__ __device__ const IntVectND< dim > & smallEnd() const &noexcept
Return the inclusive lower bound of the box.
Definition AMReX_Box.H:111
Calculates the distribution of FABs to MPI processes.
Definition AMReX_DistributionMapping.H:43
Long size() const noexcept
Length of the underlying processor map.
Definition AMReX_DistributionMapping.H:129
Convolution-based solver for open boundary conditions using Green's functions.
Definition AMReX_FFT_OpenBCSolver.H:24
3D Poisson solver for periodic, Dirichlet & Neumann boundaries in the first two dimensions,...
Definition AMReX_FFT_Poisson.H:187
Poisson solver for periodic, Dirichlet & Neumann boundaries using FFT.
Definition AMReX_FFT_Poisson.H:67
Parallel Discrete Fourier Transform.
Definition AMReX_FFT_R2C.H:47
std::conditional_t< C, cMF, std::conditional_t< std::is_same_v< T, Real >, MultiFab, FabArray< BaseFab< T > > > > MF
Definition AMReX_FFT_R2C.H:52
R2C & operator=(R2C const &)=delete
void forward(MF const &inmf, cMF &outmf, int incomp=0, int outcomp=0)
Forward transform.
Definition AMReX_FFT_R2C.H:1286
~R2C()
Definition AMReX_FFT_R2C.H:736
void setLocalDomain(std::array< int, 3 > const &local_start, std::array< int, 3 > const &local_size)
Set local domain.
Definition AMReX_FFT_R2C.H:787
R2C(Box const &domain, Info const &info=Info{})
Constructor.
Definition AMReX_FFT_R2C.H:489
void backward(cMF const &inmf, MF &outmf, int incomp=0, int outcomp=0)
Backward transform.
Definition AMReX_FFT_R2C.H:1346
void backward(MF &outmf, int outcomp=0)
Backward transform.
Definition AMReX_FFT_R2C.H:1031
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:1207
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:1249
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:1152
std::pair< std::array< int, 3 >, std::array< int, 3 > > getLocalDomain() const
Get local domain.
Definition AMReX_FFT_R2C.H:796
void forward(RT const *in, CT *out)
Forward transform.
Definition AMReX_FFT_R2C.H:1011
std::pair< BoxArray, DistributionMapping > getSpectralDataLayout() const
Get BoxArray and DistributionMapping for spectral data.
Definition AMReX_FFT_R2C.H:1415
std::pair< cMF *, IntVect > getSpectralData() const
Get the internal spectral data.
R2C(std::array< int, 3 > const &domain_size, Info const &info=Info{})
Constructor.
Definition AMReX_FFT_R2C.H:731
FabArray< BaseFab< GpuComplex< T > > > cMF
Definition AMReX_FFT_R2C.H:49
std::pair< std::array< int, 3 >, std::array< int, 3 > > getLocalSpectralDomain() const
Get local spectral domain.
Definition AMReX_FFT_R2C.H:822
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:202
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:813
void backward(CT const *in, RT *out)
Backward transform.
Definition AMReX_FFT_R2C.H:1102
void forward(MF const &inmf, int incomp=0)
Forward transform.
Definition AMReX_FFT_R2C.H:898
IntVect nGrowVect() const noexcept
Definition AMReX_FabArrayBase.H:80
int size() const noexcept
Return the number of FABs in the FabArray.
Definition AMReX_FabArrayBase.H:110
const DistributionMapping & DistributionMap() const noexcept
Return constant reference to associated DistributionMapping.
Definition AMReX_FabArrayBase.H:131
bool empty() const noexcept
Definition AMReX_FabArrayBase.H:89
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:95
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:2357
void ParallelCopy(const FabArray< FAB > &src, const Periodicity &period=Periodicity::NonPeriodic(), CpOp op=FabArrayBase::COPY)
Definition AMReX_FabArray.H:849
typename std::conditional_t< IsBaseFab< BaseFab< GpuComplex< T > > >::value, BaseFab< GpuComplex< T > >, FABType >::value_type value_type
Definition AMReX_FabArray.H:360
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:2173
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:1954
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:28
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:319
Arena * The_Arena()
Definition AMReX_Arena.cpp:805
int NProcs() noexcept
Definition AMReX_ParallelDescriptor.H:255
Definition AMReX_FFT_Helper.H:52
Direction
Definition AMReX_FFT_Helper.H:54
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
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:30
bool is_aligned(const void *p, std::size_t alignment) noexcept
Return whether the address p is aligned to alignment bytes.
Definition AMReX_Arena.H:39
BoxArray decompose(Box const &domain, int nboxes, Array< bool, 3 > const &decomp, bool no_overlap)
Decompose domain box into BoxArray.
Definition AMReX_BoxArray.cpp:1947
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:33
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition AMReX.cpp:240
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:2019
__host__ __device__ constexpr T elemwiseMin(T const &a, T const &b) noexcept
Return the element-wise minimum of the given values for types like XDim3.
Definition AMReX_Algorithm.H:62
Definition AMReX_FFT_Helper.H:64
bool twod_mode
Definition AMReX_FFT_Helper.H:75
bool oned_mode
We might have a special twod_mode: nx or ny == 1 && nz > 1.
Definition AMReX_FFT_Helper.H:78
int batch_size
Batched FFT size. Only support in R2C, not R2X.
Definition AMReX_FFT_Helper.H:81
DomainStrategy domain_strategy
Domain composition strategy.
Definition AMReX_FFT_Helper.H:66
int nprocs
Max number of processes to use.
Definition AMReX_FFT_Helper.H:84
int pencil_threshold
Definition AMReX_FFT_Helper.H:70
Definition AMReX_FFT_Helper.H:188
std::conditional_t< std::is_same_v< float, T >, cuComplex, cuDoubleComplex > VendorComplex
Definition AMReX_FFT_Helper.H:192
FabArray memory allocation information.
Definition AMReX_FabArray.H:66
MFInfo & SetAlloc(bool a) noexcept
Definition AMReX_FabArray.H:73
This class specializes behaviour on local copies and unpacking receive buffers.
Definition AMReX_NonLocalBC.H:626
Contains information about which components take part of the data transaction.
Definition AMReX_NonLocalBC.H:539
int n_components
Definition AMReX_NonLocalBC.H:542
Communication datatype (note: this structure also works without MPI)
Definition AMReX_ccse-mpi.H:78