OpenCL - 我的数组如何太大而不会导致堆栈溢出?

时间:2015-02-01 13:01:11

标签: c++ opencl gpu gpgpu

我是OpenCL的新手,我正在使用C ++包装器来编程。我有一张较旧的AMD显卡(Radeon HD 5770),这可能是问题的原因,但我想暂时解决这个问题。

我正在尝试“处理”一个“图像”,我正在伪造一个400 x 400像素^ 2作为一维整数数组。所以,我的缓冲区大小应该是4 * 400 * 400 - 大约640kb。我认为这根本不大。

我认为有些统计数据是相关的:

  • 每个工作组的最大工作项数:256
  • 每个工作组的最大工作项目维度:(256,256,256),其中x * y * z <= 256,但我认为。
  • 最大内存分配大小:536,870,912(看起来像1/2 GB)
  • 催化剂14.12
  • AMD SDK 3.0.0(测试版)
  • 使用Visual Studio Community 2013

一些代码:

#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <stdio.h>
#include <streambuf>
#include <string>

#include <CL/cl.hpp>

using namespace System;
using namespace std;
#define IMG_WIDTH 400
#define IMG_HEIGHT 400

int main(array<System::String ^> ^args)
{
    vector<cl::Platform> all_platforms;
    cl::Platform::get(&all_platforms);

    cl::Platform default_platform = all_platforms[0];

    vector<cl::Device> all_devices;
    default_platform.getDevices(CL_DEVICE_TYPE_ALL, &all_devices);
    cl::Device default_device = all_devices[0];     

    cl::Context context({ default_device });

    std::ifstream file("kernels.cl");
    std::string kcode(std::istreambuf_iterator<char>(file),
                      (std::istreambuf_iterator<char>()));

    cl::Program::Sources sources(1,
         std::make_pair(kcode.c_str(), kcode.length() + 1));

    cl::Program program(context, sources);

    if (program.build({ default_device }) != CL_SUCCESS){
        cout << "Error building " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(default_device) << endl;
    exit(1);
    }

    int h_C[IMG_WIDTH * IMG_HEIGHT]; // initialize the array.
    cl::Buffer d_C(context, CL_MEM_READ_WRITE, sizeof(int) * IMG_WIDTH * IMG_HEIGHT); // create the device memory for this array.

    cl::CommandQueue queue(context, default_device, CL_QUEUE_PROFILING_ENABLE);

    cl::Kernel kernel_to_run(program, "get_row");   
    kernel_to_run.setArg(0, d_C);
    kernel_to_run.setArg(1, IMG_WIDTH);
    kernel_to_run.setArg(2, IMG_HEIGHT);

    cl::Event evt;
    queue.enqueueNDRangeKernel(kernel_to_run, cl::NullRange, cl::NDRange(IMG_WIDTH, IMG_HEIGHT), cl::NDRange(10, 10), NULL, &evt);
    queue.finish();

    /* I think the problem is here. If I comment it out, the program
       will run fine, but I need the device information back to the
       host, though!
    */
    queue.enqueueReadBuffer(d_C, CL_TRUE, 0, sizeof(int) * IMG_WIDTH * IMG_HEIGHT, h_C);

    unsigned long elapsed = (unsigned long)(evt.getProfilingInfo<CL_PROFILING_COMMAND_END>() -
    evt.getProfilingInfo<CL_PROFILING_COMMAND_START>());
std::cout << " result: " << elapsed / (float)10e6 << " ms";

    queue.flush();
    queue.finish();
    delete &d_C;
}

内核,它只会存储每个“像素”所属的全局行:

#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
__kernel void get_row(__global int *out, int width, int height){

    int r = get_global_id(1);
    int c = get_global_id(0);

    if ((r >= height) || (c >= width))
        return; 

    int gIdx = r * width + c;

    out[gIdx] = r;

}

我做错了什么?对于400 x 400,程序给出了一个错误“进程由于堆栈溢出异常而终止”

  • 我的“图片”尺寸是否太大(仅400 x 400),总工作项目大小?
  • 我选择的工作组大小为100(10 x 10),因此,我认为我将拥有1600个400 x 400的工作组。我不认为工作组的数量有限制,甚至对于旧设备,还是在那里?
  • 也许我的主机代码没有按照正确的顺序排列。

对此有任何帮助表示赞赏。如果可能的话,我不想获得新的显卡。我不想将我的图像分割成更小的矩形,然后将它们分成工作组。

我在CUDA(在另一台机器中)中使用相当于上面的图像,图像大于400 x 400而没有任何问题。

1 个答案:

答案 0 :(得分:4)

您的变量h_C会消耗大量的堆栈内存。堆栈内存非常有限。

代替使用堆栈变量
int h_C[IMG_WIDTH * IMG_HEIGHT];

使用std::vector

等动态分配它
std::vector<int> h_C;
h_C.resize(IMG_WIDTH * IMG_HEIGHT);
...
queue.enqueueReadBuffer(d_C, CL_TRUE, 0, sizeof(int) * IMG_WIDTH * IMG_HEIGHT, h_C.data());
相关问题