Exemple #1
0
cl_int MemObject::init()
{
    // Get the device list of the context
    DeviceInterface **devices = 0;
    cl_int rs;
    
    rs = context()->info(CL_CONTEXT_NUM_DEVICES, sizeof(uint), &p_num_devices, 0);
    
    if (rs != CL_SUCCESS)
        return rs;
    
    devices = (DeviceInterface **)malloc(p_num_devices * 
                                        sizeof(DeviceInterface *));
    
    if (!devices)
        return CL_OUT_OF_HOST_MEMORY;
    
    rs = context()->info(CL_CONTEXT_DEVICES, 
                         p_num_devices * sizeof(DeviceInterface *), devices, 0);
    
    if (rs != CL_SUCCESS)
        return rs;
    
    // Allocate a table of DeviceBuffers
    p_devicebuffers = (DeviceBuffer **)malloc(p_num_devices * 
                                             sizeof(DeviceBuffer *));
    
    if (!p_devicebuffers)
        return CL_OUT_OF_HOST_MEMORY;
    
    // Create a DeviceBuffer for each device
    for (int i=0; i<p_num_devices; ++i)
    {
        DeviceInterface *device = devices[i];
        
        p_devicebuffers[i] = device->createDeviceBuffer(this, &rs);
        
        if (rs != CL_SUCCESS)
        {
            free((void *)devices);
            return rs;
        }
    }
    
    // If we have only one device, already allocate the buffer
    if (p_num_devices == 1)
    {
        rs = p_devicebuffers[0]->allocate();
        
        if (rs != CL_SUCCESS)
            return rs;
    }
    
    return CL_SUCCESS;
}
Exemple #2
0
cl_int MemObject::init()
{
    // Get the device list of the context
    DeviceInterface **devices = 0;
    cl_int rs;

    rs = ((Context *)parent())->info(CL_CONTEXT_NUM_DEVICES,
                                     sizeof(unsigned int),
                                     &p_num_devices, 0);

    if (rs != CL_SUCCESS)
        return rs;

    p_devices_to_allocate = p_num_devices;
    devices = (DeviceInterface **)std::malloc(p_num_devices *
                                        sizeof(DeviceInterface *));

    if (!devices)
        return CL_OUT_OF_HOST_MEMORY;

    rs = ((Context *)parent())->info(CL_CONTEXT_DEVICES,
                                     p_num_devices * sizeof(DeviceInterface *),
                                     devices, 0);

    if (rs != CL_SUCCESS)
    {
        std::free((void *)devices);
        return rs;
    }

    // Allocate a table of DeviceBuffers
    p_devicebuffers = (DeviceBuffer **)std::malloc(p_num_devices *
                                             sizeof(DeviceBuffer *));

    if (!p_devicebuffers)
    {
        std::free((void *)devices);
        return CL_OUT_OF_HOST_MEMORY;
    }

    // If we have more than one device, the allocation on the devices is
    // defered to first use, so host_ptr can become invalid. So, copy it in
    // a RAM location and keep it. Also, set a flag telling CPU devices that
    // they don't need to reallocate and re-copy host_ptr
    if (p_num_devices > 1 && (p_flags & CL_MEM_COPY_HOST_PTR))
    {
        void *tmp_hostptr = std::malloc(size());

        if (!tmp_hostptr)
        {
            std::free((void *)devices);
            return CL_OUT_OF_HOST_MEMORY;
        }

        std::memcpy(tmp_hostptr, p_host_ptr, size());

        p_host_ptr = tmp_hostptr;
        // Now, the client application can safely std::free() its host_ptr
    }

    // Create a DeviceBuffer for each device
    unsigned int failed_devices = 0;

    for (unsigned int i=0; i<p_num_devices; ++i)
    {
        DeviceInterface *device = devices[i];

        rs = CL_SUCCESS;
        p_devicebuffers[i] = device->createDeviceBuffer(this, &rs);

        if (rs != CL_SUCCESS)
        {
            p_devicebuffers[i] = 0;
            failed_devices++;
        }
    }

    if (failed_devices == p_num_devices)
    {
        // Each device found a reason to reject the buffer, so it's invalid
        std::free((void *)devices);
        return rs;
    }

    std::free((void *)devices);
    devices = 0;

    // If we have only one device, already allocate the buffer
    if (p_num_devices == 1)
    {
        if (!p_devicebuffers[0]->allocate())
            return CL_MEM_OBJECT_ALLOCATION_FAILURE;
    }

    return CL_SUCCESS;
}