Friday, July 2, 2010

Mersenne Twister in CUDA

The Mersenne Twister algorithm (what a cool name) for generating random numbers is implemented as an example in the SDK folder provided by NVIDIA. However, as it happens with all the examples, it depends on the CUTIL library, which is not part of the standard CUDA API. There is another problem, the compilation of each example is carried out by a general set of makefiles customized for compiling all the examples. These are big problems if we want to generate a stand alone version of the examples. This is desirable because in this way we could adapt the code for our own purposes and eventually link with extra code.

In the rest of this post I will give the makefiles that accomplish the stand alone compilation independent of CUTIL for the Mersenne Twister example.

You will have to follow the following steps :

- Make a copy of the Mersenne Twister folder from the SDK (I renamed as MersenneTwisterCUDA),

- Remove all the references for the CUTIL library

- Add the following lines on

char raw_path[] = "/YOUR_PATH/MersenneTwisterCUDA/data/MersenneTwister.raw";
char dat_path[] = "/YOUR_PATH/MersenneTwisterCUDA/data/MersenneTwister.dat";

while removing the corresponding lines that originally define raw_path and dat_path.

- Finally, paste the make files I will provide and run $make on the command line
(Define YOUR_PATH according to the structure of your directories)

file: common

CUDA_PATH = /usr/local/cuda

C := gcc
CC := c++
NVCC := $(CUDA_PATH)/bin/nvcc

CUDA_INCLUDE_PATH = /usr/local/cuda/include
CUDA_FLAGS = -lcuda -lcudart


file: Makefile:
include ./

MersenneTwister.out: genmtrand.c.o MersenneTwister_gold.cpp.o
$(NVCC) -L$(CUDA_LIB_PATH) $(CUDA_FLAGS) genmtrand.c.o MersenneTwister_gold.cpp.o -o MersenneTwister.out dci.h MersenneTwister.h

MersenneTwister_gold.cpp.o: MersenneTwister_gold.cpp
$(CC) -c MersenneTwister_gold.cpp -o MersenneTwister_gold.cpp.o

genmtrand.c.o: genmtrand.c
$(C) -c genmtrand.c -o genmtrand.c.o

No comments:

Post a Comment