phdMesh  Version of the Day
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups
Parallel.hpp
1 /*------------------------------------------------------------------------*/
2 /* phdMesh : Parallel Heterogneous Dynamic unstructured Mesh */
3 /* Copyright (2007) Sandia Corporation */
4 /* */
5 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
6 /* license for use of this work by or on behalf of the U.S. Government. */
7 /* */
8 /* This library is free software; you can redistribute it and/or modify */
9 /* it under the terms of the GNU Lesser General Public License as */
10 /* published by the Free Software Foundation; either version 2.1 of the */
11 /* License, or (at your option) any later version. */
12 /* */
13 /* This library is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
16 /* Lesser General Public License for more details. */
17 /* */
18 /* You should have received a copy of the GNU Lesser General Public */
19 /* License along with this library; if not, write to the Free Software */
20 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 */
21 /* USA */
22 /*------------------------------------------------------------------------*/
27 #ifndef util_Parallel_hpp
28 #define util_Parallel_hpp
29 
30 // phdmesh_config.h resides in the build directory and contains the
31 // complete set of #define macros for build-dependent features.
32 
33 #include <phdmesh_config.h>
34 
35 //----------------------------------------------------------------------
36 // Parallel machine
37 
38 #if defined( HAVE_MPI )
39 
40 #include <mpi.h>
41 
42 namespace phdmesh {
43 typedef MPI_Comm ParallelMachine ;
44 typedef MPI_Datatype ParallelDatatype ;
45 
46 inline ParallelMachine parallel_machine_null() { return MPI_COMM_NULL ; }
47 
48 inline ParallelMachine parallel_machine_init( int * argc , char *** argv )
49 {
50  MPI_Init( argc , argv );
51  return MPI_COMM_WORLD ;
52 }
53 
54 inline void parallel_machine_finalize()
55 {
56  MPI_Finalize();
57 }
58 
59 }
60 
61 //----------------------------------------
62 // Other parallel communication machines go here
63 // as '#elif defined( PHDMESH_HAS_<name> )'
64 
65 //----------------------------------------
66 // Stub for non-parallel
67 
68 #else
69 
70 namespace phdmesh {
71 typedef int ParallelMachine ;
72 typedef int ParallelDatatype ;
73 
74 inline ParallelMachine parallel_machine_null() { return 0 ; }
75 
76 inline ParallelMachine parallel_machine_init( int * , char *** )
77 { return 0 ; }
78 
79 inline void parallel_machine_finalize()
80 {}
81 
82 }
83 
84 #endif
85 
86 //----------------------------------------------------------------------
87 // Common parallel machine needs.
88 
89 namespace phdmesh {
90 
91 double wall_time();
92 double wall_dtime( double & );
93 
94 unsigned parallel_machine_size( ParallelMachine m );
95 
96 unsigned parallel_machine_rank( ParallelMachine m );
97 
98 void parallel_machine_barrier( ParallelMachine );
99 
102 struct IdentProc {
103  unsigned ident ;
104  unsigned proc ;
105 
106  ~IdentProc() {}
107 
108  IdentProc() {}
109 
110  IdentProc( const IdentProc & rhs ) : ident(rhs.ident), proc(rhs.proc) {}
111 
112  IdentProc( unsigned i , unsigned p ) : ident(i), proc(p) {}
113 
114  IdentProc & operator = ( const IdentProc & rhs )
115  { ident = rhs.ident ; proc = rhs.proc ; return *this ; }
116 
117  bool operator == ( const IdentProc & rhs ) const
118  { return ident == rhs.ident && proc == rhs.proc ; }
119 
120  bool operator != ( const IdentProc & rhs ) const
121  { return ident != rhs.ident || proc != rhs.proc ; }
122 
123  bool operator < ( const IdentProc & rhs ) const
124  { return ident != rhs.ident ? ident < rhs.ident : proc < rhs.proc ; }
125 
126  bool operator > ( const IdentProc & rhs ) const
127  { return rhs.operator<( *this ); }
128 
129  bool operator <= ( const IdentProc & rhs ) const
130  { return ! rhs.operator<( *this ); }
131 
132  bool operator >= ( const IdentProc & rhs ) const
133  { return ! this->operator<( rhs ); }
134 };
135 
136 }
137 
138 //----------------------------------------------------------------------
139 
140 #endif
141