forked from ddemidov/amgcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvexcl.hpp
300 lines (254 loc) · 7.62 KB
/
vexcl.hpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#ifndef AMGCL_BACKEND_VEXCL_HPP
#define AMGCL_BACKEND_VEXCL_HPP
/*
The MIT License
Copyright (c) 2012-2014 Denis Demidov <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* \file amgcl/backend/vexcl.hpp
* \author Denis Demidov <[email protected]>
* \brief VexCL backend.
*/
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <vexcl/vexcl.hpp>
#include <amgcl/util.hpp>
#include <amgcl/backend/builtin.hpp>
#include <amgcl/backend/detail/default_direct_solver.hpp>
namespace amgcl {
namespace backend {
/// VexCL backend
/**
* This is a backend that uses types defined in the VexCL GPGPU library
* (https://github.com/ddemidov/vexcl).
*
* \param real Value type.
* \ingroup backends
*/
template <typename real>
struct vexcl {
typedef real value_type;
typedef ptrdiff_t index_type;
typedef vex::SpMat<value_type, index_type, index_type> matrix;
typedef vex::vector<value_type> vector;
typedef detail::default_direct_solver<vexcl> direct_solver;
struct provides_row_iterator : boost::false_type {};
/// Backend parameters.
struct params {
/// Command queues that identify compute devices to use with VexCL.
std::vector< vex::backend::command_queue > q;
params() : q(vex::current_context().queue()) {}
params(const boost::property_tree::ptree &p) {
std::vector<vex::backend::command_queue> *ptr = 0;
ptr = p.get("q", ptr);
q = ptr ? *ptr : vex::current_context().queue();
}
};
static std::string name() { return "vexcl"; }
/// Copy matrix from builtin backend.
static boost::shared_ptr<matrix>
copy_matrix(boost::shared_ptr< typename builtin<real>::matrix > A, const params &prm)
{
precondition(!prm.q.empty(), "Empty VexCL context!");
return boost::make_shared<matrix>(prm.q, rows(*A), cols(*A),
A->ptr.data(), A->col.data(), A->val.data()
);
}
/// Copy vector from builtin backend.
static boost::shared_ptr<vector>
copy_vector(typename builtin<real>::vector const &x, const params &prm)
{
precondition(!prm.q.empty(), "Empty VexCL context!");
return boost::make_shared<vector>(prm.q, x);
}
/// Copy vector from builtin backend.
static boost::shared_ptr<vector>
copy_vector(boost::shared_ptr< typename builtin<real>::vector > x, const params &prm)
{
return copy_vector(*x, prm);
}
/// Create vector of the specified size.
static boost::shared_ptr<vector>
create_vector(size_t size, const params &prm)
{
precondition(!prm.q.empty(), "Empty VexCL context!");
return boost::make_shared<vector>(prm.q, size);
}
struct gather {
mutable vex::gather<value_type> G;
gather(size_t src_size, const std::vector<ptrdiff_t> &I, const params &prm)
: G(prm.q, src_size, std::vector<size_t>(I.begin(), I.end())) { }
void operator()(const vector &vec, std::vector<value_type> &vals) const {
G(vec, vals);
}
};
/// Create direct solver for coarse level
static boost::shared_ptr<direct_solver>
create_solver(boost::shared_ptr< typename builtin<real>::matrix > A, const params &prm)
{
return boost::make_shared<direct_solver>(A, prm);
}
};
//---------------------------------------------------------------------------
// Backend interface implementation
//---------------------------------------------------------------------------
template < typename V, typename C, typename P >
struct rows_impl< vex::SpMat<V, C, P> > {
static size_t get(const vex::SpMat<V, C, P> &A) {
return A.rows();
}
};
template < typename V, typename C, typename P >
struct cols_impl< vex::SpMat<V, C, P> > {
static size_t get(const vex::SpMat<V, C, P> &A) {
return A.cols();
}
};
template < typename V, typename C, typename P >
struct nonzeros_impl< vex::SpMat<V, C, P> > {
static size_t get(const vex::SpMat<V, C, P> &A) {
return A.nonzeros();
}
};
template < typename V, typename C, typename P >
struct spmv_impl<
vex::SpMat<V, C, P>,
vex::vector<V>,
vex::vector<V>
>
{
typedef vex::SpMat<V, C, P> matrix;
typedef vex::vector<V> vector;
static void apply(V alpha, const matrix &A, const vector &x,
V beta, vector &y)
{
if (beta)
y = alpha * (A * x) + beta * y;
else
y = alpha * (A * x);
}
};
template < typename V, typename C, typename P >
struct residual_impl<
vex::SpMat<V, C, P>,
vex::vector<V>,
vex::vector<V>,
vex::vector<V>
>
{
typedef vex::SpMat<V, C, P> matrix;
typedef vex::vector<V> vector;
static void apply(const vector &rhs, const matrix &A, const vector &x,
vector &r)
{
r = rhs - A * x;
}
};
template < typename V >
struct clear_impl< vex::vector<V> >
{
static void apply(vex::vector<V> &x)
{
x = 0;
}
};
template < typename V >
struct copy_impl<
vex::vector<V>,
vex::vector<V>
>
{
static void apply(const vex::vector<V> &x, vex::vector<V> &y)
{
y = x;
}
};
template < typename V >
struct copy_to_backend_impl<
vex::vector<V>
>
{
static void apply(const std::vector<V> &data, vex::vector<V> &x)
{
vex::copy(data, x);
}
};
template < typename V >
struct inner_product_impl<
vex::vector<V>,
vex::vector<V>
>
{
static V get(const vex::vector<V> &x, const vex::vector<V> &y)
{
vex::Reductor<V, vex::SUM_Kahan> sum( x.queue_list() );
return sum(x * y);
}
};
template < typename V >
struct axpby_impl<
vex::vector<V>,
vex::vector<V>
> {
static void apply(V a, const vex::vector<V> &x, V b, vex::vector<V> &y)
{
if (b)
y = a * x + b * y;
else
y = a * x;
}
};
template < typename V >
struct axpbypcz_impl<
vex::vector<V>,
vex::vector<V>,
vex::vector<V>
>
{
static void apply(
V a, const vex::vector<V> &x,
V b, const vex::vector<V> &y,
V c, vex::vector<V> &z
)
{
if (c)
z = a * x + b * y + c * z;
else
z = a * x + b * y;
}
};
template < typename V >
struct vmul_impl<
vex::vector<V>,
vex::vector<V>,
vex::vector<V>
>
{
static void apply(V a, const vex::vector<V> &x, const vex::vector<V> &y,
V b, vex::vector<V> &z)
{
if (b)
z = a * x * y + b * z;
else
z = a * x * y;
}
};
} // namespace backend
} // namespace amgcl
#endif