|
SOLUTIONS
|
OpenCLFunctionLoad
loads fun from source code prog, returning an OpenCLFunction object.
![]()
loads fun from source file progfile, returning an OpenCLFunction object.
DetailsDetails
- The OpenCLLink application must be loaded using Needs["OpenCLLink`"].
- If progfile is a dynamic library, then the dynamic library function fun is loaded.
- Possible argument and return types, and their corresponding OpenCL type, include:
-
_Integer mint Mathematica integer "Integer32" int 32-bit integer "Integer64" long/long long 64-bit integer _Real Real_t GPU real type "Double" double machine double "Float" float machine float {base, rank, io} CUDAMemory memory of specified base type, rank, and input/output option "Local" | "Shared" mint local or shared memory parameter {"Local" | "Shared", type} mint local or shared memory parameter - Valid io is
,
, and
. - If
is passed, then
is used by default. If
is passed, then
is used. - The rank can be omitted by using
or
. - Possible base types are:
-
_Integer _Real _Complex "Byte" "Bit16" "Integer32" "Byte[2]" "Bit16[2]" "Integer32[2]" "Byte[4]" "Bit16[4]" "Integer32[4]" "Byte[8]" "Bit16[8]" "Integer32[8]" "Byte[16]" "Bit16[16]" "Integer32[16]" "UnsignedByte" "UnsignedBit16" "UnsignedInteger" "UnsignedByte[2]" "UnsignedBit16[2]" "UnsignedInteger[2]" "UnsignedByte[4]" "UnsignedBit16[4]" "UnsignedInteger[4]" "UnsignedByte[8]" "UnsignedBit16[8]" "UnsignedInteger[8]" "UnsignedByte[16]" "UnsignedBit16[16]" "UnsignedInteger[16]" "Double" "Float" "Integer64" "Double[2]" "Float[2]" "Integer64[2]" "Double[4]" "Float[4]" "Integer64[4]" "Double[8]" "Float[8]" "Integer64[8]" "Double[16]" "Float[16]" "Integer64[16]"
can be called more than once with different arguments.- Functions loaded by
run in the same process as the Mathematica kernel. - Functions loaded by
are unloaded when the Mathematica kernel exits. - Block dimensions can be either a list or an integer denoting how many threads per block to launch.
- The maximum size of block dimensions is returned by the
property of OpenCLInformation. - On launch, if the number of threads is not specified (as an extra argument to OpenCLFunction), then the dimension of the element with largest rank and dimension is chosen. For images, the rank is set to 2.
- On launch, if the number of threads is not a multiple of the block dimension, then it is incremented to be a multiple of the block dimension.
- The following options can be given:
-
"CompileOptions" {} compile options passed directly to the OpenCL compiler "Defines" Automatic defines passed to the OpenCL preprocessor "Device" $OpenCLDevice OpenCL device used in computation "IncludeDirectories" {} directories to include in the compilation "Platform" $OpenCLPlatform OpenCL platform used in computation "ShellCommandFunction" None function to call with the shell commands used for compilation "ShellOutputFunction" None function to call with the shell output of running the compilation commands "TargetPrecision" Automatic precision used in computation "WorkingDirectory" Automatic the directory in which temporary files will be generated
ExamplesExamplesopen allclose all
Basic Examples (5)Basic Examples (5)
First, load the OpenCLLink application:
| In[1]:= |
This defines the OpenCL source code to load:
| In[2]:= |
This loads the OpenCL function:
| In[3]:= |
| Out[3]= |
This defines the input parameters:
| In[4]:= |
This calls the function with the arguments:
| In[5]:= |
This plots the result using ArrayPlot:
| In[6]:= |
| Out[6]= | ![]() |
This gets an OpenCL source file from the SupportFiles directory:
| In[1]:= |
| Out[1]= |
This loads the OpenCL function from the file:
| In[2]:= |
| Out[2]= |
| In[3]:= |
| Out[3]= |
DLLs can be loaded into OpenCLLink for use as an OpenCLFunction:
| In[1]:= |
| Out[1]= |
This makes sure that the file exists, since the precompiled library extension is operating-system dependent:
| In[2]:= |
| Out[2]= |
This loads the library using
:
| In[3]:= |
The function adds two to an input list:
| In[4]:= |
| Out[4]= |
The source code for this example is bundled with OpenCLLink:
| In[5]:= |
| Out[5]= |
An extra argument can be given when calling OpenCLFunction. The argument denotes the number of threads to launch (or the global work group size). Using the previous example:
| In[1]:= |
| Out[1]= |
This loads the OpenCL function from the file:
This calls the function with 32 threads, which results in only the first 32 values in the vector add being computed:
| In[3]:= |
| Out[3]= | ![]() |
If code contains syntax errors, then a "compilation failed" error is returned:
The
option can be used to print the build log:
The above error states that there is a typo in the code, with a
after the
in the code:







