OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
Vector_unit_tests.cpp
1/*
2 * Vector_unit_tests.hpp
3 *
4 * Created on: Apr 6, 2016
5 * Author: i-bird
6 */
7
8#ifndef OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_UNIT_TESTS_HPP_
9#define OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_UNIT_TESTS_HPP_
10
11#define BOOST_TEST_DYN_LINK
12#include <boost/test/unit_test.hpp>
13#include <VCluster/VCluster.hpp>
14
15#include <iostream>
16#include "Vector/Vector.hpp"
17
18BOOST_AUTO_TEST_SUITE( vector_test_suite )
19
20#ifdef HAVE_EIGEN
21
22BOOST_AUTO_TEST_CASE(vector_eigen_parallel)
23{
24 Vcluster<> & vcl = create_vcluster();
25
26 if (vcl.getProcessingUnits() != 3)
27 return;
28
29 // 3 Processors 9x9 Matrix to invert
30
31 Vector<double> v(9);
32
33 if (vcl.getProcessUnitID() == 0)
34 {
35 // v row1
36 v.insert(0,0);
37 v.insert(1,1);
38 v.insert(2,2);
39 }
40 else if (vcl.getProcessUnitID() == 1)
41 {
42 v.insert(3,3);
43 v.insert(4,4);
44 v.insert(5,5);
45 }
46 else if (vcl.getProcessUnitID() == 2)
47 {
48 v.insert(6,6);
49 v.insert(7,7);
50 v.insert(8,8);
51 }
52
54 v3 = v;
55
56 // force to sync
57 v.getVec();
58
60 v2 = v;
61
62 // Master has the full vector
63
64 if (vcl.getProcessUnitID() == 0)
65 {
66 BOOST_REQUIRE_EQUAL(v(0),0);
67 BOOST_REQUIRE_EQUAL(v(1),1);
68 BOOST_REQUIRE_EQUAL(v(2),2);
69
70 BOOST_REQUIRE_EQUAL(v(3),3);
71 BOOST_REQUIRE_EQUAL(v(4),4);
72 BOOST_REQUIRE_EQUAL(v(5),5);
73
74 BOOST_REQUIRE_EQUAL(v(6),6);
75 BOOST_REQUIRE_EQUAL(v(7),7);
76 BOOST_REQUIRE_EQUAL(v(8),8);
77
78 // Change the vector on Master
79
80 v(0) = 8;
81 v(1) = 7;
82 v(2) = 6;
83 v(3) = 5;
84 v(4) = 4;
85 v(5) = 3;
86 v(6) = 2;
87 v(7) = 1;
88 v(8) = 0;
89 }
90
91 v.scatter();
92 v2.scatter();
93 v3.scatter();
94
95 if (vcl.getProcessUnitID() == 0)
96 {
97 BOOST_REQUIRE_EQUAL(v(0),8);
98 BOOST_REQUIRE_EQUAL(v(1),7);
99 BOOST_REQUIRE_EQUAL(v(2),6);
100
101 BOOST_REQUIRE_EQUAL(v2(0),0);
102 BOOST_REQUIRE_EQUAL(v2(1),1);
103 BOOST_REQUIRE_EQUAL(v2(2),2);
104
105 BOOST_REQUIRE_EQUAL(v3(0),0);
106 BOOST_REQUIRE_EQUAL(v3(1),1);
107 BOOST_REQUIRE_EQUAL(v3(2),2);
108 }
109 else if (vcl.getProcessUnitID() == 1)
110 {
111 BOOST_REQUIRE_EQUAL(v(3),5);
112 BOOST_REQUIRE_EQUAL(v(4),4);
113 BOOST_REQUIRE_EQUAL(v(5),3);
114
115 BOOST_REQUIRE_EQUAL(v2(3),3);
116 BOOST_REQUIRE_EQUAL(v2(4),4);
117 BOOST_REQUIRE_EQUAL(v2(5),5);
118
119 BOOST_REQUIRE_EQUAL(v3(3),3);
120 BOOST_REQUIRE_EQUAL(v3(4),4);
121 BOOST_REQUIRE_EQUAL(v3(5),5);
122 }
123 else if (vcl.getProcessUnitID() == 2)
124 {
125 BOOST_REQUIRE_EQUAL(v(6),2);
126 BOOST_REQUIRE_EQUAL(v(7),1);
127 BOOST_REQUIRE_EQUAL(v(8),0);
128
129 BOOST_REQUIRE_EQUAL(v2(6),6);
130 BOOST_REQUIRE_EQUAL(v2(7),7);
131 BOOST_REQUIRE_EQUAL(v2(8),8);
132
133 BOOST_REQUIRE_EQUAL(v3(6),6);
134 BOOST_REQUIRE_EQUAL(v3(7),7);
135 BOOST_REQUIRE_EQUAL(v3(8),8);
136 }
137}
138
139#endif
140
141#ifdef HAVE_PETSC
142
143BOOST_AUTO_TEST_CASE(vector_petsc_parallel)
144{
145 Vcluster<> & vcl = create_vcluster();
146
147 if (vcl.getProcessingUnits() != 3)
148 return;
149
150 // 3 Processors 9x9 Matrix to invert
151
153
154 if (vcl.getProcessUnitID() == 0)
155 {
156 // v row1
157 v.insert(0,0);
158 v.insert(1,1);
159 v.insert(2,2);
160 }
161 else if (vcl.getProcessUnitID() == 1)
162 {
163 v.insert(3,3);
164 v.insert(4,4);
165 v.insert(5,5);
166 }
167 else if (vcl.getProcessUnitID() == 2)
168 {
169 v.insert(6,6);
170 v.insert(7,7);
171 v.insert(8,8);
172 }
173
175 v3 = v;
176
177 if (vcl.getProcessUnitID() == 0)
178 {
179 // v row1
180 v(0) = 8;
181 v(1) = 7;
182 v(2) = 6;
183 }
184 else if (vcl.getProcessUnitID() == 1)
185 {
186 v(3) = 5;
187 v(4) = 4;
188 v(5) = 3;
189 }
190 else if (vcl.getProcessUnitID() == 2)
191 {
192 v(6) = 2;
193 v(7) = 1;
194 v(8) = 0;
195 }
196
197 // Master has the full vector
198
199 if (vcl.getProcessUnitID() == 0)
200 {
201 BOOST_REQUIRE_EQUAL(v(0),8);
202 BOOST_REQUIRE_EQUAL(v(1),7);
203 BOOST_REQUIRE_EQUAL(v(2),6);
204
205 BOOST_REQUIRE_EQUAL(v3(0),0);
206 BOOST_REQUIRE_EQUAL(v3(1),1);
207 BOOST_REQUIRE_EQUAL(v3(2),2);
208 }
209 else if (vcl.getProcessUnitID() == 1)
210 {
211 BOOST_REQUIRE_EQUAL(v(3),5);
212 BOOST_REQUIRE_EQUAL(v(4),4);
213 BOOST_REQUIRE_EQUAL(v(5),3);
214
215 BOOST_REQUIRE_EQUAL(v3(3),3);
216 BOOST_REQUIRE_EQUAL(v3(4),4);
217 BOOST_REQUIRE_EQUAL(v3(5),5);
218 }
219 else if (vcl.getProcessUnitID() == 2)
220 {
221 BOOST_REQUIRE_EQUAL(v(6),2);
222 BOOST_REQUIRE_EQUAL(v(7),1);
223 BOOST_REQUIRE_EQUAL(v(8),0);
224
225 BOOST_REQUIRE_EQUAL(v3(6),6);
226 BOOST_REQUIRE_EQUAL(v3(7),7);
227 BOOST_REQUIRE_EQUAL(v3(8),8);
228 }
229
230 // Here we get the petsc vector
231 auto & vp = v.getVec();
232
233 // We check the correctness of the PETSC vector
234
235 if (vcl.getProcessUnitID() == 0)
236 {
237 PetscInt ix[] = {0,1,2};
238 PetscScalar y[3];
239
240 VecGetValues(vp,3,ix,y);
241
242 BOOST_REQUIRE_EQUAL(y[0],8);
243 BOOST_REQUIRE_EQUAL(y[1],7);
244 BOOST_REQUIRE_EQUAL(y[2],6);
245 }
246 else if (vcl.getProcessUnitID() == 1)
247 {
248 PetscInt ix[] = {3,4,5};
249 PetscScalar y[3];
250
251 VecGetValues(vp,3,ix,y);
252
253 BOOST_REQUIRE_EQUAL(y[0],5);
254 BOOST_REQUIRE_EQUAL(y[1],4);
255 BOOST_REQUIRE_EQUAL(y[2],3);
256 }
257 else if (vcl.getProcessUnitID() == 2)
258 {
259 PetscInt ix[] = {6,7,8};
260 PetscScalar y[3];
261
262 VecGetValues(vp,3,ix,y);
263
264 BOOST_REQUIRE_EQUAL(y[0],2);
265 BOOST_REQUIRE_EQUAL(y[1],1);
266 BOOST_REQUIRE_EQUAL(y[2],0);
267 }
268
269}
270
271#endif
272
273BOOST_AUTO_TEST_SUITE_END()
274
275
276#endif /* OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_UNIT_TESTS_HPP_ */
size_t getProcessUnitID()
Get the process unit id.
size_t getProcessingUnits()
Get the total number of processors.
Implementation of VCluster class.
Definition VCluster.hpp:59
Sparse Matrix implementation stub object when OpenFPM is compiled with no linear algebra support.
Definition Vector.hpp:40
void scatter()
scatter
Definition Vector.hpp:144