2017-04-26 135 views
0
#include <stdio.h> 
#include <stdlib.h> 
#include <cuda.h> 
#include <thrust/device_vector.h> 
#include <thrust/host_vector.h> 
#include <thrust/scan.h> 
#include <thrust/execution_policy.h> 
#include <iostream> 
#include <thrust/transform.h> 
struct text_functor { 
    text_functor() {} 
    __host__ __device__ int operator()(const char t) const { 
     if (t == '\n') return 0; 
     return 1; 
    } 
}; 

void CountPosition1(const char *text, int *pos, int text_size) 
{ 
    thrust::transform(text, text + text_size, pos, text_functor()); 
} 
int main() { 
    char s[4] = {'a', 'a', '\n', 'a'}; 
    int r[4] = {0}; 
    int *k; 
    cudaMalloc((void**) &k, sizeof(int) * 4); 
    CountPosition1(s, k, 4); 
} 

在thrust :: transform中,我混合了主迭代器和設備迭代器k。這導致分段錯誤。如果我在CountPosition1中將參數k更改爲r,程序將是正確的。 推力函數中的所有迭代器是否應來自同一個源(主機或兩個設備)?或者在這段代碼中有什麼錯誤?推迭代迭代器混合使用

回答

1

是的,要麼所有迭代器應該來自主機容器,要麼所有迭代器都應該來自設備容器。

算法發送時,thrust will dispatch either the host path or the device path。所有迭代器應該與調度方法一致。

+0

但即使我將所有原始指針應用於「text」和「pos」,它仍然遇到段錯誤。可以轉換接受原始指針作爲其參數?使用device_pointer_cast更好嗎? –

+0

不是原始**設備**指針。閱讀[this](http://stackoverflow.com/questions/25242790/simple-sorting-using-thrust-not-working)。你也許應該閱讀我在答案中鏈接的整個推力快速入門指南。 –

+0

消息0x0000000000403acd in thrust :: detail :: unary_transform_functor :: operator()>>(this = 0x7fffffffe6ef,t = ...) (t)= f(thrust :: get <0>(t)); /推力/細節/ internal_functional.h:322 322推力:得到<1>(t)= f ' –