OpenCLFunctionLoad
This feature is not supported on the Wolfram Cloud.

OpenCLFunctionLoad["src",fun,argtypes,blockdims]
compiles the string src and makes fun available in the Wolfram Language as an OpenCLFunction.

OpenCLFunctionLoad[File[srcfile],fun,argtypes,blockdim]
compiles the source code file srcfile and then loads fun as an OpenCLFunction..

OpenCLFunctionLoad[File[libfile],fun,argtypes,blockdim]
loads fun as an OpenCLFunction. from the previously compiled library libfile.

DetailsDetails

  • The OpenCLLink application must be loaded using Needs["OpenCLLink`"].
  • If libfile is a dynamic library, then the dynamic library function fun is loaded.
  • Possible argument and return types, and their corresponding OpenCL type, include:
  • _IntegermintWolfram Language integer
    "Integer32"int32-bit integer
    "Integer64"long/long long64-bit integer
    _RealReal_tGPU real type
    "Double"doublemachine double
    "Float"floatmachine float
    {base, rank, io}OpenCLMemorymemory of specified base type, rank, and input/output option
    "Local" | "Shared"mintlocal or shared memory parameter
    {"Local" | "Shared", type}mintlocal or shared memory parameter
  • In the specification {base, rank, io}, valid settings of io are "Input", "Output", and "InputOutput".
  • The argument specification {base} is equivalent to {base,_,"InputOutput"}, and {base,rank} is equivalent to {base,rank,"InputOutput"}.
  • The rank can be omitted by using {base,_,io} or {base,io}.
  • 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]"
  • OpenCLFunctionLoad can be called more than once with different arguments.
  • Functions loaded by OpenCLFunctionLoad run in the same process as the Wolfram Language kernel.
  • Functions loaded by OpenCLFunctionLoad are unloaded when the Wolfram Language 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 "MaximumWorkGroupSize" 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"Automaticdefines passed to the OpenCL preprocessor
    "Device"$OpenCLDeviceOpenCL device used in computation
    "IncludeDirectories"{}directories to include in the compilation
    "Platform"$OpenCLPlatformOpenCL platform used in computation
    "ShellCommandFunction"Nonefunction to call with the shell commands used for compilation
    "ShellOutputFunction"Nonefunction to call with the shell output of running the compilation commands
    "TargetPrecision"Automaticprecision used in computation
    "WorkingDirectory"Automaticthe directory in which temporary files will be generated

ExamplesExamplesopen allclose all

Basic Examples  (5)Basic Examples  (5)

First, load the OpenCLLink application:

In[1]:=
Click for copyable input

Define the OpenCL source code to load:

In[2]:=
Click for copyable input

Loads the OpenCL function:

In[3]:=
Click for copyable input
Out[3]=

Define the input parameters:

In[4]:=
Click for copyable input

Calls the function with the arguments:

In[8]:=
Click for copyable input

Plot the result using ArrayPlot:

In[9]:=
Click for copyable input
Out[9]=

Define the path to the OpenCL source file from the "SupportFiles/vectorAdd.cl":

In[1]:=
Click for copyable input
Out[1]=

Compile and load the OpenCL function from the file:

In[2]:=
Click for copyable input
Out[2]=

This calls the function:

In[3]:=
Click for copyable input
Out[3]=

Locate the example OpenCLLink library "addTwo_Dobule":

In[1]:=
Click for copyable input
Out[1]=

Load the library using OpenCLFunctionLoad:

In[2]:=
Click for copyable input

The function adds two to an input list:

In[3]:=
Click for copyable input
Out[3]=

The source code for this example is bundled with OpenCLLink:

In[4]:=
Click for copyable input
Out[4]=

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]:=
Click for copyable input
Out[1]=

This loads the OpenCL function from the file:

In[2]:=
Click for copyable input
Out[2]=

This calls the function with 32 threads, which results in only the first 32 values in the vector add being computed:

In[3]:=
Click for copyable input
Out[3]=

If code contains syntax errors, then a "compilation failed" error is returned:

In[1]:=
Click for copyable input

The "ShellOutputFunction" option can be used to print the build log:

In[2]:=
Click for copyable input

The above error states that there is a typo in the code, with a z after the 0 in the code:

In[3]:=
Click for copyable input
Out[3]=