vendredi 29 janvier 2021

Making a PRNG class usable both on the CPU and GPU

I have a class (minimal example below) which is used to estimate certain probabilities, and uses random sampling extensively

#ifndef _MYCLASS_CUH_
#define _MYCLASS_CUH_

#include "curand_kernel.h"
#include <thrust/device_vector.h>
#include "tml.cuh"

class MyClass
{
private:
    
    // Number of samples to generate
    int n;

    curandState prng_state;
    
    // Normal distribution mean and covariance
    TML::Vector2f mu;
    TML::Matrix2f sigma;

    // Other variables below
    // ...
public:
    __host__ __device__ MyClass();

    // Other methods below
    // ...
};

#endif

where TML is a custom matrix library made for use on the GPU. The thing is I want this class to be usable both on the CPU and the GPU, but the curandState complicates things, as it cannot be used on the host side. Using e.g. the mersenne-twister std::mt19937 or other host implemented PRNGs on the other hand are not usable on the GPU.

I am looking for tips on various ways of making this class usable on both host/device side so I dont have to duplicate the class, like say MyClass_CPU and MyClass_GPU. How can I do this? I would also ideally want to use e.g. the Eigen library on the CPU, and my own TML library on the GPU, due to computational efficiency.




Aucun commentaire:

Enregistrer un commentaire