Skip to content

Workflow for compute devicesπŸ”—

Querying available compute devicesπŸ”—

There are two operators that allow you to obtain general information as well as very specific information about compute devices.

  • To find out, which compute devices are available, call the operator query_available_compute_devices. This operator returns the list of available compute devices.

  • In order to get information details on a certain compute device, call the operator get_compute_device_info.

Opening compute deviceπŸ”—

To open a compute device for preparation, open_compute_device has to be called.

At this point, you can view or edit the device parameters.

There are two operators that allow you to view and edit parameters for compute devices. The operator get_compute_device_param lets you query the set parameters for compute devices and the operator set_compute_device_param allows you to change the settings for compute devices.

The following settings can be viewed or edited:

  • For image and buffer cache as well as for pinned memory the cache capacity can be set.

  • For image and buffer cache as well as for pinned memory the used cache can be queried.

  • Furthermore pinned memory, i.e., memory with a fixed address that cannot be moved, can be allocated. Note that the transfer rate for this kind of memory is usually faster, however it is essential to increase the cache for this memory if more may be required.

  • Asynchronous execution can be turned on and off.

Initializing compute deviceπŸ”—

To prepare the execution of operators on a compute device, call init_compute_device. It is important to call this operator before using a compute device, since it prepares the operators that are going to be used for execution on the compute device by compiling the respective kernel. If init_compute_device is not called, the initialization is performed β€˜on demand’ which means that the first time the compute device is used for an operator, it will be significantly slower.

Activating compute deviceπŸ”—

To activate the execution of operators on the compute device, call activate_compute_device. With this operator, calculations on the compute device are activated. Note that only one compute device can be active for one thread. The compute device handle is thread-specific.

Performing calculations on compute deviceπŸ”—

In this step, calculations can be performed on a compute device.

Note

Not all parameter values may be available for using an operator on a compute device. Please refer to the operator reference for more information on supported parameters.

Deactivating compute deviceπŸ”—

To deactivate the execution of operators on the compute device call deactivate_compute_device.

For most applications, it is not necessary to release compute devices with the operator release_compute_device. This operator should only be called for freeing device related resources before unloading the HALCON library, if the library was loaded using a mechanism like LoadLibrary or dlopen (to learn more about this operator, please refer to the reference documentation of release_compute_device).

Programming exampleπŸ”—

Example program

%HALCONEXAMPLES%/hdevelop/System/Compute-Devices/compute_devices.hdev

The example program is designed to show how to use the different compute device operators in HALCON as well as how to evaluate whether compute devices will speed up an application, in this case an affine transformation.

First, available compute devices are detected and information about the vendor is displayed.

query_available_compute_devices (DeviceIdentifier)
disp_message (WindowHandle, 'Found ' + |DeviceIdentifier| + ' Compute Device(s):', 'window', 12, 12, 'black', 'true')
for Index := 0 to |DeviceIdentifier| - 1 by 1
    get_compute_device_info (DeviceIdentifier[Index], 'name', DeviceName)
    get_compute_device_info (DeviceIdentifier[Index], 'vendor', DeviceVendor)
    Message[Index] := 'Device #' + Index + ': ' + DeviceVendor + ' ' + DeviceName
endfor
disp_message (WindowHandle, Message, 'window', 42, 12, 'white', 'false')

To determine whether the application runs faster with or without the use of compute devices, a benchmark can be performed. To prepare the time measurement, the compute device is opened, a previously defined DeviceIndex is used in case several compute devices are found, and the asynchronous execution is deactivated to get comparable results.

open_compute_device (DeviceIdentifier[DeviceIndex], DeviceHandle)
set_compute_device_param (DeviceHandle, 'asynchronous_execution', 'false'

First the compute device has to be initialized, otherwise the initialization is performed with the first call of the operator which would influence the results of the comparison. Then the compute device is activated.

init_compute_device (DeviceHandle, 'affine_trans_image')
activate_compute_device (DeviceHandle)

Then input data for the benchmark has to be provided.

read_image (Image, 'rings_and_nuts')

Then the number of benchmark loops is set.

Loops := 200

The operator affine_trans_image is then called once to fill the caches. Then the loops are executed and the time it takes on the GPU is calculated.

affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
count_seconds (Before)
for Index := 1 to Loops by 1
    affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
endfor
count_seconds (After)
TimeGPU := (After - Before) * 1000.0 / Loops

Since the transfer time is also relevant for the decision whether compute devices will improve speed, a modified version of this benchmark is performed that includes the transfer from CPU to GPU and back. The operator set_grayval ensures that the image must be transferred to the GPU. The operator get_image_pointer1 ensures that ImageAffineTrans is transferred back to the CPU.

count_seconds (Before)
for Index := 1 to Loops by 1
    set_grayval (Image, 0, 0, Grayval)
    affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
    get_image_pointer1 (ImageAffineTrans, Pointer, Type, Width, Height)
endfor
count_seconds (After)
TimeGPUinclTransfer := (After - Before) * 1000.0 / Loops

Then the compute device can be deactivated.

deactivate_compute_device (DeviceHandle)

Now the same benchmark is performed on the CPU to compare the results.

affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
count_seconds (Before)
for Index := 1 to Loops by 1
    affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
endfor
count_seconds (After)
TimeCPU := (After - Before) * 1000.0 / Loops
SpeedUp := TimeCPU / TimeGPU

Finally the runtimes of the CPU and GPU are compared.