OOFEM  2.4
OOFEM.org - Object Oriented Finite Element Solver
combuff.h
Go to the documentation of this file.
1 /*
2  *
3  * ##### ##### ###### ###### ### ###
4  * ## ## ## ## ## ## ## ### ##
5  * ## ## ## ## #### #### ## # ##
6  * ## ## ## ## ## ## ## ##
7  * ## ## ## ## ## ## ## ##
8  * ##### ##### ## ###### ## ##
9  *
10  *
11  * OOFEM : Object Oriented Finite Element Code
12  *
13  * Copyright (C) 1993 - 2013 Borek Patzak
14  *
15  *
16  *
17  * Czech Technical University, Faculty of Civil Engineering,
18  * Department of Structural Mechanics, 166 29 Prague, Czech Republic
19  *
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33  */
34 
35 #ifndef combuff_h
36 #define combuff_h
37 
38 #include "oofemcfg.h"
39 #include "datastream.h"
40 #include "parallel.h"
41 
42 namespace oofem {
43 
44  #define __CommunicationBuffer_ALLOC_CHUNK 1024
45 
49 typedef char ComBuff_BYTE_TYPE;
50 
51 class OOFEM_EXPORT MPIBuffer
52 {
53 protected:
55  int size, curr_pos;
57  bool isDynamic;
59  ComBuff_BYTE_TYPE *buff;
65  MPI_Request request;
66 
67 public:
69  MPIBuffer(int size, bool dynamic = 0);
71  MPIBuffer(bool dynamic = 0);
73  virtual ~MPIBuffer();
74 
83  int resize(int newSize);
87  virtual void init();
88 
90  int giveSize() { return size; }
92  int giveAvailableSpace() { return ( size - curr_pos ); }
96  MPI_Request giveRequest() { return this->request; }
97 
107  int packArray(MPI_Comm communicator, const void *src, int n, MPI_Datatype type);
116  int unpackArray(MPI_Comm communicator, void *dest, int n, MPI_Datatype type);
117 
118 
128  int givePackSize(MPI_Comm communicator, MPI_Datatype type, int size);
130 
140  virtual int iSend(MPI_Comm communicator, int dest, int tag);
150  virtual int iRecv(MPI_Comm communicator, int source, int tag, int count = 0);
158  int testCompletion();
168  int testCompletion(int &source, int &tag);
177  virtual int waitCompletion();
186  int bcast(MPI_Comm communicator, int root);
187 
188  void dump();
189 
190 private:
192  int givePosition() { return curr_pos; }
193 };
194 
195 
208 class OOFEM_EXPORT CommunicationBuffer: public DataStream
209 {
210 protected:
211  MPI_Comm communicator;
212 public:
213  CommunicationBuffer(MPI_Comm comm, int size, bool dynamic = 0) {
214  communicator = comm;
215  }
217  CommunicationBuffer(MPI_Comm comm, bool dynamic = 0) {
218  communicator = comm;
219  }
221  virtual ~CommunicationBuffer() { }
222 
231  virtual int resize(int newSize) = 0;
235  virtual void init() = 0;
236 
238  virtual void initForPacking() = 0;
240  virtual void initForUnpacking() = 0;
241 
242  using DataStream::read;
243  virtual int read(bool &data);
244 
245  using DataStream::write;
246  virtual int write(bool data);
247 
248  virtual int givePackSizeOfInt(int count);
249  virtual int givePackSizeOfDouble(int count);
250  virtual int givePackSizeOfChar(int count);
251  virtual int givePackSizeOfBool(int count);
252  virtual int givePackSizeOfLong(int count);
253 
262  virtual int iSend(int dest, int tag) = 0;
271  virtual int iRecv(int source, int tag, int count = 0) = 0;
279  virtual int testCompletion() = 0;
288  virtual int waitCompletion() = 0;
296  virtual int bcast(int root) = 0;
298 };
299 
300 
301 
302 
303 
304 class OOFEM_EXPORT StaticCommunicationBuffer : public CommunicationBuffer, public MPIBuffer
305 {
306 public:
307  StaticCommunicationBuffer(MPI_Comm comm, int size, bool dynamic = 0) : CommunicationBuffer(comm, size, dynamic),
308  MPIBuffer(size, dynamic) { }
310  StaticCommunicationBuffer(MPI_Comm comm, bool dynamic = 0) : CommunicationBuffer(comm, dynamic), MPIBuffer(dynamic) { }
313 
314  virtual int resize(int newSize) { return MPIBuffer :: resize(newSize); }
315 
316  virtual void init() { return MPIBuffer :: init(); }
317  virtual void initForPacking() { this->init(); }
318  virtual void initForUnpacking() { this->init(); }
319 
321  virtual int write(const int *src, int n)
322  { return MPIBuffer :: packArray(this->communicator, src, n, MPI_INT); }
323  virtual int write(const long *src, int n)
324  { return MPIBuffer :: packArray(this->communicator, src, n, MPI_LONG); }
325  virtual int write(const unsigned long *src, int n)
326  { return MPIBuffer :: packArray(this->communicator, src, n, MPI_UNSIGNED_LONG); }
327  virtual int write(const double *src, int n)
328  { return MPIBuffer :: packArray(this->communicator, src, n, MPI_DOUBLE); }
329  virtual int write(const char *src, int n)
330  { return MPIBuffer :: packArray(this->communicator, src, n, MPI_CHAR); }
331 
333  virtual int read(int *dest, int n)
334  { return MPIBuffer :: unpackArray(this->communicator, dest, n, MPI_INT); }
335  virtual int read(long *dest, int n)
336  { return MPIBuffer :: unpackArray(this->communicator, dest, n, MPI_LONG); }
337  virtual int read(unsigned long *dest, int n)
338  { return MPIBuffer :: unpackArray(this->communicator, dest, n, MPI_UNSIGNED_LONG); }
339  virtual int read(double *dest, int n)
340  { return MPIBuffer :: unpackArray(this->communicator, dest, n, MPI_DOUBLE); }
341  virtual int read(char *dest, int n)
342  { return MPIBuffer :: unpackArray(this->communicator, dest, n, MPI_CHAR); }
343 
344  virtual int iSend(int dest, int tag) { return MPIBuffer :: iSend(this->communicator, dest, tag); }
345 
346  virtual int iRecv(int source, int tag, int count = 0) { return MPIBuffer :: iRecv(this->communicator, source, tag, count); }
347 
348  virtual int testCompletion() { return MPIBuffer :: testCompletion(); }
349 
350  int testCompletion(int &source, int &tag) { return MPIBuffer :: testCompletion(source, tag); }
351 
352  virtual int waitCompletion() { return MPIBuffer :: waitCompletion(); }
353 
354  virtual int bcast(int root) { return MPIBuffer :: bcast(this->communicator, root); }
355 };
356 } // end namespace oofem
357 
358 #endif // combuff_h
int givePosition()
Definition: combuff.h:192
virtual int read(long *dest, int n)
Reads count long values into array pointed by data.
Definition: combuff.h:335
virtual void init()
Initializes buffer to empty state.
Definition: combuff.C:114
int packArray(MPI_Comm communicator, const void *src, int n, MPI_Datatype type)
Packs array of a values of given type into buffer.
Definition: combuff.C:123
int bcast(MPI_Comm communicator, int root)
Initializes broadcast over collaborating processes.
Definition: combuff.C:211
int testCompletion(int &source, int &tag)
Definition: combuff.h:350
virtual ~StaticCommunicationBuffer()
Destructor.
Definition: combuff.h:312
The purpose of DataStream abstract class is to allow to store/restore context to different streams...
Definition: datastream.h:54
virtual int iRecv(int source, int tag, int count=0)
Starts standard mode, nonblocking receive.
Definition: combuff.h:346
char ComBuff_BYTE_TYPE
Type with size equal to one byte (sizeof (ComBuff_BYTE_TYPE) should be 1).
Definition: combuff.h:49
ComBuff_BYTE_TYPE * buff
Buffer. Dynamically allocated.
Definition: combuff.h:59
virtual int read(double *dest, int n)
Reads count double values into array pointed by data.
Definition: combuff.h:339
virtual void initForPacking()
Initialize for packing.
Definition: combuff.h:317
virtual int iSend(MPI_Comm communicator, int dest, int tag)
Starts standard mode, nonblocking send.
Definition: combuff.C:154
virtual int write(const char *src, int n)
Writes count char values from array pointed by data.
Definition: combuff.h:329
virtual int read(int *data, int count)=0
Reads count integer values into array pointed by data.
virtual int read(unsigned long *dest, int n)
Reads count unsigned long values into array pointed by data.
Definition: combuff.h:337
virtual int waitCompletion()
Waits until a completion of a nonblocking communication.
Definition: combuff.h:352
virtual int write(const unsigned long *src, int n)
Writes count unsigned long values from array pointed by data.
Definition: combuff.h:325
virtual int waitCompletion()
Waits until a completion of a nonblocking communication.
Definition: combuff.C:202
MPI_Request request
MPI request handle.
Definition: combuff.h:65
virtual int write(const double *src, int n)
Writes count double values from array pointed by data.
Definition: combuff.h:327
virtual int write(const int *data, int count)=0
Writes count integer values from array pointed by data.
int resize(int newSize)
Resizes buffer to given size.
Definition: combuff.C:78
virtual int testCompletion()
Tests if the operation identified by this->request is complete.
Definition: combuff.h:348
Class CommunicationBuffer provides abstraction for communication buffer.
Definition: combuff.h:208
int size
Size and current position in buffer in bytes (sizeof(char)).
Definition: combuff.h:55
virtual int resize(int newSize)
Resizes buffer to given size.
Definition: combuff.h:314
virtual int iSend(int dest, int tag)
Starts standard mode, nonblocking send.
Definition: combuff.h:344
virtual int write(bool data)
Writes a bool value.
Definition: combuff.C:265
CommunicationBuffer(MPI_Comm comm, int size, bool dynamic=0)
Definition: combuff.h:213
virtual void init()
Initializes buffer to empty state.
Definition: combuff.h:316
bool isDynamic
Dynamic flag (if true, buffer can grow, but reallocation is needed).
Definition: combuff.h:57
CommunicationBuffer(MPI_Comm comm, bool dynamic=0)
Constructor. Creates empty buffer, using given communicator for packing.
Definition: combuff.h:217
virtual int read(int *dest, int n)
Reads count integer values into array pointed by data.
Definition: combuff.h:333
virtual int iRecv(MPI_Comm communicator, int source, int tag, int count=0)
Starts standard mode, nonblocking receive.
Definition: combuff.C:162
int testCompletion()
Tests if the operation identified by this->request is complete.
Definition: combuff.C:178
StaticCommunicationBuffer(MPI_Comm comm, int size, bool dynamic=0)
Definition: combuff.h:307
virtual int bcast(int root)
Initializes broadcast over collaborating processes.
Definition: combuff.h:354
virtual int write(const int *src, int n)
Writes count integer values from array pointed by data.
Definition: combuff.h:321
virtual int write(const long *src, int n)
Writes count long values from array pointed by data.
Definition: combuff.h:323
int unpackArray(MPI_Comm communicator, void *dest, int n, MPI_Datatype type)
Unpacks array of values of given type from buffer.
Definition: combuff.C:147
StaticCommunicationBuffer(MPI_Comm comm, bool dynamic=0)
Constructor. Creates empty buffer, using given communicator for packing.
Definition: combuff.h:310
int giveAvailableSpace()
Definition: combuff.h:92
virtual ~CommunicationBuffer()
Destructor.
Definition: combuff.h:221
the oofem namespace is to define a context or scope in which all oofem names are defined.
virtual int read(bool &data)
Reads a bool value from data.
Definition: combuff.C:257
virtual void initForUnpacking()
Initialize for Unpacking (data already received)
Definition: combuff.h:318
int giveSize()
Definition: combuff.h:90
virtual int read(char *dest, int n)
Reads count char values into array pointed by data.
Definition: combuff.h:341
MPI_Request giveRequest()
Returns associated MPI request handle.
Definition: combuff.h:96

This page is part of the OOFEM documentation. Copyright (c) 2011 Borek Patzak
Project e-mail: info@oofem.org
Generated at Tue Jan 2 2018 20:07:27 for OOFEM by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2011