/*
    Simulacia sirenia trhliny
    Copyright (C) 2008 Samuel Kupka

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/* POSIX.1-2001 */
#define MAX_GPURAND 32767

__device__ unsigned int *gpurandval;

__device__ inline int gpurand_kernel(unsigned int *gpurandval)
{
    unsigned int x=gpurandval[threadIdx.x];
    x=x*1103515245+12345;
    x%=2147483648;
    gpurandval[threadIdx.x]=x;
    return((x>>16)&MAX_GPURAND);
}

void init_gpurand(int num_threads)
{
    int s=sizeof(unsigned int)*num_threads;
    CUDA_SAFE_CALL(cudaMalloc((void **)&gpurandval,s));
    unsigned int *t;
    t=(unsigned int *)malloc(s);
    for(int i=0;i<num_threads;i++){
        t[i]=rand();
    }
    CUDA_SAFE_CALL(cudaMemcpy(gpurandval, t, s, cudaMemcpyHostToDevice));
    free(t);
}
