# Beginner CUDA: two dimensional blocks and two dimensional threads?

This is a follow up question to: CUDA: setting grid dimensions. The purpose of the question is to understand how Mathematica is interfacing with CUDA's architecture. I have a related question/request here: Looking for a working mathematica CUDA port of NVIDIA's nbody.cu. There is now a follow on question here: A simple experiment to understand CUDAFunctionLoad

My question is, "What do the dimensions of the last argument to CUDAFunctionLoad mean, and what does the optional last argument to a CUDAFunction mean, and how does one use the total dimensionality of 5 that is permissible in CUDA?"

The CUDA runtime model allows two block dimensions and two thread dimensions. An example, pulled from the "CUDA by Example by J. Sanders and E. Kandrot" has this C example:

void generate_frame( DataBlock *d, int ticks ) {
dim3 blocks(DIM/16,DIM/16);
dim3 threads(16,16);
kernel<<<blocks,threads>>>( d->dev_bitmap, ticks );


dim3 is a built-in CUDA type. It has three dimensions, two of which are usable. Presumably, the third dimension is sitting around waiting for new GPU architectures.

If I understand the documentation for CUDAFunctionLoad correctly, then the mathematica code (taken from the documentation) for blocks and threads goes like this:

Needs["CUDALink"]
srcf = FileNameJoin[{\$CUDALinkPath, "SupportFiles", "vecAdd.cu"}]

vectorAdd = (*16 blocks*)
CUDAFunctionLoad[{srcf},
"vecAdd", {{_Integer, _, "Input"}, {_Integer, _,
"Input"}, {_Integer, _, "Output"}, _Integer}, 16]

vectorAdd[Range, ConstantArray[2, 64], ConstantArray[0, 64], 64] (*works*)

vectorAdd[Range, ConstantArray[2, 64], ConstantArray[0, 64], 64, 256] (*also works, 256 threads for each of the 16 blocks ??*)


trying a different {blocks,threads} argument for CUDAFunctionLoad:

vectorAddALT = (*16 blocks with 32 threads per block??*)
CUDAFunctionLoad[{srcf},
"vecAdd", {{_Integer, _, "Input"}, {_Integer, _,
"Input"}, {_Integer, _, "Output"}, _Integer}, {16, 32}]


This works:

vectorAddALT[Range, ConstantArray[2, 64],  ConstantArray[0, 64], 64]


And so does this:

vectorAddALT[Range, ConstantArray[2, 64],
ConstantArray[0, 64], 64, 256] (*an additional 256 threads for each of the 16 blocks??*)


But, this doesn't (the original form of the vectorAdd compiled for 16 blocks)

vectorAdd[Range, ConstantArray[2, 64], ConstantArray[0, 64], {64,64}]
(*beginner's attempt to have a one dimensional set of blocks each with two dimensional thread indices*)

• Craig, I'm sorry that no-one seems to answer your questions about CUDA. I would have to install CUDALink` first which never worked out of the box on Ubuntu for me to even test your examples. I'm a bit stunned that no-one else seems to use it anymore. There were times, when it was hyped very much. – halirutan Jan 19 '15 at 21:21
• Thanks Halirutan, I hope to get things working eventually. I am a little surprised too, it seems to have so much potential. I'd like to collect a bunch of working examples for others to use. WCC – Craig Carter Jan 20 '15 at 2:44
• The main reason why I don't use it anymore is that it is a pain to keep things working throughout all operating systems but more important is, that my colleagues don't have the hardware I have and it is very likely I have to rewrite code if I want to share the CUDA stuff. – halirutan Jan 20 '15 at 2:48