This repository was archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmemory_utils.cpp
More file actions
118 lines (109 loc) · 3.58 KB
/
memory_utils.cpp
File metadata and controls
118 lines (109 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
# =============================================================================
# Copyright (c) 2016 - 2021 Blue Brain Project/EPFL
#
# See top-level LICENSE file for details.
# =============================================================================.
*/
/**
* @file memory_utils.cpp
* @date 25th Oct 2014
*
* @brief Provides functionality to report current memory usage
* of the simulator using interface provided by malloc.h
*
* Memory utilisation report is based on the use of mallinfo
* interface defined in malloc.h. For 64 bit platform, this
* is not portable and hence it will be replaced with new
* glibc implementation of malloc_info.
*
* @see http://man7.org/linux/man-pages/man3/malloc_info.3.html
*/
#include <stdio.h>
#include <fstream>
#include <unistd.h>
#include "coreneuron/utils/memory_utils.h"
#include "coreneuron/utils/profile/cuda_profile.h"
#include "coreneuron/mpi/nrnmpi.h"
#include "coreneuron/mpi/core/nrnmpi.hpp"
#include "coreneuron/apps/corenrn_parameters.hpp"
#if defined(__APPLE__) && defined(__MACH__)
#include <mach/mach.h>
#elif defined HAVE_MALLOC_H
#include <malloc.h>
#endif
namespace coreneuron {
double nrn_mallinfo(void) {
// -ve mem usage for non-supported platforms
double mbs = -1.0;
// on os x returns the current resident set size (physical memory in use)
#if defined(__APPLE__) && defined(__MACH__)
struct mach_task_basic_info info;
mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t) &info, &infoCount) !=
KERN_SUCCESS)
return (size_t) 0L; /* Can't access? */
return info.resident_size / (1024.0 * 1024.0);
#elif defined(MINGW)
mbs = -1;
#else
std::ifstream file("/proc/self/statm");
if (file.is_open()) {
unsigned long long int data_size;
file >> data_size >> data_size;
file.close();
mbs = (data_size * sysconf(_SC_PAGESIZE)) / (1024.0 * 1024.0);
} else {
#if defined HAVE_MALLOC_H
// The mallinfo2() function was added in glibc 2.33
#if defined(__GLIBC__) && (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 33)
struct mallinfo2 m = mallinfo2();
#else
struct mallinfo m = mallinfo();
#endif
mbs = (m.hblkhd + m.uordblks) / (1024.0 * 1024.0);
#endif
}
#endif
return mbs;
}
void report_mem_usage(const char* message, bool all_ranks) {
double mem_max, mem_min, mem_avg; // min, max, avg memory
// current memory usage on this rank
double cur_mem = nrn_mallinfo();
/* @todo: avoid three all reduce class */
#if NRNMPI
if (corenrn_param.mpi_enable) {
mem_avg = nrnmpi_dbl_allreduce(cur_mem, 1) / nrnmpi_numprocs;
mem_max = nrnmpi_dbl_allreduce(cur_mem, 2);
mem_min = nrnmpi_dbl_allreduce(cur_mem, 3);
} else
#endif
{
mem_avg = mem_max = mem_min = cur_mem;
}
// all ranks prints information if all_ranks is true
if (all_ranks) {
printf(" Memory (MBs) (Rank : %2d) : %30s : Cur %.4lf, Max %.4lf, Min %.4lf, Avg %.4lf \n",
nrnmpi_myid,
message,
cur_mem,
mem_max,
mem_min,
mem_avg);
} else if (nrnmpi_myid == 0) {
printf(" Memory (MBs) : %25s : Max %.4lf, Min %.4lf, Avg %.4lf \n",
message,
mem_max,
mem_min,
mem_avg);
#ifdef CORENEURON_ENABLE_GPU
if (corenrn_param.gpu) {
// TODO: temporary to avoid CUDA code usage with LLVM build
//print_gpu_memory_usage();
}
#endif
}
fflush(stdout);
}
} // namespace coreneuron