MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
system.h
Go to the documentation of this file.
1 #ifndef __UTILS_SYSTEM_H__
2 #define __UTILS_SYSTEM_H__
3 
4 #include <stdint.h>
5 #include <mpi.h>
6 #include <stdarg.h>
7 
8 /* Note on a 32-bit architecture MPI_LONG may be 32-bit,
9  * so these should be MPI_LONG_LONG. But in
10  * the future MPI_LONG_LONG may become 128-bit.*/
11 #define MPI_UINT64 MPI_UNSIGNED_LONG
12 #define MPI_INT64 MPI_LONG
13 
14 /* Check the version of OPENMP. We only need OpenMP 3.1, but
15  * there is a bug in gcc 4.7 and 4.8 (which are unfortunately
16  * widely deployed as default compilers) which breaks our compile in fof.c.
17  * The easiest way to avoid it is to require OpenMP 4.0 but then we lose
18  * several clang versions, so just detect those gcc versions. */
19 /* icc defines __GNUC__ to be the version from the system compiler. No intel compilers
20  report _OPENMP > 3.1 without really supporting it, so we need not error for any intel versions.*/
21 #if _OPENMP < 201107 || (__GNUC__ == 4 && __GNUC_MINOR__ < 9 && !defined(__INTEL_COMPILER) && !defined(__clang__))
22 #error MP-Gadget requires OpenMP >= 3.1. Use a newer compiler (gcc >= 4.9, intel >= 16 clang >= 5).
23 #endif
24 
25 #ifdef DEBUG
26 void catch_abort(int sig);
27 void catch_fatal(int sig);
28 void enable_core_dumps_and_fpu_exceptions(void);
29 #endif
30 
31 int cluster_get_num_hosts(void);
32 double get_physmem_bytes(void);
33 
34 /* Gets a random number in the range [0, 1). Only the low bits of the id are used,
35  * and random deviates are drawn from a pre-seeded table so that they are independent of processor.*/
36 double get_random_number(uint64_t id);
37 void set_random_numbers(int seed);
38 void sumup_large_ints(int n, int *src, int64_t *res);
39 void sumup_longs(int n, int64_t *src, int64_t *res);
40 int64_t count_sum(int64_t countLocal);
41 //int64_t count_to_offset(int64_t countLocal);
42 
43 /* Returns true if condition is true on ANY processor*/
44 int MPIU_Any(int condition, MPI_Comm comm);
45 
46 void MPIU_write_pids(char * filename);
47 
48 /* Compact an array which has segments (usually corresponding to different threads).
49  * After this is run, it will be a single contiguous array. The memory can then be realloced.
50  * Function returns size of the final array.*/
51 size_t gadget_compact_thread_arrays(int * dest, int * srcs[], size_t sizes[], int narrays);
52 
53 /* Set up pointers to different parts of a single segmented array (usually corresponding to different threads).*/
54 void gadget_setup_thread_arrays(int * dest, int * srcs[], size_t sizes[], size_t total_size, int narrays);
55 
56 int MPI_Alltoallv_smart(void *sendbuf, int *sendcnts, int *sdispls,
57  MPI_Datatype sendtype, void *recvbuf, int *recvcnts,
58  int *rdispls, MPI_Datatype recvtype, MPI_Comm comm);
59 
60 int MPI_Alltoallv_sparse(void *sendbuf, int *sendcnts, int *sdispls,
61  MPI_Datatype sendtype, void *recvbuf, int *recvcnts,
62  int *rdispls, MPI_Datatype recvtype, MPI_Comm comm);
63 
64 double timediff(double t0, double t1);
65 double second(void);
66 size_t sizemax(size_t a, size_t b);
67 
68 static inline int64_t atomic_fetch_and_add_64(int64_t * ptr, int64_t value) {
69  int64_t k;
70 #pragma omp atomic capture
71  {
72  k = (*ptr);
73  (*ptr)+=value;
74  }
75  return k;
76 }
77 
78 static inline int atomic_fetch_and_add(int * ptr, int value) {
79  int k;
80 #pragma omp atomic capture
81  {
82  k = (*ptr);
83  (*ptr)+=value;
84  }
85  return k;
86 }
87 static inline int atomic_add_and_fetch(int * ptr, int value) {
88  int k;
89 #pragma omp atomic capture
90  {
91  (*ptr)+=value;
92  k = (*ptr);
93  }
94  return k;
95 }
96 
97 void MPIU_Trace(MPI_Comm comm, int where, const char * fmt, ...);
98 void MPIU_Tracev(MPI_Comm comm, int where, int error, const char * fmt, va_list va);
99 
100 int _MPIU_Barrier(const char * fn, const int ln, MPI_Comm comm);
101 
102 /* Fancy barrier which warns if there is a lot of imbalance. */
103 #define MPIU_Barrier(comm) _MPIU_Barrier(__FILE__, __LINE__, comm)
104 
105 #ifdef MOREDEBUG
106 /* Checked versions of some MPI routines which make sure that the number we get out is sensible*/
107 int MPI_Allreduce_Checked(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm, const int line, const char * file);
108 int MPI_Reduce_Checked(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm, const int line, const char * file);
109 /* Defines, guarded so the implementation can still use the original*/
110 #ifndef __UTILS_SYSTEM_C
111 #define MPI_Allreduce(sendbuf, recvbuf, count, datatype, op, comm) \
112 MPI_Allreduce_Checked(sendbuf, recvbuf, count, datatype, op, comm, __LINE__, __FILE__)
113 #define MPI_Reduce(sendbuf, recvbuf, count, datatype, op, root, comm) \
114 MPI_Reduce_Checked(sendbuf, recvbuf, count, datatype, op, root, comm, __LINE__, __FILE__)
115 #endif
116 
117 #endif
118 
119 #endif //_UTILS_SYSTEM_H
void gadget_setup_thread_arrays(int *dest, int *srcs[], size_t sizes[], size_t total_size, int narrays)
Definition: system.c:600
static int atomic_fetch_and_add(int *ptr, int value)
Definition: system.h:78
int MPI_Alltoallv_sparse(void *sendbuf, int *sendcnts, int *sdispls, MPI_Datatype sendtype, void *recvbuf, int *recvcnts, int *rdispls, MPI_Datatype recvtype, MPI_Comm comm)
Definition: system.c:353
double get_random_number(uint64_t id)
Definition: system.c:60
int64_t count_sum(int64_t countLocal)
Definition: system.c:264
double get_physmem_bytes(void)
Definition: system.c:467
void sumup_longs(int n, int64_t *src, int64_t *res)
Definition: system.c:215
void MPIU_Tracev(MPI_Comm comm, int where, int error, const char *fmt, va_list va)
Definition: system.c:149
int cluster_get_num_hosts(void)
Definition: system.c:434
void MPIU_write_pids(char *filename)
Definition: system.c:552
int MPI_Alltoallv_smart(void *sendbuf, int *sendcnts, int *sdispls, MPI_Datatype sendtype, void *recvbuf, int *recvcnts, int *rdispls, MPI_Datatype recvtype, MPI_Comm comm)
Definition: system.c:278
void MPIU_Trace(MPI_Comm comm, int where, const char *fmt,...)
Definition: system.c:182
static int atomic_add_and_fetch(int *ptr, int value)
Definition: system.h:87
size_t sizemax(size_t a, size_t b)
Definition: system.c:270
void sumup_large_ints(int n, int *src, int64_t *res)
Definition: system.c:192
double timediff(double t0, double t1)
Definition: system.c:92
void set_random_numbers(int seed)
Definition: system.c:65
size_t gadget_compact_thread_arrays(int *dest, int *srcs[], size_t sizes[], int narrays)
Definition: system.c:587
int _MPIU_Barrier(const char *fn, const int ln, MPI_Comm comm)
Definition: system.c:500
static int64_t atomic_fetch_and_add_64(int64_t *ptr, int64_t value)
Definition: system.h:68
double second(void)
Definition: system.c:83
int MPIU_Any(int condition, MPI_Comm comm)
Definition: system.c:545