File Exchange

image thumbnail

MatCL

version 1.0 (2.64 MB) by

OpenCL wrapper for Matlab

5 Downloads

Updated

MatCL is an OpenCL interface for MathWorks Matlab. This MEX-based toolbox aims at providing a simple and easy to use solution to transfer memory and launch OpenCL kernels from Matlab using a single command.
In comparison to other Matlab OpenCL solutions, MatCL is not just an OpenCL API wrapper but encapsulates the low-level host API calls necessary to initialize devices, create OpenCL buffers from Matlab workspace variables and build and launch kernels.
MatCL is primarily intended to help in the development and testing of OpenCL kernels by allowing to transparently pass data from and to Matlab.
Because MatCL handles the entire low-level process, this toolbox makes it possible to execute kernels without in depth knowledge of the host implementation necessary to support the execution of OpenCL kernels.
MatCL is also optimized to allow efficient execution of OpenCL kernels within Matlab to accelerate computationally intensive tasks without having to rely on Nvidia CUDA. In addition to single command kernel execution, MatCL also allows for an independent two-step kernel compilation and launch workflow to save the kernel compile time and allow efficient repetitive kernel execution.
Tested using Nvidia (Tesla, GTX), AMD (Ryzen, Radeon R9, FirePro) and Intel (Xeon, Core, HD Graphics) devices with Matlab R2016b and up.

Usage:

Enumerate OpenCL Devices:
[names,dev_class,max_mem]=cl_get_devices;
names: Names of all available devices
dev_class: The device class (CPU, GPU or Other for other or unknown Accelerators)
max_mem: The available device memory in bytes

Build Kernel:
[kernels]=cl_run_kernel(ocl_dev_id,'kernel_url.cl','defines');
ocl_dev_id: ID of the OpenCL device to be used
kernel_url.cl: URL of the kernel file
defines: List of OpenCL compiler defines
kernels: List with names of all available kernels

Run Kernel:
[run_time]=cl_run_kernel(ocl_dev_id,','kernel_function',global_range,local_range,in1,out1,[rw_flags]);
ocl_dev_id: ID of the OpenCL device to be used
kernel_function: Name of the kernel function to execute
global_range: Global OpenCL range (see NDRange)
local_range: Local OpenCL range (see NDRange)
in1, out1,...: List of variables to pass from/to kernel
rw_flags: read/write flag for the Kernel variables, this can either be scalar (all variables are read&write) or a vector with an entry for each variable: 0 - read&write / 1 - kernel read only / 2 - kernel write only

Build & Run Kernel:
[run_time]=cl_run_kernel(ocl_dev_id,' kernel_url.cl ','defines ','kernel_function',global_range,local_range,in1,out1,[rw_flags]);
ocl_dev_id: ID of the OpenCL device to be used
kernel_url.cl: URL of the kernel file
defines: List of OpenCL compiler defines
kernel_function: Name of the kernel function to execute
global_range: Global OpenCL range (see NDRange)
local_range: Local OpenCL range (see NDRange)
in1, out1,...: List of variables to pass from/to kernel
rw_flags: read/write flag for the Kernel variables, this can either be scalar (all variables are read&write) or a vector with an entry for each variable: 0 - read&write / 1 - kernel read only / 2 - kernel write only

Comments and Ratings (4)

test_mul benchmark:

Elapsed time is 0.129309 seconds.
Elapsed time is 0.717135 seconds. % complete time with data trasnfer to and from GPU
Elapsed time is 0.002120 seconds. % GPU kernel time
Device: GeForce GTX TITAN
Building kernels only...
Device: GeForce GTX TITAN
Old instance found, running kernels only...
Elapsed time is 0.779391 seconds.
OpenCL Kernel time is 0.033940 seconds.
Device: GeForce GTX TITAN
Elapsed time is 1.206082 seconds.
OpenCL Kernel time is 0.033942 seconds.

% Matlab CUDA code:
% send data to GPU
Ac = gpuArray(A);
Bc = gpuArray(B);
% CUDA kernel call
mCc = Ac*Bc;
% get data from GPU
mC = gather(mCc);

So finaly, native and very user friendly matlab cuda functionality is still significantly faster then openCL version. Is definitely better to use CUDA GPU's than work with nonCUDA GPU's only via kernel functions.

Source files of cl_get _device and cl_run_kernel + mex-compile script still missing , too!!!

Still without users guide or any other kind of detailed description ... ??!!

Mex files only for windows!?
Add source files of kernel functions.
Add any description.

Updates

1.0

Added description

1.0

Added more tags

MATLAB Release
MATLAB 9.3 (R2017b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video