-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsystem.c
127 lines (106 loc) · 2.56 KB
/
system.c
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
119
120
121
122
123
124
125
126
127
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <signal.h>
#include <gsl/gsl_rng.h>
#include <mpi.h>
#include "allvars.h"
#include "proto.h"
/*! \file system.c
* \brief contains miscellaneous routines, e.g. elapsed time measurements
*/
/*! This routine returns a random number taken from a table of random numbers,
* which is refilled every timestep. This method is used to allow random
* number application to particles independent of the number of processors
* used, and independent of the particular order the particles have. In order
* to work properly, the particle IDs should be set properly to unique
* integer values.
*/
double get_random_number(int id)
{
return RndTable[(id % RNDTABLE)];
}
/*! This routine fills the random number table.
*/
void set_random_numbers(void)
{
int i;
for(i = 0; i < RNDTABLE; i++)
RndTable[i] = gsl_rng_uniform(random_generator);
}
/*! returns the number of cpu-ticks in seconds that have elapsed, or the
* wall-clock time obtained with MPI_Wtime().
*/
double second(void)
{
#ifdef WALLCLOCK
return MPI_Wtime();
#else
return ((double) clock()) / CLOCKS_PER_SEC;
#endif
/* note: on AIX and presumably many other 32bit systems,
* clock() has only a resolution of 10ms=0.01sec
*/
}
/*! returns the time difference between two measurements obtained with
* second(). The routine takes care of the possible overflow of the tick
* counter on 32bit systems, but depending on the system, this may not always
* work properly. Similarly, in some MPI implementations, the MPI_Wtime()
* function may also overflow, in which case a negative time difference would
* be returned. The routine returns instead a time difference equal to 0.
*/
double timediff(double t0, double t1)
{
double dt;
dt = t1 - t0;
if(dt < 0) /* overflow has occured (for systems with 32bit tick counter) */
{
#ifdef WALLCLOCK
dt = 0;
#else
dt = t1 + pow(2, 32) / CLOCKS_PER_SEC - t0;
#endif
}
return dt;
}
/*! returns the maximum of two double
*/
double dmax(double x, double y)
{
if(x > y)
return x;
else
return y;
}
/*! returns the minimum of two double
*/
double dmin(double x, double y)
{
if(x < y)
return x;
else
return y;
}
/*! returns the maximum of two integers
*/
int imax(int x, int y)
{
if(x > y)
return x;
else
return y;
}
/*! returns the minimum of two integers
*/
int imin(int x, int y)
{
if(x < y)
return x;
else
return y;
}