MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
mymalloc.c
Go to the documentation of this file.
1 #include <mpi.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <math.h>
8 
9 #include <omp.h>
10 #include "mymalloc.h"
11 #include "string.h"
12 #include "memory.h"
13 #include "system.h"
14 #include "endrun.h"
15 
16 /* The main allocator is used to store large objects, e.g. tree, toptree */
18 
19 /* The temp allocator is used to store objects that lives on the stack;
20  * replacing alloca and similar cases to avoid stack induced memory fragmentation
21  * */
23 
24 #ifdef VALGRIND
25 #define allocator_init allocator_malloc_init
26 #endif
27 
28 void
30 {
31  int Nt = omp_get_max_threads();
32  int NTask;
33  MPI_Comm_size(MPI_COMM_WORLD, &NTask);
34 
35  /* Reserve 4MB, 512 bytes per thread, 128 bytes per task and 128 bytes per thread per task (for export) for TEMP storage.*/
36  size_t n = 4096 * 1024 + 128 * NTask + 128 * Nt * NTask + 512 * Nt;
37 
38  message(0, "Reserving %td bytes per rank for TEMP memory allocator. \n", n);
39 
40  if (MPIU_Any(ALLOC_ENOMEMORY == allocator_init(A_TEMP, "TEMP", n, 1, NULL), MPI_COMM_WORLD)) {
41  endrun(0, "Insufficient memory for the TEMP allocator on at least one nodes."
42  "Requestion %td bytes. Try reducing MaxMemSizePerNode. Also check the node health status.\n", n);
43 
44  }
45 }
46 
47 void
48 mymalloc_init(double MaxMemSizePerNode)
49 {
50  /* Warning: this uses ta_malloc*/
51  size_t Nhost = cluster_get_num_hosts();
52 
53  MPI_Comm comm = MPI_COMM_WORLD;
54 
55  int NTask;
56 
57  MPI_Comm_size(comm, &NTask);
58 
59  double nodespercpu = (1.0 * Nhost) / (1.0 * NTask);
60  size_t n = 1.0 * MaxMemSizePerNode * nodespercpu * 1024. * 1024.;
61  message(0, "Nhost = %d\n", Nhost);
62  message(0, "Reserving %td bytes per rank for MAIN memory allocator. \n", n);
63  if(n < 1)
64  endrun(2, "Mem too small! MB/node=%g, nodespercpu = %g NTask = %d\n", MaxMemSizePerNode, nodespercpu, NTask);
65 
66 
67  if (MPIU_Any(ALLOC_ENOMEMORY == allocator_init(A_MAIN, "MAIN", n, 1, NULL), MPI_COMM_WORLD)) {
68  endrun(0, "Insufficient memory for the MAIN allocator on at least one nodes."
69  "Requestion %td bytes. Try reducing MaxMemSizePerNode. Also check the node health status.\n", n);
70  }
71 }
72 
73 static size_t highest_memory_usage = 0;
74 
75 void report_detailed_memory_usage(const char *label, const char * fmt, ...)
76 {
78  return;
79  }
80 
81  MPI_Comm comm = MPI_COMM_WORLD;
82 
83  int NTask;
84  int ThisTask;
85  MPI_Comm_size(comm, &NTask);
86  MPI_Comm_rank(comm, &ThisTask);
87 
88 
89  if (ThisTask != 0) {
90  return;
91  }
92 
94 
95  va_list va;
96  va_start(va, fmt);
97  char * buf = fastpm_strdup_vprintf(fmt, va);
98  va_end(va);
99 
100  message(1, "Peak Memory usage induced by %s\n", buf);
101  myfree(buf);
103 }
void message(int where, const char *fmt,...)
Definition: endrun.c:175
void endrun(int where, const char *fmt,...)
Definition: endrun.c:147
int allocator_init(Allocator *alloc, const char *name, const size_t request_size, const int zero, Allocator *parent)
Definition: memory.c:24
void allocator_print(Allocator *alloc)
Definition: memory.c:284
size_t allocator_get_used_size(Allocator *alloc, int dir)
Definition: memory.c:256
#define ALLOC_ENOMEMORY
Definition: memory.h:10
#define ALLOC_DIR_BOTH
Definition: memory.h:14
static size_t highest_memory_usage
Definition: mymalloc.c:73
Allocator A_TEMP[1]
Definition: mymalloc.c:22
void tamalloc_init(void)
Definition: mymalloc.c:29
void report_detailed_memory_usage(const char *label, const char *fmt,...)
Definition: mymalloc.c:75
Allocator A_MAIN[1]
Definition: mymalloc.c:17
void mymalloc_init(double MaxMemSizePerNode)
Definition: mymalloc.c:48
#define myfree(x)
Definition: mymalloc.h:19
char * fastpm_strdup_vprintf(const char *fmt, va_list va)
Definition: string.c:51
int cluster_get_num_hosts(void)
Definition: system.c:434
int MPIU_Any(int condition, MPI_Comm comm)
Definition: system.c:545
int ThisTask
Definition: test_exchange.c:23
int NTask
Definition: test_exchange.c:23