@*
EPBasis.tmpl

Base class for a Basis where each basis function has definite parity
and the parity alternates between successive basis functions.
Bases inheriting from this class will use the faster Parity Matrix Multiplication Transform (PMMT).

Created by Graham Dennis on 2008-12-27.

Copyright (c) 2008-2012, Graham Dennis

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*@
@extends xpdeint.Features.Transforms.Basis

@def transformFunctionStart
static ${matrixType} *_mmt_matrix_forward_even = NULL;
static ${matrixType} *_mmt_matrix_forward_odd  = NULL;
static ${matrixType} *_mmt_matrix_backward_even = NULL;
static ${matrixType} *_mmt_matrix_backward_odd  = NULL;
@end def


@def transformMatricesForDimReps($forwardDimRep, $backwardDimRep)
long _even_${forwardDimRep.name} = (${forwardDimRep.globalLattice} + 1)/2;
long _odd_${forwardDimRep.name} = ${forwardDimRep.globalLattice}/2;
long _even_${backwardDimRep.name} = (${backwardDimRep.globalLattice} + 1)/2;
long _odd_${backwardDimRep.name} = ${backwardDimRep.globalLattice}/2;
_mmt_matrix_forward_even = ($matrixType *)xmds_malloc(sizeof($matrixType) * _even_${forwardDimRep.name} * _even_${backwardDimRep.name});
_mmt_matrix_forward_odd  = ($matrixType *)xmds_malloc(sizeof($matrixType) * _odd_${forwardDimRep.name} * _odd_${backwardDimRep.name});
_mmt_matrix_backward_even = ($matrixType *)xmds_malloc(sizeof($matrixType) * _even_${backwardDimRep.name} * _even_${forwardDimRep.name});
_mmt_matrix_backward_odd  = ($matrixType *)xmds_malloc(sizeof($matrixType) * _odd_${backwardDimRep.name} * _odd_${forwardDimRep.name});

for (long _i0 = 0; _i0 < _even_${forwardDimRep.name}; _i0++) {
  long __i0 = ${forwardDimRep.globalLattice} - 1 - _i0;
  ${transformMatricesForwardDimConstantsAtIndex(forwardDimRep, backwardDimRep, '__i0'), autoIndent=True}@slurp
  for (long _i1 = 0; _i1 < ${backwardDimRep.globalLattice}; _i1++) {
    ${transformMatricesForDimRepsAtIndices(forwardDimRep, backwardDimRep, '_i0', '_i1'), autoIndent=True}@slurp
  }
}
@end def

@def transformMatricesForDimRepsAtIndices($forwardDimRep, $backwardDimRep, $forwardIndex, $backwardIndex)
  @#
if (${backwardIndex} & 1) {
  // ${backwardIndex} is odd
  if (${forwardIndex} < _odd_${forwardDimRep.name}) {
    ${transformMatricesForDimRepsAtIndicesOfKind(forwardDimRep, backwardDimRep, forwardIndex, backwardIndex, 'odd'), autoIndent=True}@slurp
  }
} else {
  // ${backwardIndex} is even
  ${transformMatricesForDimRepsAtIndicesOfKind(forwardDimRep, backwardDimRep, forwardIndex, backwardIndex, 'even'), autoIndent=True}@slurp
}
  @#
@end def

@def transformMatricesForDimRepsAtIndicesOfKind($forwardDimRep, $backwardDimRep, $forwardIndex, $backwardIndex, $kind)
  @#
  @set $logicalForwardIndex = '_' + forwardIndex
  @set $logicalBackwardIndex = backwardIndex
  @set $actualBackwardIndex = '_' + backwardIndex
  @if kind == 'even'
    @set $actualForwardIndex = forwardIndex
  @else
    @set $actualForwardIndex = c'(_odd_${forwardDimRep.name} -1 - $forwardIndex)'
  @end if
long ${actualBackwardIndex} = ${backwardIndex}/2;
_mmt_matrix_forward_${kind}[${actualBackwardIndex} * _${kind}_${forwardDimRep.name} + ${actualForwardIndex}] = \
  ${forwardMatrixForDimAtIndices(forwardDimRep, backwardDimRep, logicalForwardIndex, logicalBackwardIndex)};
_mmt_matrix_backward_${kind}[${actualForwardIndex} * _${kind}_${backwardDimRep.name} + ${actualBackwardIndex}] = \
  ${backwardMatrixForDimAtIndices(forwardDimRep, backwardDimRep, logicalForwardIndex, logicalBackwardIndex)};
  @#
@end def

@def performTransform($sourceDimRep, $destDimRep, $dir = None)
  @#
  @if dir == 'forward'
${performForwardTransform(sourceDimRep, destDimRep)}@slurp
  @else
${performBackwardTransform(sourceDimRep, destDimRep)}@slurp
  @end if
@end def

@def performForwardTransform($sourceDimRep, $destDimRep)
  @#
  @set $blasTypeChar = {'real': {'single': 's', 'double': 'd'}, 'complex': {'single': 'c', 'double': 'z'}}[self.matrixType][$precision]
  @set $alphaBetaPrefix = {'real': '', 'complex': '&'}[self.matrixType]
  @set $matMultFunction = 'cblas_%sgemm' % blasTypeChar
// Loop to create symmetric and antisymmetric components.
${matrixType} _temp;
long outerOffset = _i0 * innerLoopSize * ${sourceDimRep.globalLattice};
for (long _i1 = 0; _i1 < ${sourceDimRep.globalLattice}/2; _i1++) {
  ${matrixType}* __restrict__ _low = &source_data[outerOffset + _i1 * innerLoopSize];
  ${matrixType}* __restrict__ _high = &source_data[outerOffset + (${sourceDimRep.globalLattice} - 1 - _i1) * innerLoopSize];
  for (long _i2 = 0; _i2 < innerLoopSize; _i2++) {
    _temp = _low[_i2];
    _low[_i2] += _high[_i2];  // _low stores the symmetric component
    _high[_i2] -= _temp; // _high stores the antisymmetric component
  }
}
const ${matrixType} alpha = 1.0;
const ${matrixType} beta = 0.0;

// Symmetric component of the transform
${matMultFunction}(CblasRowMajor, CblasNoTrans, CblasNoTrans,
            (${destDimRep.globalLattice}+1)/2,
            /* nelem */ innerLoopSize,
            (${sourceDimRep.globalLattice}+1)/2,
            /* alpha */ ${alphaBetaPrefix}alpha,
            /* A */ _mmt_matrix_forward_even, (${sourceDimRep.globalLattice}+1)/2,
            /* B */ source_data + _i0 * ${sourceDimRep.globalLattice} * innerLoopSize,
                    innerLoopSize,
            /* beta */ ${alphaBetaPrefix}beta,
            /* C */ dest_data + _i0 * ${destDimRep.globalLattice} * innerLoopSize,
            2 * innerLoopSize);
// Antisymmetric component of the transform
${matMultFunction}(CblasRowMajor, CblasNoTrans, CblasNoTrans,
            ${destDimRep.globalLattice}/2,
            /* nelem */ innerLoopSize,
            ${sourceDimRep.globalLattice}/2,
            /* alpha */ ${alphaBetaPrefix}alpha,
            /* A */ _mmt_matrix_forward_odd, ${sourceDimRep.globalLattice}/2,
            /* B */ source_data + (_i0 * ${sourceDimRep.globalLattice} + (${sourceDimRep.globalLattice}+1)/2) * innerLoopSize,
                    innerLoopSize,
            /* beta */ ${alphaBetaPrefix}beta,
            /* C */ dest_data + (_i0 * ${destDimRep.globalLattice} + 1) * innerLoopSize,
            2 * innerLoopSize);
  @#
@end def

@def performBackwardTransform($sourceDimRep, $destDimRep)
  @#
  @set $blasTypeChar = {'real': {'single': 's', 'double': 'd'}, 'complex': {'single': 'c', 'double': 'z'}}[self.matrixType][$precision]
  @set $alphaBetaPrefix = {'real': '', 'complex': '&'}[self.matrixType]
  @set $matMultFunction = 'cblas_%sgemm' % blasTypeChar
const ${matrixType} alpha = 1.0;
const ${matrixType} beta = 0.0;

// Symmetric component of the transform
${matMultFunction}(CblasRowMajor, CblasNoTrans, CblasNoTrans,
            (${destDimRep.globalLattice}+1)/2,
            /* nelem */ innerLoopSize,
            (${sourceDimRep.globalLattice}+1)/2,
            /* alpha */ ${alphaBetaPrefix}alpha,
            /* A */ _mmt_matrix_backward_even, (${sourceDimRep.globalLattice}+1)/2,
            /* B */ source_data + _i0 * ${sourceDimRep.globalLattice} * innerLoopSize,
                    2 * innerLoopSize,
            /* beta */ ${alphaBetaPrefix}beta,
            /* C */ dest_data + _i0 * ${destDimRep.globalLattice} * innerLoopSize,
            innerLoopSize);
// Antisymmetric component of the transform
${matMultFunction}(CblasRowMajor, CblasNoTrans, CblasNoTrans,
            ${destDimRep.globalLattice}/2,
            /* nelem */ innerLoopSize,
            ${sourceDimRep.globalLattice}/2,
            /* alpha */ ${alphaBetaPrefix}alpha,
            /* A */ _mmt_matrix_backward_odd, ${sourceDimRep.globalLattice}/2,
            /* B */ source_data + (_i0 * ${sourceDimRep.globalLattice} + 1) * innerLoopSize,
                    2 * innerLoopSize,
            /* beta */ ${alphaBetaPrefix}beta,
            /* C */ dest_data + (_i0 * ${destDimRep.globalLattice} + (${destDimRep.globalLattice}+1)/2) * innerLoopSize,
            innerLoopSize);
// Loop to unravel symmetric and antisymmetric components.
${matrixType} _temp;
long outerOffset = _i0 * innerLoopSize * ${destDimRep.globalLattice};
for (long _i1 = 0; _i1 < ${destDimRep.globalLattice}/2; _i1++) {
  // _low stored the symmetric component
  ${matrixType}* __restrict__ _low = &dest_data[outerOffset + _i1 * innerLoopSize];
  // _high stored the antisymmetric component
  ${matrixType}* __restrict__ _high = &dest_data[outerOffset + (${destDimRep.globalLattice} - 1 - _i1) * innerLoopSize];
  for (long _i2 = 0; _i2 < innerLoopSize; _i2++) {
    _temp = _low[_i2];
    // _low is the negative domain
    _low[_i2] -= _high[_i2];
    // _high is the positive domain
    _high[_i2] += _temp;
  }
}
  @#
@end def

