-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench-ram.c
120 lines (109 loc) · 3.92 KB
/
bench-ram.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
/*
* Benchmark dSFMT and ranlux performances.
*
* Dario Mapelli, [email protected]
*
* Detailed analysis of the time required by the various operations.
* This benchmark tests only how quickly are the random numbers generated
* and uses huge arrays without saving them to the disk
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "ranlxd.h"
#include "dSFMT.h"
#include "dSFMT-api.h"
#include "ranlux-api.h"
int main (int numArg, char * listArg[])
{
printf( "Simple dSFMT and ranlux usage\n\n" ) ;
// parameters setting
int rand_length = 1e8 ;
double max_ram_dimensions = 8. ; //GiB
printf (" rand_length: %d\n", rand_length) ;
double rand_arr_dimension =
(double) rand_length * sizeof(double) / 1024. / 1024. / 1024. ;
printf(" rand_arr dimension: %f GiB \n\n", rand_arr_dimension);
if (2*rand_arr_dimension > max_ram_dimensions) {
printf("rand_arr too big (check on ram)\n");
return 0;
}
//all the generated numbers are copied here, otherwise (if -O3 is chosen)
//the compiler may undestand that they are not saved anywhere nor printed and
//the output_file binary doesnt contain the code to generate them
double *ranlux_storage = (double*) malloc (sizeof(double)*rand_length) ;
int i=0;
for (i=0; i<rand_length; i++) {
ranlux_storage[i] = -1.;
}
double *dsfmt_storage = (double*) malloc (sizeof(double)*rand_length) ;
for (i=0; i<rand_length; i++) {
dsfmt_storage[i] = -1.;
}
//defined in time.h, used to compute the execution times
clock_t start, checkpoint, checkpoint1, checkpoint2, end;
double alloc_time, gen_time, copy_time, free_time, cpu_time_tot;
//this is the array that will be used to generate the random numbers
double *rand_arr;
// ranlux
start = clock();
int ranlux_check = ranlux_alloc (&rand_arr, rand_length) ;
if (ranlux_check==1){
exit(EXIT_FAILURE) ;
}
checkpoint = clock();
rlxd_init(1,time(NULL));
ranlxd (rand_arr,rand_length);
checkpoint1 = clock();
for (i=0; i<rand_length; i++) {
ranlux_storage[i] = rand_arr[i] ;
}
checkpoint2 = clock();
free(rand_arr) ;
end = clock();
alloc_time = ((double) (checkpoint - start)) / CLOCKS_PER_SEC ;
printf("ranlux alloc time: %10f sec\n", alloc_time);
gen_time = ((double) (checkpoint1 - checkpoint)) / CLOCKS_PER_SEC ;
printf("ranlux gen time: %10f sec\n", gen_time);
copy_time = ((double) (checkpoint2 - checkpoint1)) / CLOCKS_PER_SEC ;
printf("ranlux copy time: %10f sec\n", copy_time);
free_time = ((double) (end - checkpoint2)) / CLOCKS_PER_SEC ;
printf("ranlux free time: %10f sec\n", free_time);
cpu_time_tot = ((double) (end - start)) / CLOCKS_PER_SEC ;
printf("ranlux total time: %10f sec\n", cpu_time_tot);
// dSFMT
printf("\n");
start = clock();
int size = dsfmt_get_min_array_size();
if (size < rand_length) {
size = rand_length;
}
int dSFMT_check = dSFMT_alloc (&rand_arr, size) ;
if (dSFMT_check == 1) {
exit(EXIT_FAILURE) ;
}
checkpoint = clock();
dsfmt_gv_init_gen_rand( time (NULL) ) ;
dsfmt_gv_fill_array_close_open(rand_arr, size);
checkpoint1 = clock();
for (i=0; i<rand_length; i++) {
dsfmt_storage[i] = rand_arr[i] ;
}
checkpoint2 = clock();
free(rand_arr) ;
end = clock();
alloc_time = ((double) (checkpoint - start)) / CLOCKS_PER_SEC ;
printf("dSFMT alloc time: %10f sec\n", alloc_time);
gen_time = ((double) (checkpoint1 - checkpoint)) / CLOCKS_PER_SEC ;
printf("dSFMT gen time: %10f sec\n", gen_time);
copy_time = ((double) (checkpoint2 - checkpoint1)) / CLOCKS_PER_SEC ;
printf("dSFMT copy time: %10f sec\n", copy_time);
free_time = ((double) (end - checkpoint2)) / CLOCKS_PER_SEC ;
printf("dSFMT free time: %10f sec\n", free_time);
cpu_time_tot = ((double) (end - start)) / CLOCKS_PER_SEC ;
printf("dSFMT total time: %10f sec\n", cpu_time_tot);
free(ranlux_storage) ;
free(dsfmt_storage) ;
return 0;
}