proteus  1.8.1
C/C++/Fortran libraries
partitioning.h
Go to the documentation of this file.
1 #ifndef PARTITIONING_H
2 #define PARTITIONING_H
3 #include <iostream>
4 #include <valarray>
5 #include "mpi.h"
6 #include "hdf5.h"
7 #include "petsc.h"
8 #include "petscsys.h"
9 #include "mesh.h"
10 #include "meshio.h"
11 
12 namespace proteus
13 {
14  //--memory profiling
15  /*
16  * Author: David Robert Nadeau
17  * Site: http://NadeauSoftware.com/
18  * License: Creative Commons Attribution 3.0 Unported License
19  * http://creativecommons.org/licenses/by/3.0/deed.en_US
20  */
21 
22 #if defined(_WIN32)
23 #include <windows.h>
24 #include <psapi.h>
25 
26 #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
27 #include <unistd.h>
28 #include <sys/resource.h>
29 
30 #if defined(__APPLE__) && defined(__MACH__)
31 #include <mach/mach.h>
32 
33 #elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
34 #include <fcntl.h>
35 #include <procfs.h>
36 
37 #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
38 #include <stdio.h>
39 
40 #endif
41 
42 #else
43 #error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS."
44 #endif
45 
51  inline size_t getPeakRSS( )
52  {
53 #if defined(_WIN32)
54  /* Windows -------------------------------------------------- */
55  PROCESS_MEMORY_COUNTERS info;
56  GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
57  return (size_t)info.PeakWorkingSetSize;
58 
59 #elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
60  /* AIX and Solaris ------------------------------------------ */
61  struct psinfo psinfo;
62  int fd = -1;
63  if ( (fd = open( "/proc/self/psinfo", O_RDONLY )) == -1 )
64  return (size_t)0L;/* Can't open? */
65  if ( read( fd, &psinfo, sizeof(psinfo) ) != sizeof(psinfo) )
66  {
67  close( fd );
68  return (size_t)0L;/* Can't read? */
69  }
70  close( fd );
71  return (size_t)(psinfo.pr_rssize * 1024L);
72 
73 #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
74  /* BSD, Linux, and OSX -------------------------------------- */
75  struct rusage rusage;
76  getrusage( RUSAGE_SELF, &rusage );
77 #if defined(__APPLE__) && defined(__MACH__)
78  return (size_t)rusage.ru_maxrss;
79 #else
80  return (size_t)(rusage.ru_maxrss * 1024L);
81 #endif
82 
83 #else
84  /* Unknown OS ----------------------------------------------- */
85  return (size_t)0L;/* Unsupported. */
86 #endif
87  }
88 
93  inline size_t getCurrentRSS( )
94  {
95 #if defined(_WIN32)
96  /* Windows -------------------------------------------------- */
97  PROCESS_MEMORY_COUNTERS info;
98  GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
99  return (size_t)info.WorkingSetSize;
100 
101 #elif defined(__APPLE__) && defined(__MACH__)
102  /* OSX ------------------------------------------------------ */
103  struct mach_task_basic_info info;
104  mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
105  if ( task_info( mach_task_self( ), MACH_TASK_BASIC_INFO,
106  (task_info_t)&info, &infoCount ) != KERN_SUCCESS )
107  return (size_t)0L;/* Can't access? */
108  return (size_t)info.resident_size;
109 
110 #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
111  /* Linux ---------------------------------------------------- */
112  long rss = 0L;
113  FILE* fp = NULL;
114  if ( (fp = fopen( "/proc/self/statm", "r" )) == NULL )
115  return (size_t)0L;/* Can't open? */
116  if ( fscanf( fp, "%*s%ld", &rss ) != 1 )
117  {
118  fclose( fp );
119  return (size_t)0L;/* Can't read? */
120  }
121  fclose( fp );
122  return (size_t)rss * (size_t)sysconf( _SC_PAGESIZE);
123 
124 #else
125  /* AIX, BSD, Solaris, and Unknown OS ------------------------ */
126  return (size_t)0L;/* Unsupported. */
127 #endif
128  }
129  //--memory profiling
130  inline int enforceMemoryLimit(const MPI_Comm& PROTEUS_COMM_WORLD, int rank, double max_rss_gb,const char* msg)
131  {
132  double current, current_global,gb(1.0e-9);
133  PetscBarrier(NULL);
134  current = double(getCurrentRSS())*gb;
135  PetscBarrier(NULL);
136  current_global=0.0;
137  MPI_Allreduce(&current,&current_global,1,MPI_DOUBLE,MPI_MAX,PROTEUS_COMM_WORLD);
138  if (current > max_rss_gb)
139  {
140  std::cout<<"Raising PETSC_ERR_MEM, Memory usage on rank "<<rank<<'\t'<<current<<"GB"<<'\t'<<"limit "<<max_rss_gb<<std::endl;
141  SETERRABORT(PROTEUS_COMM_WORLD,PETSC_ERR_MEM,"Exceeded Proteus memory limit");
142  }
143  if (rank == 0)
144  std::cout<<msg<<std::endl
145  <<"Max memory usage per core "<<current_global<<"GB"<<std::endl;
146  return 0;
147  }
148 
149  extern int partitionElementsOriginal(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh, int nElements_overlap);
150 
151  extern int partitionNodes(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh, int nNodes_overlap);
152 
153  extern int partitionNodesFromTetgenFiles(const MPI_Comm& PROTEUS_COMM_WORLD, const char* filebase, int indexBase, Mesh& newMesh, int nNodes_overlap);
154  extern int partitionNodesFromTriangleFiles(const MPI_Comm& PROTEUS_COMM_WORLD, const char* filebase, int indexBase, Mesh& newMesh, int nNodes_overlap);
155 
156  extern int partitionElements(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh, int nElements_overlap);
157 
158  extern int buildQuadraticSubdomain2GlobalMappings_1d(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh,
159  const int *elementOffsets_subdomain_owned,
160  const int *nodeOffsets_subdomain_owned,
161  const int *elementNumbering_subdomain2global,
162  const int *nodeNumbering_subdomain2global,
163  int& nDOF_all_processes,//total number of dofs in whole domain
164  int& nDOF_subdomain,//total number of dofs in sub-domain
165  int& max_dof_neighbors,//maximum number of neighbors for connectivity of dofs
166  int *offsets_subdomain_owned, //starting point of local dofs on each processor (nProcs+1)
167  int * subdomain_l2g, //local to global dof mapping on subdomain
168  int* subdomain2global,//subdomain dof to global (parallel) numbering
169  double * lagrangeNodesArray);//location of nodes corresponding to dofs
170 
171  extern int buildQuadraticSubdomain2GlobalMappings_2d(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh,
172  const int *elementBoundaryOffsets_subdomain_owned,
173  const int *nodeOffsets_subdomain_owned,
174  const int *elementBoundaryNumbering_subdomain2global,
175  const int *nodeNumbering_subdomain2global,
176  int& nDOF_all_processes,//total number of dofs in whole domain
177  int& nDOF_subdomain,//total number of dofs in sub-domain
178  int& max_dof_neighbors,//maximum number of neighbors for connectivity of dofs
179  int *offsets_subdomain_owned, //starting point of local dofs on each processor (nProcs+1)
180  int *subdomain_l2g, //local to global dof mapping on subdomain
181  int *subdomain2global,//subdomain dof to global (parallel) numbering
182  double * lagrangeNodesArray);//location of nodes corresponding to dofs
183 
184  extern int buildQuadraticSubdomain2GlobalMappings_3d(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh,
185  const int *edgeOffsets_subdomain_owned,
186  const int *nodeOffsets_subdomain_owned,
187  const int *edgeNumbering_subdomain2global,
188  const int *nodeNumbering_subdomain2global,
189  int& nDOF_all_processes,//total number of dofs in whole domain
190  int& nDOF_subdomain,//total number of dofs in sub-domain
191  int& max_dof_neighbors,//maximum number of neighbors for connectivity of dofs
192  int *offsets_subdomain_owned, //starting point of local dofs on each processor (nProcs+1)
193  int *subdomain_l2g, //local to global dof mapping on subdomain
194  int *subdomain2global,//subdomain dof to global (parallel) numbering
195  double * lagrangeNodesArray);//location of nodes corresponding to dofs
196 
197  extern int buildQuadraticCubeSubdomain2GlobalMappings_3d(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh,
198  const int *edgeOffsets_subdomain_owned,
199  const int *nodeOffsets_subdomain_owned,
200  const int *edgeNumbering_subdomain2global,
201  const int *nodeNumbering_subdomain2global,
202  int& nDOF_all_processes,//total number of dofs in whole domain
203  int& nDOF_subdomain,//total number of dofs in sub-domain
204  int& max_dof_neighbors,//maximum number of neighbors for connectivity of dofs
205  int *offsets_subdomain_owned, //starting point of local dofs on each processor (nProcs+1)
206  int *subdomain_l2g, //local to global dof mapping on subdomain
207  int *subdomain2global,//subdomain dof to global (parallel) numbering
208  double * lagrangeNodesArray);//location of nodes corresponding to dofs
209 
210  extern int buildDiscontinuousGalerkinSubdomain2GlobalMappings(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh,
211  const int *elementOffsets_subdomain_owned,
212  const int *elementNumbering_subdomain2global,
213  int nDOF_element,
214  int& nDOF_all_processes,//total number of dofs in whole domain
215  int& nDOF_subdomain,//total number of dofs in sub-domain
216  int& max_dof_neighbors,//maximum number of neighbors for connectivity of dofs
217  int *offsets_subdomain_owned, //starting point of local dofs on each processor (nProcs+1)
218  int * subdomain_l2g, //local to global dof mapping on subdomain
219  int* subdomain2global);//subdomain dof to global (parallel) numbering
220 }//proteus
221 #endif
proteus::fp
double fp(const double &g, const double &h, const double &hZ)
Definition: SW2DCV.h:66
proteus::partitionElements
int partitionElements(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, int nElements_overlap)
Definition: partitioning.cpp:4451
proteus::partitionNodesFromTriangleFiles
int partitionNodesFromTriangleFiles(const MPI_Comm &PROTEUS_COMM_WORLD, const char *filebase, int indexBase, Mesh &newMesh, int nNodes_overlap)
Definition: partitioning.cpp:2958
proteus::buildQuadraticSubdomain2GlobalMappings_2d
int buildQuadraticSubdomain2GlobalMappings_2d(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, const int *elementBoundaryOffsets_subdomain_owned, const int *nodeOffsets_subdomain_owned, const int *elementBoundaryNumbering_subdomain2global, const int *nodeNumbering_subdomain2global, int &nDOF_all_processes, int &nDOF_subdomain, int &max_dof_neighbors, int *offsets_subdomain_owned, int *subdomain_l2g, int *subdomain2global, double *lagrangeNodesArray)
Definition: partitioning.cpp:5638
Mesh
Definition: mesh.h:28
L
Double L
Definition: Headers.h:72
proteus::buildQuadraticCubeSubdomain2GlobalMappings_3d
int buildQuadraticCubeSubdomain2GlobalMappings_3d(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, const int *edgeOffsets_subdomain_owned, const int *nodeOffsets_subdomain_owned, const int *edgeNumbering_subdomain2global, const int *nodeNumbering_subdomain2global, int &nDOF_all_processes, int &nDOF_subdomain, int &max_dof_neighbors, int *offsets_subdomain_owned, int *subdomain_l2g, int *subdomain2global, double *lagrangeNodesArray)
Definition: partitioning.cpp:6076
meshio.h
proteus::getPeakRSS
size_t getPeakRSS()
Definition: partitioning.h:51
proteus::buildQuadraticSubdomain2GlobalMappings_1d
int buildQuadraticSubdomain2GlobalMappings_1d(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, const int *elementOffsets_subdomain_owned, const int *nodeOffsets_subdomain_owned, const int *elementNumbering_subdomain2global, const int *nodeNumbering_subdomain2global, int &nDOF_all_processes, int &nDOF_subdomain, int &max_dof_neighbors, int *offsets_subdomain_owned, int *subdomain_l2g, int *subdomain2global, double *lagrangeNodesArray)
Definition: partitioning.cpp:5486
proteus::buildQuadraticSubdomain2GlobalMappings_3d
int buildQuadraticSubdomain2GlobalMappings_3d(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, const int *edgeOffsets_subdomain_owned, const int *nodeOffsets_subdomain_owned, const int *edgeNumbering_subdomain2global, const int *nodeNumbering_subdomain2global, int &nDOF_all_processes, int &nDOF_subdomain, int &max_dof_neighbors, int *offsets_subdomain_owned, int *subdomain_l2g, int *subdomain2global, double *lagrangeNodesArray)
Definition: partitioning.cpp:5797
mesh.h
proteus::partitionNodes
int partitionNodes(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, int nNodes_overlap)
Definition: partitioning.cpp:539
proteus::partitionElementsOriginal
int partitionElementsOriginal(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, int nElements_overlap)
Definition: partitioning.cpp:6
proteus::partitionNodesFromTetgenFiles
int partitionNodesFromTetgenFiles(const MPI_Comm &PROTEUS_COMM_WORLD, const char *filebase, int indexBase, Mesh &newMesh, int nNodes_overlap)
Definition: partitioning.cpp:1464
proteus::getCurrentRSS
size_t getCurrentRSS()
Definition: partitioning.h:93
proteus
Definition: ADR.h:17
proteus::buildDiscontinuousGalerkinSubdomain2GlobalMappings
int buildDiscontinuousGalerkinSubdomain2GlobalMappings(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, const int *elementOffsets_subdomain_owned, const int *elementNumbering_subdomain2global, int nDOF_element, int &nDOF_all_processes, int &nDOF_subdomain, int &max_dof_neighbors, int *offsets_subdomain_owned, int *subdomain_l2g, int *subdomain2global)
Definition: partitioning.cpp:6450
proteus::enforceMemoryLimit
int enforceMemoryLimit(const MPI_Comm &PROTEUS_COMM_WORLD, int rank, double max_rss_gb, const char *msg)
Definition: partitioning.h:130