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