Intel® FPGA SDK for OpenCL™ Pro Edition: Programming Guide

ID 683846
Date 12/19/2022
Public
Document Table of Contents

6.8. Sharing Multiple Devices Across Multiple Host Programs

In a system with multiple FPGA devices, each device appears as a separate cl_device_id object in the OpenCL host API. You can query various device properties using the clGetDeviceInfo function. Based on the properties, you can select devices you want to use in your program.

You can then pass these devices to clCreateContext function where the devices are locked by that OpenCL program until it either calls clReleaseContext function or terminates.

Figure 14. Sharing Multiple Devices Across Multiple Host Programs

Multiple processes or multiple trusted users can arbitrate between devices in a multi-device system using this locking mechanism. In case users have decided ahead of time which device (by name) each person uses, they can use clGetDeviceInfo function to select the cl_device_id with the name that a user is assigned to. To arbitrate more dynamically in case each of the N users want Di devices, use the following scheme:

  1. Each user queries the clGetDeviceIDs function to obtain a list of devices.
  2. Each user chooses Di devices (ideally randomly to minimize collisions) and passes those to the clCreateContext function.

It is possible that during step 2, another user may have already called clCreateContext function with that same device, in which case, the clCreateContext function call fails. The user should then repeat steps 2 (optionally changing the device selection) until it succeeds.

Consider the following example code snippet:

do {
    for(i = num_devices - 1; i >= 0; i--) {
      context = clCreateContext(0, 1, &(device_ids[i]), NULL, NULL, &status);

      if (status != CL_SUCCESS) {
        printf("Failed to get context with %d (error: %d), waiting\n", i, status
        sleep(1);
      } else {
        device_id = device_ids[i];
        break;
      }
    }
  } while (status != CL_SUCCESS);
// Exit this loop only when we’ve succeeded in creating a context
Note: The only limitation is that only one process can own and operate on a device at a time. All other processes must wait for that process to release the context for obtaining access to the device.