Question

Pardon me if this is non-trivial. I have a small class, wrapping around cusp::csr_matrix<int,float,cusp::device_memory> like so:

class CuspMatrix 
: public cusp::csr_matrix<int,float,cusp::device_memory>
{
 ...
}

So far I'm not doing anything I would imagine would need to be compiled for the device. However I do plan on writing code in this particular class, as a member/function, that should be executed on the device, yet if I am understanding correctly, cusp already provides this functionality for me? E.g:

ValueType v_nrm2 = cusp::blas::nrm2(V);
cusp::transpose(M,M_t);

calculating a norm, or transposing, should ideally be compiled for device execution. What do I do in this case, if that is part of a class method?

Do I make the entire *.hpp header file of the class, a *.cu and add it for nvcc compilation? Do I mark only the actual code (and not the header declaration) in a *.cu and add it for nvcc compilation? Or is none of this necessary?

Was it helpful?

Solution

What you must keep in mind is that cusp is a template library built on top of thrust which is also a template library. Both cusp and thrust class methods contain a lot of device code, so if you inherit from them, you inherit a lot of device code as well.

This means that however you choose to structure the code, in "classic" CUDA compilation, the point of instantiation of your class and all the included cusp and thrust template code must be in the same translation unit and must be presented to nvcc in a form it will recognize as requiring device code compilation (ie. in a .cu file or with an appropriate compiler switch).

So this sequence of defining a derived class and instantiating it:

class CuspMatrix 
: public cusp::csr_matrix<int,float,cusp::device_memory>
{
    ...
}

...

{
    CuspMatrix A;
    ...
}

must be compiled with nvcc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top