RecvBuffer.h 24.4 KB
Newer Older
1 2
//======================================================================================================================
//
3
//  This file is part of waLBerla. waLBerla is free software: you can
4
//  redistribute it and/or modify it under the terms of the GNU General Public
5
//  License as published by the Free Software Foundation, either version 3 of
6
//  the License, or (at your option) any later version.
7 8 9 10
//
//  waLBerla is distributed in the hope that it will be useful, but WITHOUT
//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11
//  for more details.
12
//
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
//  You should have received a copy of the GNU General Public License along
//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file RecvBuffer.h
//! \ingroup core
//! \author Klaus Iglberger
//! \brief Implementation of a MPI receive buffer.
//!
//! Copyright (C) 2009 Klaus Iglberger
//! Taken from "pe Physics Engine" with small changes
//
//======================================================================================================================

#pragma once

#include "waLBerlaDefinitions.h"
#include "BufferSizeTrait.h"
#include "SendBuffer.h"

#include "core/debug/Debug.h"
33
#include "core/Sanitizer.h"
34 35 36 37 38 39 40

#include <boost/mpl/logical.hpp>
#include <boost/type_traits/is_enum.hpp>
#include <boost/type_traits/is_arithmetic.hpp>
#include <boost/utility/enable_if.hpp>

#include <algorithm>
41
#include <cstring>
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

namespace walberla {
namespace mpi {

//======================================================================================================================
//
//  CLASS DEFINITION
//
//======================================================================================================================

//**********************************************************************************************************************
/*!\brief Implementation of a MPI receive buffer.
// \ingroup mpi
//
// The RecvBuffer class is a special purpose implementation for the MPI communication
// functionality. It offers a convenient and safe access to the received data values
// even for mixed-type communication. The following example gives an impression of the
// usage of the RecvBuffer class:

   \code
   using namespace mpi;

   // Preparing a receive buffer for an incoming message of 32 bytes
   RecvBuffer buffer;
   buffer.resize( 32 );

   // Receiving a MPI message from process 0 in a blocking MPI_Recv() function
   MPI_Status status;
   MPI_Recv( buffer.ptr(), 32, MPI_BYTE, 0, MPI_ANY_TAG, MPI_COMM_WORLD, status );

   // Extracting a double and an integer from the buffer
   double d;
   int i;
   buffer >> d >> i;
   \endcode
// For another example see also the Buffer Unit Test in File BufferTest.cpp
//
// Note that the order of data values in the receive buffer is depending on the message
// sent via MPI. See also the SendBuffer class description for the sender side of the MPI
// communication.
*/
template< typename T = unsigned char >  // Element type
class GenericRecvBuffer
{
public:
   //**Type definitions*************************************************************************************************
   typedef T  ElementType;  //!< Type of the receive buffer elements.
   //*******************************************************************************************************************

   //**Constructors*****************************************************************************************************
   /*!\name Constructors */
   //@{
   explicit inline GenericRecvBuffer();
            inline GenericRecvBuffer( const GenericRecvBuffer & rb );
   template< typename G >
   explicit inline GenericRecvBuffer( GenericSendBuffer<T,G> & sb );
   //@}
   //*******************************************************************************************************************

   //**Destructor*******************************************************************************************************
   /*!\name Destructor */
   //@{
   inline ~GenericRecvBuffer();
   //@}
   //*******************************************************************************************************************

   //**Assignment operator**********************************************************************************************
   /*!\name Assignment operator */
   //@{
   GenericRecvBuffer& operator=( const GenericRecvBuffer& sb );
   template< typename G >
   GenericRecvBuffer& operator=( const GenericSendBuffer<T,G> & sb );
   //@}
   //*******************************************************************************************************************

   //**Get functions****************************************************************************************************
   /*!\name Get functions */
   //@{
   inline size_t maxSize () const;
   inline size_t size    () const;
   inline size_t capacity() const;
   inline bool   isEmpty () const;
   //@}
   //*******************************************************************************************************************

   //**Operators********************************************************************************************************
   /*!\name Operators */
   //@{
   template< typename V >
   typename boost::enable_if< boost::mpl::or_< boost::is_arithmetic<V>, boost::is_enum<V> >,
                              GenericRecvBuffer& >::type
   operator>>( V& value );
   //@}
   //*******************************************************************************************************************

   //**Utility functions************************************************************************************************
   /*!\name Utility functions */
   //@{
                          inline T*   ptr    () const;
                          inline void reserve( size_t newCapacity );
                          inline void resize ( size_t newSize     );
   template< typename V >        void peek   ( V& value ) const;
144
                          inline T *  skip   ( size_t elements    );
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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
                          inline void clear  ();
                          inline void reset  ();
                          inline void readDebugMarker( const char * marker );
   //@}
   //*******************************************************************************************************************

private:
   //**Member variables*************************************************************************************************
   /*!\name Member variables */
   //@{
   size_t capacity_;  //!< The current size of the receive buffer.
   T* begin_;         //!< Pointer to the first element of the receive buffer.
   T* cur_;           //!< Pointer to the current element of the receive buffer.
   T* end_;           //!< Pointer to the last element of the receive buffer.
   //@}
   //*******************************************************************************************************************

   //**Utility functions************************************************************************************************
   /*!\name Utility functions */
   //@{
   template< typename V >
   typename boost::enable_if< boost::mpl::or_< boost::is_arithmetic<V>, boost::is_enum<V> >,
                              GenericRecvBuffer& >::type
   get( V& value );
   //@}
   //*******************************************************************************************************************



   //**Compile time checks**********************************************************************************************
   static_assert( boost::is_arithmetic<T>::value, "SendBuffer<T>: T has to be native datatype" ) ;
   //*******************************************************************************************************************
};
//**********************************************************************************************************************

typedef GenericRecvBuffer<> RecvBuffer;



//======================================================================================================================
//
//  CONSTRUCTORS
//
//======================================================================================================================

//**********************************************************************************************************************
/*!\brief Standard constructor for RecvBuffer.
*/
template< typename T >  // Element type
inline GenericRecvBuffer<T>::GenericRecvBuffer()
   : capacity_( 0    )  // Capacity of the receive buffer
   , begin_   ( NULL )  // Pointer to the first element
   , cur_     ( NULL )  // Pointer to the current element
   , end_     ( NULL )  // Pointer to the last element
{}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\brief Copy constructor for RecvBuffer.
//
// \param rb The receive buffer to be copied.
*/
template< typename T >  // Element type
inline GenericRecvBuffer<T>::GenericRecvBuffer( const GenericRecvBuffer& rb )
   : capacity_( rb.size()        )  // Capacity of the receive buffer
   , begin_   ( new T[capacity_] )  // Pointer to the first element
   , cur_     ( begin_           )  // Pointer to the current element
   , end_     ( begin_+capacity_ )  // Pointer to the last element
{
   for( size_t i=0; i<capacity_; ++i )
      cur_[i] = rb.cur_[i];
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\brief Constructor for RecvBuffer.
//
// \param sb The send buffer whose content is transfered to this receive buffer.
*/
template< typename T >  // Element type
template< typename G >
inline GenericRecvBuffer<T>::GenericRecvBuffer( GenericSendBuffer<T,G> & sb )
   : capacity_( sb.capacity() )
   , begin_( sb.begin_ )
   , cur_( sb.begin_ )
   , end_( sb.end_ )
{
   sb.begin_ = new T[0];
   sb.cur_   = sb.begin_;
   sb.end_   = sb.begin_;
}
//**********************************************************************************************************************



//======================================================================================================================
//
//  DESTRUCTOR
//
//======================================================================================================================

//**********************************************************************************************************************
/*!\brief Destructor for RecvBuffer.
*/
template< typename T >  // Element type
inline GenericRecvBuffer<T>::~GenericRecvBuffer()
{
   delete [] begin_;
}
//**********************************************************************************************************************




//======================================================================================================================
//
//  ASSIGNMENT OPERATOR
//
//======================================================================================================================

//**********************************************************************************************************************
/*!\brief Copy assignment operator for RecvBuffer.
//
// \param rb The receive buffer to be copied.
// \return Reference to the assigned receive buffer.
*/
template< typename T >  // Element type
GenericRecvBuffer<T>& GenericRecvBuffer<T>::operator=( const GenericRecvBuffer& rb )
{
   if( &rb == this ) return *this;

   if( rb.size() > capacity_ ) {
      T* newBegin( new T[rb.size()] );
      end_ = std::copy( rb.cur_, rb.end_, newBegin );
      std::swap( begin_, newBegin );
      delete [] newBegin;

      capacity_ = rb.size();
      cur_ = begin_;
   }
   else {
      end_ = std::copy( rb.cur_, rb.end_, begin_ );
      cur_ = begin_;
   }

   return *this;
}

template< typename T >  // Element type
template< typename G >
GenericRecvBuffer<T>& GenericRecvBuffer<T>::operator=( const GenericSendBuffer<T,G> & sb )
{
   if( sb.size() > capacity_ ) {
      T* newBegin( new T[sb.size()] );
      end_ = std::copy( sb.begin_, sb.cur_, newBegin );
      std::swap( begin_, newBegin );
      delete [] newBegin;

      capacity_ = sb.size();
      cur_ = begin_;
   }
   else {
      end_ = std::copy( sb.begin_, sb.cur_, begin_ );
      cur_ = begin_;
   }

   return *this;
}
//**********************************************************************************************************************




//======================================================================================================================
//
//  GET FUNCTIONS
//
//======================================================================================================================

//**********************************************************************************************************************
/*!\brief Returns the maximum possible size of the receive buffer.
//
// \return The maximum possible size.
*/
template< typename T >  // Element type
inline size_t GenericRecvBuffer<T>::maxSize() const
{
   return size_t(-1) / sizeof(T);
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\brief Returns the current size of the receive buffer.
//
// \return The current size.
*/
template< typename T >  // Element type
inline size_t GenericRecvBuffer<T>::size() const
{
   return numeric_cast< size_t >( end_ - cur_ );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\brief Returns the capacity of the receive buffer.
//
// \return The capacity.
*/
template< typename T >  // Element type
inline size_t GenericRecvBuffer<T>::capacity() const
{
   return numeric_cast< size_t >( capacity_ );
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\brief Returns \a true if the receive buffer is empty.
//
// \return \a true if the receive buffer is empty, \a false if it is not.
*/
template< typename T >  // Element type
inline bool GenericRecvBuffer<T>::isEmpty() const
{
   return cur_ >= end_;
}
//**********************************************************************************************************************




//======================================================================================================================
//
//  OPERATORS
//
//======================================================================================================================
//**********************************************************************************************************************
/*!\brief Implements operator>> without debugging system.
//
// \return Reference to the receive buffer.
*/
template< typename T >  // Element type
template< typename V >  // Type of the built-in data value
typename boost::enable_if< boost::mpl::or_< boost::is_arithmetic<V>, boost::is_enum<V> >,
                           GenericRecvBuffer<T> & >::type
GenericRecvBuffer<T>::get( V& value )
{
   // Compile time check that V is built-in data type
   static_assert( boost::is_arithmetic<V>::value || boost::is_enum<V>::value,
                            "RecvBuffer accepts only built-in data types");


   static_assert( sizeof(V) >= sizeof(T), "V has to be bigger than T" );
   static_assert( sizeof(V) %  sizeof(T) == 0, "V has to be divisible by T" );



   // Checking the validity of the read operation
   WALBERLA_ASSERT_LESS_EQUAL( cur_ + (sizeof(V) / sizeof(T)), end_ );

   // Extracting the data value
410 411
   std::memcpy( &value, cur_, sizeof(V) );
   cur_ += sizeof(V) / sizeof(T);
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580

   // Invariants check
   WALBERLA_ASSERT_LESS_EQUAL( cur_, end_);

   return *this;
}
//**********************************************************************************************************************



//**********************************************************************************************************************
/*!\brief Reads a built-in data value from the receive buffer.
//
// \return Reference to the receive buffer.
//
// This function extracts one data value of built-in data type from the receive buffer.
//
// \b Note: This operator may only be used for built-in data types. The attempt to use
// a user-defined data type results in a compile time error!
*/
template< typename T >  // Element type
template< typename V >  // Type of the built-in data value
typename boost::enable_if< boost::mpl::or_< boost::is_arithmetic<V>, boost::is_enum<V> >,
                           GenericRecvBuffer<T> & >::type
GenericRecvBuffer<T>::operator>>( V& value )
{
   readDebugMarker( typeid(V).name() );
   return get( value );

}
//**********************************************************************************************************************




//======================================================================================================================
//
//  UTILITY FUNCTIONS
//
//======================================================================================================================

//**********************************************************************************************************************
/*!\brief Returns a pointer to the first element of the receive buffer.
//
// \return Pointer to the first element of the receive buffer.
//
// This utility function enables the RecvBuffer to be used directly as receive buffer in MPI
// receive functions. Note however, that this operation is only allowed for a reinitialized
// receive buffer (for instance via the resize() function).

   \code
   using namespace pe;

   // Preparing a receive buffer for a message of 50 bytes
   RecvBuffer<byte> buffer;
   buffer.resize( 50 );

   // Receiving an MPI message from process 0 in a blocking MPI_Recv() function
   MPI_Status status;
   MPI_Recv( buffer.ptr(), buffer.size(), MPI_BYTE, 0, MPI_ANY_TAG, MPI_COMM_WORLD, status );
   \endcode
*/
template< typename T >  // Element type
inline T* GenericRecvBuffer<T>::ptr() const
{
   WALBERLA_ASSERT_EQUAL( begin_, cur_);
   return begin_;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\brief Setting the minimum capacity of the receive buffer.
//
// \param newCapacity The new minimum capacity of the receive buffer.
// \return void
//
// This function reserves at least \a newCapacity elements of data type \a T for the receive
// buffer. Note that this operation involves a complete reset of the receive buffer!
*/
template< typename T >  // Element type
void GenericRecvBuffer<T>::reserve( size_t newCapacity )
{
   if( newCapacity > capacity_ )
   {
      // Allocating a new array
      T* tmp = new T[newCapacity];

      // Replacing the old array
      std::swap( tmp, begin_ );
      delete [] tmp;

      // Adjusting the members
      capacity_ = newCapacity;
   }

   // Clearing the receive buffer
   clear();
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\brief Changing the size of the receive buffer.
//
// \param newSize The new size of the receive buffer.
// \return void
//
// This function resizes the receive buffer to the given size \a newSize. Note that this
// operation does not preserve the current contents of the receive buffer!
*/
template< typename T >  // Element type
void GenericRecvBuffer<T>::resize( size_t newSize )
{
   if( newSize > capacity_ )
   {
      // Allocating a new array
      T* tmp = new T[newSize];

      // Replacing the old array
      std::swap( tmp, begin_ );
      delete [] tmp;

      // Adjusting the capacity
      capacity_ = newSize;
   }

   cur_ = begin_;
   end_ = begin_+newSize;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\brief Reads a built-in data value from the receive buffer without extracting it.
//
// \return void
//
// This function reads the next built-in data value of type \a V from the receive buffer
// without extracting/removing it from the buffer.
*/
template< typename T >  // Element type
template< typename V >  // Type of the built-in data value
inline void GenericRecvBuffer<T>::peek( V& value ) const
{
   BOOST_STATIC_ASSERT( boost::is_arithmetic<V>::value );

   BOOST_STATIC_ASSERT( sizeof(V) > sizeof(T) );
   BOOST_STATIC_ASSERT( sizeof(V) % sizeof(T) == 0);

   // Checking the validity of the read operation
   WALBERLA_ASSERT_LESS_EQUAL( cur_ + BUFFER_DEBUG_OVERHEAD + sizeof(V)/sizeof(T), end_);

   // Extracting the data value
   V* tmp( reinterpret_cast<V*>( cur_ + BUFFER_DEBUG_OVERHEAD ) );
   value = *tmp;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\brief Skipping the given number of elements.
//
// \param elements The number of elements to be skipped.
// \return void
//
// This function skips \a element receive buffer elements of type \a T.
*/
template< typename T >  // Element type
581
T * GenericRecvBuffer<T>::skip( size_t elements )
582
{
583
   auto previous = cur_;
584
   cur_ += elements;
585 586 587 588 589

   // Invariants check
   WALBERLA_ASSERT_LESS_EQUAL( cur_, end_ );

   return previous;
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\brief Clearing the receive buffer.
//
// \return void
//
// This function performs a complete reset of the receive buffer.
*/
template< typename T >  // Element type
inline void GenericRecvBuffer<T>::clear()
{
   cur_ = begin_;
   end_ = begin_;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\brief Clearing the receive buffer.
//
// \return void
//
// This function performs a complete reset of the receive buffer - including the deletion of allocated memory!
*/
template< typename T >  // Element type
inline void GenericRecvBuffer<T>::reset()
{
   delete [] begin_;
   capacity_ = 0;
   begin_    = NULL;
   cur_      = NULL;
   end_      = NULL;
}
//**********************************************************************************************************************


//**********************************************************************************************************************
/*!\brief Reads debug marker and raises assert when marker does not match
//
//
*/
#ifdef WALBERLA_BUFFER_DEBUG

template< typename T >  // Element type
inline void GenericRecvBuffer<T>::readDebugMarker( const char * expectedMarker )
{
   bool valid = true;
   uint_t len = std::strlen( expectedMarker );
   // read the first BUFFER_DEBUG_OVERHEAD chars of the marker
   for( uint_t i = 0; i < len && i < BUFFER_DEBUG_OVERHEAD; ++i )
   {
      T readMarker;
      get( readMarker );
      if ( readMarker != T( expectedMarker[i] ))
         valid = false;
   }
   // If marker was shorter then BUFFER_DEBUG_OVERHEAD the rest should be '-'
   // to always have the same length
   for( uint_t i = len; i < BUFFER_DEBUG_OVERHEAD; ++i ) {
      T readMarker;
      get( readMarker );
      if ( readMarker != T('-') )
         valid = false;
   }

   WALBERLA_ASSERT( valid , "Packed and unpacked datatype do not match." );

}
#else

template< typename T >  // Element type
inline void GenericRecvBuffer<T>::readDebugMarker( const char *  ){}

#endif

//**********************************************************************************************************************




} // namespace mpi
} // namespace walberla