Debian Codegraph @

packages/rol/adapters/tpetra/test/vector/test_04.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include "ROL_Stream.hpp"
11 #include "Teuchos_GlobalMPISession.hpp"
12 #include "Teuchos_DefaultMpiComm.hpp"
13
14 #include "ROL_PinTVector.hpp"
15 #include "ROL_PinTVectorCommunication_Tpetra.hpp"
16
17 #include "Tpetra_Core.hpp"
18
19 typedef double RealT;
20 typedef double ElementT;
21
22 typedef Tpetra::Map<>::local_ordinal_type LO;
23 typedef Tpetra::Map<>::global_ordinal_type GO;
24 typedef Tpetra::Map<>::node_type Node;
25 typedef Tpetra::Map<LO, GO, Node> Map;
26 typedef Tpetra::MultiVector<RealT, LO, GO, Node> MV;
27
28
29 int main(int argc, char* argv[])
30 {
31 typedef ROL::Ptr<MV> MVP;
32 typedef ROL::Ptr<ROL::Vector<RealT>> PtrVector;
33
34 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
35 ROL::Ptr<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
36
37 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
38 int iprint = argc - 1;
39 ROL::Ptr<std::ostream> outStream;
40 ROL::nullstream bhs; // outputs nothing
41 if (iprint > 0)
42 outStream = ROL::makePtrFromRef(std::cout);
43 else
44 outStream = ROL::makePtrFromRef(bhs);
45
46 int errorFlag = 0;
47 RealT errtol = ROL::ROL_THRESHOLD<RealT>();
48
49 int numRanks = -1;
50 int myRank = -1;
51
52 MPI_Comm_size(MPI_COMM_WORLD, &numRanks);
53 MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
54 std::string procStr = std::to_string(myRank) + "/" + std::to_string(numRanks) + ": ";
55
56 *outStream << "Proc " << myRank << "/" << numRanks << std::endl;
57
58 try {
59
60
61 int spatialProcs = 1;
62 ROL::Ptr<ROL::PinTCommunicators> pintComm = ROL::makePtr<ROL::PinTCommunicators>(MPI_COMM_WORLD,spatialProcs);
63 ROL::Ptr<const ROL::PinTVectorCommunication<RealT>> vectorComm = ROL::makePtr<ROL::PinTVectorCommunication_Tpetra<RealT>>();
64
65 ROL::Ptr<const Teuchos::Comm<int>> mpiSpaceComm = ROL::makePtr<Teuchos::MpiComm<int>>(pintComm->getSpaceCommunicator());
66
67 int numEntries = 20;
68 ROL::Ptr<Map> map = ROL::makePtr<Map>(numEntries,0,mpiSpaceComm);
69
70 *outStream << "Testing checkVector" << std::endl;
71
72 {
73 // allocate state vector
74 MVP x_data = ROL::makePtr<MV>(map,1,true);
75 MVP y_data = ROL::makePtr<MV>(map,1,true);
76 MVP z_data = ROL::makePtr<MV>(map,1,true);
77
78 x_data->randomize();
79 y_data->randomize();
80 z_data->randomize();
81
82 PtrVector x_vec = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(x_data);
83 PtrVector y_vec = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(y_data);
84 PtrVector z_vec = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(z_data);
85
86 std::vector<int> stencil = {-1,0};
87
88 ROL::Ptr<ROL::PinTVector<RealT>> x_pint = ROL::makePtr<ROL::PinTVector<RealT>>(pintComm,vectorComm,x_vec,3*numRanks,stencil);
89 ROL::Ptr<ROL::PinTVector<RealT>> y_pint = ROL::makePtr<ROL::PinTVector<RealT>>(pintComm,vectorComm,y_vec,3*numRanks,stencil);
90 ROL::Ptr<ROL::PinTVector<RealT>> z_pint = ROL::makePtr<ROL::PinTVector<RealT>>(pintComm,vectorComm,z_vec,3*numRanks,stencil);
91
92 /*
93 *outStream << "X = " << std::endl;
94 x_pint->print(*outStream);
95 *outStream << "\n\nY = " << std::endl;
96 y_pint->print(*outStream);
97 *outStream << "\n\nZ = " << std::endl;
98 z_pint->print(*outStream);
99 */
100
101 if(x_pint->numOwnedVectors()!=4) {
102 std::stringstream ss;
103 ss << procStr << "Number owned vectors is " << x_pint->numOwnedVectors() << " is not 4!";
104 throw std::logic_error("Rank " + ss.str());
105 }
106
107 if(x_pint->numOwnedSteps()!=3) {
108 std::stringstream ss;
109 ss << procStr << "Number owned steps is " << x_pint->numOwnedSteps() << " is not 3!";
110 throw std::logic_error("Rank " + ss.str());
111 }
112
113 std::vector<RealT> consistency = x_pint->checkVector(*y_pint,*z_pint,(myRank==0 ? true : false),*outStream);
114 ROL::StdVector<RealT> checkvec( ROL::makePtrFromRef(consistency) );
115 if (checkvec.norm() > std::sqrt(errtol)) {
116 errorFlag++;
117 std::stringstream ss;
118 ss << procStr << "Failed check vector!";
119 throw std::logic_error("Rank " + ss.str());
120 }
121 }
122
123 // test boundary exchange (insert into buffer from vector)
124 /////////////////////////////////////////////////////////////////////////////////////////////
125
126 *outStream << "Testing boundary exchange (left stencil)" << std::endl;
127
128 {
129 std::vector<int> stencil = {-1,0};
130
131 MVP p_data = ROL::makePtr<MV>(map,1,true);
132 p_data->putScalar(1.0);
133 PtrVector p_vec = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(p_data);
134 ROL::Ptr<ROL::PinTVector<RealT>> p_pint = ROL::makePtr<ROL::PinTVector<RealT>>(pintComm,vectorComm,p_vec,3*numRanks,stencil);
135
136 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(-1)) ); // backwards time is owned for this stencil
137 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 0)) );
138 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 1)) );
139 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 2)) );
140
141 p_pint->getVectorPtr(-1)->scale((myRank+1)*100-1); // backwards time is owned for this stencil
142 p_pint->getVectorPtr( 0)->scale((myRank+1)*100+0);
143 p_pint->getVectorPtr( 1)->scale((myRank+1)*100+1);
144 p_pint->getVectorPtr( 2)->scale((myRank+1)*100+2);
145
146 p_pint->boundaryExchange();
147
148 if(myRank!=0) { // no left boundary exchange to check
149 dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getRemoteBufferPtr(-1)).getVector()->sync<Kokkos::HostSpace>();
150 auto p_array = dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getRemoteBufferPtr(-1)).getVector()->getLocalView<Kokkos::HostSpace>();
151
152 TEUCHOS_ASSERT(p_array.rank==2);
153 TEUCHOS_ASSERT(p_array.dimension(0)==numEntries);
154 TEUCHOS_ASSERT(p_array.dimension(1)==1);
155
156 for(int i=0;i<p_array.dimension(0);i++) {
157 bool correct = (p_array(i,0) == (myRank)*100+2);
158 if(not correct) {
159 std::stringstream ss;
160 ss << procStr << "Checking of left boundary exchange failed: expected " << myRank*100+2 << " found " << p_array(i,0) << std::endl;
161 throw std::logic_error("Rank " + ss.str());
162 }
163 }
164 } // end if myRank
165
166 } // end check left
167
168 *outStream << "Passed left boundary exchange" << std::endl;
169
170 *outStream << "Testing boundary exchange (right stencil)" << std::endl;
171
172 {
173 std::vector<int> stencil = {1,0};
174
175 MVP p_data = ROL::makePtr<MV>(map,1,true);
176 p_data->putScalar(1.0);
177 PtrVector p_vec = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(p_data);
178 ROL::Ptr<ROL::PinTVector<RealT>> p_pint = ROL::makePtr<ROL::PinTVector<RealT>>(pintComm,vectorComm,p_vec,3*numRanks,stencil);
179
180 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(0)) );
181 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(1)) );
182 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(2)) );
183
184 p_pint->getVectorPtr( 0)->scale((myRank+1)*100+-2);
185 p_pint->getVectorPtr( 1)->scale((myRank+1)*100+1);
186 p_pint->getVectorPtr( 2)->scale((myRank+1)*100+2);
187 p_pint->getVectorPtr( 3)->scale((myRank+1)*100+3);
188
189 p_pint->boundaryExchange();
190
191 if(myRank!=2) { // no right boundary exchange to check
192 dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getRemoteBufferPtr(p_pint->numOwnedSteps())).getVector()->sync<Kokkos::HostSpace>();
193 auto p_array = dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getRemoteBufferPtr(p_pint->numOwnedSteps())).getVector()->getLocalView<Kokkos::HostSpace>();
194
195 TEUCHOS_ASSERT(p_array.rank==2);
196 TEUCHOS_ASSERT(p_array.dimension(0)==numEntries);
197 TEUCHOS_ASSERT(p_array.dimension(1)==1);
198
199 for(int i=0;i<p_array.dimension(0);i++) {
200 bool correct = (p_array(i,0)== (myRank+2)*100-2);
201 if(not correct) {
202 std::stringstream ss;
203 ss << procStr << "Checking of right boundary exchange failed: expected " << (myRank+2)*100-2 << " found " << p_array(i,0) << std::endl;
204 throw std::logic_error("Rank " + ss.str());
205 }
206 }
207 } // end if myRank
208
209 } // end check right
210
211 *outStream << "Passed right boundary exchange" << std::endl;
212
213 *outStream << "Testing boundary exchange (left,right stencil)" << std::endl;
214
215 {
216 std::vector<int> stencil = {-1,0,1};
217
218 MVP p_data = ROL::makePtr<MV>(map,1,true);
219 p_data->putScalar(1.0);
220 PtrVector p_vec = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(p_data);
221 ROL::Ptr<ROL::PinTVector<RealT>> p_pint = ROL::makePtr<ROL::PinTVector<RealT>>(pintComm,vectorComm,p_vec,3*numRanks,stencil);
222
223 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(0)) );
224 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(1)) );
225 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(2)) );
226
227 p_pint->getVectorPtr(-1)->scale((myRank+1)*100-3);
228 p_pint->getVectorPtr( 0)->scale((myRank+1)*100-2); // this vector is actually sent (because of the stencil)
229 p_pint->getVectorPtr( 1)->scale((myRank+1)*100+1);
230 p_pint->getVectorPtr( 2)->scale((myRank+1)*100+2); // this vector is actually sent (because of the stencil)
231 p_pint->getVectorPtr( 3)->scale((myRank+1)*100+3);
232
233 p_pint->boundaryExchange();
234
235 if(myRank!=0) { // no left boundary exchange to check
236 dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getRemoteBufferPtr(-1)).getVector()->sync<Kokkos::HostSpace>();
237 auto p_array = dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getRemoteBufferPtr(-1)).getVector()->getLocalView<Kokkos::HostSpace>();
238
239 TEUCHOS_ASSERT(p_array.rank==2);
240 TEUCHOS_ASSERT(p_array.dimension(0)==numEntries);
241 TEUCHOS_ASSERT(p_array.dimension(1)==1);
242
243 for(int i=0;i<p_array.dimension(0);i++) {
244 bool correct = (p_array(i,0)== (myRank)*100+2);
245 if(not correct) {
246 std::stringstream ss;
247 ss << procStr << "Checking of left/right boundary exchange failed: expected " << myRank*100+2 << " found " << p_array(i,0) << std::endl;
248 throw std::logic_error("Rank " + ss.str());
249 }
250 }
251 } // end if myRank
252
253 if(myRank!=2) { // no right boundary exchange to check
254 dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getRemoteBufferPtr(p_pint->numOwnedSteps())).getVector()->sync<Kokkos::HostSpace>();
255 auto p_array = dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getRemoteBufferPtr(p_pint->numOwnedSteps())).getVector()->getLocalView<Kokkos::HostSpace>();
256
257 for(int i=0;i<p_array.dimension(0);i++) {
258 bool correct = (p_array(i,0)== (myRank+2)*100-2);
259 if(not correct) {
260 std::stringstream ss;
261 ss << procStr << "Checking of left/right boundary exchange failed: expected " << (myRank+2)*100-2 << " found " << p_array(i,0) << std::endl;
262 throw std::logic_error("Rank " + ss.str());
263 }
264 }
265 } // end if myRank
266
267 } // end check left/rigth
268
269 *outStream << "Passed left/right boundary exchange" << std::endl;
270
271 // test boundary exchange (sum into vector from buffer)
272 /////////////////////////////////////////////////////////////////////////////////////////////
273
274 *outStream << "Testing sum boundary exchange (left stencil)" << std::endl;
275
276 {
277 std::vector<int> stencil = {-1,0};
278
279 MVP p_data = ROL::makePtr<MV>(map,1,true);
280 p_data->putScalar(1.0);
281 PtrVector p_vec = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(p_data);
282 ROL::Ptr<ROL::PinTVector<RealT>> p_pint = ROL::makePtr<ROL::PinTVector<RealT>>(pintComm,vectorComm,p_vec,3*numRanks,stencil);
283
284 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(-1)) ); // backwards time is owned for this stencil
285 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 0)) );
286 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 1)) );
287 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 2)) );
288 TEUCHOS_ASSERT( ROL::is_nullPtr(p_pint->getVectorPtr( 3)) );
289
290 p_pint->getRemoteBufferPtr(-1)->scale((myRank)*100-5);
291
292 p_pint->getVectorPtr(-1)->scale((myRank)*100-1);
293 p_pint->getVectorPtr( 0)->scale((myRank)*100+0);
294 p_pint->getVectorPtr( 1)->scale((myRank)*100+1);
295 p_pint->getVectorPtr( 2)->scale((myRank)*100+2);
296
297 p_pint->boundaryExchangeSumInto();
298
299 if(myRank!=2) {
300 dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getVectorPtr(2)).getVector()->sync<Kokkos::HostSpace>();
301 auto p_array = dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getVectorPtr(2)).getVector()->getLocalView<Kokkos::HostSpace>();
302
303 TEUCHOS_ASSERT(p_array.rank==2);
304 TEUCHOS_ASSERT(p_array.dimension(0)==numEntries);
305 TEUCHOS_ASSERT(p_array.dimension(1)==1);
306
307 for(int i=0;i<p_array.dimension(0);i++) {
308 double correct_value = ((myRank)*100+2) + ((myRank+1)*100-5);
309 bool correct = (p_array(i,0)== correct_value);
310 if(not correct) {
311 std::stringstream ss;
312 ss << procStr << "Checking of left boundary exchange failed: expected " << correct_value << " found " << p_array(i,0) << std::endl;
313 throw std::logic_error("Rank " + ss.str());
314 }
315 }
316 } // end if myRank
317
318 } // end check left
319
320 *outStream << "Passed left sum boundary exchange" << std::endl;
321
322 *outStream << "Testing sum boundary exchange (right stencil)" << std::endl;
323
324 {
325 std::vector<int> stencil = {1,0};
326
327 MVP p_data = ROL::makePtr<MV>(map,1,true);
328 p_data->putScalar(1.0);
329 PtrVector p_vec = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(p_data);
330 ROL::Ptr<ROL::PinTVector<RealT>> p_pint = ROL::makePtr<ROL::PinTVector<RealT>>(pintComm,vectorComm,p_vec,3*numRanks,stencil);
331
332 TEUCHOS_ASSERT( ROL::is_nullPtr(p_pint->getVectorPtr(-1)) );
333 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 0)) );
334 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 1)) );
335 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 2)) );
336 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 3)) );
337
338 p_pint->getRemoteBufferPtr(3)->scale((myRank)*100+5);
339
340 p_pint->getVectorPtr( 0)->scale((myRank)*100+-2);
341 p_pint->getVectorPtr( 1)->scale((myRank)*100+1);
342 p_pint->getVectorPtr( 2)->scale((myRank)*100+2);
343 p_pint->getVectorPtr( 3)->scale((myRank)*100+3);
344
345 p_pint->boundaryExchangeSumInto();
346
347 if(myRank!=0) {
348 dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getVectorPtr(0)).getVector()->sync<Kokkos::HostSpace>();
349 auto p_array = dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getVectorPtr(0)).getVector()->getLocalView<Kokkos::HostSpace>();
350
351 for(int i=0;i<p_array.dimension(0);i++) {
352 double correct_value = ((myRank)*100-2) + ((myRank-1)*100+5);
353 bool correct = (p_array(i,0) == correct_value);
354 if(not correct) {
355 std::stringstream ss;
356 ss << procStr << "Checking of right boundary exchange failed: expected " << correct_value << " found " << p_array(i,0) << std::endl;
357 throw std::logic_error("Rank " + ss.str());
358 }
359 }
360 } // end if myRank
361
362 } // end check right
363
364 *outStream << "Passed right sum boundary exchange" << std::endl;
365
366 *outStream << "Testing sum boundary exchange (left,right stencil)" << std::endl;
367
368 // comments are correct in this block: ECC 1/25/2018
369 {
370 std::vector<int> stencil = {-1,0,1};
371
372 MVP p_data = ROL::makePtr<MV>(map,1,true);
373 p_data->putScalar(1.0);
374 PtrVector p_vec = ROL::makePtr<ROL::TpetraMultiVector<RealT>>(p_data);
375 ROL::Ptr<ROL::PinTVector<RealT>> p_pint = ROL::makePtr<ROL::PinTVector<RealT>>(pintComm,vectorComm,p_vec,3*numRanks,stencil);
376
377 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr(-1)) ); // this comes from the stencil
378 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 0)) );
379 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 1)) );
380 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 2)) );
381 TEUCHOS_ASSERT( !ROL::is_nullPtr(p_pint->getVectorPtr( 3)) ); // this comes from the stencil
382
383 p_pint->getRemoteBufferPtr(-1)->scale((myRank)*100-5); // this vector is being sent to left
384 p_pint->getRemoteBufferPtr( 3)->scale((myRank)*100+5); // this vector is being sent to right
385
386 p_pint->getVectorPtr(-1)->scale((myRank)*100-3);
387 p_pint->getVectorPtr( 0)->scale((myRank)*100-2); // this vector is recv (from left)
388 p_pint->getVectorPtr( 1)->scale((myRank)*100+1);
389 p_pint->getVectorPtr( 2)->scale((myRank)*100+2); // this vector is recv (from right)
390 p_pint->getVectorPtr( 3)->scale((myRank)*100+3);
391
392 p_pint->boundaryExchangeSumInto();
393
394 if(myRank!=2) {
395 dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getVectorPtr(2)).getVector()->sync<Kokkos::HostSpace>();
396 auto p_array = dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getVectorPtr(2)).getVector()->getLocalView<Kokkos::HostSpace>();
397
398 for(int i=0;i<p_array.dimension(0);i++) {
399 double correct_value = ((myRank)*100+2) + ((myRank+1)*100-5);
400 bool correct = (p_array(i,0)== correct_value);
401 if(not correct) {
402 std::stringstream ss;
403 ss << procStr << "Checking of left/right boundary exchange failed: expected " << correct_value << " found " << p_array(i,0) << std::endl;
404 throw std::logic_error("Rank " + ss.str());
405 }
406 }
407 } // end if myRank
408
409 if(myRank!=0) {
410 dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getVectorPtr(0)).getVector()->sync<Kokkos::HostSpace>();
411 auto p_array = dynamic_cast<ROL::TpetraMultiVector<RealT>&>(*p_pint->getVectorPtr(0)).getVector()->getLocalView<Kokkos::HostSpace>();
412
413 for(int i=0;i<p_array.dimension(0);i++) {
414 double correct_value = ((myRank)*100-2) + ((myRank-1)*100+5);
415 bool correct = (p_array(i,0)== correct_value);
416 if(not correct) {
417 std::stringstream ss;
418 ss << procStr << "Checking of left/right boundary exchange failed: expected " << correct_value << " found " << p_array(i,0) << std::endl;
419 throw std::logic_error("Rank " + ss.str());
420 }
421 }
422 } // end if myRank
423
424 } // end check left/rigth
425
426 *outStream << "Passed left/right sum boundary exchange" << std::endl;
427 }
428 catch (std::logic_error& err) {
429 *outStream << err.what() << "\n";
430 errorFlag = -1000;
431 }; // end try
432
433 int errors = std::abs(errorFlag);
434 MPI_Allreduce(&errors,&errorFlag,1,MPI_INT,MPI_MAX,MPI_COMM_WORLD);
435
436 if (errorFlag != 0)
437 std::cout << "End Result: TEST FAILED\n";
438 else
439 std::cout << "End Result: TEST PASSED\n";
440
441 return 0;
442 }