推迭代迭代器混合使用
问题描述:
#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,程序将是正确的。 推力函数中的所有迭代器是否应来自同一个源(主机或两个设备)?或者在这段代码中有什么错误?推迭代迭代器混合使用
答
是的,要么所有迭代器应该来自主机容器,要么所有迭代器都应该来自设备容器。
算法发送时,thrust will dispatch either the host path or the device path。所有迭代器应该与调度方法一致。
但即使我将所有原始指针应用于“text”和“pos”,它仍然遇到段错误。可以转换接受原始指针作为其参数?使用device_pointer_cast更好吗? –
不是原始**设备**指针。阅读[this](http://*.com/questions/25242790/simple-sorting-using-thrust-not-working)。你也许应该阅读我在答案中链接的整个推力快速入门指南。 –
消息0x0000000000403acd in thrust :: detail :: unary_transform_functor :: operator()>>(this = 0x7fffffffe6ef,t = ...) (t)= f(thrust :: get (t)); /推力/细节/ internal_functional.h:322 322推力:得到(t)= f ' –