OpenCLInformation
tells me that my computer has three available OpenCL devices. How can I choose which one to use for computations? How can I verify that Mathematica is honouring this choice?
The rest of this post is about how I tried to do this and failed:
Here's what SystemInformation[]
(as well as OpenCLInformation[]
) gives me:
(I am using OS X 10.9.1)
I tried to control the device using $OpenCLDevice
, like this:
Load OpenCLLink:
In[1]:= Needs["OpenCLLink`"]
Check what device it would use:
In[2]:= {$OpenCLPlatform, $OpenCLDevice}
Out[2]= {Automatic, Automatic}
According to the docs,
- Set to Automatic, the OpenCL computation is performed on the fastest device.
- Changes to
$OpenCLDevice
performed after execution of an OpenCLLink function are ignored.- If modified, then
$OpenCLPlatform
must be changed also.
So as I understand, I must set both of these variables, and I must set them before running any computations. Let's do it now:
In[3]:= $OpenCLPlatform = 1;
$OpenCLDevice = 2;
In[5]:= {$OpenCLPlatform, $OpenCLDevice}
Out[5]= {1, 2}
Now let's try a simple computation borrowed from the documentation:
In[6]:= doubleFun = OpenCLFunctionLoad["
__kernel void doubleVec(__global mint * in, mint length) {
int index = get_global_id(0);
if (index < length)
in[index] = 2*in[index];
}", "doubleVec", {{_Integer}, _Integer}, 256]
Out[6]= OpenCLFunction["<>", "doubleVec", {{_Integer}, "Integer64"}]
In[7]:= doubleFun[Range[20], 20]
Out[7]= {{2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40}}
It did work, but I wonder which device was used ...
In[8]:= $OpenCLDevice
Out[8]= 3
It appears it was device 3, not device 2, which I wanted to use. How can I force the system to use device 2, which is supposed to be faster than device 3?
Device 2 on my machine is an NVIDIA GPU and it works correctly with CUDALink. The documentation says that if a device works with CUDALink, it should also work with OpenCLLink.
OpenCLFunctionLoad
has"Platform"
and"Device"
options which are supposed to have values$OpenCLPlatform
and$OpenCLDevice
according to the docs. But in reality both have default valueAutomatic
. Specifying these options explicitly does seem to work, but I am not yet 100% convinced that Mathematica is really using the selected devices. Now I'd like to be able to verify that it does indeed use the chosen device. $\endgroup$