Пример #1
0
  /**
   * Create a new context with the given platform and devices, using the given additional context
   * properties. The additional context properties are assumed to contain pairs of long values,
   * where even entries contain the context property name, and odd values the corresponding value.
   * This array may be <code>null</code>.
   *
   * @param platform The platform
   * @param devices The devices
   * @param additionalContextProperties Additional context properties
   * @return The new context
   */
  public static cl_context create(
      cl_platform_id platform, List<cl_device_id> devices, long additionalContextProperties[]) {
    cl_device_id devicesArray[] = devices.toArray(new cl_device_id[devices.size()]);

    cl_context_properties contextProperties = new cl_context_properties();
    contextProperties.addProperty(CL_CONTEXT_PLATFORM, platform);
    if (additionalContextProperties != null) {
      for (int i = 0; i < additionalContextProperties.length / 2; i++) {
        contextProperties.addProperty(
            additionalContextProperties[i * 2 + 0], additionalContextProperties[i * 2 + 1]);
      }
    }
    cl_context context =
        clCreateContext(contextProperties, devicesArray.length, devicesArray, null, null, null);
    return context;
  }
Пример #2
0
  public void reinit() throws OpenClException {
    // Obtain the platform IDs and initialize the context properties
    cl_platform_id platforms[] = new cl_platform_id[1];
    clGetPlatformIDs(platforms.length, platforms, null);
    cl_context_properties contextProperties = new cl_context_properties();
    contextProperties.addProperty(CL_CONTEXT_PLATFORM, platforms[0]);

    // Create an OpenCL context on a GPU device
    context =
        CL.clCreateContextFromType(contextProperties, CL.CL_DEVICE_TYPE_GPU, null, null, null);
    if (context == null) {
      // If no context for a GPU device could be created,
      // try to create one for a CPU device.
      context =
          CL.clCreateContextFromType(contextProperties, CL.CL_DEVICE_TYPE_CPU, null, null, null);

      if (context == null) {
        throw new OpenClException("Unable to create a context");
      }
    }

    // Enable exceptions and subsequently omit error checks in this sample
    CL.setExceptionsEnabled(true);

    // Get the list of GPU devices associated with the context
    long numBytes[] = new long[1];
    CL.clGetContextInfo(context, CL.CL_CONTEXT_DEVICES, 0, null, numBytes);

    // Obtain the cl_device_id for the first device
    int numDevices = (int) numBytes[0] / Sizeof.cl_device_id;
    cl_device_id[] devices = new cl_device_id[numDevices];
    CL.clGetContextInfo(context, CL.CL_CONTEXT_DEVICES, numBytes[0], Pointer.to(devices), null);
    device = devices[0];

    // Create a command-queue
    commandQueue = clCreateCommandQueue(context, devices[0], CL.CL_QUEUE_PROFILING_ENABLE, null);
  }