i get bad results when i try to implement any functor. For example i tried a negate functor similar to thrust::negate<T>
Here is a sample code producing good results using build in negate functor:
int data[10] = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};
int out[10];
thrust::negate<int> op;
thrust::transform(data, data + 10, out, op);
out
variable becomes {5, 0, -2, 3, -2, -4, 0, 1, -2, -8}
, but when i implemented my own functor like
struct NegateFunctor{
__host__ __device__
int operator()(const int &val) const {
return -val;
}
};
and call it as thrust::transform(data, data + 10, out, NegateFunctor())
out
contains {-858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460, -858993460}
I'm using Visual Studio 2010, 5.0 CUDA on a 64 bit machine.
Thanks
If I compile and run your code (only modified to added printing of the result of the thrust::transform
call):
#include <thrust/transform.h>
#include <thrust/functional.h>
struct NegateFunctor{
__host__ __device__
int operator()(const int &val) const {
return -val;
}
};
int main(int argc, char** argv){
int data[10] = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};
int out[10];
//thrust::negate<int> op;
thrust::transform(data, data + 10, out, NegateFunctor());
for(int i=0; i<10; i++) {
std::cout << data[i] << " " << out[i] << std::endl;
}
return 0;
}
I get this (CUDA 5.0 on OS 10.6.7):
$ nvcc thrust_neg2.cu
$ ./a.out
-5 5
0 0
2 -2
-3 3
2 -2
4 -4
0 0
-1 1
2 -2
8 -8
Which appears to be correct. If you aren't seeing the same result, then either this is a specific issue with the toolchain you are using, or something else you haven't told us is causing the problem.
EDIT: And it appears from comments you are building with nvcc in debug mode, which is known not to work with Thrust. I would recommend only building code for release. If the problem persists, this should probably be a bug report to the Thrust developers, not a Stack Overflow question.