将STL向量复制到device_vector

时间:2014-03-20 17:02:25

标签: c++ vector stl cuda thrust

我正在尝试将.cpp文件中的矢量发送到.cu文件,但程序崩溃并出现以下错误:

表达式:矢量迭代器不兼容

这是我的.cpp文件:

int main()
{
    vector<int> v_path;
    kernel_wrapper(v_path, silence_indeces[i].first);
}

这是我的.cu文件:

void kernel_wrapper(vector<int>& path, int startIndex)
{
    thrust::device_vector<int> storePath(path.begin(), path.end());
}

我做错了什么?

编辑:

我发现问题是因为我尝试将OpenMP与CUDA一起使用。我没有认为这很重要,所以我没有发布,我的错误。这是我的整个.cpp文件代码:

#include <omp.h>
#include <stdio.h>
#include <vector>

using namespace std;

extern void kernel_wrapper(vector<int>& path, int startIndex);
vector<int> foo(int start, int finish);

vector<pair<int, int>> divideInput (int* input, int size)
{
    int first = 0;
    vector<pair<int, int>> chunks;

    for (int i = 1; i < size; i++)
    {
        if (input[i] == 0)
        {
            chunks.push_back(make_pair(first, i));
            first = i + 1;
        }
    }

    chunks.push_back(make_pair(first, size-1));

    return chunks;
}

int main()
{
    int input[] = {7,7,0,4,6,5,0,2,0,1,4,6,5,3,2,0,4,2,4,7,8,4,0,6,4,2,2,4,6,8,6,4,0,1,4,6,76};

    vector<pair<int, int>> silence_indeces = divideInput(input,sizeof(input)/sizeof(input[0]));
    vector<int> v_path;

    #pragma omp parallel for
    for (int i = 0; i < (int) silence_indeces.size(); i++)
    {
        int ID = omp_get_thread_num();
        printf("ID:%d i:%d\n", ID, i);
        v_path = foo(silence_indeces[i].first, silence_indeces[i].second);
        kernel_wrapper(v_path, silence_indeces[i].first);
    }

    for (vector<int>::iterator it = v_path.begin(); it != v_path.end(); ++it)
    {
        printf("%d, ", *it);
    }
    return 0;
}

vector<int> foo(int start, int finish)
{
    vector<int> bla(30, 0);
    return bla;
}

我的.cu文件:

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <vector>
#include <stdio.h>
#include <cuda.h>
#include <thrust\device_vector.h>
#include <thrust\copy.h>
using namespace std;

__global__ void stam(int* path)
{
    path[threadIdx.x] = threadIdx.x;
}

void kernel_wrapper(vector<int>& path, int startIndex)
{
    thrust::device_vector<int> storePath(path);
    int* dvPtr = thrust::raw_pointer_cast(storePath.data());
    stam<<<1, path.size()>>>(dvPtr);
    thrust::copy(storePath.begin(), storePath.end(), path.begin());
}

代码在循环上运行几次,然后显示我之前提到的错误。

我正在使用:

  • Windows 7
  • CUDA版本5.5
  • Visual Studio 2010(Visual C ++)

有什么想法吗?

0 个答案:

没有答案