VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
MemoryUtil.h
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
34
35#ifndef _MEMORYUTIL_H_
36#define _MEMORYUTIL_H_
37
38#if defined(_WIN32) || defined(_WIN64)
39#define MEMORYUTIL_WINDOWS
40#include "windows.h"
41#include "psapi.h"
42#pragma comment(lib, "psapi.lib")
43#elif defined __APPLE__
44#define MEMORYUTIL_APPLE
45#include <mach/mach.h>
46#include <mach/mach_host.h>
48#include "sys/sysctl.h"
49#include "sys/types.h"
50#include <cstdio>
51#include <cstdlib>
52#include <cstring>
53#elif (defined(__amd64) || defined(__amd64__) || defined(__unix__) || defined(__CYGWIN__)) && !defined(__AIX__)
54#define MEMORYUTIL_LINUX
55#include "cstdio"
56#include "cstdlib"
57#include "cstring"
58#include "sys/sysinfo.h"
59#include "sys/types.h"
60#else
61#error "MemoryUtil::UnknownMachine"
62#endif
63
64#if defined(__CYGWIN__)
65#define MEMORYUTIL_CYGWIN
66#endif
67
68#include <iostream>
69#include <sstream>
70#include <string>
71#include <vector>
72#include "Grid3D.h"
73#include "lbm/constants/D3Q27.h"
74
76// MemoryUtil
78namespace utilities
79{
81static long long getTotalPhysMem()
82{
83#if defined(MEMORYUTIL_WINDOWS)
85 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
87 DWORDLONG totalPhysMem = memInfo.ullTotalPhys;
88#elif defined(MEMORYUTIL_LINUX)
89 struct sysinfo memInfo;
91 long long totalPhysMem = memInfo.totalram;
92 // Multiply in next statement to avoid int overflow on right hand side...
93 totalPhysMem *= memInfo.mem_unit;
94#elif defined(MEMORYUTIL_APPLE)
95 int mib[] = { CTL_HW, HW_MEMSIZE };
97 size_t length = sizeof(totalPhysMem);
98
99 if (sysctl(mib, 2, &totalPhysMem, &length, nullptr, 0) == -1)
100 return 0;
101#else
102#error "MemoryUtil::getTotalPhysMem - UnknownMachine"
103#endif
104
105 return (long long)totalPhysMem;
106}
108static long long getPhysMemUsed()
109{
110#if defined(MEMORYUTIL_WINDOWS)
112 memInfo.dwLength = sizeof(MEMORYSTATUSEX);
114 DWORDLONG physMemUsed = memInfo.ullTotalPhys - memInfo.ullAvailPhys;
115#elif defined(MEMORYUTIL_LINUX)
116 struct sysinfo memInfo;
118 long long physMemUsed = memInfo.totalram - memInfo.freeram;
119 // Multiply in next statement to avoid int overflow on right hand side...
120 physMemUsed *= memInfo.mem_unit;
121#elif defined(MEMORYUTIL_APPLE)
125 UB_THROW(UbException(UB_EXARGS, "MemoryUtil::getPhysMemUsed - host_page_size failed on macOS"));
126
130 reinterpret_cast<host_info64_t>(&vm_stats), &count) != KERN_SUCCESS)
131 UB_THROW(UbException(UB_EXARGS, "MemoryUtil::getPhysMemUsed - host_statistics64 failed on macOS"));
132
133 const long long used_pages =
134 static_cast<long long>(vm_stats.active_count) +
135 static_cast<long long>(vm_stats.inactive_count) +
136 static_cast<long long>(vm_stats.wire_count) +
137 static_cast<long long>(vm_stats.speculative_count) +
138 static_cast<long long>(vm_stats.compressor_page_count);
139 long long physMemUsed = used_pages * static_cast<long long>(page_size);
140#else
141#error "MemoryUtil::getPhysMemUsed - UnknownMachine"
142#endif
143
144 return (long long)physMemUsed;
145}
147#if defined(MEMORYUTIL_LINUX) || defined(MEMORYUTIL_CYGWIN)
148static int parseLine(char *line)
149{
150 int i = strlen(line);
151 while (*line < '0' || *line > '9')
152 line++;
153 line[i - 3] = '\0';
154 i = atoi(line);
155 return i;
156}
157
158static int getValue()
159{ // Note: this value is in KB!
160 FILE *file = fopen("/proc/self/status", "r");
161 if (!file)
162 return 0;
163 int result = -1;
164 char line[128];
165
166 while (fgets(line, 128, file) != NULL) {
167 if (strncmp(line, "VmRSS:", 6) == 0) {
168 result = parseLine(line);
169 break;
170 }
171 }
172 fclose(file);
173 return result;
174}
175#endif
177static long long getPhysMemUsedByMe()
178{
179#if defined(MEMORYUTIL_WINDOWS) && !defined(__CYGWIN__)
182 SIZE_T physMemUsedByMe = pmc.WorkingSetSize;
183#elif defined(MEMORYUTIL_LINUX) || defined(MEMORYUTIL_CYGWIN)
184 long long physMemUsedByMe = (long long)getValue() * (long long)1024;
185#elif defined(MEMORYUTIL_APPLE)
189 reinterpret_cast<task_info_t>(&info), &count);
190 if (status != KERN_SUCCESS)
191 UB_THROW(UbException(UB_EXARGS, "MemoryUtil::getPhysMemUsedByMe - task_info failed on macOS"));
192 long long physMemUsedByMe = static_cast<long long>(info.resident_size);
193#else
194#error "MemoryUtil::getPhysMemUsedByMe - UnknownMachine"
195#endif
196
197 return (long long)physMemUsedByMe;
198}
200
201static std::string toString(SPtr<Grid3D> grid, int numberOfProcesses)
202{
203 unsigned long long numberOfBlocks = (unsigned long long)grid->getNumberOfBlocks();
204 int ghostLayer = grid->getGhostLayerWidth()*2+1;
205 UbTupleInt3 blockNx = grid->getBlockNX();
206
207 unsigned long long numberOfNodesPerBlock = (unsigned long long)(val<1>(blockNx)) *
208 (unsigned long long)(val<2>(blockNx)) *
209 (unsigned long long)(val<3>(blockNx));
210 unsigned long long numberOfNodes = numberOfBlocks * numberOfNodesPerBlock;
211 unsigned long long numberOfNodesPerBlockWithGhostLayer = numberOfBlocks * (val<1>(blockNx) + ghostLayer) *
214 real needMemAll = real(numberOfNodesPerBlockWithGhostLayer*(27*sizeof(real)+sizeof(int)+sizeof(real)*4));
215 real needMem = needMemAll / real(numberOfProcesses);
216
217 std::ostringstream out;
218 out << "Grid information:" << std::endl;
219 out << "###################################################" << std::endl;
220 out << "# Number of blocks = " << numberOfBlocks << std::endl;
221 out << "# Number of nodes = " << numberOfNodes << std::endl;
222 int minInitLevel = grid->getCoarsestInitializedLevel();
223 int maxInitLevel = grid->getFinestInitializedLevel();
224 for (int level = minInitLevel; level<=maxInitLevel; level++)
225 {
226 int nobl = grid->getNumberOfBlocks(level);
227 out << "# Number of blocks for level " << level << " = " << nobl << std::endl;
228 out << "# Number of nodes for level " << level << " = " << nobl * numberOfNodesPerBlock << std::endl;
229 }
230 out << "# Necessary memory = " << needMemAll << " bytes" << std::endl;
231 out << "# Necessary memory per process = " << needMem << " bytes" << std::endl;
232 out << "# Available memory per process = " << (real)getTotalPhysMem() << " bytes" << std::endl;
233 out << "###################################################" << std::endl;
234
235 return out.str();
236}
237
238} // namespace utilities
239
240#endif
241
std::shared_ptr< T > SPtr
float real
Definition DataTypes.h:42
#define UB_THROW(e)
Definition UbException.h:78
#define UB_EXARGS
Definition UbException.h:73
std::string toString(const T &x, int precision=15)
Definition UbSystem.h:199