OpenCLFunctionLoad
This feature is not supported on the Wolfram Cloud.


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:
  • _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
  • 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 Wolfram Language kernel.
  • Functions loaded by 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 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

This defines the OpenCL source code to load:

In[2]:=
Click for copyable input

This loads the OpenCL function:

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

This defines the input parameters:

In[4]:=
Click for copyable input

This calls the function with the arguments:

In[5]:=
Click for copyable input

This plots the result using ArrayPlot:

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

This gets an OpenCL source file from the SupportFiles directory:

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:

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

DLLs can be loaded into OpenCLLink for use as an OpenCLFunction:

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

This makes sure that the file exists, since the precompiled library extension is operating-system dependent:

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

This loads the library using :

In[3]:=
Click for copyable input

The function adds two to an input list:

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

The source code for this example is bundled with OpenCLLink:

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

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:

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