Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

c++ - 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.

question from:https://stackoverflow.com/questions/65952028/making-a-prng-class-usable-both-on-the-cpu-and-gpu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...