I am porting some physics simulation code from C++ to CUDA.
The fundamental algorithm can be understood as: applying an operator to each element of a vector. In pseudocode, a simulation might include the following kernel call:
apply(Operator o, Vector v){
...
}
For instance:
apply(add_three_operator, some_vector)
would add three to each element in the vector.
In my C++ code, I have an abstract base class Operator, with many different concrete implementations. The important method is class Operator{ virtual double operate(double x) =0; Operator compose(Operator lo, Operator ro); ... }
The implementation for AddOperator might look like 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 for scaling and composing concrete implementations of Operator. This abstraction allows me to simply compose "leaf" operators into more general transformations.
For instance:
apply(compose(add_three_operator, square_operator), some_vector);
would add three then square each element of the vector.
The problem is CUDA doesn't support virtual method calls in the kernel. My current thought is to use templates. Then kernel calls will look something like:
apply<Composition<AddOperator,SquareOperator>>
(compose(add_three_operator, square_operator), some_vector);
Any suggestions?