-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdense.cpp
310 lines (237 loc) · 7.95 KB
/
dense.cpp
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
301
302
303
304
305
306
307
308
309
310
//
// Copyright (c) 2005, 2006 by CodeSourcery
// Copyright (c) 2013 Stefan Seefeld
// All rights reserved.
//
// This file is part of OpenVSIP. It is made available under the
// license contained in the accompanying LICENSE.GPL file.
#include <vsip/initfin.hpp>
#include <vsip/support.hpp>
#include <vsip/dense.hpp>
#include "test.hpp"
using namespace ovxx;
template <typename Order>
index_type
linear_index(index_type idx0, index_type idx1,
length_type size0, length_type size1);
template <>
index_type
linear_index<row2_type>(index_type idx0, index_type idx1,
length_type /*size0*/, length_type size1)
{
return size1*idx0+idx1;
}
template <>
index_type
linear_index<col2_type>(index_type idx0, index_type idx1,
length_type size0, length_type /*size1*/)
{
return idx0+size0*idx1;
}
/// Check that a block's 1-dimensional and 2-dimensional accessors
/// are consistent with a data layout.
template <typename T,
typename Order>
void
check_order(Dense<2, T, Order>& block)
{
for (index_type i=0; i<block.size(2, 0); ++i)
for (index_type j=0; j<block.size(2, 1); ++j)
block.put(i, j, T(100*i + j));
for (index_type i=0; i<block.size(2, 0); ++i)
{
for (index_type j=0; j<block.size(2, 1); ++j)
{
index_type idx = linear_index<Order>(i, j, block.size(2, 0), block.size(2, 1));
test_assert(equal(block.get(idx), T(100*i + j)));
block.put(idx, T(i + 1000*j));
}
}
for (index_type i=0; i<block.size(2, 0); ++i)
for (index_type j=0; j<block.size(2, 1); ++j)
test_assert(equal(block.get(i, j), T(i + 1000*j)));
}
/// Checks that block can be passed as const arguement.
template <typename T>
void
check_block_const(Dense<1, T> const& block)
{
// Increment the block reference count to make sure that the block
// handles reference counts as a mutable value.
block.increment_count();
for (index_type i=0; i<block.size(); ++i)
test_assert(equal(block.get(i), T(2*i)));
block.decrement_count();
}
/// Simple check of a 1-dimensional block's get() and put() accessors.
template <typename T>
void
check_block_gp(Dense<1, T>& block)
{
for (index_type i=0; i<block.size(); ++i)
block.put(i, T(2*i));
for (index_type i=0; i<block.size(); ++i)
test_assert(equal(block.get(i), T(2*i)));
check_block_const(block);
}
/// Check 2-dimensional block's get() and put() accessors, and dimension
/// order.
template <typename T,
typename Order>
void
check_block_gp(Dense<2, T, Order>& block)
{
for (index_type i=0; i<block.size(2, 0); ++i)
for (index_type j=0; j<block.size(2, 1); ++j)
block.put(i, j, T(100*i + j));
for (index_type i=0; i<block.size(2, 0); ++i)
for (index_type j=0; j<block.size(2, 1); ++j)
test_assert(equal(block.get(i, j), T(100*i + j)));
check_order(block);
}
/// Check 3-dimensional block's get() and put() accessors, and dimension
/// order.
template <typename T,
typename Order>
void
check_block_gp(Dense<3, T, Order>& block)
{
for (index_type i=0; i<block.size(3, 0); ++i)
for (index_type j=0; j<block.size(3, 1); ++j)
for (index_type k=0; k<block.size(3, 2); ++k)
block.put(i, j, k, T(1000*i + 100*j + k));
for (index_type i=0; i<block.size(3, 0); ++i)
for (index_type j=0; j<block.size(3, 1); ++j)
for (index_type k=0; k<block.size(3, 2); ++k)
test_assert(equal(block.get(i, j, k), T(1000*i + 100*j + k)));
// check_order(block);
}
/// Simple check of a 1-dimensional block's ref() accessor.
template <typename T>
void
check_block_at(Dense<1, T>& block)
{
// ref() is only valid if block stores complex in interleaved
// format. Otherwise lvalue_proxy's are used.
if (!is_split_block<Dense<1, T> >::value)
{
for (index_type i=0; i<block.size(); ++i)
block.ref(i) = T(2*i);
for (index_type i=0; i<block.size(); ++i)
test_assert(equal(block.ref(i), T(2*i)));
}
}
/// Simple check of a 2-dimensional block's ref() accessor.
template <typename T,
typename Order>
void
check_block_at(Dense<2, T, Order>& block)
{
if (!is_split_block<Dense<2, T> >::value)
{
for (index_type i=0; i<block.size(2, 0); ++i)
for (index_type j=0; j<block.size(2, 1); ++j)
block.ref(i, j) = T(100*i + j);
for (index_type i=0; i<block.size(2, 0); ++i)
for (index_type j=0; j<block.size(2, 1); ++j)
test_assert(equal(block.ref(i, j), T(100*i + j)));
}
}
/// Simple check of a 2-dimensional block's ref() accessor.
template <typename T,
typename Order>
void
check_block_at(Dense<3, T, Order>& block)
{
if (!is_split_block<Dense<3, T> >::value)
{
for (index_type i=0; i<block.size(3, 0); ++i)
for (index_type j=0; j<block.size(3, 1); ++j)
for (index_type k=0; k<block.size(3, 2); ++k)
block.ref(i, j, k) = T(1000*i + 100*j + k);
for (index_type i=0; i<block.size(3, 0); ++i)
for (index_type j=0; j<block.size(3, 1); ++j)
for (index_type k=0; k<block.size(3, 2); ++k)
test_assert(equal(block.ref(i, j, k), T(1000*i + 100*j + k)));
}
}
/// Create Dense block on stack and check functionality.
template <dimension_type Dim,
typename T>
void
test_stack_dense(Domain<Dim> const& dom)
{
Dense<Dim, T> block(dom);
// Initial reference count is 1. This block will be freed when it
// goes out of scope.
// Check out user-storage functions
test_assert(block.admitted() == true);
test_assert(block.user_storage() == no_user_format);
T* ptr;
block.find(ptr);
test_assert(ptr == NULL);
// Check that block dimension sizes match domain.
length_type total_size = 1;
for (dimension_type d=0; d<Dim; ++d)
{
test_assert(block.size(Dim, d) == dom[d].size());
total_size *= block.size(Dim, d);
}
test_assert(total_size == block.size());
check_block_gp(block);
check_block_at(block);
}
/// Create Dense block on heap and check functionality.
template <dimension_type Dim,
typename T,
typename Order>
void
test_heap_dense(Domain<Dim> const& dom)
{
Dense<Dim, T, Order>* block = new Dense<Dim, T, Order>(dom);
// Initial reference count is 1.
// Check out user-storage functions
test_assert(block->admitted() == true);
test_assert(block->user_storage() == no_user_format);
T* ptr;
block->find(ptr);
test_assert(ptr == NULL);
// Check that block dimension sizes match domain.
length_type total_size = 1;
for (dimension_type d=0; d<Dim; ++d)
{
test_assert(block->size(Dim, d) == dom[d].size());
total_size *= block->size(Dim, d);
}
test_assert(total_size == block->size());
check_block_gp(*block);
check_block_at(*block);
block->decrement_count();
}
int
main(int argc, char** argv)
{
vsipl init(argc, argv);
test_stack_dense<1, int> (Domain<1>(10));
test_stack_dense<1, float> (Domain<1>(10));
test_stack_dense<1, complex<float> >(Domain<1>(10));
test_stack_dense<2, int> (Domain<2>(15, 10));
test_stack_dense<2, float> (Domain<2>(10, 15));
test_stack_dense<2, complex<float> >(Domain<2>(10, 15));
test_stack_dense<3, int> (Domain<3>(15, 10, 25));
test_stack_dense<3, float> (Domain<3>(10, 15, 25));
test_stack_dense<3, complex<double> >(Domain<3>(10, 15, 25));
test_heap_dense<1, int, row1_type>(Domain<1>(10));
test_heap_dense<1, float, row1_type>(Domain<1>(10));
test_heap_dense<1, complex<float>, row1_type>(Domain<1>(10));
test_heap_dense<2, int, row2_type>(Domain<2>(10, 10));
test_heap_dense<2, float, row2_type>(Domain<2>(10, 15));
test_heap_dense<2, complex<double>, row2_type>(Domain<2>(16, 8));
test_heap_dense<2, int, col2_type>(Domain<2>(10, 10));
test_heap_dense<2, float, col2_type>(Domain<2>(10, 15));
test_heap_dense<2, complex<double>, col2_type>(Domain<2>(15, 5));
test_heap_dense<3, float, row3_type>(Domain<3>(15, 5, 3));
test_heap_dense<3, float, col3_type>(Domain<3>(5, 7, 15));
test_heap_dense<3, complex<float>, row3_type>(Domain<3>(5, 7, 3));
test_heap_dense<3, complex<float>, col3_type>(Domain<3>(3, 5, 7));
}