1 /************************************************************************/
2 /************************************************************************/
3 /*                                                                      */
4 /*      This package of C++ wrappers is intended to be called from a    */
5 /*      FORTRAN program. The main purpose of the package is to mediate  */
6 /*      between the call-by-address and call-by-value conventions in    */
7 /*      the two languages. In most cases, the arguments of the C++      */
8 /*      routines and the wrappers are the same.                         */
9 /*                                                                      */
10 /*      The wrappers should be treated as FORTRAN function calls.       */
11 /*      Their names will be the same, except in FORTRAN they will       */
12 /*      not have the final _F77. (Example: ffree_rng_F77 becomes        */
13 /*      ffree_rng                                                       */
14 /*                                                                      */
15 /* Note: This code is a header file to facilitte inlining.              */
16 /************************************************************************/
17 /************************************************************************/
18 
19 extern "C"
20 {
21   #include "fwrap.h"
22 }
23 
24 #include "sprng_cpp.h"
25 #include "memory.h"
26 
27 #if __GNUC__ > 3
28  #include <iostream>
29 #else
30  #include <iostream.h>
31 #endif
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
36 #include <sys/resource.h>
37 
38 
39 extern "C"
40 {
41   int fget_seed_rng_F77(int **genptr);
42   int ffree_rng_F77(int **genptr);
43   int fmake_new_seed_F77(void);
44   int * finit_rng_sim_F77(int *seed,  int *mult, int *gtype = 0);
45   int * finit_rng_F77(int * rng_type, int *gennum, int *total_gen, int *seed, int *length);
46   int fspawn_rng_F77(int **genptr,  int *nspawned, int **newGen);
47   int fget_rn_int_sim_F77(void);
48   int fget_rn_int_F77(int **genptr);
49   float fget_rn_flt_sim_F77(void);
50   float fget_rn_flt_F77(int **genptr);
51   double fget_rn_dbl_sim_F77(void);
52   double fget_rn_dbl_F77(int **genptr);
53   int fpack_rng_F77(int **genptr, char *buffer);
54   int fpack_rng_simple_F77(char *buffer);
55   int * funpack_rng_F77(char *buffer, int *rng_type);
56   int * funpack_rng_simple_F77(char *buffer, int *rng_type);
57   int fprint_rng_F77( int **genptr);
58   int fprint_rng_simple_F77(void);
59   int fseed_mpi_F77(void);
60 
61   int * finit_rng_simmpi_F77(int *seed, int *mult, int * rng_type);
62   int fget_rn_int_simmpi_F77(void);
63   float fget_rn_flt_simmpi_F77(void);
64   double fget_rn_dbl_simmpi_F77(void);
65 
66   double fcpu_t_F77(void);
67 }
68 
69 
fcpu_t_F77(void)70 double fcpu_t_F77(void)
71 {
72   double   current_time;
73 
74 #ifdef RUSAGE_SELF
75   struct rusage temp;
76 
77   getrusage(RUSAGE_SELF, &temp);
78 
79   current_time = (temp.ru_utime.tv_sec + temp.ru_stime.tv_sec +
80 		  1.0e-6*(temp.ru_utime.tv_usec + temp.ru_stime.tv_usec));
81 
82 #elif defined(CLOCKS_PER_SEC)
83   current_time = clock()/((double) CLOCKS_PER_SEC);
84 
85 #else
86   fprintf(stderr,"\nERROR: Timing routines not available\n\n");
87   current_time = 0.0;
88 #endif
89 
90   return (current_time);
91 }
92 
fget_seed_rng_F77(int ** genptr)93 int fget_seed_rng_F77(int **genptr)
94 {
95   Sprng * ptr = (Sprng *) *genptr;
96 
97   return ptr->get_seed_rng();
98 }
99 
ffree_rng_F77(int ** genptr)100 int ffree_rng_F77(int **genptr)
101 {
102   Sprng * ptr = (Sprng *) *genptr;
103 
104   return ptr->free_rng();
105 }
106 
fmake_new_seed_F77(void)107 int fmake_new_seed_F77(void)
108 {
109   return make_new_seed();
110 }
111 
finit_rng_sim_F77(int * seed,int * mult,int * gtype)112 int * finit_rng_sim_F77(int *seed,  int *mult, int *gtype)
113 {
114   return init_rng_simple(*seed, *mult, *gtype);
115 }
116 
finit_rng_F77(int * rng_type,int * gennum,int * total_gen,int * seed,int * length)117 int * finit_rng_F77(int * rng_type, int *gennum, int *total_gen, int *seed, int *length)
118 {
119   Sprng * ptr = SelectType(* rng_type);
120   int status = ptr->init_rng(*gennum, *total_gen, *seed, *length);
121 
122   if (status == 1)
123     return (int *) ptr;
124   else
125     return (int *) NULL;
126 }
127 
fspawn_rng_F77(int ** genptr,int * nspawned,int ** newGen)128 int fspawn_rng_F77(int **genptr,  int *nspawned, int **newGen)
129 {
130   int i, n;
131   Sprng ** tmpGen;
132   Sprng * ptr = (Sprng *) *genptr;
133 
134   n =  ptr->spawn_rng(*nspawned, &tmpGen);
135 
136   for (i=0; i< n; i++)
137     newGen[i] = (int *) tmpGen[i];
138 
139   if(n != 0)
140     delete [] tmpGen;
141 
142   return n;
143 }
144 
fget_rn_int_sim_F77(void)145 int fget_rn_int_sim_F77(void)
146 {
147   return get_rn_int_simple();
148 }
149 
fget_rn_int_F77(int ** genptr)150 int fget_rn_int_F77(int **genptr)
151 {
152   Sprng * ptr = (Sprng *) *genptr;
153 
154   return ptr->get_rn_int();
155 }
156 
fget_rn_flt_sim_F77(void)157 float fget_rn_flt_sim_F77(void)
158 {
159   return get_rn_flt_simple();
160 }
161 
fget_rn_flt_F77(int ** genptr)162 float fget_rn_flt_F77(int **genptr)
163 {
164   Sprng * ptr = (Sprng *) *genptr;
165 
166   return ptr->get_rn_flt();
167 }
168 
fget_rn_dbl_sim_F77(void)169 double fget_rn_dbl_sim_F77(void)
170 {
171   return get_rn_dbl_simple();
172 }
173 
fget_rn_dbl_F77(int ** genptr)174 double fget_rn_dbl_F77(int **genptr)
175 {
176   Sprng * ptr = (Sprng *) *genptr;
177 
178   return ptr->get_rn_dbl();
179 }
180 
fpack_rng_F77(int ** genptr,char * buffer)181 int fpack_rng_F77(int **genptr, char *buffer)
182 {
183   int size;
184   char *temp;
185   Sprng * ptr = (Sprng *) *genptr;
186 
187   size = ptr->pack_rng(&temp);
188 
189   if(temp != NULL)
190     {
191       memcpy(buffer,temp,size);
192       delete [] temp;
193     }
194 
195   return size;
196 }
197 
fpack_rng_simple_F77(char * buffer)198 int fpack_rng_simple_F77(char *buffer)
199 {
200   int size;
201   char *temp;
202 
203   size = pack_rng_simple(&temp);
204 
205   if(temp != NULL)
206     {
207       memcpy(buffer,temp,size);
208       delete [] temp;
209     }
210 
211   return size;
212 }
213 
funpack_rng_F77(char * buffer,int * rng_type)214 int * funpack_rng_F77(char *buffer, int * rng_type)
215 {
216   Sprng * ptr = SelectType(* rng_type);
217   int status = ptr->unpack_rng(buffer);
218 
219   if (status == 1)
220     return (int *) ptr;
221   else
222     return (int *) NULL;
223 }
224 
funpack_rng_simple_F77(char * buffer,int * rng_type)225 int * funpack_rng_simple_F77(char *buffer, int * rng_type)
226 {
227   return unpack_rng_simple(buffer, *rng_type);
228 }
229 
fprint_rng_F77(int ** genptr)230 int fprint_rng_F77( int **genptr)
231 {
232   Sprng * ptr = (Sprng *) *genptr;
233 
234   return ptr->print_rng();
235 }
236 
fprint_rng_simple_F77(void)237 int fprint_rng_simple_F77(void)
238 {
239   return print_rng_simple();
240 }
241 
242 
243 
244 
fseed_mpi_F77(void)245 int fseed_mpi_F77(void)
246 {
247 #ifdef SPRNG_MPI
248   return make_new_seed_mpi();
249 #else
250   return -1;
251 #endif
252 }
253 
254 
255 
finit_rng_simmpi_F77(int * seed,int * mult,int * rng_type)256 int * finit_rng_simmpi_F77(int *seed, int *mult, int *rng_type)
257 {
258   return init_rng_simple_mpi(*seed, *mult, *rng_type);
259 }
260 
fget_rn_int_simmpi_F77(void)261 int fget_rn_int_simmpi_F77(void)
262 {
263   return get_rn_int_simple_mpi();
264 }
265 
fget_rn_flt_simmpi_F77(void)266 float fget_rn_flt_simmpi_F77(void)
267 {
268   return get_rn_flt_simple_mpi();
269 }
270 
fget_rn_dbl_simmpi_F77(void)271 double fget_rn_dbl_simmpi_F77(void)
272 {
273   return get_rn_dbl_simple_mpi();
274 }
275 
276 
277 
278 /***********************************************************************************
279 * SPRNG (c) 2014 by Florida State University                                       *
280 *                                                                                  *
281 * SPRNG is licensed under a                                                        *
282 * Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. *
283 *                                                                                  *
284 * You should have received a copy of the license along with this                   *
285 * work. If not, see <http://creativecommons.org/licenses/by-nc-sa/4.0/>.           *
286 ************************************************************************************/
287