FFT.hpp 126 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//
// IPPL FFT
//
// Copyright (c) 2008-2018
// Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved.
//
// OPAL is licensed under GNU GPL version 3.
//

/**
   Implementations for FFT constructor/destructor and transforms
*/

gsell's avatar
gsell committed
15 16 17 18 19 20 21 22 23
#include "FFT/FFT.h"
#include "FieldLayout/FieldLayout.h"
#include "Field/BareField.h"
#include "Utility/IpplStats.h"

//=============================================================================
// FFT CCTransform Constructors
//=============================================================================

24
/**
25 26
   Create a new FFT object of type CCTransform, with a
   given domain. Also specify which dimensions to transform along.
gsell's avatar
gsell committed
27 28
*/

gsell's avatar
gsell committed
29
template <size_t Dim, class T>
gsell's avatar
gsell committed
30
FFT<CCTransform,Dim,T>::FFT(
31 32 33 34 35
    const typename FFT<CCTransform,Dim,T>::Domain_t& cdomain,
    const bool transformTheseDims[Dim],
    const bool& compressTemps)
: FFTBase<Dim,T>(FFT<CCTransform,Dim,T>::ccFFT, cdomain,
                 transformTheseDims, compressTemps)
gsell's avatar
gsell committed
36
{
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    // construct array of axis lengths
    size_t nTransformDims = this->numTransformDims();
    int* lengths = new int[nTransformDims];
    size_t d;
    for (d=0; d<nTransformDims; ++d)
        lengths[d] = cdomain[this->activeDimension(d)].length();

    // construct array of transform types for FFT Engine, compute normalization
    int* transformTypes = new int[nTransformDims];
    T& normFact = this->getNormFact();
    normFact = 1.0;
    for (d=0; d<nTransformDims; ++d) {
        transformTypes[d] = FFTBase<Dim,T>::ccFFT;  // all transforms are complex-to-complex
        normFact /= lengths[d];
    }

    // set up FFT Engine
    this->getEngine().setup(nTransformDims, transformTypes, lengths);
    delete [] transformTypes;
    delete [] lengths;
    // set up the temporary fields
    setup();
gsell's avatar
gsell committed
60 61 62
}


63
/**
64 65
   setup performs all the initializations necessary after the transform
   directions have been specified.
gsell's avatar
gsell committed
66
*/
gsell's avatar
gsell committed
67
template <size_t Dim, class T>
gsell's avatar
gsell committed
68 69 70
void
FFT<CCTransform,Dim,T>::setup(void)
{
71 72
    // Tau profiling

gsell's avatar
gsell committed
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    size_t d, activeDim;
    size_t nTransformDims = this->numTransformDims();
    // Set up the arrays of temporary Fields and FieldLayouts:
    e_dim_tag serialParallel[Dim];  // Specifies SERIAL, PARALLEL dims in temp
    // make zeroth dimension always SERIAL
    serialParallel[0] = SERIAL;
    // all other dimensions parallel
    for (d=1; d<Dim; ++d)
        serialParallel[d] = PARALLEL;

    tempLayouts_m = new Layout_t*[nTransformDims];
    tempFields_m = new ComplexField_t*[nTransformDims];

    // loop over transform dimensions
    for (size_t dim=0; dim<nTransformDims; ++dim) {
        // get number of dimension to be transformed
        activeDim = this->activeDimension(dim);
        // Get input Field's domain
        const Domain_t& ndic = this->getDomain();
        // make new domain with permuted Indexes, activeDim first
        Domain_t ndip;
        ndip[0] = ndic[activeDim];
        for (d=1; d<Dim; ++d) {
            size_t nextDim = activeDim + d;
            if (nextDim >= Dim) nextDim -= Dim;
            ndip[d] = ndic[nextDim];
        }
        // generate temporary field layout
        tempLayouts_m[dim] = new Layout_t(ndip, serialParallel, this->transVnodes());
        // generate temporary Field
        tempFields_m[dim] = new ComplexField_t(*tempLayouts_m[dim]);
        // If user requests no intermediate compression, uncompress right now:
        if (!this->compressTemps()) (*tempFields_m[dim]).Uncompress();
    }

    return;
gsell's avatar
gsell committed
110 111 112 113 114 115
}

//-----------------------------------------------------------------------------
// Destructor
//-----------------------------------------------------------------------------

gsell's avatar
gsell committed
116
template <size_t Dim, class T>
gsell's avatar
gsell committed
117 118
FFT<CCTransform,Dim,T>::~FFT(void) {

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    // Tau profiling

    /*
      #ifdef IPPL_OPENCL
      base.ocl_cleanUp();
      #endif
    */

    // delete arrays of temporary fields and field layouts
    size_t nTransformDims = this->numTransformDims();
    for (size_t d=0; d<nTransformDims; ++d) {
        delete tempFields_m[d];
        delete tempLayouts_m[d];
    }
    delete [] tempFields_m;
    delete [] tempLayouts_m;
gsell's avatar
gsell committed
135 136 137 138 139 140 141
}


//-----------------------------------------------------------------------------
// do the CC FFT; separate input and output fields
//-----------------------------------------------------------------------------

gsell's avatar
gsell committed
142
template <size_t Dim, class T>
gsell's avatar
gsell committed
143 144
void
FFT<CCTransform,Dim,T>::transform(
145 146 147 148
    int direction,
    typename FFT<CCTransform,Dim,T>::ComplexField_t& f,
    typename FFT<CCTransform,Dim,T>::ComplexField_t& g,
    const bool& constInput)
gsell's avatar
gsell committed
149
{
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    // indicate we're doing another FFT
    //INCIPPLSTAT(incFFTs);

    // Check domain of incoming Fields
    const Layout_t& in_layout = f.getLayout();
    const Domain_t& in_dom = in_layout.getDomain();
    const Layout_t& out_layout = g.getLayout();
    const Domain_t& out_dom = out_layout.getDomain();
    PAssert_EQ( this->checkDomain(this->getDomain(),in_dom) &&
                this->checkDomain(this->getDomain(),out_dom), true);

    // Common loop iterate and other vars:
    size_t d;
    int idim;            // idim loops over the number of transform dims.
    int begdim, enddim;  // beginning and end of transform dim loop
    size_t nTransformDims = this->numTransformDims();
    // Field* for temp Field management:
    ComplexField_t* temp = &f;
    // Local work array passed to FFT:
    Complex_t* localdata;

    // Loop over the dimensions be transformed:
    begdim = (direction == +1) ? 0 : static_cast<int>(nTransformDims-1);
    enddim = (direction == +1) ? static_cast<int>(nTransformDims) : -1;
    for (idim = begdim; idim != enddim; idim += direction) {

        // Now do the serial transforms along this dimension:

        bool skipTranspose = false;
        // if this is the first transform dimension, we might be able
        // to skip the transpose into the first temporary Field
        if (idim == begdim && !constInput) {
            // get domain for comparison
            const Domain_t& first_dom = tempLayouts_m[idim]->getDomain();
            // check that zeroth axis is the same and is serial
            // and that there are no guard cells
            skipTranspose = ( (in_dom[0].sameBase(first_dom[0])) &&
                              (in_dom[0].length() == first_dom[0].length()) &&
                              (in_layout.getDistribution(0) == SERIAL) &&
                              (f.getGC() == FFT<CCTransform,Dim,T>::nullGC) );
        }

        // if this is the last transform dimension, we might be able
        // to skip the last temporary and transpose right into g
        if (idim == enddim-direction) {
            // get the domain for comparison
            const Domain_t& last_dom = tempLayouts_m[idim]->getDomain();
            // check that zeroth axis is the same and is serial
            // and that there are no guard cells
            skipTranspose = ( (out_dom[0].sameBase(last_dom[0])) &&
                              (out_dom[0].length() == last_dom[0].length()) &&
                              (out_layout.getDistribution(0) == SERIAL) &&
                              (g.getGC() == FFT<CCTransform,Dim,T>::nullGC) );
        }

        if (!skipTranspose) {
            // transpose and permute to Field with transform dim first
            (*tempFields_m[idim])[tempLayouts_m[idim]->getDomain()] =
                (*temp)[temp->getLayout().getDomain()];

            // Compress out previous iterate's storage:
            if (this->compressTemps() && temp != &f) *temp = 0;
            temp = tempFields_m[idim];  // Field* management aid
        }
        else if (idim == enddim-direction && temp != &g) {
            // last transform and we can skip the last temporary field
            // so do the transpose here using g instead

            // transpose and permute to Field with transform dim first
            g[out_dom] = (*temp)[temp->getLayout().getDomain()];

            // Compress out previous iterate's storage:
            if (this->compressTemps() && temp != &f) *temp = 0;
            temp = &g;  // Field* management aid
        }

        // Loop over all the Vnodes, working on the LField in each.
        typename ComplexField_t::const_iterator_if l_i, l_end = temp->end_if();
        for (l_i = temp->begin_if(); l_i != l_end; ++l_i) {

            // Get the LField
            ComplexLField_t* ldf = (*l_i).second.get();
            // make sure we are uncompressed
            ldf->Uncompress();
            // get the raw data pointer
            localdata = ldf->getP();

            // Do 1D complex-to-complex FFT's on all the strips in the LField:
            int nstrips = 1, length = ldf->size(0);
            for (d=1; d<Dim; ++d) nstrips *= ldf->size(d);
            for (int istrip=0; istrip<nstrips; ++istrip) {
                // Do the 1D FFT:
                this->getEngine().callFFT(idim, direction, localdata);
                // advance the data pointer
                localdata += length;
            } // loop over 1D strips
        } // loop over all the LFields

    } // loop over all transformed dimensions

    // skip final assignment and compress if we used g as final temporary
    if (temp != &g) {

        // Now assign into output Field, and compress last temp's storage:
        g[out_dom] = (*temp)[temp->getLayout().getDomain()];
        if (this->compressTemps() && temp != &f) *temp = 0;

    }

    // Normalize:
    if (direction == +1)
        g *= Complex_t(this->getNormFact(), 0.0);
gsell's avatar
gsell committed
262

263
    return;
gsell's avatar
gsell committed
264 265
}

gsell's avatar
gsell committed
266
template <size_t Dim, class T>
gsell's avatar
gsell committed
267 268
void
FFT<CCTransform,Dim,T>::transform(
269 270
    int direction,
    typename FFT<CCTransform,Dim,T>::ComplexField_t& f)
gsell's avatar
gsell committed
271 272
{

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    // indicate we're doing another FFT
    // INCIPPLSTAT(incFFTs);

    // Check domain of incoming Field
    const Layout_t& in_layout = f.getLayout();
    const Domain_t& in_dom = in_layout.getDomain();
    PAssert_EQ(this->checkDomain(this->getDomain(),in_dom), true);

    // Common loop iterate and other vars:
    size_t d;
    int idim;            // idim loops over the number of transform dims.
    int begdim, enddim;  // beginning and end of transform dim loop
    size_t nTransformDims = this->numTransformDims();
    // Field* for temp Field management:
    ComplexField_t* temp = &f;
    // Local work array passed to FFT:
    Complex_t* localdata;

    // Loop over the dimensions be transformed:
    begdim = (direction == +1) ? 0 : static_cast<int>(nTransformDims-1);
    enddim = (direction == +1) ? static_cast<int>(nTransformDims) : -1;
    for (idim = begdim; idim != enddim; idim += direction) {

        // Now do the serial transforms along this dimension:

        bool skipTranspose = false;
        // if this is the first transform dimension, we might be able
        // to skip the transpose into the first temporary Field
        if (idim == begdim) {
            // get domain for comparison
            const Domain_t& first_dom = tempLayouts_m[idim]->getDomain();
            // check that zeroth axis is the same and is serial
            // and that there are no guard cells
            skipTranspose = ( (in_dom[0].sameBase(first_dom[0])) &&
                              (in_dom[0].length() == first_dom[0].length()) &&
                              (in_layout.getDistribution(0) == SERIAL) &&
                              (f.getGC() == FFT<CCTransform,Dim,T>::nullGC) );
        }

        // if this is the last transform dimension, we might be able
        // to skip the last temporary and transpose right into f
        if (idim == enddim-direction) {
            // get domain for comparison
            const Domain_t& last_dom = tempLayouts_m[idim]->getDomain();
            // check that zeroth axis is the same and is serial
            // and that there are no guard cells
            skipTranspose = ( (in_dom[0].sameBase(last_dom[0])) &&
                              (in_dom[0].length() == last_dom[0].length()) &&
                              (in_layout.getDistribution(0) == SERIAL) &&
                              (f.getGC() == FFT<CCTransform,Dim,T>::nullGC) );
        }

        if (!skipTranspose) {
            // transpose and permute to Field with transform dim first
            (*tempFields_m[idim])[tempLayouts_m[idim]->getDomain()] =
                (*temp)[temp->getLayout().getDomain()];

            // Compress out previous iterate's storage:
            if (this->compressTemps() && temp != &f) *temp = 0;
            temp = tempFields_m[idim];  // Field* management aid
        }
        else if (idim == enddim-direction && temp != &f) {
            // last transform and we can skip the last temporary field
            // so do the transpose here using f instead

            // transpose and permute to Field with transform dim first
            f[in_dom] = (*temp)[temp->getLayout().getDomain()];

            // Compress out previous iterate's storage:
            if (this->compressTemps()) *temp = 0;
            temp = &f;  // Field* management aid
        }

        // Loop over all the Vnodes, working on the LField in each.
        typename ComplexField_t::const_iterator_if l_i, l_end = temp->end_if();
        for (l_i = temp->begin_if(); l_i != l_end; ++l_i) {

            // Get the LField
            ComplexLField_t* ldf = (*l_i).second.get();
            // make sure we are uncompressed
            ldf->Uncompress();
            // get the raw data pointer
            localdata = ldf->getP();

            // Do 1D complex-to-complex FFT's on all the strips in the LField:
            int nstrips = 1, length = ldf->size(0);
            for (d=1; d<Dim; ++d) nstrips *= ldf->size(d);
            for (int istrip=0; istrip<nstrips; ++istrip) {
                // Do the 1D FFT:
                this->getEngine().callFFT(idim, direction, localdata);
                // advance the data pointer
                localdata += length;
            } // loop over 1D strips
        } // loop over all the LFields


    } // loop over all transformed dimensions

    // skip final assignment and compress if we used f as final temporary
    if (temp != &f) {

        // Now assign back into original Field, and compress last temp's storage:
        f[in_dom] = (*temp)[temp->getLayout().getDomain()];
        if (this->compressTemps()) *temp = 0;

    }

    // Normalize:
    if (direction == +1)
        f *= Complex_t(this->getNormFact(), 0.0);
gsell's avatar
gsell committed
383

384
    return;
gsell's avatar
gsell committed
385 386 387 388 389 390 391 392 393 394 395 396 397
}

//=============================================================================
// 1D FFT CCTransform Constructors
//=============================================================================

//-----------------------------------------------------------------------------
// Create a new FFT object of type CCTransform, with a
// given domain. Also specify which dimensions to transform along.
//-----------------------------------------------------------------------------

template <class T>
FFT<CCTransform,1U,T>::FFT(
398 399 400 401
    const typename FFT<CCTransform,1U,T>::Domain_t& cdomain,
    const bool transformTheseDims[1U], const bool& compressTemps)
: FFTBase<1U,T>(FFT<CCTransform,1U,T>::ccFFT, cdomain,
                transformTheseDims, compressTemps)
gsell's avatar
gsell committed
402 403
{

404
    // Tau profiling
405 406 407



gsell's avatar
gsell committed
408

409 410 411 412
    size_t nTransformDims = 1U;
    // get axis length
    int length;
    length = cdomain[0].length();
gsell's avatar
gsell committed
413

414 415 416 417 418
    // get transform type for FFT Engine, compute normalization
    int transformType;
    transformType = FFTBase<1U,T>::ccFFT;  // all transforms are complex-to-complex
    T& normFact = this->getNormFact();
    normFact = 1.0 / length;
gsell's avatar
gsell committed
419

420 421 422 423
    // set up FFT Engine
    this->getEngine().setup(nTransformDims, &transformType, &length);
    // set up the temporary fields
    setup();
gsell's avatar
gsell committed
424 425 426 427 428 429 430 431 432
}

//-----------------------------------------------------------------------------
// Create a new FFT object of type CCTransform, with a
// given domain. Default case of transforming along all dimensions.
//-----------------------------------------------------------------------------

template <class T>
FFT<CCTransform,1U,T>::FFT(
433 434 435
    const typename FFT<CCTransform,1U,T>::Domain_t& cdomain,
    const bool& compressTemps)
: FFTBase<1U,T>(FFT<CCTransform,1U,T>::ccFFT, cdomain, compressTemps)
gsell's avatar
gsell committed
436 437
{

438
    // Tau profiling
gsell's avatar
gsell committed
439

440 441 442
    // get axis length
    int length;
    length = cdomain[0].length();
gsell's avatar
gsell committed
443

444 445 446 447 448
    // get transform type for FFT Engine, compute normalization
    int transformType;
    transformType = FFTBase<1U,T>::ccFFT;  // all transforms are complex-to-complex
    T& normFact = this->getNormFact();
    normFact = 1.0 / length;
gsell's avatar
gsell committed
449

450 451 452 453
    // set up FFT Engine
    this->getEngine().setup(1U, &transformType, &length);
    // set up the temporary fields
    setup();
gsell's avatar
gsell committed
454 455 456 457 458 459 460 461 462 463 464
}

//-----------------------------------------------------------------------------
// setup performs all the initializations necessary after the transform
// directions have been specified.
//-----------------------------------------------------------------------------

template <class T>
void
FFT<CCTransform,1U,T>::setup(void)
{
465
    // Tau profiling
466

467 468 469 470 471 472 473 474
    // Get input Field's domain
    const Domain_t& ndic = this->getDomain();
    // generate temporary field layout
    tempLayouts_m = new Layout_t(ndic[0], PARALLEL, 1);
    // generate temporary Field
    tempFields_m = new ComplexField_t(*tempLayouts_m);
    // If user requests no intermediate compression, uncompress right now:
    if (!this->compressTemps()) tempFields_m->Uncompress();
475

476
    return;
gsell's avatar
gsell committed
477 478 479 480 481 482 483 484 485
}

//-----------------------------------------------------------------------------
// Destructor
//-----------------------------------------------------------------------------

template <class T>
FFT<CCTransform,1U,T>::~FFT(void) {

486
    // Tau profiling
487 488 489



gsell's avatar
gsell committed
490

491 492 493
    // delete temporary fields and field layouts
    delete tempFields_m;
    delete tempLayouts_m;
gsell's avatar
gsell committed
494 495 496 497 498 499 500 501 502 503
}


//-----------------------------------------------------------------------------
// do the CC FFT; separate input and output fields
//-----------------------------------------------------------------------------

template <class T>
void
FFT<CCTransform,1U,T>::transform(
504 505 506 507
    int direction,
    typename FFT<CCTransform,1U,T>::ComplexField_t& f,
    typename FFT<CCTransform,1U,T>::ComplexField_t& g,
    const bool& constInput)
gsell's avatar
gsell committed
508 509
{

510 511
    // indicate we're doing another FFT
    // INCIPPLSTAT(incFFTs);
gsell's avatar
gsell committed
512

513 514 515 516 517 518 519
    // Check domain of incoming Fields
    const Layout_t& in_layout = f.getLayout();
    const Domain_t& in_dom = in_layout.getDomain();
    const Layout_t& out_layout = g.getLayout();
    const Domain_t& out_dom = out_layout.getDomain();
    PAssert_EQ( this->checkDomain(this->getDomain(),in_dom) &&
                this->checkDomain(this->getDomain(),out_dom), true);
gsell's avatar
gsell committed
520

521 522 523 524
    // Field* for temp Field management:
    ComplexField_t* temp = &f;
    // Local work array passed to FFT:
    Complex_t* localdata;
525 526


527
    // Now do the serial transforms along this dimension:
gsell's avatar
gsell committed
528

529 530
    // get temp domain for comparison
    const Domain_t& temp_dom = tempLayouts_m->getDomain();
gsell's avatar
gsell committed
531

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
    bool skipTranspose = false;
    // if this is the first transform dimension, we might be able
    // to skip the transpose into the first temporary Field
    if (!constInput) {
        // check that zeroth axis is the same, has one vnode,
        // and that there are no guard cells
        skipTranspose = ( (in_dom[0].sameBase(temp_dom[0])) &&
                          (in_dom[0].length() == temp_dom[0].length()) &&
                          (in_layout.numVnodes() == 1) &&
                          (f.getGC() == FFT<CCTransform,1U,T>::nullGC) );
    }

    bool skipFinal;
    // we might be able
    // to skip the last temporary and transpose right into g

    // check that zeroth axis is the same, has one vnode
gsell's avatar
gsell committed
549
    // and that there are no guard cells
550 551 552 553
    skipFinal = ( (out_dom[0].sameBase(temp_dom[0])) &&
                  (out_dom[0].length() == temp_dom[0].length()) &&
                  (out_layout.numVnodes() == 1) &&
                  (g.getGC() == FFT<CCTransform,1U,T>::nullGC) );
gsell's avatar
gsell committed
554

555 556 557 558 559 560 561 562
    if (!skipTranspose) {
        // assign to Field with proper layout
        (*tempFields_m) = (*temp);
        temp = tempFields_m;  // Field* management aid
    }
    if (skipFinal) {
        // we can skip the last temporary field
        // so do the transpose here using g instead
563

564 565
        // assign to Field with proper layout
        g = (*temp);
566

567 568 569 570
        // Compress out previous iterate's storage:
        if (this->compressTemps() && temp != &f) *temp = 0;
        temp = &g;  // Field* management aid
    }
571

gsell's avatar
gsell committed
572 573 574



575 576 577 578 579 580 581 582 583
    // should be only one LField!
    typename ComplexField_t::const_iterator_if l_i = temp->begin_if();
    if (l_i != temp->end_if()) {
        // Get the LField
        ComplexLField_t* ldf = (*l_i).second.get();
        // make sure we are uncompressed
        ldf->Uncompress();
        // get the raw data pointer
        localdata = ldf->getP();
584

585 586 587
        // Do the 1D FFT:
        this->getEngine().callFFT(0, direction, localdata);
    }
gsell's avatar
gsell committed
588

589 590


591 592 593 594 595 596
    // skip final assignment and compress if we used g as final temporary
    if (temp != &g) {

        // Now assign into output Field, and compress last temp's storage:
        g = (*temp);
        if (this->compressTemps() && temp != &f) *temp = 0;
gsell's avatar
gsell committed
597

598 599 600 601 602
    }

    // Normalize:
    if (direction == +1)
        g *= Complex_t(this->getNormFact(), 0.0);
gsell's avatar
gsell committed
603

604
    return;
gsell's avatar
gsell committed
605 606 607 608 609 610 611 612 613
}

//-----------------------------------------------------------------------------
// "in-place" FFT; specify +1 or -1 to indicate forward or inverse transform.
//-----------------------------------------------------------------------------

template <class T>
void
FFT<CCTransform,1U,T>::transform(
614 615
    int direction,
    typename FFT<CCTransform,1U,T>::ComplexField_t& f)
gsell's avatar
gsell committed
616 617
{

618 619
    // indicate we're doing another FFT
    // INCIPPLSTAT(incFFTs);
gsell's avatar
gsell committed
620

621 622 623 624
    // Check domain of incoming Field
    const Layout_t& in_layout = f.getLayout();
    const Domain_t& in_dom = in_layout.getDomain();
    PAssert_EQ(this->checkDomain(this->getDomain(),in_dom), true);
gsell's avatar
gsell committed
625

626 627 628 629
    // Field* for temp Field management:
    ComplexField_t* temp = &f;
    // Local work array passed to FFT:
    Complex_t* localdata;
630 631


632
    // Now do the serial transforms along this dimension:
gsell's avatar
gsell committed
633

634 635
    // get domain for comparison
    const Domain_t& temp_dom = tempLayouts_m->getDomain();
gsell's avatar
gsell committed
636

637 638 639
    bool skipTranspose;
    // we might be able
    // to skip the transpose into the first temporary Field
gsell's avatar
gsell committed
640

641 642 643 644 645 646
    // check that zeroth axis is the same, has one vnode,
    // and that there are no guard cells
    skipTranspose = ( (in_dom[0].sameBase(temp_dom[0])) &&
                      (in_dom[0].length() == temp_dom[0].length()) &&
                      (in_layout.numVnodes() == 1) &&
                      (f.getGC() == FFT<CCTransform,1U,T>::nullGC) );
gsell's avatar
gsell committed
647

648 649 650 651 652
    if (!skipTranspose) {
        // assign to Field with proper layout
        (*tempFields_m) = (*temp);
        temp = tempFields_m;  // Field* management aid
    }
653 654 655



gsell's avatar
gsell committed
656

657 658 659 660 661 662 663 664 665
    // should be only one LField!
    typename ComplexField_t::const_iterator_if l_i = temp->begin_if();
    if (l_i != temp->end_if()) {
        // Get the LField
        ComplexLField_t* ldf = (*l_i).second.get();
        // make sure we are uncompressed
        ldf->Uncompress();
        // get the raw data pointer
        localdata = ldf->getP();
gsell's avatar
gsell committed
666

667 668 669
        // Do the 1D FFT:
        this->getEngine().callFFT(0, direction, localdata);
    }
gsell's avatar
gsell committed
670

671

gsell's avatar
gsell committed
672

673 674
    // skip final assignment and compress if we used f as final temporary
    if (temp != &f) {
675

676 677 678
        // Now assign back into original Field, and compress last temp's storage:
        f = (*temp);
        if (this->compressTemps()) *temp = 0;
679

680
    }
gsell's avatar
gsell committed
681

682 683 684
    // Normalize:
    if (direction == +1)
        f *= Complex_t(this->getNormFact(), 0.0);
gsell's avatar
gsell committed
685

686
    return;
gsell's avatar
gsell committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
}



//=============================================================================
// FFT RCTransform Constructors
//=============================================================================

//-----------------------------------------------------------------------------
// Create a new FFT object of type RCTransform, with a
// given domain. Also specify which dimensions to transform along.
// Note that RC transform of a real array of length n results in a
// complex array of length n/2+1.
//-----------------------------------------------------------------------------

gsell's avatar
gsell committed
702
template <size_t Dim, class T>
gsell's avatar
gsell committed
703
FFT<RCTransform,Dim,T>::FFT(
704 705 706 707 708
    const typename FFT<RCTransform,Dim,T>::Domain_t& rdomain,
    const typename FFT<RCTransform,Dim,T>::Domain_t& cdomain,
    const bool transformTheseDims[Dim], const bool& compressTemps)
: FFTBase<Dim,T>(FFT<RCTransform,Dim,T>::rcFFT, rdomain,
                 transformTheseDims, compressTemps),
gsell's avatar
gsell committed
709 710
    complexDomain_m(cdomain), serialAxes_m(1)
{
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
    // construct array of axis lengths
    size_t nTransformDims = this->numTransformDims();
    int* lengths = new int[nTransformDims];
    size_t d;
    for (d=0; d<nTransformDims; ++d)
        lengths[d] = rdomain[this->activeDimension(d)].length();

    // construct array of transform types for FFT Engine, compute normalization
    int* transformTypes = new int[nTransformDims];
    T& normFact = this->getNormFact();
    normFact = 1.0;
    transformTypes[0] = FFTBase<Dim,T>::rcFFT;    // first transform is real-to-complex
    normFact /= lengths[0];
    for (d=1; d<nTransformDims; ++d) {
        transformTypes[d] = FFTBase<Dim,T>::ccFFT;  // all other transforms are complex-to-complex
        normFact /= lengths[d];
    }

    // set up FFT Engine
    this->getEngine().setup(nTransformDims, transformTypes, lengths);
    delete [] transformTypes;
    delete [] lengths;

    // set up the temporary fields
    setup();
gsell's avatar
gsell committed
736 737 738 739 740 741 742
}

//-----------------------------------------------------------------------------
// Create a new FFT object of type RCTransform, with
// given real and complex domains. Default: transform along all dimensions.
//-----------------------------------------------------------------------------

gsell's avatar
gsell committed
743
template <size_t Dim, class T>
gsell's avatar
gsell committed
744
FFT<RCTransform,Dim,T>::FFT(
745 746 747 748 749
    const typename FFT<RCTransform,Dim,T>::Domain_t& rdomain,
    const typename FFT<RCTransform,Dim,T>::Domain_t& cdomain,
    const bool& compressTemps,
    int serialAxes)
: FFTBase<Dim,T>(FFT<RCTransform,Dim,T>::rcFFT, rdomain, compressTemps),
gsell's avatar
gsell committed
750 751
    complexDomain_m(cdomain), serialAxes_m(serialAxes)
{
752
    // Tau profiling
753

754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
    // construct array of axis lengths
    int lengths[Dim];
    size_t d;
    for (d=0; d<Dim; ++d)
        lengths[d] = rdomain[d].length();

    // construct array of transform types for FFT Engine, compute normalization
    int transformTypes[Dim];
    T& normFact = this->getNormFact();
    normFact = 1.0;
    transformTypes[0] = FFTBase<Dim,T>::rcFFT;    // first transform is real-to-complex
    normFact /= lengths[0];
    for (d=1; d<Dim; ++d) {
        transformTypes[d] = FFTBase<Dim,T>::ccFFT;  // all other transforms are complex-to-complex
        normFact /= lengths[d];
    }
770

771 772
    // set up FFT Engine
    this->getEngine().setup(Dim, transformTypes, lengths);
773

774 775
    // set up the temporary fields
    setup();
gsell's avatar
gsell committed
776 777 778 779 780 781 782
}

//-----------------------------------------------------------------------------
// setup performs all the initializations necessary after the transform
// directions have been specified.
//-----------------------------------------------------------------------------

gsell's avatar
gsell committed
783
template <size_t Dim, class T>
gsell's avatar
gsell committed
784 785 786
void
FFT<RCTransform,Dim,T>::setup(void) {

787
    // Tau profiling
gsell's avatar
gsell committed
788

789 790 791



792 793
    PAssert_GT(serialAxes_m, 0);
    PAssert_LT((size_t) serialAxes_m, Dim);
gsell's avatar
gsell committed
794

795 796
    size_t d, d2, activeDim;
    size_t nTransformDims = this->numTransformDims();
gsell's avatar
gsell committed
797

798
    // Set up the arrays of temporary Fields and FieldLayouts:
gsell's avatar
gsell committed
799

800 801 802 803 804 805 806
    // make first dimension(s) always SERIAL, all other dimensions parallel
    // for the real FFT; make first serialAxes_m axes serial for others
    e_dim_tag serialParallel[Dim];
    e_dim_tag NserialParallel[Dim];
    for (d=0; d < Dim; ++d) {
        serialParallel[d] = (d == 0 ? SERIAL : PARALLEL);
        NserialParallel[d] = (d < (size_t) serialAxes_m ? SERIAL : PARALLEL);
gsell's avatar
gsell committed
807 808
    }

809 810 811 812 813 814 815 816 817 818 819 820 821 822
    // check that domain lengths agree between real and complex domains
    const Domain_t& domain = this->getDomain();
    activeDim = this->activeDimension(0);
    bool match = true;
    for (d=0; d<Dim; ++d) {
        if (d == activeDim) {
            // real array length n, complex array length n/2+1
            if ( complexDomain_m[d].length() !=
                 (domain[d].length()/2 + 1) ) match = false;
        }
        else {
            // real and complex arrays should be same length for all other dims
            if (complexDomain_m[d].length() != domain[d].length()) match = false;
        }
gsell's avatar
gsell committed
823
    }
824 825 826 827 828 829
    PInsist(match,
            "Domains provided for real and complex Fields are incompatible!");

    // allocate arrays of temp fields and layouts for complex fields
    tempLayouts_m = new Layout_t*[nTransformDims];
    tempFields_m = new ComplexField_t*[nTransformDims];
gsell's avatar
gsell committed
830

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
    // set up the single temporary real field, with first dim serial, others par

    // make new domains with permuted Indexes, activeDim first
    Domain_t ndip;
    Domain_t ndipc;
    ndip[0] = domain[activeDim];
    ndipc[0] = complexDomain_m[activeDim];
    for (d=1; d<Dim; ++d) {
        size_t nextDim = activeDim + d;
        if (nextDim >= Dim) nextDim -= Dim;
        ndip[d] = domain[nextDim];
        ndipc[d] = complexDomain_m[nextDim];
    }

    // generate layout and object for temporary real field
    tempRLayout_m = new Layout_t(ndip, serialParallel, this->transVnodes());
    tempRField_m = new RealField_t(*tempRLayout_m);

    // generate layout and object for first temporary complex Field
    tempLayouts_m[0] = new Layout_t(ndipc, serialParallel, this->transVnodes());
    tempFields_m[0] = new ComplexField_t(*tempLayouts_m[0]);

    // determine the order in which dimensions will be transposed.  Put
    // the transposed dims first, and the others at the end.
    int fftorder[Dim], tmporder[Dim];
    int nofft = nTransformDims;
    for (d=0; d < nTransformDims; ++d)
        fftorder[d] = this->activeDimension(d);
    for (d=0; d < Dim; ++d) {
        // see if the dth dimension is one to transform
        bool active = false;
        for (d2=0; d2 < nTransformDims; ++d2) {
            if (this->activeDimension(d2) == d) {
                active = true;
                break;
            }
        }

        if (!active)
            // no it is not; put it at the bottom of list
            fftorder[nofft++] = d;
    }

    // But since the first FFT is done on a S,[P,P,...] field, permute
    // the order of this to get the first activeDimension at the end.
    nofft = fftorder[0];
    for (d=0; d < (Dim - 1); ++d)
        fftorder[d] = fftorder[d+1];
    fftorder[Dim-1] = nofft;

    // now construct the remaining temporary complex fields

    // loop through and create actual permuted layouts, and also fields
    size_t dim = 1;			// already have one temp field
    while (dim < nTransformDims) {

        int sp;
        for (sp=0; sp < serialAxes_m && dim < nTransformDims; ++sp, ++dim) {

            // make new domain with permuted Indexes
            for (d=0; d < Dim; ++d)
                ndip[d] = complexDomain_m[fftorder[d]];

            // generate layout and object for temporary complex Field
            tempLayouts_m[dim] = new Layout_t(ndip, NserialParallel, this->transVnodes());
            tempFields_m[dim] = new ComplexField_t(*tempLayouts_m[dim]);

            // permute the fft order for the first 'serialAxes_m' axes
            if (serialAxes_m > 1) {
                tmporder[0] = fftorder[0];
                for (d=0; d < (size_t) (serialAxes_m-1); ++d)
                    fftorder[d] = fftorder[d+1];
                fftorder[serialAxes_m - 1] = tmporder[0];
            }
        }

        // now, permute ALL the axes by serialAxes_m steps, to get the next
        // set of axes in the first n serial slots
        for (d=0; d < Dim; ++d)
            tmporder[d] = fftorder[d];
        for (d=0; d < Dim; ++d)
            fftorder[d] = tmporder[(d + serialAxes_m) % Dim];
    }
gsell's avatar
gsell committed
914 915 916 917 918 919 920
}


//-----------------------------------------------------------------------------
// Destructor
//-----------------------------------------------------------------------------

gsell's avatar
gsell committed
921
template <size_t Dim, class T>
gsell's avatar
gsell committed
922 923
FFT<RCTransform,Dim,T>::~FFT(void) {

924
    // Tau profiling
925 926


927 928 929 930 931 932 933 934 935 936
    // delete temporary fields and layouts
    size_t nTransformDims = this->numTransformDims();
    for (size_t d=0; d<nTransformDims; ++d) {
        delete tempFields_m[d];
        delete tempLayouts_m[d];
    }
    delete [] tempFields_m;
    delete [] tempLayouts_m;
    delete tempRField_m;
    delete tempRLayout_m;
uldis_l's avatar
uldis_l committed
937

gsell's avatar
gsell committed
938 939 940
}

//-----------------------------------------------------------------------------
uldis_l's avatar
uldis_l committed
941
// real-to-complex fft; direction is +1 or -1
gsell's avatar
gsell committed
942 943
//-----------------------------------------------------------------------------

uldis_l's avatar
uldis_l committed
944 945 946 947 948
/*
  gpu version of fft if dks enabled transfers realfield_t to gpu, allocates memory
  on gpu for result field (complex), does the fft and returns
*/
#ifdef IPPL_DKS
gsell's avatar
gsell committed
949
template <size_t Dim, class T>
gsell's avatar
gsell committed
950
void
uldis_l's avatar
uldis_l committed
951
FFT<RCTransform,Dim,T>::transformDKSRC(
952 953 954 955 956 957 958
    int direction,
    typename FFT<RCTransform,Dim,T>::RealField_t& f,
    void* real_ptr,
    void* comp_ptr,
    DKSOPAL &dksbase,
    int streamId,
    const bool& constInput)
gsell's avatar
gsell committed
959
{
960 961 962
    //check the domain of incoming field
    const Layout_t& in_layout = f.getLayout();
    const Domain_t& in_dom = in_layout.getDomain();
963

964
    PAssert_EQ( this->checkDomain(this->getDomain(), in_dom), true);
uldis_l's avatar
uldis_l committed
965

966
    size_t nTransformDims = this->numTransformDims();
uldis_l's avatar
uldis_l committed
967

968 969
    //*** just use f field as is and keep decomposition as defined in input file ***//
    RealField_t* tempR = &f;
970

snuverink_j's avatar
snuverink_j committed
971
    typename RealField_t::const_iterator_if rl_i = tempR->begin_if();
972

973 974 975 976 977 978
    // get the lfields
    RealLField_t* rldf = (*rl_i).second.get();
    // make sure we are uncompressed
    rldf->Uncompress();
    // get the raw data pointers
    T* localreal = rldf->getP();
uldis_l's avatar
uldis_l committed
979

980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
    /** get global dimensions of real domain and local dimensions of real subdomain
        calc global dimensions of complex subdomain */
    int NR_l[Dim], NR_g[Dim], NC_g[Dim];
    for (size_t d = 0; d < Dim; d++) {
        NR_l[d] = (int)rldf->size(d);
        NR_g[d] = (int)tempR->getDomain()[d].length();
        NC_g[d] = NR_g[d];
    }
    NC_g[0] = (NC_g[0] / 2) + 1;

    //get global and local domain sizes
    int sizereal = NR_l[0]*NR_l[1]*NR_l[2];
    int totalreal = tempR->getDomain().size();
    //int totalcomp = NC_g[0]*NC_g[1]*NC_g[2];

    //local vnodes get starting position for real field subdomains
    int *idx = new int[Ippl::getNodes()];
    int *idy = new int[Ippl::getNodes()];
    int *idz = new int[Ippl::getNodes()];
    for (typename Layout_t::const_iterator_iv i_s = tempR->getLayout().begin_iv(); i_s != tempR->getLayout().end_iv(); ++i_s) {
        Domain_t tmp = (*i_s).second->getDomain();
        int node = (*i_s).second->getNode();
        idx[node] = tmp[0].min();
        idy[node] = tmp[1].min();
        idz[node] = tmp[2].min();
    }

    //remote vnodes get starting position for real field subdomains
    for (typename Layout_t::iterator_dv remote = tempR->getLayout().begin_rdv(); remote != tempR->getLayout().end_rdv(); ++remote) {
        Domain_t tmp = (*remote).second->getDomain();
        int node = (*remote).second->getNode();
        idx[node] = tmp[0].min();
        idy[node] = tmp[1].min();
        idz[node] = tmp[2].min();
    }

    int id[3] = {idx[Ippl::myNode()], idy[Ippl::myNode()], idz[Ippl::myNode()]};

    if (Ippl::myNode() == 0) {

        //if only one node is working do dksbase write otherwise use cuda aware mpi
        if (Ippl::getNodes() > 1) {

            if (streamId == -1) {
                //gather data from different mpi processes directly into gpu buffer
                dksbase.gather3DData( real_ptr, localreal, sizereal, MPI_DOUBLE, NR_g, NR_l,
                                      idx, idy, idz,
                                      Ippl::getNodes(), Ippl::myNode(), 0, Ippl::getComm() );
            } else {
                //gather data using CUDA IPC for async data transfer
                dksbase.gather3DDataAsync<T>( real_ptr, localreal, NR_g, NR_l, id, streamId);
                //sync needed to wait for data transfer to finish
                dksbase.syncDevice();
                MPI_Barrier(Ippl::getComm());
            }

        } else {
            //write real data to device
            dksbase.writeDataAsync<T>(real_ptr, localreal, totalreal, streamId);
            //dksbase.writeData<T>(real_ptr, localreal, totalreal);
        }

        //call real to complex fft
        dksbase.callR2CFFT(real_ptr, comp_ptr, nTransformDims, (int*)NR_g, streamId);

        //normalize fft
        if (direction == +1)
            dksbase.callNormalizeFFT(comp_ptr, nTransformDims, (int*) NC_g, streamId);
1048

uldis_l's avatar
uldis_l committed
1049
    } else {
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
        if (streamId == -1) {
            //send data via gatherv to gpu controled by root process
            dksbase.gather3DData( NULL, localreal, sizereal, MPI_DOUBLE, NR_g, NR_l, idx, idy, idz,
                                  Ippl::getNodes(), Ippl::myNode(), 0, Ippl::getComm() );
        } else {
            //transfer data to device memory
            dksbase.gather3DDataAsync<T>( real_ptr, localreal, NR_g, NR_l, id, streamId);
            //sync needed to wait for data transfer to finish
            dksbase.syncDevice();
            MPI_Barrier(Ippl::getComm());
        }

    }
    /* end dks part */

    // finish timing the whole mess
uldis_l's avatar
uldis_l committed
1066 1067 1068
}
#endif

1069
template <size_t Dim, class T>
uldis_l's avatar
uldis_l committed
1070 1071
void
FFT<RCTransform,Dim,T>::transform(
1072 1073 1074 1075
    int direction,
    typename FFT<RCTransform,Dim,T>::RealField_t& f,
    typename FFT<RCTransform,Dim,T>::ComplexField_t& g,
    const bool& constInput)
uldis_l's avatar
uldis_l committed
1076
{
1077
    // time the whole mess
uldis_l's avatar
uldis_l committed
1078

1079 1080
    // indicate we're doing another fft
    // incipplstat(incffts);
1081

1082 1083 1084 1085 1086
    // check domain of incoming fields
    const Layout_t& in_layout = f.getLayout();
    const Domain_t& in_dom = in_layout.getDomain();
    const Layout_t& out_layout = g.getLayout();
    const Domain_t& out_dom = out_layout.getDomain();
1087

gsell's avatar
gsell committed
1088

1089 1090
    PAssert_EQ( this->checkDomain(this->getDomain(),in_dom) &&
                this->checkDomain(complexDomain_m,out_dom), true);
uldis_l's avatar
uldis_l committed
1091

1092 1093 1094 1095
    // common loop iterate and other vars:
    size_t d;
    size_t idim;      // idim loops over the number of transform dims.
    size_t nTransformDims = this->numTransformDims();
uldis_l's avatar
uldis_l committed
1096

1097 1098
    // handle first rc transform separately
    idim = 0;
uldis_l's avatar
uldis_l committed
1099

1100 1101 1102 1103
    RealField_t* tempR = tempRField_m;  // field* management aid
    if (!constInput) {
        // see if we can use input field f as a temporary
        bool skipTemp = true;
1104

1105 1106 1107 1108 1109 1110 1111 1112
        // more rigorous match required here; check that layouts are identical
        if ( !(in_layout == *tempRLayout_m) ) {
            skipTemp = false;
        } else {
            // make sure distributions match
            for (d=0; d<Dim; ++d)
                if (in_layout.getDistribution(d) != tempRLayout_m->getDistribution(d))
                    skipTemp = false;
1113

1114 1115 1116
            // make sure vnode counts match
            if (in_layout.numVnodes() != tempRLayout_m->numVnodes())
                skipTemp = false;
1117

1118 1119 1120 1121
            // also make sure there are no guard cells
            if (!(f.getGC() == FFT<RCTransform,Dim,T>::nullGC))
                skipTemp = false;
        }
uldis_l's avatar
uldis_l committed
1122

1123 1124 1125 1126 1127 1128 1129
        // if we can skip using this temporary, set the tempr pointer to the
        // original incoming field.  otherwise, it will stay pointing at the
        // temporary real field, and we'll need to do a transpose of the data
        // from the original into the temporary.
        if (skipTemp)
            tempR = &f;
    }
1130

1131 1132
    // if we're not using input as a temporary ...
    if (tempR != &f) {
1133 1134


1135 1136
        // transpose AND PERMUTE TO REAL FIELD WITH TRANSFORM DIM FIRST
        (*tempR)[tempR->getDomain()] = f[in_dom];
1137

uldis_l's avatar
uldis_l committed
1138
    }
1139

1140 1141
    // field* for temp field management:
    ComplexField_t* temp = tempFields_m[0];
1142

1143 1144 1145 1146
    // see if we can put final result directly into g.  this is useful if
    // we're doing just a 1d fft of one dimension of a multi-dimensional field.
    if (nTransformDims == 1) {  // only a single rc transform
        bool skipTemp = true;
1147

1148 1149 1150 1151 1152 1153 1154 1155
        // more rigorous match required here; check that layouts are identical
        if (!(out_layout == *tempLayouts_m[0])) {
            skipTemp = false;
        } else {
            for (d=0; d<Dim; ++d)
                if (out_layout.getDistribution(d) !=
                    tempLayouts_m[0]->getDistribution(d))
                    skipTemp = false;
1156

1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
            if ( out_layout.numVnodes() != tempLayouts_m[0]->numVnodes() )
                skipTemp = false;

            // also make sure there are no guard cells
            if (!(g.getGC() == FFT<RCTransform,Dim,T>::nullGC))
                skipTemp = false;

            // if we can skip using the temporary, set the pointer to the output
            // field for the first fft to the second provided field (g)
            if (skipTemp)
                temp = &g;
        }
1169
    }
uldis_l's avatar
uldis_l committed
1170

1171 1172 1173 1174 1175 1176 1177
    // loop over all the vnodes, working on the lfield in each.
    typename RealField_t::const_iterator_if rl_i, rl_end = tempR->end_if();
    typename ComplexField_t::const_iterator_if cl_i = temp->begin_if();
    for (rl_i = tempR->begin_if(); rl_i != rl_end; ++rl_i, ++cl_i) {
        // get the lfields
        RealLField_t* rldf = (*rl_i).second.get();
        ComplexLField_t* cldf = (*cl_i).second.get();
1178

1179 1180 1181
        // make sure we are uncompressed
        rldf->Uncompress();
        cldf->Uncompress();
1182

1183 1184 1185
        // get the raw data pointers
        T* localreal = rldf->getP();
        Complex_t* localcomp = cldf->getP();
1186

1187 1188 1189 1190
        // number of strips should be the same for real and complex lfields!
        int nstrips = 1, lengthreal = rldf->size(0), lengthcomp = cldf->size(0);
        for (d=1; d<Dim; ++d)
            nstrips *= rldf->size(d);
1191 1192


1193 1194 1195 1196 1197
        for (int istrip=0; istrip<nstrips; ++istrip) {
            // move the data into the complex strip, which is two reals longer
            for (int ilen=0; ilen<lengthreal; ilen+=2) {
                localcomp[ilen/2] = Complex_t(localreal[ilen],localreal[ilen+1]);
            }
1198

1199 1200 1201
            // do the 1d real-to-complex fft:
            // note that real-to-complex fft direction is always +1
            this->getEngine().callFFT(idim, +1, localcomp);
1202

1203 1204 1205 1206
            // advance the data pointers
            localreal += lengthreal;
            localcomp += lengthcomp;
        } // loop over 1d strips
1207

1208
    } // loop over all the lfields
1209

1210 1211 1212
    // compress temporary storage
    if (this->compressTemps() && tempR != &f)
        *tempR = 0;
1213

1214
    // now proceed with the other complex-to-complex transforms
gsell's avatar
gsell committed
1215

1216 1217
    // local work array passed to fft:
    Complex_t* localdata;
1218

1219 1220
    // loop over the remaining dimensions to be transformed:
    for (idim = 1; idim < nTransformDims; ++idim) {
1221

1222
        bool skipTranspose = false;
gsell's avatar
gsell committed
1223

1224 1225 1226 1227 1228
        // if this is the last transform dimension, we might be able
        // to skip the last temporary and transpose right into g
        if (idim == nTransformDims-1) {
            // get the domain for comparison
            const Domain_t& last_dom = tempLayouts_m[idim]->getDomain();
gsell's avatar
gsell committed
1229

1230 1231 1232 1233 1234 1235 1236 1237
            // make sure there are no guard cells, and that the first
            // axis matches what we expect and is serial.  only need to
            // check first axis since we're just fft'ing that one dimension.
            skipTranspose = (g.getGC() == FFT<RCTransform,Dim,T>::nullGC &&
                             out_dom[0].sameBase(last_dom[0]) &&
                             out_dom[0].length() == last_dom[0].length() &&
                             out_layout.getDistribution(0) == SERIAL);
        }
gsell's avatar
gsell committed
1238

1239 1240 1241 1242
        if (!skipTranspose) {
            // transpose and permute to field with transform dim first
            (*tempFields_m[idim])[tempLayouts_m[idim]->getDomain()] =
                (*temp)[temp->getLayout().getDomain()];
1243

1244 1245 1246 1247
            // compress out previous iterate's storage:
            if (this->compressTemps())
                *temp = 0;
            temp = tempFields_m[idim];  // field* management aid
gsell's avatar
gsell committed
1248

1249 1250 1251
        } else if (idim == nTransformDims-1) {
            // last transform and we can skip the last temporary field
            // so do the transpose here using g instead
1252

1253
            // transpose and permute to field with transform dim first