Question Details

No question body available.

Tags

c++ cuda thrust

Answers (2)

Accepted Answer Available
Accepted Answer
March 26, 2026 Score: 4 Rep: 15,468 Quality: Expert Completeness: 80%

devicevector allocates its memory on the GPU. You cannot access objects in GPU memory directly.

Therefore [index] provides only a proxy object of type devicereference. Note the following paragraph from its documentation.

Some operations which are possible with regular objects are impossible with their corresponding devicereference objects due to the requirements of the C++ language. For example, because the member access operator cannot be overloaded, member variables and functions of a referent object cannot be directly accessed through its devicereference.

Instead you have to use basic assign operations to convert from and to host types.

using cf = thrust::complex;
using devicerefcf = thrust::devicereference;
thrust::devicevector data(1);
devicerefcf reference = data[0];
cf value = reference; // or directly value = data[0]. Copies to host
printf("data[0] = %f %fi\n", value.real(), value.imag());
value = cf(1.0f, 2.0f);
value *= 5.0f;
reference = value; // or directly data[0] = value. copy to device

Be aware that copying single values to and from the GPU is very expensive. It is much better to initialize a host vector with all data and then copy it over in a large batch.

March 26, 2026 Score: 2 Rep: 44,308 Quality: Low Completeness: 40%

Short error message: thrust::devicereference has no member "real". This is a proxy object, you cannot access members directly, you need type casts of the reference.

printf("data[0] = %f %fi\n",
  staticcast(data[0]).real(),
  static_cast(data[0]).imag());

I guess, data[0]->imag() may also work.