在OpenCL中创建子缓冲区 [英] creating subbuffer in openCL

查看:16
本文介绍了在OpenCL中创建子缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在OpenCL中运行我的第一批代码。为了创建子缓冲区,我有"OpenCL in Action"一书中的以下代码。代码显示创建子缓冲区失败的错误。我的电脑配备英特尔CPU和NVIDIA GPU。然而,当我在另一台装有AMD CPU和GPU的计算机上运行代码时,它工作得很好。你知道为什么这个代码不能在非AMD平台上运行吗? 以下是错误:

代码是这样的:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#ifdef MAC
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif

/* Find a GPU or CPU associated with the first available platform */
cl_device_id create_device() {

    cl_platform_id platform;
    cl_device_id dev;
    int err;


    /* Identify a platform */
    err = clGetPlatformIDs(1, &platform, NULL);
    if (err < 0) {
        perror("Couldn't identify a platform");
        exit(1);
    }

    /* Access a device */
    err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &dev, NULL);
    if (err == CL_DEVICE_NOT_FOUND) {
        err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &dev, NULL);
    }
    if (err < 0) {
        perror("Couldn't access any devices");
        exit(1);
    }

    return dev;
}

int main() {

    /* Host/device data structures */
    cl_device_id device;
    cl_context context;
    cl_int err;

    /* Data and buffers */
    float main_data[100] = {};
    cl_mem main_buffer, sub_buffer;
    void *main_buffer_mem = NULL, *sub_buffer_mem = NULL;
    size_t main_buffer_size, sub_buffer_size;
    cl_buffer_region region;

    /* Create device and context */
    device = create_device();
    context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
    if (err < 0) {
        perror("Couldn't create a context");
        exit(1);
    }

    /* Create a buffer to hold 100 floating-point values */
    main_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY |
        CL_MEM_COPY_HOST_PTR, sizeof(main_data), main_data, &err);
    if (err < 0) {
        perror("Couldn't create a buffer");
        exit(1);
    }

    /* Create a sub-buffer */
    /* Modified on 2/12/2014 to account for unaligned memory error */
    region.origin = 0 * sizeof(float);
    region.size = 20 * sizeof(float);
    sub_buffer = clCreateSubBuffer(main_buffer, CL_MEM_READ_ONLY |
        CL_MEM_COPY_HOST_PTR, CL_BUFFER_CREATE_TYPE_REGION, &region, &err);
    if (err < 0) {
        perror("Couldn't create a sub-buffer.");
        exit(1);
    }

    /* Obtain size information about the buffers */
    clGetMemObjectInfo(main_buffer, CL_MEM_SIZE,
        sizeof(main_buffer_size), &main_buffer_size, NULL);
    clGetMemObjectInfo(sub_buffer, CL_MEM_SIZE,
        sizeof(sub_buffer_size), &sub_buffer_size, NULL);
    printf("Main buffer size: %lu
", main_buffer_size);
    printf("Sub-buffer size:  %lu
", sub_buffer_size);

    /* Obtain the host pointers */
    clGetMemObjectInfo(main_buffer, CL_MEM_HOST_PTR, sizeof(main_buffer_mem),
        &main_buffer_mem, NULL);
    clGetMemObjectInfo(sub_buffer, CL_MEM_HOST_PTR, sizeof(sub_buffer_mem),
        &sub_buffer_mem, NULL);
    printf("Main buffer memory address: %p
", main_buffer_mem);
    printf("Sub-buffer memory address:  %p
", sub_buffer_mem);

    /* Print the address of the main data */
    printf("Main array address: %p
", main_data);

    /* Deallocate resources */
    clReleaseMemObject(main_buffer);
    clReleaseMemObject(sub_buffer);
    clReleaseContext(context);

    return 0;
}

推荐答案

因为您在创建main_buffer时已经为缓冲区分配了内存,所以在获取子缓冲区时不需要再次分配内存。您应该仅使用CL_MEM_READ_ONLY作为cl_mem_flags

sub_buffer = clCreateSubBuffer(main_buffer, CL_MEM_READ_ONLY,
                               CL_BUFFER_CREATE_TYPE_REGION, &region, &err);

这篇关于在OpenCL中创建子缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆