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 311 312 313 314 315 316
|
/////////////////////////////////////////////////////////////
// //
// Copyright (c) 2003-2014 by The University of Queensland //
// Centre for Geoscience Computing //
// http://earth.uq.edu.au/centre-geoscience-computing //
// //
// Primary Business: Brisbane, Queensland, Australia //
// Licensed under the Open Software License version 3.0 //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
/////////////////////////////////////////////////////////////
#include "test_pack.h"
//--- I/O includes ---
#include <iostream>
using std::cout;
using std::endl;
using std::flush;
//--- STL ---
#include <vector>
using std::vector;
//--- project includes ---
#include "packed_message.h"
#include "packed_multi_message.h"
// test 1 :
// pack and unpack some buildin data types (no communication)
bool test_pack(TML_Comm *comm, int rank)
{
bool res=true;
TML_Packed_Message msg(comm->comm(),4); // force grow()
int j,i=42;
double e,d=2.718;
if(rank==0){
// pack
msg.pack(i);
msg.pack(d);
cout << "packed size: " << msg.size() << endl;
// unpack
msg.begin_unpack();
msg.unpack(j);
msg.unpack(e);
cout << "unpacked : " << j << " , " << e << endl;
// ckeck
res=(j==42)&&(e=2.718);
}
return res;
}
// test 2
// pack/unpack some buildin data types (no communication), checked
// pack/unpack buildin data types to/from multi-message
bool test_pack_multi(TML_Comm *comm, int rank)
{
bool res=true;
TML_PackedMultiMessage msg(comm->comm(),4); // force grow()
int j,i=42;
double e,d=2.718;
if(rank==0){
// pack
msg[1].pack(i);
msg[1].pack(d);
//cout << "packed size: " << msg.size() << endl;
// unpack
msg.begin_unpack(1);
msg[1].unpack(j);
msg[1].unpack(e);
cout << "multi unpacked : " << j << " , " << e << endl;
// ckeck
res=(j==42)&&(e=2.718);
}
return res;
}
// test 3 :
// packed send and receive a container of builtin values
bool test_container_packed(TML_Comm *comm, int rank)
{
vector<int> vdata0;
vector<int> vdata1;
bool res=true;
if(rank==0){
vdata0.push_back(69);
vdata0.push_back(70);
comm->send_cont_packed(vdata0,1,false,3);
}else if (rank==1){
comm->receive_cont_packed(vdata1,0,false,3);
std::cout << "rank " << rank << " received cont.: \n";
for(std::vector<int>::iterator iter=vdata1.begin();
iter!=vdata1.end();
iter++){
std::cout << *iter << " ";
}
std::cout << std::endl;
// check
if(vdata1.size()==2){
res=(vdata1[0]==69)&&(vdata1[1]==70);
} else {
res=false;
}
}
return res;
}
// test 4:
// packed sendrecv container of builtin values
bool test_container_sendrecv_packed(TML_Comm *comm, int rank)
{
vector<int> vdata0;
vector<int> vdata1;
bool res=true;
if(rank==0){
//vdata0.push_back(69);
//vdata0.push_back(70);
comm->sendrecv_cont_packed(vdata0,vdata1,MPI_PROC_NULL,1,0);
cout << "rank " << rank << " received packed cont.: \n";
for(std::vector<int>::iterator iter=vdata1.begin();
iter!=vdata1.end();
iter++){
cout << *iter << " ";
}
// check
if(vdata1.size()==3){
res=(vdata1[0]==42)&&(vdata1[1]==43)&&(vdata1[2]==44);
} else {
res=false;
}
}else if (rank==1){
vdata0.push_back(42);
vdata0.push_back(43);
vdata0.push_back(44);
comm->sendrecv_cont_packed(vdata0,vdata1,0,MPI_PROC_NULL,0);
cout << "rank " << rank << " received packed cont.: \n";
for(std::vector<int>::iterator iter=vdata1.begin();
iter!=vdata1.end();
iter++){
cout << *iter << " ";
}
cout << endl;
// check
if(vdata1.size()==2){
res=(vdata1[0]==69)&&(vdata1[1]==70);
} else {
res=false;
}
}
return res;
}
// test 5:
// packed replace sendrecv container of builtin values
bool test_container_sendrecv_packed_replace(TML_Comm *comm, int rank)
{
vector<int> vdata;
bool res=true;
if(rank==0){
vdata.push_back(69);
vdata.push_back(70);
comm->sendrecv_cont_packed_replace(vdata,1,1,0);
cout << "rank " << rank << " received packed cont.: \n";
for(std::vector<int>::iterator iter=vdata.begin();
iter!=vdata.end();
iter++){
cout << *iter << " ";
}
// check
if(vdata.size()==3){
res=(vdata[0]==42)&&(vdata[1]==43)&&(vdata[2]==44);
} else {
res=false;
}
}else if (rank==1){
vdata.push_back(42);
vdata.push_back(43);
vdata.push_back(44);
comm->sendrecv_cont_packed_replace(vdata,0,0,0);
cout << "rank " << rank << " received packed cont.: \n";
for(std::vector<int>::iterator iter=vdata.begin();
iter!=vdata.end();
iter++){
cout << *iter << " ";
}
cout << endl;
// check
if(vdata.size()==2){
res=(vdata[0]==69)&&(vdata[1]==70);
} else {
res=false;
}
}
return res;
}
// packed send/receive of user defined type
// new pack/unpack functions
template<>
void TML_PackedMessageInterface::pack<pair<int,double> >(const pair<int,double>& p)
{
append(p.first);
append(p.second);
}
template<>
void TML_PackedMessageInterface::unpack<pair<int,double> >(pair<int,double>& p)
{
p.first=pop_int();
p.second=pop_double();
}
bool test_container_packed_user(TML_Comm *comm, int rank)
{
vector<pair<int,double> > vdata0;
vector<pair<int,double> > vdata1;
bool res=true;
if(rank==0){
vdata0.push_back(pair<int,double>(69,2.718));
vdata0.push_back(pair<int,double>(42,3.14159));
comm->send_cont_packed(vdata0,1,false,3);
}else if (rank==1){
comm->receive_cont_packed(vdata1,0,false,3);
std::cout << "rank " << rank << " received cont.: \n";
for(std::vector<pair<int,double> >::iterator iter=vdata1.begin();
iter!=vdata1.end();
iter++){
std::cout << "(" << iter->first << "," << iter->second << ") " ;
}
std::cout << std::endl;
// check
if(vdata1.size()==2){
res=(vdata1[0].first==69)&&(vdata1[0].second==2.718)&&
(vdata1[1].first==42)&&(vdata1[1].second==3.14159);
} else {
res=false;
}
}
return res;
}
// do all tests in this group
bool test_group_pack(TML_Comm *comm, int rank)
{
bool res=true;
cout << "begin test group packed" << endl;
// test 1
if(test_pack(comm,rank)){
cout << "pack/unpack test sucessfull" << endl;
}else{
res=false;
cout << "pack/unpack test failed" << endl;
}
// multi message pack/unpack
if(test_pack_multi(comm,rank)){
cout << "multi message pack/unpack test sucessfull" << endl;
}else{
res=false;
cout << "multi message pack/unpack test failed" << endl;
}
// test 3
if(test_container_packed(comm,rank)){
cout << "test_container_packed sucessfull" << endl;
}else{
res=false;
cout << "test_container_packed failed" << endl;
}
// test 3 user def
if(test_container_packed_user(comm,rank)){
cout << "test_container_packed_user sucessfull" << endl << flush;
}else{
res=false;
cout << "test_container_packed_user failed" << endl<< flush;
}
MPI_Barrier(MPI_COMM_WORLD);
cout << "past barrier" << endl << flush;
// test 4
if(test_container_sendrecv_packed(comm,rank)){
cout << "test_container_sendrecv_packed sucessfull" << endl << flush;
}else{
res=false;
cout << "test_container_sendrecv_packed failed" << endl << flush;
}
MPI_Barrier(MPI_COMM_WORLD);
// test 5
if(test_container_sendrecv_packed_replace(comm,rank)){
cout << "test_container_sendrecv_packed_replace sucessfull" << endl << flush;
}else{
res=false;
cout << "test_container_sendrecv_packed_replace failed" << endl << flush;
}
MPI_Barrier(MPI_COMM_WORLD);
cout << "finished test group packed" << endl << flush;
return res;
}
|