第一单线程 (ST) NVPL RAND 程序#
综上所述,这是一个简单的示例,使用单线程 NVPL RAND 生成器生成具有标准正态分布的 10000 个 FP32 数字。
/*
* 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);
}