First Single-threaded (ST) NVPL RAND ProgramΒΆ
Putting it together, here is a simple example generating 10000 FP32 numbers with standard normal distribution using a NVPL RAND generator with a single thread.
/*
* This program uses single-threaded NVPL RAND library to generate 10000 FP32 numbers with standard normal distribution
*/
#include <vector>
#include <iostream>
#include "nvpl_rand.h"
#define NVPL_RAND_CHECK(x) \
do { \
if ((x) != NVPL_RAND_STATUS_SUCCESS) { \
printf("Error at %s:%d\n", __FILE__, __LINE__); \
_Exit(EXIT_FAILURE); \
} \
} while (0)
int main(int argc, char* argv[]) {
nvplRandGenerator_t gen;
const size_t n = 10000;
std::vector<float> array(n);
/* Create pseudo-random number generator */
NVPL_RAND_CHECK(nvplRandCreateGenerator(&gen, NVPL_RAND_RNG_PSEUDO_PCG));
/* Set seed */
NVPL_RAND_CHECK(nvplRandSetPseudoRandomGeneratorSeed(gen, 1234ULL));
/* Set Offset */
NVPL_RAND_CHECK(nvplRandSetGeneratorOffset(gen, 10ULL));
/* Generate n 32-bit random numbers */
NVPL_RAND_CHECK(nvplRandGenerateNormal(gen, array.data(), n, 0, 1));
for (auto i = 0; i < 10; i++) {
std::cout << " i " << i << " " << array[i] << std::endl;
}
/* Cleanup */
nvplRandDestroyGenerator(gen);
}