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/hmatrix.cabal | 3 ++- packages/base/src/C/vector-aux.c | 57 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) (limited to 'packages') diff --git a/packages/base/hmatrix.cabal b/packages/base/hmatrix.cabal index c007509..b3559a9 100644 --- a/packages/base/hmatrix.cabal +++ b/packages/base/hmatrix.cabal @@ -1,5 +1,5 @@ Name: hmatrix -Version: 0.16.1.1 +Version: 0.16.1.2 License: BSD3 License-file: LICENSE Author: Alberto Ruiz @@ -118,6 +118,7 @@ library if arch(i386) cc-options: -arch i386 frameworks: Accelerate + cpp-options: -DOSX if os(freebsd) extra-lib-dirs: /usr/local/lib 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