Hi all,
I have a question about the applicability of the thrust library. I am aware of thrust’s utility in managing memory between host and device using the device_vector and host_vector. Unfortunately the declaration of such vectors is host code and as such I am having difficulty applying this kind of memory management to C++ classes I have defined for host and device. Does anyone know of a good way to handle dynamic vectors within Cuda classes (possibly using the thrust framework)? I have included a simple programme to illustrate what I would be looking for (though it does not work for the aforementioned reasons). I have tried with just host constructors that create the host vector and copy it to the device but had no luck. Since the number of values will be dynamically changing it would be more convenient to do this with vectors than arrays. Thanks in advance.
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
class foo
{
public:
/*These functions illustrate my basic needs from the class.
Essentially I just need to create and manipulate the
class on both the host and the device*/
__host__ __device__
foo() { }
__host__ __device__
foo(int x) { m_x = x; }
__host__ __device__
int x() { return m_x; }
__host__ __device__
void set( int x ) { m_x = x; }
__host__ __device__
void setValue( unsigned int i, int value ) { values[i] = value; }
__host__ __device__
void getValue( unsigned int i ) { return values[i]; }
//Some method to resize the 'values' vector would be helpful
private:
int m_x;
int m_y;
//Declaration of this causes problems with constructor since it is host code
thrust::device_vector<int> values;
};
__global__ void
applyValues( foo *ptr, unsigned int numValues )
{
unsigned int index = __umul24(blockIdx.x,blockDim.x) + threadIdx.x;
if (index >= numValues) return;
foo temp = ptr[index];
//The setup here won't allow for query of values.size() but this is desirable if possible
for(unsigned int i = 0; i < temp.values.size(); i++){
temp.set(temp.x() * temp.getValue(i));
}
}
int main()
{
const int N = 10;
thrust::host_vector<foo> H(N);
for(unsigned int i = 0; i < N; i++){
H[i] = foo(i);
}
thrust::device_vector<foo> D = H;
foo *ptr = thrust::raw_pointer_cast(&D[0]);
applyValues<<< 1, N >>>(ptr, N);
return 0;
}