FFT.hpp 141 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4
// -*- C++ -*-
/***************************************************************************
 *
 * The IPPL Framework
5 6
 *
 * This program was prepared by PSI.
gsell's avatar
gsell committed
7 8 9 10 11 12 13 14 15 16 17 18
 * All rights in the program are reserved by PSI.
 * Neither PSI nor the author(s)
 * makes any warranty, express or implied, or assumes any liability or
 * responsibility for the use of this software
 *
 *
 ***************************************************************************/

// -*- C++ -*-
/***************************************************************************
 *
 * The IPPL Framework
19
 *
gsell's avatar
gsell committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * Visit http://people.web.psi.ch/adelmann/ for more details
 *
 ***************************************************************************/

// include files
#include "FFT/FFT.h"
#include "FieldLayout/FieldLayout.h"
#include "Field/BareField.h"
#include "Utility/IpplStats.h"

#ifdef IPPL_PRINTDEBUG
#define FFTDBG(x) x
#else
#define FFTDBG(x)
#endif


38 39
/**
    FFT.cpp:  implementations for FFT constructor/destructor and transforms
gsell's avatar
gsell committed
40 41 42 43 44 45 46
*/


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

47
/**
gsell's avatar
gsell committed
48 49 50 51
    Create a new FFT object of type CCTransform, with a
    given domain. Also specify which dimensions to transform along.
*/

gsell's avatar
gsell committed
52
template <size_t Dim, class T>
gsell's avatar
gsell committed
53
FFT<CCTransform,Dim,T>::FFT(
54
			    const typename FFT<CCTransform,Dim,T>::Domain_t& cdomain,
uldis_l's avatar
uldis_l committed
55 56
			    const bool transformTheseDims[Dim],
			    const bool& compressTemps)
gsell's avatar
gsell committed
57 58 59
  : FFTBase<Dim,T>(FFT<CCTransform,Dim,T>::ccFFT, cdomain,
                   transformTheseDims, compressTemps)
{
60

61
/*
uldis_l's avatar
uldis_l committed
62 63 64 65 66 67
#ifdef IPPL_DKS
#ifdef IPPL_DKS_OPENCL
  INFOMSG("Init DKS base opencl" << endl);
  base.setAPI("OpenCL", 6);
  base.setDevice("-gpu", 4);
  base.initDevice();
68

uldis_l's avatar
uldis_l committed
69
#endif
70

uldis_l's avatar
uldis_l committed
71 72 73 74 75 76
#ifdef IPPL_DKS_CUDA
  INFOMSG("Init DKS base cuda" << endl);
  base.setAPI("Cuda", 4);
  base.setDevice("-gpu", 4);
  base.initDevice();
#endif
77

uldis_l's avatar
uldis_l committed
78 79
#ifdef IPPL_DKS_MIC
  INFOMSG("Init DKS base MIC" << endl);
80
  base.setAPI("OpenMP", 6);
uldis_l's avatar
uldis_l committed
81 82 83 84
  base.setDevice("-mic", 4);
  base.initDevice();
#endif
#endif
85 86
*/

gsell's avatar
gsell committed
87
  // construct array of axis lengths
gsell's avatar
gsell committed
88
  size_t nTransformDims = this->numTransformDims();
gsell's avatar
gsell committed
89
  int* lengths = new int[nTransformDims];
gsell's avatar
gsell committed
90
  size_t d;
gsell's avatar
gsell committed
91 92 93 94 95 96 97 98
  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) {
uldis_l's avatar
uldis_l committed
99 100
    transformTypes[d] = FFTBase<Dim,T>::ccFFT;  // all transforms are complex-to-complex
    normFact /= lengths[d];
gsell's avatar
gsell committed
101 102 103 104 105 106 107 108 109 110 111 112
  }

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



113
/**
gsell's avatar
gsell committed
114 115 116
    setup performs all the initializations necessary after the transform
    directions have been specified.
*/
gsell's avatar
gsell committed
117
template <size_t Dim, class T>
gsell's avatar
gsell committed
118 119 120 121
void
FFT<CCTransform,Dim,T>::setup(void)
{
  // Tau profiling
122

gsell's avatar
gsell committed
123

gsell's avatar
gsell committed
124 125
  size_t d, activeDim;
  size_t nTransformDims = this->numTransformDims();
gsell's avatar
gsell committed
126 127 128 129 130 131 132 133 134 135 136 137
  // 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
gsell's avatar
gsell committed
138
  for (size_t dim=0; dim<nTransformDims; ++dim) {
gsell's avatar
gsell committed
139 140 141 142 143 144 145 146
    // 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) {
gsell's avatar
gsell committed
147
      size_t nextDim = activeDim + d;
gsell's avatar
gsell committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
      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;
}

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

gsell's avatar
gsell committed
166
template <size_t Dim, class T>
gsell's avatar
gsell committed
167 168 169
FFT<CCTransform,Dim,T>::~FFT(void) {

  // Tau profiling
170 171

  /*
uldis_l's avatar
uldis_l committed
172 173 174 175
	  #ifdef IPPL_OPENCL
	  base.ocl_cleanUp();
	  #endif
  */
176

gsell's avatar
gsell committed
177
  // delete arrays of temporary fields and field layouts
gsell's avatar
gsell committed
178 179
  size_t nTransformDims = this->numTransformDims();
  for (size_t d=0; d<nTransformDims; ++d) {
gsell's avatar
gsell committed
180 181 182 183 184 185 186 187 188 189 190 191
    delete tempFields_m[d];
    delete tempLayouts_m[d];
  }
  delete [] tempFields_m;
  delete [] tempLayouts_m;
}


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

gsell's avatar
gsell committed
192
template <size_t Dim, class T>
gsell's avatar
gsell committed
193 194
void
FFT<CCTransform,Dim,T>::transform(
uldis_l's avatar
uldis_l committed
195 196 197 198
				  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
199 200 201 202 203 204 205 206 207
{
  // 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();
208 209
  PAssert_EQ( this->checkDomain(this->getDomain(),in_dom) &&
              this->checkDomain(this->getDomain(),out_dom), true);
gsell's avatar
gsell committed
210 211

  // Common loop iterate and other vars:
gsell's avatar
gsell committed
212
  size_t d;
gsell's avatar
gsell committed
213 214
  int idim;            // idim loops over the number of transform dims.
  int begdim, enddim;  // beginning and end of transform dim loop
gsell's avatar
gsell committed
215
  size_t nTransformDims = this->numTransformDims();
gsell's avatar
gsell committed
216 217 218 219
  // Field* for temp Field management:
  ComplexField_t* temp = &f;
  // Local work array passed to FFT:
  Complex_t* localdata;
220

gsell's avatar
gsell committed
221
  // Loop over the dimensions be transformed:
222 223
  begdim = (direction == +1) ? 0 : static_cast<int>(nTransformDims-1);
  enddim = (direction == +1) ? static_cast<int>(nTransformDims) : -1;
gsell's avatar
gsell committed
224
  for (idim = begdim; idim != enddim; idim += direction) {
225

gsell's avatar
gsell committed
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
    // 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
257
      (*tempFields_m[idim])[tempLayouts_m[idim]->getDomain()] =
gsell's avatar
gsell committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        (*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
    }
275

gsell's avatar
gsell committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    // 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
297 298

  } // loop over all transformed dimensions
gsell's avatar
gsell committed
299 300 301

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

gsell's avatar
gsell committed
303 304 305
    // 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;
306

gsell's avatar
gsell committed
307 308 309 310 311 312 313 314 315 316 317 318
  }

  // Normalize:
  if (direction == +1)
    g *= Complex_t(this->getNormFact(), 0.0);

  return;
}

//-----------------------------------------------------------------------------
// "in-place" FFT; specify +1 or -1 to indicate forward or inverse transform.
//-----------------------------------------------------------------------------
319
/*
uldis_l's avatar
uldis_l committed
320 321 322 323 324 325 326
#ifdef IPPL_DKS
template <unsigned Dim, class T>
void
FFT<CCTransform, Dim, T>::transform(
				    int direction,
				    typename FFT<CCTransform,Dim,T>::ComplexField_t& f)
{
327

uldis_l's avatar
uldis_l committed
328 329 330 331 332 333 334 335
  const Layout_t& in_layout = f.getLayout();
  const Domain_t& in_dom = in_layout.getDomain();
  PAssert(this->checkDomain(this->getDomain(),in_dom));

  //Field* for temp Field management:
  ComplexField_t* temp = &f;
  //Local work array to get data from ComplexField
  Complex_t* localdata;
336

uldis_l's avatar
uldis_l committed
337 338 339 340 341 342 343 344 345 346
  //long total_size = in_dom.size();

  typename ComplexField_t::const_iterator_if l_i, l_end = temp->end_if();
  l_i = temp->begin_if();

  //get the field
  ComplexLField_t* ldf = (*l_i).second.get();
  //make sure we are uncomplressed
  ldf->Uncompress();
  int N[3] = {ldf->size(0), ldf->size(1), ldf->size(2)};
347

uldis_l's avatar
uldis_l committed
348
  localdata = ldf->getP();
349

350
  // DKS part
uldis_l's avatar
uldis_l committed
351 352 353
  int ierr;
  void *mem_ptr;
  int size = N[0]*N[1]*N[2];
354

355
  base.setupFFT(3, N);
uldis_l's avatar
uldis_l committed
356 357
  mem_ptr = base.allocateMemory<Complex_t>(size, ierr);
  ierr = base.writeData<Complex_t>(mem_ptr, localdata, size);
358

uldis_l's avatar
uldis_l committed
359 360
  if (direction == 1) {
    base.callFFT(mem_ptr, 3, N);
361
    base.callNormalizeFFT(mem_ptr, 3, N);
uldis_l's avatar
uldis_l committed
362 363 364 365
  } else {
    base.callIFFT(mem_ptr, 3, N);
    //base.callNormalizeFFT(mem_ptr, 3, N);
  }
366 367


uldis_l's avatar
uldis_l committed
368 369
  base.readData<Complex_t>(mem_ptr, localdata, size);
  base.freeMemory<Complex_t>(mem_ptr, size);
370 371

  //assign back to the original Field
uldis_l's avatar
uldis_l committed
372 373 374 375
  if (temp != &f) {
    f[in_dom] = (*temp)[temp->getLayout().getDomain()];
    if(this->compressTemps()) *temp = 0;
  }
376

uldis_l's avatar
uldis_l committed
377 378 379
  return;
}
#else
380
*/
gsell's avatar
gsell committed
381
template <size_t Dim, class T>
gsell's avatar
gsell committed
382 383
void
FFT<CCTransform,Dim,T>::transform(
uldis_l's avatar
uldis_l committed
384 385
				  int direction,
				  typename FFT<CCTransform,Dim,T>::ComplexField_t& f)
gsell's avatar
gsell committed
386 387 388 389
{

  // indicate we're doing another FFT
  // INCIPPLSTAT(incFFTs);
390

gsell's avatar
gsell committed
391 392 393
  // Check domain of incoming Field
  const Layout_t& in_layout = f.getLayout();
  const Domain_t& in_dom = in_layout.getDomain();
394
  PAssert_EQ(this->checkDomain(this->getDomain(),in_dom), true);
gsell's avatar
gsell committed
395 396

  // Common loop iterate and other vars:
gsell's avatar
gsell committed
397
  size_t d;
gsell's avatar
gsell committed
398 399
  int idim;            // idim loops over the number of transform dims.
  int begdim, enddim;  // beginning and end of transform dim loop
gsell's avatar
gsell committed
400
  size_t nTransformDims = this->numTransformDims();
gsell's avatar
gsell committed
401 402 403 404
  // Field* for temp Field management:
  ComplexField_t* temp = &f;
  // Local work array passed to FFT:
  Complex_t* localdata;
405

gsell's avatar
gsell committed
406
  // Loop over the dimensions be transformed:
407 408
  begdim = (direction == +1) ? 0 : static_cast<int>(nTransformDims-1);
  enddim = (direction == +1) ? static_cast<int>(nTransformDims) : -1;
gsell's avatar
gsell committed
409
  for (idim = begdim; idim != enddim; idim += direction) {
410

gsell's avatar
gsell committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    // 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
442
      (*tempFields_m[idim])[tempLayouts_m[idim]->getDomain()] =
gsell's avatar
gsell committed
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
        (*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
    }
460

gsell's avatar
gsell committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    // 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

483 484

  } // loop over all transformed dimensions
gsell's avatar
gsell committed
485 486 487

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

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

gsell's avatar
gsell committed
493 494 495 496 497 498 499 500
  }

  // Normalize:
  if (direction == +1)
    f *= Complex_t(this->getNormFact(), 0.0);

  return;
}
501
//#endif
gsell's avatar
gsell committed
502 503 504 505 506 507 508 509 510 511 512 513

//=============================================================================
// 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(
514
			   const typename FFT<CCTransform,1U,T>::Domain_t& cdomain,
uldis_l's avatar
uldis_l committed
515
			   const bool transformTheseDims[1U], const bool& compressTemps)
gsell's avatar
gsell committed
516 517 518 519 520
  : FFTBase<1U,T>(FFT<CCTransform,1U,T>::ccFFT, cdomain,
                  transformTheseDims, compressTemps)
{

  // Tau profiling
521 522 523



gsell's avatar
gsell committed
524

gsell's avatar
gsell committed
525
  size_t nTransformDims = 1U;
gsell's avatar
gsell committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
  // get axis length
  int length;
  length = cdomain[0].length();

  // 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;

  // set up FFT Engine
  this->getEngine().setup(nTransformDims, &transformType, &length);
  // set up the temporary fields
  setup();
}

//-----------------------------------------------------------------------------
// 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(
uldis_l's avatar
uldis_l committed
549
			   const typename FFT<CCTransform,1U,T>::Domain_t& cdomain,
550
			   const bool& compressTemps)
gsell's avatar
gsell committed
551 552 553 554
  : FFTBase<1U,T>(FFT<CCTransform,1U,T>::ccFFT, cdomain, compressTemps)
{

  // Tau profiling
555 556 557



gsell's avatar
gsell committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

  // get axis length
  int length;
  length = cdomain[0].length();

  // 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;

  // set up FFT Engine
  this->getEngine().setup(1U, &transformType, &length);
  // set up the temporary fields
  setup();
}

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

template <class T>
void
FFT<CCTransform,1U,T>::setup(void)
{
  // Tau profiling
585 586 587



gsell's avatar
gsell committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

  // 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();

  return;
}

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

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

  // Tau profiling
609 610 611



gsell's avatar
gsell committed
612 613 614 615 616 617 618 619 620 621 622 623 624 625

  // delete temporary fields and field layouts
  delete tempFields_m;
  delete tempLayouts_m;
}


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

template <class T>
void
FFT<CCTransform,1U,T>::transform(
uldis_l's avatar
uldis_l committed
626 627 628 629
				 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
630 631 632 633 634 635 636 637 638 639
{

  // 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();
640 641
  PAssert_EQ( this->checkDomain(this->getDomain(),in_dom) &&
              this->checkDomain(this->getDomain(),out_dom), true);
gsell's avatar
gsell committed
642 643 644 645 646

  // Field* for temp Field management:
  ComplexField_t* temp = &f;
  // Local work array passed to FFT:
  Complex_t* localdata;
647 648


gsell's avatar
gsell committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
  // Now do the serial transforms along this dimension:

  // get temp domain for comparison
  const Domain_t& temp_dom = tempLayouts_m->getDomain();

  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
  // and that there are no guard cells
  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) );

  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

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

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



gsell's avatar
gsell committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710

  // 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();

    // Do the 1D FFT:
    this->getEngine().callFFT(0, direction, localdata);
  }

711

gsell's avatar
gsell committed
712 713 714

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

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

gsell's avatar
gsell committed
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
  }

  // Normalize:
  if (direction == +1)
    g *= Complex_t(this->getNormFact(), 0.0);

  return;
}

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

template <class T>
void
FFT<CCTransform,1U,T>::transform(
uldis_l's avatar
uldis_l committed
736 737
				 int direction,
				 typename FFT<CCTransform,1U,T>::ComplexField_t& f)
gsell's avatar
gsell committed
738 739 740 741 742 743 744 745
{

  // 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();
746
  PAssert_EQ(this->checkDomain(this->getDomain(),in_dom), true);
gsell's avatar
gsell committed
747 748 749 750 751

  // Field* for temp Field management:
  ComplexField_t* temp = &f;
  // Local work array passed to FFT:
  Complex_t* localdata;
752 753


gsell's avatar
gsell committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
  // Now do the serial transforms along this dimension:

  // get domain for comparison
  const Domain_t& temp_dom = tempLayouts_m->getDomain();

  bool skipTranspose;
  // we might be able
  // to skip the transpose into the first temporary Field

  // 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) );

  if (!skipTranspose) {
    // assign to Field with proper layout
    (*tempFields_m) = (*temp);
    temp = tempFields_m;  // Field* management aid
  }
775 776 777



gsell's avatar
gsell committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792

  // 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();

    // Do the 1D FFT:
    this->getEngine().callFFT(0, direction, localdata);
  }

793

gsell's avatar
gsell committed
794 795 796

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

gsell's avatar
gsell committed
798 799 800
    // Now assign back into original Field, and compress last temp's storage:
    f = (*temp);
    if (this->compressTemps()) *temp = 0;
801

gsell's avatar
gsell committed
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
  }

  // Normalize:
  if (direction == +1)
    f *= Complex_t(this->getNormFact(), 0.0);

  return;
}



//=============================================================================
// 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
824
template <size_t Dim, class T>
gsell's avatar
gsell committed
825
FFT<RCTransform,Dim,T>::FFT(
uldis_l's avatar
uldis_l committed
826 827 828
			    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)
gsell's avatar
gsell committed
829
  : FFTBase<Dim,T>(FFT<RCTransform,Dim,T>::rcFFT, rdomain,
830
                   transformTheseDims, compressTemps),
gsell's avatar
gsell committed
831 832
    complexDomain_m(cdomain), serialAxes_m(1)
{
833
/*
uldis_l's avatar
uldis_l committed
834 835 836 837 838 839 840
#ifdef IPPL_DKS
#ifdef IPPL_DKS_OPENCL
  INFOMSG("Init DKS base opencl" << endl);
  //base = DKSBase();
  base.setAPI("OpenCL", 6);
  base.setDevice("-gpu", 4);
  base.initDevice();
841

uldis_l's avatar
uldis_l committed
842
#endif
843

uldis_l's avatar
uldis_l committed
844 845 846 847 848 849 850 851 852 853
#ifdef IPPL_DKS_CUDA
  INFOMSG("Init DKS base cuda" << endl);
  base.setAPI("Cuda", 4);
  base.setDevice("-gpu", 4);
  base.initDevice();

  //create a stream for fft execution other than default
  base.createStream(fftStreamId);

#endif
854

uldis_l's avatar
uldis_l committed
855 856
#ifdef IPPL_DKS_MIC
  INFOMSG("Init DKS base MIC" <<  endl);
857
  base.setAPI("OpenMP", 6);
uldis_l's avatar
uldis_l committed
858 859
  base.setDevice("-mic", 4);
  base.initDevice();
860 861 862 863 864 865 866

  int dimsize[Dim];
  for (int d=0; d<Dim; ++d)
    dimsize[d] = rdomain[d].length();

  base.setupFFTRC(Dim, dimsize);
  base.setupFFTCR(Dim, dimsize,1./(dimsize[0]*dimsize[1]*dimsize[2]));
uldis_l's avatar
uldis_l committed
867 868
#endif
#endif
869
*/
gsell's avatar
gsell committed
870
  // construct array of axis lengths
gsell's avatar
gsell committed
871
  size_t nTransformDims = this->numTransformDims();
gsell's avatar
gsell committed
872
  int* lengths = new int[nTransformDims];
gsell's avatar
gsell committed
873
  size_t d;
gsell's avatar
gsell committed
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
  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();
}

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

gsell's avatar
gsell committed
902
template <size_t Dim, class T>
gsell's avatar
gsell committed
903
FFT<RCTransform,Dim,T>::FFT(
uldis_l's avatar
uldis_l committed
904 905 906 907
			    const typename FFT<RCTransform,Dim,T>::Domain_t& rdomain,
			    const typename FFT<RCTransform,Dim,T>::Domain_t& cdomain,
			    const bool& compressTemps,
			    int serialAxes)
gsell's avatar
gsell committed
908 909 910 911
  : FFTBase<Dim,T>(FFT<RCTransform,Dim,T>::rcFFT, rdomain, compressTemps),
    complexDomain_m(cdomain), serialAxes_m(serialAxes)
{
  // Tau profiling
912

913
/*
uldis_l's avatar
uldis_l committed
914 915 916 917 918 919
#ifdef IPPL_DKS
#ifdef IPPL_DKS_OPENCL
  INFOMSG("Init DKS base opencl" << endl);
  base.setAPI("OpenCL", 6);
  base.setDevice("-gpu", 4);
  base.initDevice();
920

uldis_l's avatar
uldis_l committed
921
#endif
922

uldis_l's avatar
uldis_l committed
923 924 925 926 927
#ifdef IPPL_DKS_CUDA
  INFOMSG("Init DKS base cuda" << endl);
  base.setAPI("Cuda", 4);
  base.setDevice("-gpu", 4);
  base.initDevice();
928
  base.setupFFT(0, NULL);
uldis_l's avatar
uldis_l committed
929 930 931

  base.createStream(fftStreamId);
#endif
932

933

uldis_l's avatar
uldis_l committed
934 935
#ifdef IPPL_DKS_MIC
  INFOMSG("Init DKS base MIC" << endl);
936
  base.setAPI("OpenMP", 6);
uldis_l's avatar
uldis_l committed
937 938
  base.setDevice("-mic", 4);
  base.initDevice();
939 940 941 942 943 944 945 946
//BENI: Setup MIC for RC FFT and CR FFT (creates the different handles)

  int dimsize[Dim];
  for (int d=0; d<Dim; ++d)
    dimsize[d] = rdomain[d].length();

  base.setupFFTRC(Dim, dimsize);
  base.setupFFTCR(Dim, dimsize,1./(dimsize[0]*dimsize[1]*dimsize[2]));
uldis_l's avatar
uldis_l committed
947 948
#endif
#endif
949
*/
gsell's avatar
gsell committed
950 951
  // construct array of axis lengths
  int lengths[Dim];
gsell's avatar
gsell committed
952
  size_t d;
gsell's avatar
gsell committed
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
  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];
  }

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

  // set up the temporary fields
  setup();
}

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

gsell's avatar
gsell committed
979
template <size_t Dim, class T>
gsell's avatar
gsell committed
980 981 982 983 984
void
FFT<RCTransform,Dim,T>::setup(void) {

  // Tau profiling

985 986 987 988 989



  PAssert_GT(serialAxes_m, 0);
  PAssert_LT((size_t) serialAxes_m, Dim);
gsell's avatar
gsell committed
990

gsell's avatar
gsell committed
991 992
  size_t d, d2, activeDim;
  size_t nTransformDims = this->numTransformDims();
gsell's avatar
gsell committed
993 994 995 996 997 998 999 1000 1001

  // Set up the arrays of temporary Fields and FieldLayouts:

  // 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);
gsell's avatar
gsell committed
1002
    NserialParallel[d] = (d < (size_t) serialAxes_m ? SERIAL : PARALLEL);
gsell's avatar
gsell committed
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
  }

  // 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;
    }
  }
  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];

  // 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) {
gsell's avatar
gsell committed
1035
    size_t nextDim = activeDim + d;
gsell's avatar
gsell committed
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
    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
gsell's avatar
gsell committed
1080
  size_t dim = 1;			// already have one temp field
gsell's avatar
gsell committed
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
  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];
gsell's avatar
gsell committed
1097
	for (d=0; d < (size_t) (serialAxes_m-1); ++d)
gsell's avatar
gsell committed
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
	  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];
  }
}


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

gsell's avatar
gsell committed
1117
template <size_t Dim, class T>
gsell's avatar
gsell committed
1118 1119 1120
FFT<RCTransform,Dim,T>::~FFT(void) {

  // Tau profiling
1121 1122


gsell's avatar
gsell committed
1123
  // delete temporary fields and layouts
gsell's avatar
gsell committed
1124 1125
  size_t nTransformDims = this->numTransformDims();
  for (size_t d=0; d<nTransformDims; ++d) {
gsell's avatar
gsell committed
1126 1127 1128 1129 1130 1131 1132
    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
1133

gsell's avatar
gsell committed
1134 1135 1136
}

//-----------------------------------------------------------------------------
uldis_l's avatar
uldis_l committed
1137
// real-to-complex fft; direction is +1 or -1
gsell's avatar
gsell committed
1138 1139
//-----------------------------------------------------------------------------

uldis_l's avatar
uldis_l committed
1140 1141 1142 1143 1144
/*
  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
1145
template <size_t Dim, class T>
gsell's avatar
gsell committed
1146
void
uldis_l's avatar
uldis_l committed
1147 1148 1149 1150 1151
FFT<RCTransform,Dim,T>::transformDKSRC(
				       int direction,
				       typename FFT<RCTransform,Dim,T>::RealField_t& f,
				       void* real_ptr,
				       void* comp_ptr,
1152
				       DKSOPAL &dksbase,
uldis_l's avatar
uldis_l committed
1153 1154
				       int streamId,
				       const bool& constInput)
gsell's avatar
gsell committed
1155
{
uldis_l's avatar
uldis_l committed
1156 1157 1158
  //check the domain of incoming field
  const Layout_t& in_layout = f.getLayout();
  const Domain_t& in_dom = in_layout.getDomain();
1159

1160
  PAssert_EQ( this->checkDomain(this->getDomain(), in_dom), true);
1161

gsell's avatar
gsell committed
1162
  size_t nTransformDims = this->numTransformDims();
1163 1164 1165 1166

  //*** using tempRField_m and transposing f field ***//
  /*
  RealField_t* tempR = tempRField_m;
1167

uldis_l's avatar
uldis_l committed
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
  if (!constInput) {
    // see if we can use input field f as a temporary
    bool skipTemp = true;

    // more rigorous match required here; check that layouts are identical
    if ( !(in_layout == *tempRLayout_m) ) {
      skipTemp = false;
    } else {
      // make sure distributions match
      for (unsigned d=0; d<Dim; ++d)
	if (in_layout.getDistribution(d) != tempRLayout_m->getDistribution(d))
	  skipTemp = false;

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

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

    // 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;
  }
1197

uldis_l's avatar
uldis_l committed
1198 1199 1200 1201 1202
  // if we're not using input as a temporary ...
  if (tempR != &f) {
    // transpose and permute to real field with transform dim first
    (*tempR)[tempR->getDomain()] = f[in_dom];
  }
1203 1204 1205
  */
  //*** just use f field as is and keep decomposition as defined in input file ***//
  RealField_t* tempR = &f;
1206

uldis_l's avatar
uldis_l committed
1207 1208
  typename RealField_t::const_iterator_if rl_i, rl_end = tempR->end_if();
  rl_i = tempR->begin_if();
1209

uldis_l's avatar
uldis_l committed
1210 1211 1212 1213 1214 1215 1216
  // 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();

1217
  /** get global dimensions of real domain and local dimensions of real subdomain
uldis_l's avatar
uldis_l committed
1218
      calc global dimensions of complex subdomain */
1219
  int NR_l[Dim], NR_g[Dim], NC_g[Dim];
gsell's avatar
gsell committed
1220
  for (size_t d = 0; d < Dim; d++) {
uldis_l's avatar
uldis_l committed
1221 1222 1223 1224
    NR_l[d] = (int)rldf->size(d);
    NR_g[d] = (int)tempR->getDomain()[d].length();
    NC_g[d] = NR_g[d];
  }
1225
  NC_g[0] = (NC_g[0] / 2) + 1;
1226 1227

  //get global and local domain sizes
uldis_l's avatar
uldis_l committed
1228 1229 1230
  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];
1231

uldis_l's avatar
uldis_l committed
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
  //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();
  }
1243

uldis_l's avatar
uldis_l committed
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
  //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()]};
1254

uldis_l's avatar
uldis_l committed
1255
  if (Ippl::myNode() == 0) {
1256

uldis_l's avatar
uldis_l committed
1257 1258 1259 1260 1261
    //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
1262 1263
	dksbase.gather3DData( real_ptr, localreal, sizereal, MPI_DOUBLE, NR_g, NR_l,
			   idx, idy, idz,
uldis_l's avatar
uldis_l committed
1264 1265 1266 1267 1268 1269 1270 1271
			   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());
      }
1272

uldis_l's avatar
uldis_l committed
1273 1274
    } else {
      //write real data to device
1275 1276
      dksbase.writeDataAsync<T>(real_ptr, localreal, totalreal, streamId);
      //dksbase.writeData<T>(real_ptr, localreal, totalreal);
uldis_l's avatar
uldis_l committed
1277
    }
1278

1279 1280
    //call real to complex fft
    dksbase.callR2CFFT(real_ptr, comp_ptr, nTransformDims, (int*)NR_g, streamId);
uldis_l's avatar
uldis_l committed
1281 1282 1283

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

uldis_l's avatar
uldis_l committed
1286 1287 1288
  } else {
    if (streamId == -1) {
      //send data via gatherv to gpu controled by root process
1289
      dksbase.gather3DData( NULL, localreal, sizereal, MPI_DOUBLE, NR_g, NR_l, idx, idy, idz,
uldis_l's avatar