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
|
/////////////////////////////////////////////////////////////
// //
// 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 "tml/message/packed_multi_message.h"
//--- I/O ---
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
using std::flush;
/*!
Constructor for TML_PackedMultiMessage
\param comm the MPI communicator
\param isize initial buffer size per slice, default 64 byte
*/
TML_PackedMultiMessage::TML_PackedMultiMessage(MPI_Comm comm,int isize)
{
m_comm=comm;
MPI_Comm_size(m_comm,&m_size);
m_vbuffersize=isize;
m_vbuffer=new char[m_vbuffersize*m_size];
m_position=new int[m_size];
m_rpos=new int[m_size];
m_recvcount=new int[m_size];
// setup initial displacements in buffer
m_displ=new int[m_size];
for(int i=0;i<m_size;i++){
m_displ[i]=i*m_vbuffersize;
m_position[i]=i*m_vbuffersize;
m_rpos[i]=0;
}
m_position[0]=0;
m_recvcount[0]=0;
MPI_Pack_size(1,MPI_INT,m_comm,&m_int_increment);
MPI_Pack_size(1,MPI_DOUBLE,m_comm,&m_dbl_increment);
}
TML_PackedMultiMessage::~TML_PackedMultiMessage()
{
delete m_vbuffer;
delete m_position;
delete m_recvcount;
delete m_displ;
delete m_rpos;
}
/*!
return a slab
*/
TML_PackedMultiMessageSlab TML_PackedMultiMessage::operator[](int i)
{
return TML_PackedMultiMessageSlab(this,i);
}
/*!
Grows the buffer to twice its current size, thus guaranteeing that append works in amortized constant time. Currently grows the buffer homogeneously, i.e. all slices have the same size.
\warning no check if there is enough space for the new buffer
*/
void TML_PackedMultiMessage::grow()
{
char *temp=m_vbuffer;
int vbs_old=m_vbuffersize;
m_vbuffersize+=m_vbuffersize;
m_vbuffer=new char[m_vbuffersize*m_size];
for(int i=0;i<m_size;i++){
memcpy((void*)(&m_vbuffer[i*m_vbuffersize]),(void*)(&temp[i*vbs_old]),size_t(m_position[i]-m_displ[i]));
m_position[i]=m_position[i]+i*vbs_old;;
m_displ[i]+=m_displ[i];
m_rpos[i]=m_position[i]-m_displ[i];
}
delete temp;
}
/*!
Grow buffer to a specified size
\param size the size to grow to
*/
void TML_PackedMultiMessage::growTo(int size)
{
if(size>m_vbuffersize){
char *temp=m_vbuffer;
int vbs_old=m_vbuffersize;
m_vbuffersize=size;
m_vbuffer=new char[m_vbuffersize*m_size];
for(int i=0;i<m_size;i++){
memcpy((void*)(&m_vbuffer[i*m_vbuffersize]),(void*)(&temp[m_displ[i]]),size_t(m_position[i]-m_displ[i]));
m_position[i]=m_position[i]+i*(m_vbuffersize-vbs_old);
m_displ[i]=i*m_vbuffersize;
m_rpos[i]=m_position[i]-m_displ[i];
}
delete temp;
}
}
/*!
clear message buffer, i.e. reset all positions to 0
*/
void TML_PackedMultiMessage::clear()
{
for(int i=1;i<m_size;i++) {
m_position[i]=m_displ[i];
m_rpos[i]=0;
}
}
/*!
reset single packing posn to 0
*/
void TML_PackedMultiMessage::begin_pack(int i)
{
m_position[i]=m_displ[i];
}
/*!
reset single unpacking posn to 0
*/
void TML_PackedMultiMessage::begin_unpack(int i)
{
m_position[i]=m_displ[i];
}
/*!
Append an integer to a given slice of the buffer.
\param i the integer
\param nslice the nr. of the slice
\warning No check for overflow
*/
void TML_PackedMultiMessage::append(int i,int nslice)
{
if((m_position[nslice]-m_displ[nslice])+m_int_increment>m_vbuffersize){ // to small, grow
grow();
}
MPI_Pack(&i,1,MPI_INT,m_vbuffer,m_vbuffersize*m_size,&(m_position[nslice]),m_comm);
m_rpos[nslice]=m_position[nslice]-m_displ[nslice];
}
/*!
Append a double to a given slice of the buffer.
\param d the double
\param nslice the nr. of the slice
\warning No check for overflow
*/
void TML_PackedMultiMessage::append(double d,int nslice)
{
if((m_position[nslice]-m_displ[nslice])+m_dbl_increment>m_vbuffersize){ // to small, grow
grow();
}
MPI_Pack(&d,1,MPI_DOUBLE,m_vbuffer,m_vbuffersize*m_size,&(m_position[nslice]),m_comm);
m_rpos[nslice]=m_position[nslice]-m_displ[nslice];
}
/*!
Append a STL-string to a given slice of the buffer.
\param str the string
\param nslice the nr. of the slice
\warning No check for overflow
*/
void TML_PackedMultiMessage::append(const string& str,int nslice)
{
int len=str.size();
if((m_position[nslice]-m_displ[nslice])+m_int_increment+len>m_vbuffersize){ // to small, grow
grow();
}
MPI_Pack(&len,1,MPI_INT,m_vbuffer,m_vbuffersize*m_size,&(m_position[nslice]),m_comm);
MPI_Pack((void *)str.c_str(),len,MPI_CHAR,m_vbuffer,m_vbuffersize*m_size,&(m_position[nslice]),m_comm);
m_rpos[nslice]=m_position[nslice]-m_displ[nslice];
}
/*!
Append boolean value to a given slice of the buffer.
\param b the boolean
\param nslice the nr. of the slice
*/
void TML_PackedMultiMessage::append(bool b, int nslice)
{
int i;
if(b) {i=1;}
else {i=0;}
append(i,nslice);
}
/*!
Pops an integer from a given slice of the the buffer, i.e. it pops the last sizeof(MPI_INT) bytes of the buffer, interpreting them as an int.
\param nslice the nr. of the slice
\return the int.
\warning No check for underflow
*/
int TML_PackedMultiMessage::pop_int(int nslice)
{
int res;
MPI_Unpack(m_vbuffer,m_vbuffersize*m_size,&(m_position[nslice]),&res,1,MPI_INT,m_comm);
return res;
}
/*!
Pops a double from a given slice of the the buffer.
\param nslice the nr. of the slice
\return the double.
\warning No check for underflow
*/
double TML_PackedMultiMessage::pop_double(int nslice)
{
double res;
MPI_Unpack(m_vbuffer,m_vbuffersize*m_size,&(m_position[nslice]),&res,1,MPI_DOUBLE,m_comm);
return res;
}
/*!
Pops a string from a given slice of the the buffer.
\param nslice the nr. of the slice
\return the string.
\warning Not implemented
\todo implement
*/
//string TML_PackedMultiMessage::pop_string(int nslice)
string TML_PackedMultiMessage::pop_string()
{
return "";
}
/*!
Pops a boolean from a given slice of the the buffer, i.e. it pops the last sizeof(MPI_INT) bytes of the buffer, interpreting them as a boolean (via pop_int()).
\param nslice the nr. of the slice
\return the boolean value
\warning No check for underflow
*/
bool TML_PackedMultiMessage::pop_bool(int nslice)
{
int i=pop_int(nslice);
return (i==1);
}
|