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,程序將是正確的。 推力函數中的所有迭代器是否應來自同一個源(主機或兩個設備)?或者在這段代碼中有什麼錯誤?推迭代迭代器混合使用
但即使我將所有原始指針應用於「text」和「pos」,它仍然遇到段錯誤。可以轉換接受原始指針作爲其參數?使用device_pointer_cast更好嗎? –
不是原始**設備**指針。閱讀[this](http://stackoverflow.com/questions/25242790/simple-sorting-using-thrust-not-working)。你也許應該閱讀我在答案中鏈接的整個推力快速入門指南。 –
消息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 ' –