CpuRandom.cpp 4.45 KB
Newer Older
1
/* Portions copyright (c) 2013-2024 Stanford University and Simbios.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * Authors: Peter Eastman
 * Contributors: 
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "CpuRandom.h"
Jason Swails's avatar
Jason Swails committed
26
#include "openmm/internal/OSRngSeed.h"
27
28
#include "openmm/OpenMMException.h"
#include <cmath>
29
#include <iostream>
30
31
32
33
34
35
36
37

using namespace std;
using namespace OpenMM;

CpuRandom::CpuRandom() : hasInitialized(false) {
}

CpuRandom::~CpuRandom() {
peastman's avatar
peastman committed
38
39
    for (auto random : threadRandom)
        delete random;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
}

void CpuRandom::initialize(int seed, int numThreads) {
    if (hasInitialized) {
        if (seed == randomSeed)
            return; // Already initialized with the same seed.
        throw OpenMMException("Random number generator initialized twice with different seeds");
    }
    randomSeed = seed;
    hasInitialized = true;
    threadRandom.resize(numThreads);
    nextGaussian.resize(numThreads);
    nextGaussianIsValid.resize(numThreads, false);

54
    /* Use a quick and dirty RNG to pick seeds for the real random number generator.
55
     * A random seed of 0 means pick a unique seed
56
     */
57
58

    unsigned int r = (unsigned int) seed;
59
60
    if (r == 0)
        r = (unsigned int) osrngseed();
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
    for (int i = 0; i < numThreads; i++) {
        r = (1664525*r + 1013904223) & 0xFFFFFFFF;
        threadRandom[i] = new OpenMM_SFMT::SFMT();
        init_gen_rand(r, *threadRandom[i]);
    }
}

float CpuRandom::getGaussianRandom(int threadIndex) {
    if (nextGaussianIsValid[threadIndex]) {
        nextGaussianIsValid[threadIndex] = false;
        return nextGaussian[threadIndex];
    }
    
    // Use the polar form of the Box-Muller transformation to generate two Gaussian random numbers.
    
    float x, y, r2;
    do {
        x = 2.0f*(float) genrand_real2(*threadRandom[threadIndex])-1.0f;
        y = 2.0f*(float) genrand_real2(*threadRandom[threadIndex])-1.0f;
        r2 = x*x + y*y;
    } while (r2 >= 1.0f || r2 == 0.0f);
    float multiplier = sqrtf((-2.0f*logf(r2))/r2);
    nextGaussian[threadIndex] = y*multiplier;
    nextGaussianIsValid[threadIndex] = true;
    return x*multiplier;
}

float CpuRandom::getUniformRandom(int threadIndex) {
    return genrand_real2(*threadRandom[threadIndex]);
}
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
121
122
123

void CpuRandom::createCheckpoint(std::ostream& stream) {
    int initialized = hasInitialized;
    stream.write((char*) &initialized, sizeof(int));
    if (hasInitialized) {
        stream.write((char*) &randomSeed, sizeof(int));
        int numThreads = threadRandom.size();
        stream.write((char*) &numThreads, sizeof(int));
        stream.write((char*) nextGaussian.data(), sizeof(float)*numThreads);
        stream.write((char*) nextGaussianIsValid.data(), sizeof(int)*numThreads);
        for (int i = 0; i < numThreads; i++)
            threadRandom[i]->createCheckpoint(stream);
    }
}

void CpuRandom::loadCheckpoint(std::istream& stream) {
    int initialized;
    stream.read((char*) &initialized, sizeof(int));
    hasInitialized = false;
    threadRandom.clear();
    nextGaussian.clear();
    nextGaussianIsValid.clear();
    if (initialized) {
        int seed, numThreads;
        stream.read((char*) &seed, sizeof(int));
        stream.read((char*) &numThreads, sizeof(int));
        initialize(seed, numThreads);
        stream.read((char*) nextGaussian.data(), sizeof(float)*numThreads);
        stream.read((char*) nextGaussianIsValid.data(), sizeof(float)*numThreads);
        for (int i = 0; i < numThreads; i++)
            threadRandom[i]->loadCheckpoint(stream);
    }
}