How to use polymorphism in CUDA -
i porting physics simulation code c++ cuda.
the fundamental algorithm can understood as: applying operator each element of vector. in pseudocode, simulation might include following kernel call:
apply(operator o, vector v){ ... }
for instance:
apply(add_three_operator, some_vector)
would add 3 each element in vector.
in c++ code, have abstract base class operator, many different concrete implementations. important method class operator{ virtual double operate(double x) =0; operator compose(operator lo, operator ro); ... }
the implementation addoperator might this:
class addoperator : public operator{ private: double to_add; public: addoperator(double to_add): to_add(to_add){} double operator(double x){ return x + to_add; } };
the operator class has methods scaling , composing concrete implementations of operator. abstraction allows me compose "leaf" operators more general transformations.
for instance:
apply(compose(add_three_operator, square_operator), some_vector);
would add 3 square each element of vector.
the problem cuda doesn't support virtual method calls in kernel. current thought use templates. kernel calls like:
apply<composition<addoperator,squareoperator>> (compose(add_three_operator, square_operator), some_vector);
any suggestions?
something perhaps...
template <class op1, class op2> class composition {...} template <class op1, class op2> composition<op1, op2> compose(op1& op1, op2& op2) {...} template<class c> void apply(c& c, vectype& vec){...}
Comments
Post a Comment