9 #ifndef stk_util_parallel_ParallelComm_hpp 10 #define stk_util_parallel_ParallelComm_hpp 14 #include <stk_util/parallel/Parallel.hpp> 47 const unsigned num_msg_bound ,
48 unsigned & num_msg_maximum ,
49 const unsigned *
const send_size ,
50 unsigned *
const recv_size ,
51 bool local_flag =
false );
56 const unsigned *
const send_size ,
57 unsigned *
const recv_size ,
58 bool local_flag =
false );
66 template<
typename T> CommBuffer &pack(
const T & value );
69 template<
typename T> CommBuffer &pack(
const T * value ,
size_t number );
72 template<
typename T> CommBuffer &unpack( T & value );
75 template<
typename T> CommBuffer &unpack( T * value ,
size_t number );
78 template<
typename T> CommBuffer &peek( T & value );
81 template<
typename T> CommBuffer &peek( T * value ,
size_t number );
84 template<
typename T> CommBuffer &skip(
size_t number );
92 size_t capacity()
const ;
106 ptrdiff_t remaining()
const ;
109 void * buffer()
const ;
115 friend class CommAll ;
116 friend class CommGather ;
117 friend class CommBroadcast ;
119 static CommBuffer * allocate(
const unsigned,
const unsigned *
const );
120 static void deallocate(
const unsigned , CommBuffer * );
122 void pack_overflow()
const ;
123 void unpack_overflow()
const ;
125 CommBuffer(
const CommBuffer & );
126 CommBuffer & operator = (
const CommBuffer & );
128 typedef unsigned char * ucharp ;
145 CommBuffer & send_buffer(
unsigned )
const ;
148 CommBuffer & recv_buffer(
unsigned )
const ;
164 const unsigned num_msg_bounds ,
165 const unsigned *
const send_size ,
166 const unsigned *
const recv_size ,
167 const bool local_flag =
false );
190 bool allocate_buffers(
const unsigned num_msg_bounds ,
191 const bool symmetric =
false ,
192 const bool local_flag =
false );
200 void swap_send_recv();
205 void reset_buffers();
211 CommAll(
const CommAll & );
212 CommAll & operator = (
const CommAll & );
214 void rank_error(
const char * ,
unsigned )
const ;
216 bool allocate_buffers(
const unsigned *
const send_size ,
217 const unsigned *
const recv_size ,
225 CommBuffer * m_send ;
226 CommBuffer * m_recv ;
231 class CommBroadcast {
239 CommBuffer & send_buffer();
242 CommBuffer & recv_buffer();
250 bool allocate_buffer(
const bool local_flag =
false );
257 CommBroadcast(
const CommBroadcast & );
258 CommBroadcast & operator = (
const CommBroadcast & );
263 unsigned m_root_rank ;
264 CommBuffer m_buffer ;
278 CommGather(
ParallelMachine ,
unsigned root_rank ,
unsigned send_size );
280 CommBuffer & send_buffer() {
return m_send ; }
284 CommBuffer & recv_buffer(
unsigned );
291 CommGather(
const CommBroadcast & );
292 CommGather & operator = (
const CommBroadcast & );
297 unsigned m_root_rank ;
299 CommBuffer * m_recv ;
312 template<
unsigned N>
struct CommBufferAlign ;
315 struct CommBufferAlign<1> {
316 static size_t align(
size_t ) {
return 0 ; }
320 struct CommBufferAlign {
321 static size_t align(
size_t i ) { i %= N ;
return i ? ( N - i ) : 0 ; }
326 CommBuffer &CommBuffer::pack(
const T & value )
328 enum { Size =
sizeof(T) };
329 size_t nalign = CommBufferAlign<Size>::align( m_ptr - m_beg );
331 if ( m_end < m_ptr + nalign + Size ) { pack_overflow(); }
332 while ( nalign ) { --nalign ; *m_ptr = 0 ; ++m_ptr ; }
333 T * tmp =
reinterpret_cast<T*
>(m_ptr);
335 m_ptr =
reinterpret_cast<ucharp
>( ++tmp );
338 m_ptr += nalign + Size ;
345 CommBuffer &CommBuffer::pack(
const T * value ,
size_t number )
347 enum { Size =
sizeof(T) };
348 size_t nalign = CommBufferAlign<Size>::align( m_ptr - m_beg );
350 if ( m_end < m_ptr + nalign + number * Size ) { pack_overflow(); }
351 while ( nalign ) { --nalign ; *m_ptr = 0 ; ++m_ptr ; }
352 T * tmp =
reinterpret_cast<T*
>(m_ptr);
353 while ( number ) { --number ; *tmp = *value ; ++tmp ; ++value ; }
354 m_ptr =
reinterpret_cast<ucharp
>( tmp );
357 m_ptr += nalign + number * Size ;
364 CommBuffer &CommBuffer::skip(
size_t number )
366 enum { Size =
sizeof(T) };
367 m_ptr += CommBufferAlign<Size>::align( m_ptr - m_beg ) + Size * number ;
368 if ( m_beg && m_end < m_ptr ) { unpack_overflow(); }
374 CommBuffer &CommBuffer::unpack( T & value )
376 enum { Size =
sizeof(T) };
377 const size_t nalign = CommBufferAlign<Size>::align( m_ptr - m_beg );
378 T * tmp =
reinterpret_cast<T*
>( m_ptr + nalign );
380 m_ptr =
reinterpret_cast<ucharp
>( ++tmp );
381 if ( m_end < m_ptr ) { unpack_overflow(); }
387 CommBuffer &CommBuffer::unpack( T * value ,
size_t number )
389 enum { Size =
sizeof(T) };
390 const size_t nalign = CommBufferAlign<Size>::align( m_ptr - m_beg );
391 T * tmp =
reinterpret_cast<T*
>( m_ptr + nalign );
392 while ( number ) { --number ; *value = *tmp ; ++tmp ; ++value ; }
393 m_ptr =
reinterpret_cast<ucharp
>( tmp );
394 if ( m_end < m_ptr ) { unpack_overflow(); }
400 CommBuffer &CommBuffer::peek( T & value )
402 enum { Size =
sizeof(T) };
403 const size_t nalign = CommBufferAlign<Size>::align( m_ptr - m_beg );
404 T * tmp =
reinterpret_cast<T*
>( m_ptr + nalign );
406 if ( m_end < reinterpret_cast<ucharp>(++tmp) ) { unpack_overflow(); }
412 CommBuffer &CommBuffer::peek( T * value ,
size_t number )
414 enum { Size =
sizeof(T) };
415 const size_t nalign = CommBufferAlign<Size>::align( m_ptr - m_beg );
416 T * tmp =
reinterpret_cast<T*
>( m_ptr + nalign );
417 while ( number ) { --number ; *value = *tmp ; ++tmp ; ++value ; }
418 if ( m_end < reinterpret_cast<ucharp>(tmp) ) { unpack_overflow(); }
427 size_t CommBuffer::capacity()
const 428 {
return m_end - m_beg ; }
431 size_t CommBuffer::size()
const 432 {
return m_ptr - m_beg ; }
435 ptrdiff_t CommBuffer::remaining()
const 436 {
return m_end - m_ptr ; }
439 void * CommBuffer::buffer()
const 440 {
return static_cast<void*
>( m_beg ); }
446 CommBuffer & CommAll::send_buffer(
unsigned p )
const 448 if ( m_size <= p ) { rank_error(
"send_buffer",p); }
453 CommBuffer & CommAll::recv_buffer(
unsigned p )
const 455 if ( m_size <= p ) { rank_error(
"recv_buffer",p); }
bool comm_dense_sizes(ParallelMachine comm, const unsigned *const send_size, unsigned *const recv_size, bool local_flag)
int parallel_rank()
function parallel_rank returns the rank of this processor in the current mpi communicator.
bool comm_sizes(ParallelMachine comm, const unsigned num_msg_bound, unsigned &num_msg_maximum, const unsigned *const send_size, unsigned *const recv_size, bool local_flag)
void reset(MPI_Comm new_comm)
Function reset determines new parallel_size and parallel_rank. Flushes, closes, and reopens log files...
int parallel_size()
function parallel_size returns the number of processors in the current mpi communicator.