user2380547
user2380547

Reputation: 1

compile error occured in thrust/type_traits.h

I just try to add trust::sort in my cuda code, but nvcc tell me that:

type_traits.h(322): error C2660: 'test' : function does not take 1 arguments
type_traits.h(322): error C2866:'thrust::detail::tt_detail::is_convertible_sfinae<From,To>::value' : a const static data member of a managed type must be initialized at the point of declaration

type_traits.h(355): error C2057: expected constant expressiontype_traits.h(363): error C2975: '__v' : invalid template argument for 'thrust::detail::integral_constant', expected compile-time constant expression
type_traits.h(363): error C2975: '__v' : invalid template argument for 'thrust::detail::integral_constant', expected compile-time constant expression

I have search for that but there seems no one get the same problem as me

The part of my code that about thrust:

#include <thrust\sort.h>

struct prepare_struct 
{
float xp;
float yp;
float zp;
float xs;
float ys;
float zs;
float sep;
int idSrc_idEve;
};

int compare_sort(prepare_struct &a, prepare_struct &b){ return a.sep > b.sep;}

void func(...){
...

prepare_struct* sPos_d;
checkCudaErrors( cudaMalloc((void**)&sPos_d, n*sizeof(prepare_struct) ) );

//a kernel that will fill sPos_d

thrust::sort(sPos_d, sPos_d + n, compare_sort);

...
}

If I remove thrust::sort(), it could be compiled without error

I've tried thrust::device_vector, but it will get the same error

And raw_pointer_cast() will get the same error message too

Is this a bug inside thrust or nvcc?

or something wrong in my code?

environment:

win7 x64 vs 2010 cuda 5.0 sm_20

the device_vector version:

#include <thrust/device_vector.h>
void func(...){
...

thrust::device_vector<prepare_struct> sPos_dv(n_src_sta);
prepare_struct* sPos_d = thrust::raw_pointer_cast(sPos_dv.data());

//a kernel that will fill sPos_d

thrust::sort(sPos_dv.begin(),sPos_dv.end(),compare_sort);

...
}

Upvotes: 0

Views: 884

Answers (1)

BenC
BenC

Reputation: 8976

When writing a post on Stack Overflow, make sure that you provide a Short, Self Contained, Correct (Compilable), Example (aka SSCCE). That would make life easier for other members who are trying to help you, and it would also help you find the actual error in your code. If your code is not compilable, provide an example that demonstrates the compilation issue.

As for your program, there must be things that you are not telling us. What you are trying to achieve is easily handled by Thrust without any error. Here is a modified (and completed) version of your program:

#include <thrust/sort.h>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>

struct prepare_struct
{
  float xp;
  float yp;
  float zp;
  float xs;
  float ys;
  float zs;
  float sep;
  int idSrc_idEve;
};

struct prepare_struct_compare {
  __host__ __device__
  bool operator() (const prepare_struct& a, const prepare_struct& b)
  {
    return a.sep < b.sep;
  }
};

void initialize(thrust::host_vector<prepare_struct>& v)
{
  for(size_t i = 0; i < v.size(); i++)
    v[i].sep = v.size() - i;
}

void print(const thrust::host_vector<prepare_struct>& v)
{
  for(size_t i = 0; i < v.size(); i++)
    std::cout << " " << v[i].sep;
  std::cout << "\n";
}

int main ()
{
  const int n = 10;

  // Initialize vector of prepare_struct on host
  thrust::host_vector<prepare_struct> vec_h(n);
  initialize(vec_h);
  std::cout << "Initial vector:" << std::endl;
  print(vec_h);

  // Copy vector to device
  thrust::device_vector<prepare_struct> vec_d = vec_h;

  // Sort on device
  thrust::sort (vec_d.begin(), vec_d.end(), prepare_struct_compare());

  // Copy result back to host
  thrust::host_vector<prepare_struct> res_h = vec_d;
  std::cout << "Final vector:" << std::endl;
  print(res_h);
}

Running this program gives us:

Initial vector:
 10 9 8 7 6 5 4 3 2 1
Final vector:
 1 2 3 4 5 6 7 8 9 10

Upvotes: 1

Related Questions