I am new to Thrust. I just make below code works, but it is slow. I
know it operates memery too many times.
So I want help to merge these operations.
Thank you guys!
template <typename T>
struct is_LessThanZero : public thrust::unary_function<T,bool>
{
__host__ __device__
bool operator()(T x)
{
return (x < 0);
}
};
struct is_zero
{
__host__ __device__
bool operator()(const int x)
{
return (x == 0);
}
};
struct functor1 : public thrust::binary_function<float,float,float>
{
__host__ __device__
float operator()(const float& x, const float& y) const {
return (x - y)/x;
}
};
int main(void)
{
int m = 3; // number of rows
int n = 5; // number of columns
thrust::host_vector<unsigned int> _values(m*n);
_values[0]=1;_values[1]=5;_values[2]=4;_values[3]=5;_values[4]=1;
_values[5]=1;_values[6]=1;_values[7]=2;_values[8]=3;_values[9]=6;
_values[10]=3;_values[11]=3;_values[12]=3;_values[13]=3;_values[14]=3;
thrust::device_vector<unsigned int> _values_d = _values;
thrust::device_vector<unsigned int> _values_d2(m*n);
thrust::copy(_values_d.begin()+5,_values_d.begin() +10,_values_d2.begin());//I just need a part of data and dont wanna modify the source data.
size_t new_size = thrust::remove_if(_values_d2.begin(),_values_d2.end(),is_zero())- _values_d2.begin();//delete all 0 in the copied values.
_values_d2.resize(new_size);//resize to the real size.
thrust::device_vector<unsigned int> _values_d3(new_size);//another copy
thrust::remove_copy(_values_d2.end()-4,_values_d2.end(),_values_d3.begin(), 0);//only need some data starting from the end.
_values_d3.resize(_values_d2.end()-(_values_d2.end()-4));//resize to the real size
thrust::device_vector<float> _values_d4(_values_d3.size());//
thrust::transform(_values_d3.begin(),_values_d3.end(),_values_d3.begin() +1,_values_d4.begin(),functor1());//make a thansform
_values_d4.resize(_values_d4.size()-1);
int result = thrust::count_if(_values_d4.begin(), _values_d4.end(), is_LessThanZero<float>());
system("pause");
return 0;
}