File: EPBasis.tmpl

package info (click to toggle)
xmds2 2.2.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 53,384 kB
  • ctags: 7,223
  • sloc: python: 54,076; cpp: 3,929; ansic: 1,463; makefile: 135; sh: 20
file content (193 lines) | stat: -rw-r--r-- 9,185 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
@*
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