OpenCL没有为String Matching程序提供正确的输出

时间:2016-10-27 17:32:26

标签: c++ visual-studio opencl intel-xdk

我试图在Visual Studio 2013社区中使用VC ++学习OpenCL。我安装了英特尔XDK的OpenCL,因为我的硬件是“i5 CPU”和“Intel HD Graphics 4000 GPU”16核。

我在“kernel.cl”中写了以下内容 这里'pCount'指的是Pattern Count(“babb”=> 4),'lim'指的是string length => 10

__kernel void vsearch( __global char* str, __global char* pat, __global int* res, const int pCount, const unsigned int lim)
{
    int tid = get_global_id(0);
    int flag = 1;
    if(tid < (lim - pCount)) {
        flag = 1;
        for(int i = 0; i < pCount; i++){
            if(str[tid + i] != pat[i])
                flag = 0;
        }
        if(flag == 1)
            res[0] = tid;
   }
}

我使用以下程序在Visual Studio VC ++中调用它

#define __CL_ENABLE_EXCEPTIONS

#include "cl.hpp"
#include "util.hpp"

#include <vector>
#include <iostream>
#include <conio.h>
#include <iomanip>

using namespace std;
using namespace cl;

#ifndef DEVICE
#define DEVICE CL_DEVICE_TYPE_DEFAULT
#endif

#include "err_code.h"

int main(void)
{
    vector<char> str(10), pat(4);
    vector<int> res(1);
    Buffer d_str, d_pat, d_res;

    string s = "aababbabcb";
    string p = "babb";

    for (int i = 0; i < 10; i++){
        str.push_back(s[i]);
    }
    for (int i = 0; i < 4; i++){
        pat.push_back(p[i]);
    }

    try
    {
        Context context(DEVICE);
        Program program(context, util::loadProgram("kernel.cl"), true);
        CommandQueue queue(context);
        make_kernel<Buffer, Buffer, Buffer, int, int> vsearch(program, "vsearch");

        d_str = Buffer(context, str.begin(), str.end(), true);
        d_pat = Buffer(context, pat.begin(), pat.end(), true);
        d_res = Buffer(context, CL_MEM_READ_WRITE, sizeof(int));

        vsearch(EnqueueArgs(queue, NDRange(str.size())), d_str, d_pat, d_res, 4, 10);

        copy(queue, d_res, res.begin(), res.end());

        cout << res[0];
    }
    catch (Error err) {
        cout << "Exception\n";
        cerr << "ERROR: " << err.what() << "(" << err_code(err.err()) << ")" << std::endl;
    }

    _getch();
    return 0;
}

控制台中的输出为“5”,但应为“2”

谢谢。

Screenshot of Visual Studio 2013

enter image description here

0 个答案:

没有答案