From cc38f9a8e6d51b2e96037375d338546cc606640d Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Sun, 21 Dec 2014 12:07:38 +0100 Subject: temporary non thread safe randomVector workaround for OSX --- packages/base/src/C/vector-aux.c | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'packages/base/src/C') diff --git a/packages/base/src/C/vector-aux.c b/packages/base/src/C/vector-aux.c index 54cc413..946d069 100644 --- a/packages/base/src/C/vector-aux.c +++ b/packages/base/src/C/vector-aux.c @@ -700,6 +700,61 @@ int saveMatrix(char * file, char * format, KDMAT(a)){ //////////////////////////////////////////////////////////////////////////////// +#ifdef OSX + +#pragma message "randomVector is not thread-safe in OSX" + +double gaussrand(int *phase, double *pV1, double *pV2, double *pS) +{ + double V1=*pV1, V2=*pV2, S=*pS; + double X; + + if(*phase == 0) { + do { + double U1 = (double)random() / (double)RAND_MAX; + double U2 = (double)random() / (double)RAND_MAX; + + V1 = 2 * U1 - 1; + V2 = 2 * U2 - 1; + S = V1 * V1 + V2 * V2; + } while(S >= 1 || S == 0); + + X = V1 * sqrt(-2 * log(S) / S); + } else + X = V2 * sqrt(-2 * log(S) / S); + + *phase = 1 - *phase; + *pV1=V1; *pV2=V2; *pS=S; + + return X; + +} + +int random_vector(unsigned int seed, int code, DVEC(r)) { + int phase = 0; + double V1,V2,S; + + int k; + switch (code) { + case 0: { // uniform + for (k=0; k