Skip to content

Commit 900fd9c

Browse files
committed
Add and change default to 32-bit mersenne twister
1 parent f425106 commit 900fd9c

File tree

9 files changed

+450
-144
lines changed

9 files changed

+450
-144
lines changed

example/sort.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ program sort_example
88
use, intrinsic :: ISO_C_BINDING
99
use flc
1010
use flc_algorithm, only : sort, shuffle
11-
use flc_random, only : Engine, normal_distribution
11+
use flc_random, only : Engine => MersenneEngine4, normal_distribution
1212
use example_utils, only : write_version, read_positive_int, STDOUT
1313
implicit none
1414
integer :: arr_size

include/flc_algorithm.i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static bool includes_impl(const T *data1, size_t size1,
212212

213213
%inline {
214214
template<class T>
215-
static void shuffle(std::SWIG_MERSENNE_TWISTER& g, T *DATA, size_t DATASIZE) {
215+
static void shuffle(std::FLC_DEFAULT_ENGINE& g, T *DATA, size_t DATASIZE) {
216216
std::shuffle(DATA, DATA + DATASIZE, g);
217217
}
218218
}

include/flc_random.i

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,66 +9,60 @@
99
%include "import_flc.i"
1010
%flc_add_header
1111

12-
/* -------------------------------------------------------------------------
13-
* Generator class definition
14-
* ------------------------------------------------------------------------- */
15-
1612
%{
1713
#include <random>
1814
%}
1915

20-
// TODO: define a CMake-configurable option for selecting the 32-bit twister
21-
#if 0
22-
#define SWIG_MERSENNE_TWISTER mt19937
23-
#define SWIG_MERSENNE_RESULT_TYPE int32_t
24-
#else
25-
#define SWIG_MERSENNE_TWISTER mt19937_64
26-
#define SWIG_MERSENNE_RESULT_TYPE int64_t
27-
#endif
28-
29-
%rename(Engine) std::SWIG_MERSENNE_TWISTER;
30-
%fortran_autofree_rvalue(std::SWIG_MERSENNE_TWISTER);
16+
/* -------------------------------------------------------------------------
17+
* Macros
18+
* ------------------------------------------------------------------------- */
3119

20+
%define %flc_random_engine(NAME, GENERATOR, RESULT_TYPE)
21+
%fortran_autofree_rvalue(std::GENERATOR);
3222
namespace std {
33-
class SWIG_MERSENNE_TWISTER
23+
24+
%rename(NAME) GENERATOR;
25+
%rename("next") GENERATOR::operator();
26+
27+
class GENERATOR
3428
{
3529
public:
36-
typedef SWIG_MERSENNE_RESULT_TYPE result_type;
30+
typedef RESULT_TYPE result_type;
3731

38-
SWIG_MERSENNE_TWISTER();
39-
explicit SWIG_MERSENNE_TWISTER(result_type seed_value);
32+
GENERATOR();
33+
explicit GENERATOR(result_type seed_value);
4034
void seed(result_type seed_value);
4135
void discard(unsigned long long count);
36+
result_type operator()();
4237
};
38+
4339
} // namespace std
40+
%enddef
4441

4542
/* -------------------------------------------------------------------------
4643
* RNG distribution routines
47-
*
48-
* The generated subroutines will be called from Fortran like:
49-
*
50-
* call uniform_real_distribution(gen, -10, 10, fill_array)
5144
* ------------------------------------------------------------------------- */
5245

53-
%define %flc_random_distribution1(DISTNAME, TYPE, ARG1)
46+
%define %flc_random_dist1(NAME, TYPE, GENERATOR, ARG1)
5447
%inline {
55-
static void DISTNAME(TYPE ARG1,
56-
std::SWIG_MERSENNE_TWISTER& g,
48+
static void NAME##_distribution(TYPE ARG1,
49+
std::GENERATOR& g,
5750
TYPE *DATA, size_t DATASIZE) {
58-
std::DISTNAME<TYPE> dist(ARG1);
51+
std::NAME##_distribution<TYPE> dist(ARG1);
5952
TYPE *end = DATA + DATASIZE;
6053
while (DATA != end) {
6154
*DATA++ = dist(g);
6255
}
6356
}
6457
}
6558
%enddef
66-
%define %flc_random_distribution2(DISTNAME, TYPE, ARG1, ARG2)
59+
60+
%define %flc_random_dist2(NAME, TYPE, GENERATOR, ARG1, ARG2)
6761
%inline {
68-
static void DISTNAME(TYPE ARG1, TYPE ARG2,
69-
std::SWIG_MERSENNE_TWISTER& g,
62+
static void NAME##_distribution(TYPE ARG1, TYPE ARG2,
63+
std::GENERATOR& g,
7064
TYPE *DATA, size_t DATASIZE) {
71-
std::DISTNAME<TYPE> dist(ARG1, ARG2);
65+
std::NAME##_distribution<TYPE> dist(ARG1, ARG2);
7266
TYPE *end = DATA + DATASIZE;
7367
while (DATA != end) {
7468
*DATA++ = dist(g);
@@ -77,11 +71,17 @@ static void DISTNAME(TYPE ARG1, TYPE ARG2,
7771
}
7872
%enddef
7973

74+
#define FLC_DEFAULT_ENGINE mt19937
75+
76+
// Engines
77+
%flc_random_engine(MersenneEngine4, mt19937, int32_t)
78+
%flc_random_engine(MersenneEngine8, mt19937_64, int64_t)
79+
8080
// Uniform distributions
81-
%flc_random_distribution2(uniform_int_distribution, int32_t, left, right)
82-
%flc_random_distribution2(uniform_int_distribution, int64_t, left, right)
83-
%flc_random_distribution2(uniform_real_distribution, double, left, right)
81+
%flc_random_dist2(uniform_int, int32_t, FLC_DEFAULT_ENGINE, left, right)
82+
%flc_random_dist2(uniform_int, int64_t, FLC_DEFAULT_ENGINE, left, right)
83+
%flc_random_dist2(uniform_real, double, FLC_DEFAULT_ENGINE, left, right)
8484

8585
// Gaussian distribution
86-
%flc_random_distribution1(normal_distribution, double, mean)
87-
%flc_random_distribution2(normal_distribution, double, mean, stddev)
86+
%flc_random_dist1(normal, double, FLC_DEFAULT_ENGINE, mean)
87+
%flc_random_dist2(normal, double, FLC_DEFAULT_ENGINE, mean, stddev)

src/flc_algorithm.f90

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ function swigf_includes__SWIG_7(data1, data2, cmp) &
15081508

15091509
subroutine swigf_shuffle__SWIG_1(g, data)
15101510
use, intrinsic :: ISO_C_BINDING
1511-
class(Engine), intent(in) :: g
1511+
class(MersenneEngine4), intent(in) :: g
15121512
integer(C_INT32_T), dimension(:), target :: data
15131513
type(SwigClassWrapper) :: farg1
15141514
type(SwigArrayWrapper) :: farg2
@@ -1520,7 +1520,7 @@ subroutine swigf_shuffle__SWIG_1(g, data)
15201520

15211521
subroutine swigf_shuffle__SWIG_2(g, data)
15221522
use, intrinsic :: ISO_C_BINDING
1523-
class(Engine), intent(in) :: g
1523+
class(MersenneEngine4), intent(in) :: g
15241524
integer(C_INT64_T), dimension(:), target :: data
15251525
type(SwigClassWrapper) :: farg1
15261526
type(SwigArrayWrapper) :: farg2
@@ -1532,7 +1532,7 @@ subroutine swigf_shuffle__SWIG_2(g, data)
15321532

15331533
subroutine swigf_shuffle__SWIG_3(g, data)
15341534
use, intrinsic :: ISO_C_BINDING
1535-
class(Engine), intent(in) :: g
1535+
class(MersenneEngine4), intent(in) :: g
15361536
real(C_DOUBLE), dimension(:), target :: data
15371537
type(SwigClassWrapper) :: farg1
15381538
type(SwigArrayWrapper) :: farg2
@@ -1544,7 +1544,7 @@ subroutine swigf_shuffle__SWIG_3(g, data)
15441544

15451545
subroutine swigf_shuffle__SWIG_4(g, data)
15461546
use, intrinsic :: ISO_C_BINDING
1547-
class(Engine), intent(in) :: g
1547+
class(MersenneEngine4), intent(in) :: g
15481548
type(C_PTR), dimension(:), target :: data
15491549
type(SwigClassWrapper) :: farg1
15501550
type(SwigArrayWrapper) :: farg2

src/flc_algorithmFORTRAN_wrap.cxx

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ enum AssignmentType {
230230
};
231231
}
232232

233+
#define SWIGPOLICY_std_mt19937 swig::ASSIGNMENT_DEFAULT
233234
#define SWIGPOLICY_std_mt19937_64 swig::ASSIGNMENT_DEFAULT
234235

235236
#include <stdexcept>
@@ -453,7 +454,7 @@ static bool includes_cmp(const T *DATA1,size_t DATASIZE1,const T *DATA2,size_t D
453454

454455

455456
template<class T>
456-
static void shuffle(std::mt19937_64& g, T *DATA, size_t DATASIZE) {
457+
static void shuffle(std::mt19937& g, T *DATA, size_t DATASIZE) {
457458
std::shuffle(DATA, DATA + DATASIZE, g);
458459
}
459460

@@ -1360,58 +1361,58 @@ SWIGEXPORT int _wrap_includes__SWIG_7(SwigArrayWrapper *farg1, SwigArrayWrapper
13601361

13611362

13621363
SWIGEXPORT void _wrap_shuffle__SWIG_1(SwigClassWrapper *farg1, SwigArrayWrapper *farg2) {
1363-
std::mt19937_64 *arg1 = 0 ;
1364+
std::mt19937 *arg1 = 0 ;
13641365
int32_t *arg2 = (int32_t *) 0 ;
13651366
size_t arg3 ;
13661367

1367-
SWIG_check_nonnull(*farg1, "std::mt19937_64 &", "Engine", "shuffle< int32_t >(std::mt19937_64 &,int32_t *,size_t)", return );
1368-
arg1 = (std::mt19937_64 *)farg1->cptr;
1368+
SWIG_check_nonnull(*farg1, "std::mt19937 &", "MersenneEngine4", "shuffle< int32_t >(std::mt19937 &,int32_t *,size_t)", return );
1369+
arg1 = (std::mt19937 *)farg1->cptr;
13691370
arg2 = (int32_t *)farg2->data;
13701371
arg3 = farg2->size;
13711372
shuffle< int32_t >(*arg1,arg2,arg3);
1372-
SWIG_free_rvalue< std::mt19937_64, SWIGPOLICY_std_mt19937_64 >(*farg1);
1373+
SWIG_free_rvalue< std::mt19937, SWIGPOLICY_std_mt19937 >(*farg1);
13731374
}
13741375

13751376

13761377
SWIGEXPORT void _wrap_shuffle__SWIG_2(SwigClassWrapper *farg1, SwigArrayWrapper *farg2) {
1377-
std::mt19937_64 *arg1 = 0 ;
1378+
std::mt19937 *arg1 = 0 ;
13781379
int64_t *arg2 = (int64_t *) 0 ;
13791380
size_t arg3 ;
13801381

1381-
SWIG_check_nonnull(*farg1, "std::mt19937_64 &", "Engine", "shuffle< int64_t >(std::mt19937_64 &,int64_t *,size_t)", return );
1382-
arg1 = (std::mt19937_64 *)farg1->cptr;
1382+
SWIG_check_nonnull(*farg1, "std::mt19937 &", "MersenneEngine4", "shuffle< int64_t >(std::mt19937 &,int64_t *,size_t)", return );
1383+
arg1 = (std::mt19937 *)farg1->cptr;
13831384
arg2 = (int64_t *)farg2->data;
13841385
arg3 = farg2->size;
13851386
shuffle< int64_t >(*arg1,arg2,arg3);
1386-
SWIG_free_rvalue< std::mt19937_64, SWIGPOLICY_std_mt19937_64 >(*farg1);
1387+
SWIG_free_rvalue< std::mt19937, SWIGPOLICY_std_mt19937 >(*farg1);
13871388
}
13881389

13891390

13901391
SWIGEXPORT void _wrap_shuffle__SWIG_3(SwigClassWrapper *farg1, SwigArrayWrapper *farg2) {
1391-
std::mt19937_64 *arg1 = 0 ;
1392+
std::mt19937 *arg1 = 0 ;
13921393
double *arg2 = (double *) 0 ;
13931394
size_t arg3 ;
13941395

1395-
SWIG_check_nonnull(*farg1, "std::mt19937_64 &", "Engine", "shuffle< double >(std::mt19937_64 &,double *,size_t)", return );
1396-
arg1 = (std::mt19937_64 *)farg1->cptr;
1396+
SWIG_check_nonnull(*farg1, "std::mt19937 &", "MersenneEngine4", "shuffle< double >(std::mt19937 &,double *,size_t)", return );
1397+
arg1 = (std::mt19937 *)farg1->cptr;
13971398
arg2 = (double *)farg2->data;
13981399
arg3 = farg2->size;
13991400
shuffle< double >(*arg1,arg2,arg3);
1400-
SWIG_free_rvalue< std::mt19937_64, SWIGPOLICY_std_mt19937_64 >(*farg1);
1401+
SWIG_free_rvalue< std::mt19937, SWIGPOLICY_std_mt19937 >(*farg1);
14011402
}
14021403

14031404

14041405
SWIGEXPORT void _wrap_shuffle__SWIG_4(SwigClassWrapper *farg1, SwigArrayWrapper *farg2) {
1405-
std::mt19937_64 *arg1 = 0 ;
1406+
std::mt19937 *arg1 = 0 ;
14061407
void **arg2 = (void **) 0 ;
14071408
size_t arg3 ;
14081409

1409-
SWIG_check_nonnull(*farg1, "std::mt19937_64 &", "Engine", "shuffle< void * >(std::mt19937_64 &,void **,size_t)", return );
1410-
arg1 = (std::mt19937_64 *)farg1->cptr;
1410+
SWIG_check_nonnull(*farg1, "std::mt19937 &", "MersenneEngine4", "shuffle< void * >(std::mt19937 &,void **,size_t)", return );
1411+
arg1 = (std::mt19937 *)farg1->cptr;
14111412
arg2 = (void **)farg2->data;
14121413
arg3 = farg2->size;
14131414
shuffle< void * >(*arg1,arg2,arg3);
1414-
SWIG_free_rvalue< std::mt19937_64, SWIGPOLICY_std_mt19937_64 >(*farg1);
1415+
SWIG_free_rvalue< std::mt19937, SWIGPOLICY_std_mt19937 >(*farg1);
14151416
}
14161417

14171418

0 commit comments

Comments
 (0)