-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPUConvolutionDriver.cxx
201 lines (179 loc) · 5 KB
/
CPUConvolutionDriver.cxx
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
194
195
196
197
198
199
200
/*
____ _ __ ____ __ ____
/ __/___(_) / ___ ____/ __ \__ _____ ___ / /_ / _/__ ____
_\ \/ __/ / _ \/ -_) __/ /_/ / // / -_|_-</ __/ _/ // _ \/ __/
/___/\__/_/_.__/\__/_/ \___\_\_,_/\__/___/\__/ /___/_//_/\__(_)
Copyright 2012 SciberQuest Inc.
*/
#include "CPUConvolutionDriver.h"
#include "CartesianExtent.h"
#include "MemOrder.hxx"
#include "Numerics.hxx"
#include "SQMacros.h"
#include "postream.h"
#include "vtkDataArray.h"
#include <iostream>
using std::cerr;
using std::endl;
#include <vector>
using std::vector;
#include <cstdlib>
//#define CPUConvolutionDriverDEBUG
//-----------------------------------------------------------------------------
CPUConvolutionDriver::CPUConvolutionDriver()
:
Optimization(OPT_NONE)
{}
//-----------------------------------------------------------------------------
int CPUConvolutionDriver::Convolution(
CartesianExtent &extV,
CartesianExtent &extW,
CartesianExtent &extK,
int nGhost,
int mode,
vtkDataArray *V,
vtkDataArray *W,
float *K)
{
// TODO - make sure nothing is leaked if an error occurs!
#ifdef CPUConvolutionDriverDEBUG
pCerr()
<< "===============CPUConvolutionDriver::Convolution" << endl;
#endif
int nV[3];
extV.Size(nV);
unsigned long vnijk=extV.Size();
int nW[3];
extW.Size(nW);
unsigned long wnijk=extW.Size();
unsigned long nComp=W->GetNumberOfComponents();
int nK[3];
extK.Size(nK);
unsigned long knijk=extK.Size();
int fastDim=0;
int slowDim=1;
switch (mode)
{
case CartesianExtent::DIM_MODE_2D_XY:
fastDim=0;
slowDim=1;
break;
case CartesianExtent::DIM_MODE_2D_XZ:
fastDim=0;
slowDim=2;
break;
case CartesianExtent::DIM_MODE_2D_YZ:
fastDim=1;
slowDim=2;
break;
case CartesianExtent::DIM_MODE_3D:
fastDim=0;
slowDim=1;
break;
default:
sqErrorMacro(cerr,"Bad dim mode.");
return -1;
}
#ifdef CPUConvolutionDriverDEBUG
pCerr() << "wnijk=" << wnijk << endl;
pCerr() << "fastDim=" << fastDim << endl;
pCerr() << "slowDim=" << slowDim << endl;
pCerr() << "extV=" << extV << endl;
pCerr() << "nV=(" << nV[fastDim] << ", " << nV[slowDim] << ")" << endl;
pCerr() << "extW=" << extW << endl;
pCerr() << "nW=(" << nW[fastDim] << ", " << nW[slowDim] << ")" << endl;
#endif
switch (this->Optimization)
{
///
case OPT_NONE:
switch (V->GetDataType())
{
vtkTemplateMacro(
::Convolution<VTK_TT>(
extV.GetData(),
extW.GetData(),
extK.GetData(),
nComp,
mode,
(VTK_TT*)V->GetVoidPointer(0),
(VTK_TT*)W->GetVoidPointer(0),
K));
}
break;
///
case OPT_FLATTEN_VTK:
case OPT_Z_ORDER:
switch (V->GetDataType())
{
// TODO -- replace with vtkTemplateMacro
case VTK_FLOAT:
{
vector<float*> sV(nComp,0);
vector<float*> sW(nComp,0);
for (unsigned long q=0; q<nComp; ++q)
{
posix_memalign((void**)&sV[q],16,vnijk*sizeof(float));
posix_memalign((void**)&sW[q],16,wnijk*sizeof(float));
}
// convert vtk vectors/tensors into scalar component arrays
float *hV=(float*)V->GetVoidPointer(0);
Split<float>(vnijk,hV,sV);
// apply convolution
for (unsigned long q=0; q<nComp; ++q)
{
if ((mode==CartesianExtent::DIM_MODE_2D_XY)
||(mode==CartesianExtent::DIM_MODE_2D_XZ)
||(mode==CartesianExtent::DIM_MODE_2D_YZ))
{
::ScalarConvolution2D(
nV[fastDim],
nW[fastDim],
wnijk,
nK[fastDim],
knijk,
nGhost,
sV[q],
sW[q],
K);
}
else
{
::ScalarConvolution3D(
nV[fastDim],
nV[fastDim]*nV[slowDim],
nW[fastDim],
nW[fastDim]*nW[slowDim],
wnijk,
nK[fastDim],
nK[fastDim]*nK[slowDim],
knijk,
nGhost,
sV[q],
sW[q],
K);
}
}
// put results in vtk order
float *hW=(float*)W->GetVoidPointer(0);
Interleave(wnijk,sW,hW);
// clean up
for (unsigned long q=0; q<nComp; ++q)
{
free(sW[q]);
free(sV[q]);
}
}
break;
default:
// TODO
sqErrorMacro(cerr,"Not currently using vtkTemplateMacro");
return -1;
}
break;
default:
sqErrorMacro(pCerr(),"Invalid optimzation code " << this->Optimization);
return -1;
}
return 0;
}