Debian Codegraph @

packages/isorropia/test/utest/utst_serial.cpp

raw
1 //@HEADER
2 //************************************************************************
3 //
4 // Isorropia: Partitioning and Load Balancing Package
5 // Copyright (2006) Sandia Corporation
6 //
7 //Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 //license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 //************************************************************************
38 //@HEADER
39
40 //This file is organized as follows:
41 //- include-directives
42 //- unit-test functions, each function unit-tests a single capability
43 // in either Isorropia itself, or the Isorropia test-utilities.
44 //- function 'run_serial_utests', which calls the above unit-test functions.
45
46 //---------------------------------------------------------------
47 //Include-directives, to include relevant Isorropia declarations
48 //
49 #include <Isorropia_ConfigDefs.hpp>
50 #include <Isorropia_Exception.hpp>
51 #include <Isorropia_Utils.hpp>
52
53 #include <ispatest_read_distribution.hpp>
54
55 //---------------------------------------------------------------
56 //Unit-test functions, one function per unit-test.
57 //
58 void test_create_comm_plan(bool verbose)
59 {
60 if (verbose) {
61 std::cout << "testing Isorropia::Utils::create_comm_plan... ";
62 }
63
64 std::vector<int> all_old_offsets(4);
65 std::vector<int> all_new_offsets(4);
66
67 all_old_offsets[0] = 0;
68 all_old_offsets[1] = 5;
69 all_old_offsets[2] = 9;
70 all_old_offsets[3] = 15;
71
72 all_new_offsets[0] = 0;
73 all_new_offsets[1] = 7;
74 all_new_offsets[2] = 12;
75 all_new_offsets[3] = 15;
76
77 std::vector<int> send_info;
78 std::vector<int> recv_info;
79
80 //this is a serial test, but imagine we were proc 2...
81 int myPID = 2;
82 Isorropia::Utils::create_comm_plan(myPID, all_old_offsets, all_new_offsets,
83 send_info, recv_info);
84
85 //from the above old/new offset data, we expect that proc 2 would
86 //send 3 elements, starting at offset 0, to proc 1, and that's all.
87 //So we expect recv_info.size()==0 and send_info.size()==3.
88 if (recv_info.size() != 0) {
89 throw Isorropia::Exception("create_comm_plan test 1a failed.");
90 }
91 if (send_info.size() != 3) {
92 throw Isorropia::Exception("create_comm_plan test 1b failed.");
93 }
94
95 //We expect send_info's contents to be the following:
96 if (send_info[0] != 1 || send_info[1] != 0 || send_info[2] != 3) {
97 throw Isorropia::Exception("create_comm_plan test 1c failed.");
98 }
99
100 //now imagine we were proc 1...
101 myPID = 1;
102 Isorropia::Utils::create_comm_plan(myPID, all_old_offsets, all_new_offsets,
103 send_info, recv_info);
104
105 //from the old/new offset data, we expect that proc 1 would send
106 //2 elements, starting at offset 0, to proc 0.
107 //we also expect that proc 1 would recv 3 elements from proc 2,
108 //to be stored at offset 2 in the new local element list.
109 if (recv_info.size() != 3 || send_info.size() != 3) {
110 throw Isorropia::Exception("create_comm_plan test 2a failed.");
111 }
112
113 if (recv_info[0] != 2 || recv_info[1] != 2 || recv_info[2] != 3) {
114 throw Isorropia::Exception("create_comm_plan test 2b failed.");
115 }
116
117 if (send_info[0] != 0 || send_info[1] != 0 || send_info[2] != 2) {
118 throw Isorropia::Exception("create_comm_plan test 2c failed.");
119 }
120
121 all_old_offsets[0] = 0;
122 all_old_offsets[1] = 2;
123 all_old_offsets[2] = 4;
124 all_old_offsets[3] = 15;
125
126 all_new_offsets[0] = 0;
127 all_new_offsets[1] = 5;
128 all_new_offsets[2] = 10;
129 all_new_offsets[3] = 15;
130
131 //now imagine we were proc 1...
132 myPID = 1;
133 Isorropia::Utils::create_comm_plan(myPID, all_old_offsets, all_new_offsets,
134 send_info, recv_info);
135
136 //from the old/new offset data, we expect that proc 1 would send
137 //2 elements, starting at offset 0, to proc 0.
138 //we also expect that proc 1 would recv 5 elements from proc 2,
139 //to be stored at offset 0 in the new local element list.
140 if (recv_info.size() != 3 || send_info.size() != 3) {
141 throw Isorropia::Exception("create_comm_plan test 3a failed.");
142 }
143
144 if (recv_info[0] != 2 || recv_info[1] != 0 || recv_info[2] != 5) {
145 throw Isorropia::Exception("create_comm_plan test 3b failed.");
146 }
147
148 if (send_info[0] != 0 || send_info[1] != 0 || send_info[2] != 2) {
149 throw Isorropia::Exception("create_comm_plan test 3c failed.");
150 }
151
152 //now imagine we were proc 2...
153 myPID = 2;
154 Isorropia::Utils::create_comm_plan(myPID, all_old_offsets, all_new_offsets,
155 send_info, recv_info);
156
157 //from the old/new offset data, we expect that proc 2 would send
158 //1 element, starting at offset 0, to proc 0.
159 //we also expect that proc 2 would send 5 elements, starting at
160 //offset 1, to proc 1.
161 //we expect that proc 2 would not recv any elements.
162 if (recv_info.size() != 0 || send_info.size() != 6) {
163 throw Isorropia::Exception("create_comm_plan test 4a failed.");
164 }
165
166 if (send_info[0] != 0 || send_info[1] != 0 || send_info[2] != 1) {
167 throw Isorropia::Exception("create_comm_plan test 4b failed.");
168 }
169
170 if (send_info[3] != 1 || send_info[4] != 1 || send_info[5] != 5) {
171 throw Isorropia::Exception("create_comm_plan test 4c failed.");
172 }
173
174 if (verbose) {
175 std::cout << "ok" << std::endl;
176 }
177 }
178
179 void test_read_distribution(bool verbose, const char* path)
180 {
181 if (verbose) {
182 std::cout << "testing ispatest::read_distribution... ";
183 }
184
185 std::vector<int> rows1;
186 std::vector<int> cols1;
187 std::vector<int> partitions1;
188
189 std::string fullname(path);
190 fullname += "/utest_dist1";
191 ispatest::read_distribution(fullname.c_str(), rows1, cols1, partitions1);
192
193 std::vector<int> rows2;
194 std::vector<int> cols2;
195 std::vector<int> partitions2;
196
197 fullname = path;
198 fullname += "/utest_dist2";
199 ispatest::read_distribution(fullname.c_str(), rows2, cols2, partitions2);
200
201 //We know that the utest_dist1 file should contain 6
202 //row-col-partition triplets. So we'll check the vectors to see if
203 //they all have size 6.
204 if (rows1.size() != 6 || cols1.size() != 6 || partitions1.size() != 6) {
205 throw Isorropia::Exception("test_read_distribution check1 failed");
206 }
207
208 //We also know that the contents of utest_dist1 and
209 //utest_dist2 should be the same.
210 if (rows1 != rows2 || cols1 != cols2 ||
211 partitions1 != partitions2) {
212 throw Isorropia::Exception("test_read_distribution check2 failed");
213 }
214
215 std::vector<int> rows3;
216 std::vector<int> cols3;
217 std::vector<int> partitions3;
218
219 fullname = path;
220 fullname += "/utest_dist3";
221 ispatest::read_distribution(fullname.c_str(), rows3, cols3, partitions3);
222
223 //We know that utest_dist3 should contain the same rows and cols as
224 //utest_dist1 and utest_dist2, but different partitions.
225 if (rows3 != rows2 || cols3 != cols1 ||
226 partitions3 == partitions1) {
227 throw Isorropia::Exception("test_read_distribution check3 failed");
228 }
229
230 if (verbose) {
231 std::cout << "ok"<<std::endl;
232 }
233 }
234
235 //---------------------------------------------------------------
236 //The following function, run_serial_utests, is kind of like a 'main',
237 //it calls each of the above unit-test functions.
238 //
239 void run_serial_utests(bool verbose)
240 {
241 if (verbose) {
242 #ifdef HAVE_EPETRA
243 std::cout << "run_serial_utests: HAVE_EPETRA defined."<<std::endl;
244 #else
245 std::cout << "run_serial_utests: HAVE_EPETRA not defined."<<std::endl;
246 #endif
247 }
248
249 test_create_comm_plan(verbose);
250
251 try {
252 test_read_distribution(verbose, ".");
253 }
254 catch(...) {
255 test_read_distribution(verbose, "./utest");
256 }
257 }
258