-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauxiliary.h
114 lines (87 loc) · 3.41 KB
/
auxiliary.h
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
/*
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef AUXILIARY_H
#define AUXILIARY_H
#include "buffer.h"
#include <cassert>
cl::Buffer performReduction(const cl::Buffer & in,cl::Kernel & ker,cl::CommandQueue & q,int size);
real L2Norm(const Buffer2D & in,cl::CommandQueue & q);
inline real LInfNorm(const Buffer2D & in,cl::CommandQueue & q)
{
cl::Buffer buf = performReduction(in.data(),
CLContextLoader::getRedLInfKer(),
q,
in.width()*in.height());
real ans;
q.enqueueReadBuffer(buf,true,0,sizeof(real),&ans);
return ans;
}
inline real Average(Buffer2D & in,cl::CommandQueue & q)
{
cl::Buffer buf = performReduction(in.data(),
CLContextLoader::getRedSumAllKer(),
q,
in.width()*in.height());
real ans;
q.enqueueReadBuffer(buf,true,0,sizeof(real),&ans);
return ans/ (in.width()*in.height());;
}
inline Buffer2D Difference(const Buffer2D & a,const Buffer2D & b,cl::CommandQueue & q)
{
assert (a.width() == b.width() && a.height() == b.height());
cl::NDRange dim ( a.width()*a.height());
Buffer2D ans (a.width(),a.height());
CLContextLoader::getDiffKer().setArg(0,ans());
CLContextLoader::getDiffKer().setArg(1,a());
CLContextLoader::getDiffKer().setArg(2,b());
q.enqueueNDRangeKernel( CLContextLoader::getDiffKer(),cl::NDRange(0),dim,getBestWorkspaceDim(dim));
return ans;
}
real L2Norm(const Buffer3D & in,cl::CommandQueue & q);
inline real LInfNorm(const Buffer3D & in,cl::CommandQueue & q)
{
cl::Buffer buf = performReduction(in.data(),
CLContextLoader::getRedLInfKer(),
q,
in.width()*in.height()*in.depth());
real ans;
q.enqueueReadBuffer(buf,true,0,sizeof(real),&ans);
return ans;
}
inline real Average(Buffer3D & in,cl::CommandQueue & q)
{
cl::Buffer buf = performReduction(in.data(),
CLContextLoader::getRedSumAllKer(),
q,
in.width()*in.height()*in.depth());
real ans;
q.enqueueReadBuffer(buf,true,0,sizeof(real),&ans);
return ans/ (in.width()*in.height()*in.depth());
}
inline Buffer3D Difference(const Buffer3D & a,const Buffer3D & b,cl::CommandQueue & q)
{
assert (a.width() == b.width() && a.height() == b.height() && a.depth() == b.depth());
cl::NDRange dim ( a.width()*a.height()*a.depth());
Buffer3D ans (a.width(),a.height(),a.depth());
CLContextLoader::getDiffKer().setArg(0,ans());
CLContextLoader::getDiffKer().setArg(1,a());
CLContextLoader::getDiffKer().setArg(2,b());
q.enqueueNDRangeKernel( CLContextLoader::getDiffKer(),cl::NDRange(0),dim,getBestWorkspaceDim(dim));
return ans;
}
Buffer2D fromBitmap(const char * filename);
void toBitmap(const Buffer2D & in,cl::CommandQueue & q,const char * filename);
#endif //AUXILIARY_H