1 #include <iostream>
2 #include <cstdio>
3 #include <cstdlib>
4 #include <ctime>
5 #include "memory.h"
6 #include "communicate.h"
7 #include "sprng.h"
8 #include "sprng_cpp.h"
9
10 using namespace std;
11
12
13 Sprng *defaultgen = NULL;
14 int junk; /* pass useless pointer at times */
15 int junkmpi;
16
17 /********************* SIMPLE ************************/
18
init_rng_simple(int seed,int mult,int gtype)19 int * init_rng_simple(int seed, int mult, int gtype /* = 0 */)
20 {
21 int myid=0, nprocs=1;
22 int val;
23 Sprng * temp;
24
25 temp = SelectType(gtype);
26
27 val = temp->init_rng(myid,nprocs,seed,mult);
28
29 if(val == 0)
30 return NULL;
31 else {
32 if(defaultgen != NULL)
33 defaultgen->free_rng();
34
35 defaultgen = temp;
36
37 return &junk; /* return "garbage" value */
38 }
39 }
40
get_rn_int_simple()41 int get_rn_int_simple()
42 {
43 if(defaultgen == NULL) {
44 if(init_rng_simple(0,0) == NULL) {
45 return static_cast<int>(-1.0);
46 }
47 }
48
49 return defaultgen->get_rn_int();
50 }
51
get_rn_flt_simple()52 float get_rn_flt_simple()
53 {
54 if(defaultgen == NULL)
55 if(init_rng_simple(0,0) == NULL)
56 return -1.0;
57
58 return defaultgen->get_rn_flt();
59 }
60
61
get_rn_dbl_simple()62 double get_rn_dbl_simple()
63 {
64 if(defaultgen == NULL)
65 if(init_rng_simple(0,0) == NULL)
66 return -1.0;
67
68 return defaultgen->get_rn_dbl();
69 }
70
pack_rng_simple(char ** buffer)71 int pack_rng_simple(char **buffer)
72 {
73 if(defaultgen == NULL)
74 return 0;
75
76 return defaultgen->pack_rng(buffer);
77 }
78
unpack_rng_simple(char * packed,int gtype)79 int * unpack_rng_simple(char *packed, int gtype)
80 {
81 Sprng * temp;
82 int val;
83
84 temp = SelectType(gtype);
85 val = temp->unpack_rng(packed);
86
87 if(val == 0)
88 return NULL;
89 else {
90 if(defaultgen != NULL)
91 defaultgen->free_rng();
92
93 defaultgen = temp;
94 return &junk; /* return "garbage" value */
95 }
96 }
97
print_rng_simple()98 int print_rng_simple()
99 {
100 if(defaultgen == NULL) {
101 fprintf(stderr,"WARNING: No generator initialized so far\n");
102 return 0;
103 }
104
105 return defaultgen->print_rng();
106 }
107
make_new_seed()108 int make_new_seed()
109 {
110 time_t tp;
111 struct tm *temp;
112 unsigned int temp2, temp3;
113 static unsigned int temp4 = 0xe0e1;
114
115 time(&tp);
116 temp = localtime(&tp);
117
118 temp2 = (temp->tm_sec<<26)+(temp->tm_min<<20)+(temp->tm_hour<<15)+
119 (temp->tm_mday<<10)+(temp->tm_mon<<6);
120 temp3 = (temp->tm_year<<13)+(temp->tm_wday<<10)+(temp->tm_yday<<1)+
121 temp->tm_isdst;
122 temp2 ^= clock()^temp3;
123
124 temp4 = (temp4*0xeeee)%0xffff;
125 temp2 ^= temp4<<16;
126 temp4 = (temp4*0xaeee)%0xffff;
127 temp2 ^= temp4;
128
129 temp2 &= 0x7fffffff;
130
131 return temp2;
132 }
133
134 /********************* SIMPLE_MPI ************************/
135
136
init_rng_simple_mpi(int seed,int mult,int gtype)137 int * init_rng_simple_mpi(int seed, int mult, int gtype /* = 0 */)
138 {
139 int myid=0, nprocs=1;
140 Sprng *temp;
141
142 get_proc_info_mpi(&myid,&nprocs);
143
144 temp = SelectType(gtype);
145 temp->init_rng(myid,nprocs,seed,mult);
146
147 if(temp == NULL)
148 return NULL;
149 else {
150 if(defaultgen != NULL)
151 defaultgen->free_rng();
152
153 defaultgen = temp;
154
155 return &junkmpi; /* return "garbage" value */
156 }
157 }
158
get_rn_int_simple_mpi()159 int get_rn_int_simple_mpi()
160 {
161 if(defaultgen == NULL)
162 if(init_rng_simple_mpi(0,0) == NULL)
163 return static_cast<int>(-1.0);
164
165 return defaultgen->get_rn_int();
166 }
167
get_rn_flt_simple_mpi()168 float get_rn_flt_simple_mpi()
169 {
170 if(defaultgen == NULL)
171 if(init_rng_simple_mpi(0,0) == NULL)
172 return -1.0;
173
174 return defaultgen->get_rn_flt();
175 }
176
get_rn_dbl_simple_mpi()177 double get_rn_dbl_simple_mpi()
178 {
179 if(defaultgen == NULL)
180 if(init_rng_simple_mpi(0,0) == NULL)
181 return -1.0;
182
183 return defaultgen->get_rn_dbl();
184 }
185
186
187 #include "fwrap.cpp"
188
189
190
191 /***********************************************************************************
192 * SPRNG (c) 2014 by Florida State University *
193 * *
194 * SPRNG is licensed under a *
195 * Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. *
196 * *
197 * You should have received a copy of the license along with this *
198 * work. If not, see <http://creativecommons.org/licenses/by-nc-sa/4.0/>. *
199 ************************************************************************************/
200