Euphoria Yang
Euphoria Yang

Reputation: 91

thrust iterator mix usage

#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);
}

In thrust::transform, I mix host iterator s and device iterator k. This results in segmentation fault. If I change argument k to r in CountPosition1 the program will be correct. Should all iterator in a thrust function be from same source(both host or both device)? Or is there something wrong in this code?

Upvotes: 0

Views: 256

Answers (1)

Robert Crovella
Robert Crovella

Reputation: 152003

Yes, either all iterators should be from host containers, or all iterators should be from device containers.

Upon algorithm dispatch, thrust will dispatch either the host path or the device path. All iterators should be consistent with the dispatch method.

Upvotes: 1

Related Questions