First Multi-threaded (MT) NVPL RAND ProgramΒΆ

Below is a similar example generating 10000 FP32 numbers with standard normal distribution using multiple threads with NVPL RAND MT library. The only difference of the code from the previous example is in creating generator handle.

/*
 * This program uses multi-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(nvplRandMTCreateGeneratorDefault(&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);
}