/perf/kseta

To get this branch, use:
bzr branch http://darksoft.org/webbzr/perf/kseta

« back to all changes in this revision

Viewing changes to tutorials/4_pi/random123/conventional/gsl_cbrng.h

  • Committer: Suren A. Chilingaryan
  • Date: 2013-10-08 23:53:50 UTC
  • Revision ID: csa@dside.dyndns.org-20131008235350-hsu8oukzkh05gtcm
Add tutorials

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright 2010-2011, D. E. Shaw Research.
 
3
All rights reserved.
 
4
 
 
5
Redistribution and use in source and binary forms, with or without
 
6
modification, are permitted provided that the following conditions are
 
7
met:
 
8
 
 
9
* Redistributions of source code must retain the above copyright
 
10
  notice, this list of conditions, and the following disclaimer.
 
11
 
 
12
* Redistributions in binary form must reproduce the above copyright
 
13
  notice, this list of conditions, and the following disclaimer in the
 
14
  documentation and/or other materials provided with the distribution.
 
15
 
 
16
* Neither the name of D. E. Shaw Research nor the names of its
 
17
  contributors may be used to endorse or promote products derived from
 
18
  this software without specific prior written permission.
 
19
 
 
20
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
21
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
22
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
23
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
24
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
25
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
26
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
27
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
28
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
29
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
30
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
31
*/
 
32
#ifndef __r123_compat_gslrng_dot_h__
 
33
#define __r123_compat_gslrng_dot_h__
 
34
 
 
35
#include <gsl/gsl_rng.h>
 
36
#include <string.h>
 
37
 
 
38
/**
 
39
   The macro:  GSL_CBRNG(NAME, CBRNGNAME)
 
40
   declares the necessary structs and  constants that define a
 
41
   gsl_rng_NAME type based on the counter-based RNG CBRNGNAME.  For example:
 
42
 
 
43
   Usage:
 
44
 
 
45
   @code
 
46
   #include <Random123/threefry.h>
 
47
   #include <Random123/conventional/gsl_cbrng.h>  // this file
 
48
   GSL_CBRNG(cbrng, threefry4x32); // creates gsl_rng_cbrng
 
49
 
 
50
   int main(int argc, char **argv){
 
51
       gsl_rng *r = gsl_rng_alloc(gsl_rng_cbrng);
 
52
       ... use r as you would use any other gsl_rng ...   
 
53
    }
 
54
    @endcode
 
55
 
 
56
    It requires that NAME be the name of a CBRNG that follows the
 
57
    naming and stylistic conventions of the Random123 library.
 
58
 
 
59
    Note that wrapping a \ref CBRNG "counter-based PRNG" with a traditional API in
 
60
    this way obscures much of the power of the CBRNG API.
 
61
    Nevertheless, it may be of value to applications that are already
 
62
    coded to work with GSL random number generators, and that wish
 
63
    to use the RNGs in the Random123 library.
 
64
 
 
65
 */ 
 
66
 
 
67
#define GSL_CBRNG(NAME, CBRNGNAME)                                      \
 
68
const gsl_rng_type *gsl_rng_##NAME;                                     \
 
69
                                                                        \
 
70
typedef struct{                                                         \
 
71
    CBRNGNAME##_ctr_t ctr;                                                   \
 
72
    CBRNGNAME##_ctr_t r;                                                     \
 
73
    CBRNGNAME##_key_t key;                                                   \
 
74
    int elem;                                                           \
 
75
} NAME##_state;                                                         \
 
76
                                                                        \
 
77
static unsigned long int NAME##_get(void *vstate){                      \
 
78
    NAME##_state *st = (NAME##_state *)vstate;                          \
 
79
    const int N=sizeof(st->ctr.v)/sizeof(st->ctr.v[0]);                 \
 
80
    if( st->elem == 0 ){                                                \
 
81
        ++st->ctr.v[0];                                                 \
 
82
        if( N>1 && st->ctr.v[0] == 0 ) ++st->ctr.v[1];                  \
 
83
        if( N>2 && st->ctr.v[1] == 0 ) ++st->ctr.v[2];                  \
 
84
        if( N>3 && st->ctr.v[2] == 0 ) ++st->ctr.v[3];                  \
 
85
        st->r = CBRNGNAME(st->ctr, st->key);                                 \
 
86
        st->elem = N;                                                   \
 
87
    }                                                                   \
 
88
    return 0xffffffffUL & st->r.v[--st->elem];                          \
 
89
}                                                                       \
 
90
                                                                        \
 
91
static double                                                           \
 
92
NAME##_get_double (void * vstate)                                       \
 
93
{                                                                       \
 
94
    return NAME##_get (vstate)/4294967296.0;                            \
 
95
}                                                                       \
 
96
                                                                        \
 
97
static void NAME##_set(void *vstate, unsigned long int s){              \
 
98
    NAME##_state *st = (NAME##_state *)vstate;                          \
 
99
    st->elem = 0;                                                       \
 
100
    /* Assume that key and ctr have an array member, v,                 \
 
101
       as if they are r123arrayNxW.  If not, this will fail             \
 
102
       to compile.  In particular, this macro fails to compile          \
 
103
       when the underlying CBRNG requires use of keyinit */             \
 
104
    memset(&st->ctr.v[0], 0, sizeof(st->ctr.v));                        \
 
105
    memset(&st->key.v[0], 0, sizeof(st->key.v));                        \
 
106
    /* GSL 1.15 documentation says this about gsl_rng_set:              \
 
107
         Note that the most generators only accept 32-bit seeds, with higher \
 
108
         values being reduced modulo 2^32.  For generators with smaller \
 
109
         ranges the maximum seed value will typically be lower.         \
 
110
     so we won't jump through any hoops here to deal with               \
 
111
     high bits if sizeof(unsigned long) > sizeof(uint32_t). */          \
 
112
    st->key.v[0] = s;                                                   \
 
113
}                                                                       \
 
114
                                                                        \
 
115
static const gsl_rng_type NAME##_type = {                               \
 
116
    #NAME,                                                              \
 
117
    0xffffffffUL,                                                       \
 
118
    0,                                                                  \
 
119
    sizeof(NAME##_state),                                               \
 
120
    &NAME##_set,                                                        \
 
121
    &NAME##_get,                                                        \
 
122
    &NAME##_get_double                                                  \
 
123
};                                                                      \
 
124
                                                                        \
 
125
const gsl_rng_type *gsl_rng_##NAME = &NAME##_type
 
126
 
 
127
#endif
 
128