推力迭代器混合使用 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 结构文本函数{ text_函子(){} __主机\uuuuuu\uuuu设备\uuuuuu int运算符()(const char t)const{ 如果(t=='\n')返回0; 返回1; } }; void CountPosition1(常量字符*文本、整数*位置、整数文本大小) { 转换(文本,文本+文本大小,位置,文本函数()); } int main(){ 字符s[4]={'a','a','n','a'}; int r[4]={0}; int*k; Cudamaloc((void**)和k,sizeof(int)*4); 位置1(s、k、4); }

推力迭代器混合使用 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 结构文本函数{ text_函子(){} __主机\uuuuuu\uuuu设备\uuuuuu int运算符()(const char t)const{ 如果(t=='\n')返回0; 返回1; } }; void CountPosition1(常量字符*文本、整数*位置、整数文本大小) { 转换(文本,文本+文本大小,位置,文本函数()); } int main(){ 字符s[4]={'a','a','n','a'}; int r[4]={0}; int*k; Cudamaloc((void**)和k,sizeof(int)*4); 位置1(s、k、4); },cuda,thrust,Cuda,Thrust,在推力::转换中,我混合了主机迭代器s和设备迭代器k。这会导致分割错误。如果我在CountPosition1中将参数k更改为r,程序将正确。 推力函数中的所有迭代器应该来自同一个源(两个主机还是两个设备)?或者这段代码中有错误吗?是的,或者所有迭代器都应该来自主机容器,或者所有迭代器都应该来自设备容器 算法调度时。所有迭代器都应该与dispatch方法一致。但即使我将所有原始指针应用于“text”和“pos”,它仍然会遇到段错误。转换能否接受原始指针作为其参数?使用设备指针转换是否更好?而不是原

在推力::转换中,我混合了主机迭代器s和设备迭代器k。这会导致分割错误。如果我在
CountPosition1
中将参数k更改为r,程序将正确。
推力函数中的所有迭代器应该来自同一个源(两个主机还是两个设备)?或者这段代码中有错误吗?

是的,或者所有迭代器都应该来自主机容器,或者所有迭代器都应该来自设备容器


算法调度时。所有迭代器都应该与dispatch方法一致。

但即使我将所有原始指针应用于“text”和“pos”,它仍然会遇到段错误。转换能否接受原始指针作为其参数?使用设备指针转换是否更好?而不是原始设备指针。阅读您可能还应该阅读我在回答中链接的完整的《推力快速入门指南》。在/usr/local/cuda/bin/。//include/推力/detail/internal_functional.h:322 322推力::get(t)=f(推力::get(t))
#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);
}