145 P3MFFT(stream_type gpu_stream, boost::mpi::communicator comm,
150 auto constexpr row_major_order = std::array<int, 3>{2, 1, 0};
151 auto constexpr col_major_order = std::array<int, 3>{0, 1, 2};
152 auto constexpr in_box_order =
156 auto constexpr out_box_order =
162 auto const global_out_box_full = Box({0, 0, 0}, high, out_box_order);
163 auto const global_out_box =
164 FFTConfig::use_r2c ? global_out_box_full.r2c(FFTConfig::r2c_dir)
165 : global_out_box_full;
166 auto best_grid = node_grid;
167 for (
auto i : {0u, 1u, 2u}) {
168 if (global_mesh[i] % (2 * n_procs) == 0) {
169 best_grid = {n_procs, 1, 1};
173 m_batch_size = n_fields;
174 auto const this_rank = comm.rank();
177 auto out_boxes = heffte::split_world(global_out_box, to_array(best_grid));
178 out_box = std::make_unique<Box>(out_boxes[this_rank]);
180 in_box = std::make_unique<Box>(
181 to_array(rs_local_ld_index),
186 heffte::plan_options options = heffte::default_options<backend>();
192 options.use_reorder =
true;
198 options.algorithm = heffte::reshape_algorithm::p2p_plined;
204 options.use_pencils =
true;
206 auto const make_fft3d = [&](Box
const &output_box,
207 heffte::plan_options
const &opts) {
208 if constexpr (FFTConfig::use_r2c) {
209 auto constexpr r2c_dir =
static_cast<int>(FFTConfig::r2c_dir);
210 return std::make_unique<FFT3D>(gpu_stream, *in_box, output_box, r2c_dir,
213 return std::make_unique<FFT3D>(gpu_stream, *in_box, output_box, comm,
217 auto const make_plan = [&](heffte::ioboxes<int>
const &boxes,
220 opts.use_pencils = use_pencils;
221 auto constexpr r2c_dir =
static_cast<int>(FFTConfig::r2c_dir);
222 return heffte::plan_operations(
223 boxes, FFTConfig::use_r2c ? r2c_dir : -1,
224 heffte::set_options<backend, FFTConfig::use_r2c>(opts), this_rank);
227 std::vector<Box> out_boxes;
228 unsigned int n_mpi_reshapes;
231 auto const is_empty = [](Box
const &box) {
return box.empty(); };
232 auto const make_candidate = [&](heffte::ioboxes<int>
const &seed,
234 auto const seed_plan = make_plan(seed, use_pencils);
235 auto boxes = heffte::ioboxes<int>{seed.in, {}};
236 for (
auto const &box : seed_plan.out_shape[2]) {
237 boxes.out.emplace_back(box.low, box.high, out_box_order);
239 auto const plan = make_plan(boxes, use_pencils);
240 auto n_mpi_reshapes = 0u;
242 for (
auto stage : {0u, 1u, 2u, 3u}) {
243 if (not heffte::match(plan.in_shape[stage], plan.out_shape[stage])) {
246 valid &= std::ranges::none_of(plan.out_shape[stage], is_empty);
248 return Candidate{std::move(boxes.out), n_mpi_reshapes, valid};
264 if constexpr (Architecture ==
Arch::CPU) {
265 auto const seed = heffte::mpi::gather_boxes(*in_box, *out_box, comm);
266 auto const pencils = make_candidate(seed,
true);
267 auto const slabs = make_candidate(seed,
false);
269 std::unique_ptr<FFT3D>
fft;
275 auto const make_layout = [&](Box
const &obox,
bool pencils) -> Layout {
277 opts.use_pencils = pencils;
278 auto fft = make_fft3d(obox, opts);
279 auto const is_local_buffer_even = obox.count() % 2ll == 0ll;
280 auto const batchable =
282 boost::mpi::all_reduce(comm, is_local_buffer_even,
283 std::logical_and<bool>());
284 auto const local_simd_veto =
285 std::is_same_v<FloatType, float> and
286 (not is_local_buffer_even or
fft->size_comm_buffers() % 2u == 1u);
287 auto const simd_veto = boost::mpi::all_reduce(comm, local_simd_veto,
288 std::logical_or<bool>());
289 return {std::move(
fft), obox, pencils, batchable, simd_veto};
291 std::vector<Layout> layouts;
293 layouts.emplace_back(make_layout(pencils.out_boxes[this_rank],
true));
296 if ((FFTConfig::use_r2c or
299 slabs.valid and slabs.n_mpi_reshapes < pencils.n_mpi_reshapes) {
300 layouts.emplace_back(make_layout(slabs.out_boxes[this_rank],
false));
302 auto *best_layout = &layouts.front();
303 auto best_batched =
false;
304 if (layouts.size() >= 2ul or layouts[0].batchable) {
305 auto constexpr n_rounds = 3u;
306 auto constexpr n_steps = 5u;
307 auto best_time = std::numeric_limits<double>::max();
308 for (
auto &layout : layouts) {
309 if (layout.simd_veto) {
313 auto const batch_modes = layout.batchable
314 ? std::vector<bool>{
false,
true}
315 : std::vector<bool>{
false};
316 for (
bool const batched : batch_modes) {
317 auto min_time = std::numeric_limits<double>::max();
318 for (
auto round = 0u; round < n_rounds; ++round) {
320 time_fft_step(*layout.fft, comm, n_fields, batched, n_steps);
324 cur_time *= (comm.size() > 4) ? 1.25 : 1.1;
326 min_time = std::min(min_time, cur_time);
328 if (min_time < best_time) {
329 best_time = min_time;
330 best_layout = &layout;
331 best_batched = batched;
336 if (best_layout->simd_veto) {
339 options.use_pencils = best_layout->use_pencils;
340 out_box = std::make_unique<Box>(best_layout->box);
341 fft3d = std::move(best_layout->fft);
342 use_batched_fft = best_batched;
352 auto const trap_pause = (Architecture ==
Arch::CUDA)
358 fft3d = make_fft3d(*out_box, options);
360 time_fft_step(*fft3d, comm, 1,
false, 0u);
362 m_workspace =
decltype(m_workspace)((use_batched_fft ? n_fields : 1u) *
363 fft3d->size_workspace());