mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-26 14:48:17 +08:00
Apply consistent code formatting across the repo. Add clang-format and pre-commit hooks.
This commit is contained in:
@@ -53,16 +53,15 @@
|
||||
* ACM Trans. on Math. Software, 29(1):49-57, 2003
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <cuda_runtime.h> // CUDA Runtime Functions
|
||||
#include <helper_cuda.h> // helper functions for CUDA error checking and initialization
|
||||
#include <helper_functions.h> // helper functions
|
||||
|
||||
#include <stdexcept>
|
||||
#include <math.h>
|
||||
|
||||
#include "sobol.h"
|
||||
|
||||
#include <cuda_runtime.h> // CUDA Runtime Functions
|
||||
#include <helper_cuda.h> // helper functions for CUDA error checking and initialization
|
||||
#include <helper_functions.h> // helper functions
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "sobol_gold.h"
|
||||
#include "sobol_gpu.h"
|
||||
|
||||
@@ -70,236 +69,238 @@
|
||||
|
||||
const char *sSDKsample = "Sobol Quasi-Random Number Generator";
|
||||
|
||||
void printHelp(int argc, char *argv[]) {
|
||||
if (argc > 0) {
|
||||
std::cout << "\nUsage: " << argv[0] << " <options>\n\n";
|
||||
} else {
|
||||
std::cout << "\nUsage: <program name> <options>\n\n";
|
||||
}
|
||||
void printHelp(int argc, char *argv[])
|
||||
{
|
||||
if (argc > 0) {
|
||||
std::cout << "\nUsage: " << argv[0] << " <options>\n\n";
|
||||
}
|
||||
else {
|
||||
std::cout << "\nUsage: <program name> <options>\n\n";
|
||||
}
|
||||
|
||||
std::cout << "\t--vectors=M specify number of vectors (required)\n";
|
||||
std::cout << "\t The generator will output M vectors\n\n";
|
||||
std::cout << "\t--dimensions=N specify number of dimensions (required)\n";
|
||||
std::cout << "\t Each vector will consist of N components\n\n";
|
||||
std::cout << std::endl;
|
||||
std::cout << "\t--vectors=M specify number of vectors (required)\n";
|
||||
std::cout << "\t The generator will output M vectors\n\n";
|
||||
std::cout << "\t--dimensions=N specify number of dimensions (required)\n";
|
||||
std::cout << "\t Each vector will consist of N components\n\n";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
bool ok = true;
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
bool ok = true;
|
||||
|
||||
// We will generate n_vectors vectors of n_dimensions numbers
|
||||
int n_vectors = 100000;
|
||||
int n_dimensions = 100;
|
||||
// We will generate n_vectors vectors of n_dimensions numbers
|
||||
int n_vectors = 100000;
|
||||
int n_dimensions = 100;
|
||||
|
||||
printf("%s Starting...\n\n", sSDKsample);
|
||||
printf("%s Starting...\n\n", sSDKsample);
|
||||
|
||||
// Print help if requested
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "help")) {
|
||||
printHelp(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
// Print help if requested
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "help")) {
|
||||
printHelp(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "qatest")) {
|
||||
// For QA testing set a default number of vectors and dimensions
|
||||
n_vectors = 100000;
|
||||
n_dimensions = 100;
|
||||
} else {
|
||||
// Parse the command line to determine the required number of vectors
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "vectors")) {
|
||||
n_vectors = getCmdLineArgumentInt(argc, (const char **)argv, "vectors");
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "qatest")) {
|
||||
// For QA testing set a default number of vectors and dimensions
|
||||
n_vectors = 100000;
|
||||
n_dimensions = 100;
|
||||
}
|
||||
else {
|
||||
// Parse the command line to determine the required number of vectors
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "vectors")) {
|
||||
n_vectors = getCmdLineArgumentInt(argc, (const char **)argv, "vectors");
|
||||
|
||||
if (n_vectors < 1) {
|
||||
std::cerr << "Illegal argument: number of vectors must be positive "
|
||||
"(--vectors=N)"
|
||||
if (n_vectors < 1) {
|
||||
std::cerr << "Illegal argument: number of vectors must be positive "
|
||||
"(--vectors=N)"
|
||||
<< std::endl;
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "> number of vectors = " << n_vectors << std::endl;
|
||||
|
||||
// Parse the command line to determine the number of dimensions in each
|
||||
// vector
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "dimensions")) {
|
||||
n_dimensions = getCmdLineArgumentInt(argc, (const char **)argv, "dimensions");
|
||||
|
||||
if (n_dimensions < 1) {
|
||||
std::cerr << "Illegal argument: number of dimensions must be positive "
|
||||
"(--dimensions=N)"
|
||||
<< std::endl;
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "> number of dimensions = " << n_dimensions << std::endl;
|
||||
}
|
||||
|
||||
// If any of the command line checks failed, exit
|
||||
if (!ok) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
// Create a timer to measure performance
|
||||
StopWatchInterface *hTimer = NULL;
|
||||
double time;
|
||||
sdkCreateTimer(&hTimer);
|
||||
|
||||
// Allocate memory for the arrays
|
||||
std::cout << "Allocating CPU memory..." << std::endl;
|
||||
unsigned int *h_directions = 0;
|
||||
float *h_outputCPU = 0;
|
||||
float *h_outputGPU = 0;
|
||||
|
||||
try {
|
||||
h_directions = new unsigned int[n_dimensions * n_directions];
|
||||
h_outputCPU = new float[n_vectors * n_dimensions];
|
||||
h_outputGPU = new float[n_vectors * n_dimensions];
|
||||
}
|
||||
catch (std::exception e) {
|
||||
std::cerr << "Caught exception: " << e.what() << std::endl;
|
||||
std::cerr << "Unable to allocate CPU memory (try running with fewer "
|
||||
"vectors/dimensions)"
|
||||
<< std::endl;
|
||||
ok = false;
|
||||
}
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
std::cout << "> number of vectors = " << n_vectors << std::endl;
|
||||
std::cout << "Allocating GPU memory..." << std::endl;
|
||||
unsigned int *d_directions;
|
||||
float *d_output;
|
||||
|
||||
// Parse the command line to determine the number of dimensions in each
|
||||
// vector
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "dimensions")) {
|
||||
n_dimensions =
|
||||
getCmdLineArgumentInt(argc, (const char **)argv, "dimensions");
|
||||
try {
|
||||
cudaError_t cudaResult;
|
||||
cudaResult = cudaMalloc((void **)&d_directions, n_dimensions * n_directions * sizeof(unsigned int));
|
||||
|
||||
if (n_dimensions < 1) {
|
||||
std::cerr << "Illegal argument: number of dimensions must be positive "
|
||||
"(--dimensions=N)"
|
||||
if (cudaResult != cudaSuccess) {
|
||||
throw std::runtime_error(cudaGetErrorString(cudaResult));
|
||||
}
|
||||
|
||||
cudaResult = cudaMalloc((void **)&d_output, n_vectors * n_dimensions * sizeof(float));
|
||||
|
||||
if (cudaResult != cudaSuccess) {
|
||||
throw std::runtime_error(cudaGetErrorString(cudaResult));
|
||||
}
|
||||
}
|
||||
catch (std::runtime_error e) {
|
||||
std::cerr << "Caught exception: " << e.what() << std::endl;
|
||||
std::cerr << "Unable to allocate GPU memory (try running with fewer "
|
||||
"vectors/dimensions)"
|
||||
<< std::endl;
|
||||
ok = false;
|
||||
}
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
std::cout << "> number of dimensions = " << n_dimensions << std::endl;
|
||||
}
|
||||
// Initialize the direction numbers (done on the host)
|
||||
std::cout << "Initializing direction numbers..." << std::endl;
|
||||
initSobolDirectionVectors(n_dimensions, h_directions);
|
||||
|
||||
// If any of the command line checks failed, exit
|
||||
if (!ok) {
|
||||
return -1;
|
||||
}
|
||||
// Copy the direction numbers to the device
|
||||
std::cout << "Copying direction numbers to device..." << std::endl;
|
||||
checkCudaErrors(cudaMemcpy(
|
||||
d_directions, h_directions, n_dimensions * n_directions * sizeof(unsigned int), cudaMemcpyHostToDevice));
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
|
||||
// Use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
findCudaDevice(argc, (const char **)argv);
|
||||
// Execute the QRNG on the device
|
||||
std::cout << "Executing QRNG on GPU..." << std::endl;
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
sobolGPU(n_vectors, n_dimensions, d_directions, d_output);
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkStopTimer(&hTimer);
|
||||
time = sdkGetTimerValue(&hTimer);
|
||||
|
||||
// Create a timer to measure performance
|
||||
StopWatchInterface *hTimer = NULL;
|
||||
double time;
|
||||
sdkCreateTimer(&hTimer);
|
||||
|
||||
// Allocate memory for the arrays
|
||||
std::cout << "Allocating CPU memory..." << std::endl;
|
||||
unsigned int *h_directions = 0;
|
||||
float *h_outputCPU = 0;
|
||||
float *h_outputGPU = 0;
|
||||
|
||||
try {
|
||||
h_directions = new unsigned int[n_dimensions * n_directions];
|
||||
h_outputCPU = new float[n_vectors * n_dimensions];
|
||||
h_outputGPU = new float[n_vectors * n_dimensions];
|
||||
} catch (std::exception e) {
|
||||
std::cerr << "Caught exception: " << e.what() << std::endl;
|
||||
std::cerr << "Unable to allocate CPU memory (try running with fewer "
|
||||
"vectors/dimensions)"
|
||||
<< std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
std::cout << "Allocating GPU memory..." << std::endl;
|
||||
unsigned int *d_directions;
|
||||
float *d_output;
|
||||
|
||||
try {
|
||||
cudaError_t cudaResult;
|
||||
cudaResult = cudaMalloc((void **)&d_directions,
|
||||
n_dimensions * n_directions * sizeof(unsigned int));
|
||||
|
||||
if (cudaResult != cudaSuccess) {
|
||||
throw std::runtime_error(cudaGetErrorString(cudaResult));
|
||||
if (time < 1e-6) {
|
||||
std::cout << "Gsamples/s: problem size too small to measure, try "
|
||||
"increasing number of vectors or dimensions"
|
||||
<< std::endl;
|
||||
}
|
||||
else {
|
||||
std::cout << "Gsamples/s: " << (double)n_vectors * (double)n_dimensions * 1E-9 / (time * 1E-3) << std::endl;
|
||||
}
|
||||
|
||||
cudaResult = cudaMalloc((void **)&d_output,
|
||||
n_vectors * n_dimensions * sizeof(float));
|
||||
std::cout << "Reading results from GPU..." << std::endl;
|
||||
checkCudaErrors(
|
||||
cudaMemcpy(h_outputGPU, d_output, n_vectors * n_dimensions * sizeof(float), cudaMemcpyDeviceToHost));
|
||||
|
||||
if (cudaResult != cudaSuccess) {
|
||||
throw std::runtime_error(cudaGetErrorString(cudaResult));
|
||||
std::cout << std::endl;
|
||||
// Execute the QRNG on the host
|
||||
std::cout << "Executing QRNG on CPU..." << std::endl;
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
sobolCPU(n_vectors, n_dimensions, h_directions, h_outputCPU);
|
||||
sdkStopTimer(&hTimer);
|
||||
time = sdkGetTimerValue(&hTimer);
|
||||
|
||||
if (time < 1e-6) {
|
||||
std::cout << "Gsamples/s: problem size too small to measure, try "
|
||||
"increasing number of vectors or dimensions"
|
||||
<< std::endl;
|
||||
}
|
||||
} catch (std::runtime_error e) {
|
||||
std::cerr << "Caught exception: " << e.what() << std::endl;
|
||||
std::cerr << "Unable to allocate GPU memory (try running with fewer "
|
||||
"vectors/dimensions)"
|
||||
<< std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Initialize the direction numbers (done on the host)
|
||||
std::cout << "Initializing direction numbers..." << std::endl;
|
||||
initSobolDirectionVectors(n_dimensions, h_directions);
|
||||
|
||||
// Copy the direction numbers to the device
|
||||
std::cout << "Copying direction numbers to device..." << std::endl;
|
||||
checkCudaErrors(cudaMemcpy(d_directions, h_directions,
|
||||
n_dimensions * n_directions * sizeof(unsigned int),
|
||||
cudaMemcpyHostToDevice));
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
|
||||
// Execute the QRNG on the device
|
||||
std::cout << "Executing QRNG on GPU..." << std::endl;
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
sobolGPU(n_vectors, n_dimensions, d_directions, d_output);
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkStopTimer(&hTimer);
|
||||
time = sdkGetTimerValue(&hTimer);
|
||||
|
||||
if (time < 1e-6) {
|
||||
std::cout << "Gsamples/s: problem size too small to measure, try "
|
||||
"increasing number of vectors or dimensions"
|
||||
<< std::endl;
|
||||
} else {
|
||||
std::cout << "Gsamples/s: "
|
||||
<< (double)n_vectors * (double)n_dimensions * 1E-9 / (time * 1E-3)
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Reading results from GPU..." << std::endl;
|
||||
checkCudaErrors(cudaMemcpy(h_outputGPU, d_output,
|
||||
n_vectors * n_dimensions * sizeof(float),
|
||||
cudaMemcpyDeviceToHost));
|
||||
|
||||
std::cout << std::endl;
|
||||
// Execute the QRNG on the host
|
||||
std::cout << "Executing QRNG on CPU..." << std::endl;
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
sobolCPU(n_vectors, n_dimensions, h_directions, h_outputCPU);
|
||||
sdkStopTimer(&hTimer);
|
||||
time = sdkGetTimerValue(&hTimer);
|
||||
|
||||
if (time < 1e-6) {
|
||||
std::cout << "Gsamples/s: problem size too small to measure, try "
|
||||
"increasing number of vectors or dimensions"
|
||||
<< std::endl;
|
||||
} else {
|
||||
std::cout << "Gsamples/s: "
|
||||
<< (double)n_vectors * (double)n_dimensions * 1E-9 / (time * 1E-3)
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
// Check the results
|
||||
std::cout << "Checking results..." << std::endl;
|
||||
float l1norm_diff = 0.0F;
|
||||
float l1norm_ref = 0.0F;
|
||||
float l1error;
|
||||
|
||||
// Special case if n_vectors is 1, when the vector should be exactly 0
|
||||
if (n_vectors == 1) {
|
||||
for (int d = 0, v = 0; d < n_dimensions; d++) {
|
||||
float ref = h_outputCPU[d * n_vectors + v];
|
||||
l1norm_diff += fabs(h_outputGPU[d * n_vectors + v] - ref);
|
||||
l1norm_ref += fabs(ref);
|
||||
else {
|
||||
std::cout << "Gsamples/s: " << (double)n_vectors * (double)n_dimensions * 1E-9 / (time * 1E-3) << std::endl;
|
||||
}
|
||||
|
||||
// Output the L1-Error
|
||||
l1error = l1norm_diff;
|
||||
// Check the results
|
||||
std::cout << "Checking results..." << std::endl;
|
||||
float l1norm_diff = 0.0F;
|
||||
float l1norm_ref = 0.0F;
|
||||
float l1error;
|
||||
|
||||
if (l1norm_ref != 0) {
|
||||
std::cerr << "Error: L1-Norm of the reference is not zero (for single "
|
||||
"vector), golden generator appears broken\n";
|
||||
} else {
|
||||
std::cout << "L1-Error: " << l1error << std::endl;
|
||||
// Special case if n_vectors is 1, when the vector should be exactly 0
|
||||
if (n_vectors == 1) {
|
||||
for (int d = 0, v = 0; d < n_dimensions; d++) {
|
||||
float ref = h_outputCPU[d * n_vectors + v];
|
||||
l1norm_diff += fabs(h_outputGPU[d * n_vectors + v] - ref);
|
||||
l1norm_ref += fabs(ref);
|
||||
}
|
||||
|
||||
// Output the L1-Error
|
||||
l1error = l1norm_diff;
|
||||
|
||||
if (l1norm_ref != 0) {
|
||||
std::cerr << "Error: L1-Norm of the reference is not zero (for single "
|
||||
"vector), golden generator appears broken\n";
|
||||
}
|
||||
else {
|
||||
std::cout << "L1-Error: " << l1error << std::endl;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int d = 0; d < n_dimensions; d++) {
|
||||
for (int v = 0; v < n_vectors; v++) {
|
||||
float ref = h_outputCPU[d * n_vectors + v];
|
||||
l1norm_diff += fabs(h_outputGPU[d * n_vectors + v] - ref);
|
||||
l1norm_ref += fabs(ref);
|
||||
}
|
||||
else {
|
||||
for (int d = 0; d < n_dimensions; d++) {
|
||||
for (int v = 0; v < n_vectors; v++) {
|
||||
float ref = h_outputCPU[d * n_vectors + v];
|
||||
l1norm_diff += fabs(h_outputGPU[d * n_vectors + v] - ref);
|
||||
l1norm_ref += fabs(ref);
|
||||
}
|
||||
}
|
||||
|
||||
// Output the L1-Error
|
||||
l1error = l1norm_diff / l1norm_ref;
|
||||
|
||||
if (l1norm_ref == 0) {
|
||||
std::cerr << "Error: L1-Norm of the reference is zero, golden generator "
|
||||
"appears broken\n";
|
||||
}
|
||||
else {
|
||||
std::cout << "L1-Error: " << l1error << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Output the L1-Error
|
||||
l1error = l1norm_diff / l1norm_ref;
|
||||
// Cleanup and terminate
|
||||
std::cout << "Shutting down..." << std::endl;
|
||||
sdkDeleteTimer(&hTimer);
|
||||
delete h_directions;
|
||||
delete h_outputCPU;
|
||||
delete h_outputGPU;
|
||||
checkCudaErrors(cudaFree(d_directions));
|
||||
checkCudaErrors(cudaFree(d_output));
|
||||
|
||||
if (l1norm_ref == 0) {
|
||||
std::cerr << "Error: L1-Norm of the reference is zero, golden generator "
|
||||
"appears broken\n";
|
||||
} else {
|
||||
std::cout << "L1-Error: " << l1error << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup and terminate
|
||||
std::cout << "Shutting down..." << std::endl;
|
||||
sdkDeleteTimer(&hTimer);
|
||||
delete h_directions;
|
||||
delete h_outputCPU;
|
||||
delete h_outputGPU;
|
||||
checkCudaErrors(cudaFree(d_directions));
|
||||
checkCudaErrors(cudaFree(d_output));
|
||||
|
||||
// Check pass/fail using L1 error
|
||||
exit(l1error < L1ERROR_TOLERANCE ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
// Check pass/fail using L1 error
|
||||
exit(l1error < L1ERROR_TOLERANCE ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -53,13 +53,14 @@
|
||||
* ACM Trans. on Math. Software, 29(1):49-57, 2003
|
||||
*/
|
||||
|
||||
#include "sobol_gold.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "sobol.h"
|
||||
#include "sobol_gold.h"
|
||||
#include "sobol_primitives.h"
|
||||
|
||||
#define k_2powneg32 2.3283064E-10F
|
||||
@@ -68,106 +69,110 @@
|
||||
// fairly simple implementation.
|
||||
// WIN32 is defined on 32 and 64 bit Windows
|
||||
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
|
||||
int ffs(const unsigned int &i) {
|
||||
unsigned int v = i;
|
||||
unsigned int count;
|
||||
int ffs(const unsigned int &i)
|
||||
{
|
||||
unsigned int v = i;
|
||||
unsigned int count;
|
||||
|
||||
if (!v) {
|
||||
count = 0;
|
||||
} else {
|
||||
count = 2;
|
||||
if (!v) {
|
||||
count = 0;
|
||||
}
|
||||
else {
|
||||
count = 2;
|
||||
|
||||
if ((v & 0xffff) == 0) {
|
||||
v >>= 16;
|
||||
count += 16;
|
||||
if ((v & 0xffff) == 0) {
|
||||
v >>= 16;
|
||||
count += 16;
|
||||
}
|
||||
|
||||
if ((v & 0xff) == 0) {
|
||||
v >>= 8;
|
||||
count += 8;
|
||||
}
|
||||
|
||||
if ((v & 0xf) == 0) {
|
||||
v >>= 4;
|
||||
count += 4;
|
||||
}
|
||||
|
||||
if ((v & 0x3) == 0) {
|
||||
v >>= 2;
|
||||
count += 2;
|
||||
}
|
||||
|
||||
count -= v & 0x1;
|
||||
}
|
||||
|
||||
if ((v & 0xff) == 0) {
|
||||
v >>= 8;
|
||||
count += 8;
|
||||
}
|
||||
|
||||
if ((v & 0xf) == 0) {
|
||||
v >>= 4;
|
||||
count += 4;
|
||||
}
|
||||
|
||||
if ((v & 0x3) == 0) {
|
||||
v >>= 2;
|
||||
count += 2;
|
||||
}
|
||||
|
||||
count -= v & 0x1;
|
||||
}
|
||||
|
||||
return count;
|
||||
return count;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Create the direction numbers, based on the primitive polynomials.
|
||||
void initSobolDirectionVectors(int n_dimensions, unsigned int *directions) {
|
||||
unsigned int *v = directions;
|
||||
void initSobolDirectionVectors(int n_dimensions, unsigned int *directions)
|
||||
{
|
||||
unsigned int *v = directions;
|
||||
|
||||
for (int dim = 0; dim < n_dimensions; dim++) {
|
||||
// First dimension is a special case
|
||||
if (dim == 0) {
|
||||
for (int i = 0; i < n_directions; i++) {
|
||||
// All m's are 1
|
||||
v[i] = 1 << (31 - i);
|
||||
}
|
||||
} else {
|
||||
int d = sobol_primitives[dim].degree;
|
||||
|
||||
// The first direction numbers (up to the degree of the polynomial)
|
||||
// are simply v[i] = m[i] / 2^i (stored in Q0.32 format)
|
||||
for (int i = 0; i < d; i++) {
|
||||
v[i] = sobol_primitives[dim].m[i] << (31 - i);
|
||||
}
|
||||
|
||||
// The remaining direction numbers are computed as described in
|
||||
// the Bratley and Fox paper.
|
||||
// v[i] = a[1]v[i-1] ^ a[2]v[i-2] ^ ... ^ a[v-1]v[i-d+1] ^ v[i-d] ^
|
||||
// v[i-d]/2^d
|
||||
for (int i = d; i < n_directions; i++) {
|
||||
// First do the v[i-d] ^ v[i-d]/2^d part
|
||||
v[i] = v[i - d] ^ (v[i - d] >> d);
|
||||
|
||||
// Now do the a[1]v[i-1] ^ a[2]v[i-2] ^ ... part
|
||||
// Note that the coefficients a[] are zero or one and for compactness in
|
||||
// the input tables they are stored as bits of a single integer. To
|
||||
// extract the relevant bit we use right shift and mask with 1.
|
||||
// For example, for a 10 degree polynomial there are ten useful bits in
|
||||
// a, so to get a[2] we need to right shift 7 times (to get the 8th bit
|
||||
// into the LSB) and then mask with 1.
|
||||
for (int j = 1; j < d; j++) {
|
||||
v[i] ^= (((sobol_primitives[dim].a >> (d - 1 - j)) & 1) * v[i - j]);
|
||||
for (int dim = 0; dim < n_dimensions; dim++) {
|
||||
// First dimension is a special case
|
||||
if (dim == 0) {
|
||||
for (int i = 0; i < n_directions; i++) {
|
||||
// All m's are 1
|
||||
v[i] = 1 << (31 - i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
int d = sobol_primitives[dim].degree;
|
||||
|
||||
v += n_directions;
|
||||
}
|
||||
// The first direction numbers (up to the degree of the polynomial)
|
||||
// are simply v[i] = m[i] / 2^i (stored in Q0.32 format)
|
||||
for (int i = 0; i < d; i++) {
|
||||
v[i] = sobol_primitives[dim].m[i] << (31 - i);
|
||||
}
|
||||
|
||||
// The remaining direction numbers are computed as described in
|
||||
// the Bratley and Fox paper.
|
||||
// v[i] = a[1]v[i-1] ^ a[2]v[i-2] ^ ... ^ a[v-1]v[i-d+1] ^ v[i-d] ^
|
||||
// v[i-d]/2^d
|
||||
for (int i = d; i < n_directions; i++) {
|
||||
// First do the v[i-d] ^ v[i-d]/2^d part
|
||||
v[i] = v[i - d] ^ (v[i - d] >> d);
|
||||
|
||||
// Now do the a[1]v[i-1] ^ a[2]v[i-2] ^ ... part
|
||||
// Note that the coefficients a[] are zero or one and for compactness in
|
||||
// the input tables they are stored as bits of a single integer. To
|
||||
// extract the relevant bit we use right shift and mask with 1.
|
||||
// For example, for a 10 degree polynomial there are ten useful bits in
|
||||
// a, so to get a[2] we need to right shift 7 times (to get the 8th bit
|
||||
// into the LSB) and then mask with 1.
|
||||
for (int j = 1; j < d; j++) {
|
||||
v[i] ^= (((sobol_primitives[dim].a >> (d - 1 - j)) & 1) * v[i - j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
v += n_directions;
|
||||
}
|
||||
}
|
||||
|
||||
// Reference model for generating Sobol numbers on the host
|
||||
void sobolCPU(int n_vectors, int n_dimensions, unsigned int *directions,
|
||||
float *output) {
|
||||
unsigned int *v = directions;
|
||||
void sobolCPU(int n_vectors, int n_dimensions, unsigned int *directions, float *output)
|
||||
{
|
||||
unsigned int *v = directions;
|
||||
|
||||
for (int d = 0; d < n_dimensions; d++) {
|
||||
unsigned int X = 0;
|
||||
// x[0] is zero (in all dimensions)
|
||||
output[n_vectors * d] = 0.0;
|
||||
for (int d = 0; d < n_dimensions; d++) {
|
||||
unsigned int X = 0;
|
||||
// x[0] is zero (in all dimensions)
|
||||
output[n_vectors * d] = 0.0;
|
||||
|
||||
for (int i = 1; i < n_vectors; i++) {
|
||||
// x[i] = x[i-1] ^ v[c]
|
||||
// where c is the index of the rightmost zero bit in i
|
||||
// minus 1 (since C arrays count from zero)
|
||||
// In the Bratley and Fox paper this is equation (**)
|
||||
X ^= v[ffs(~(i - 1)) - 1];
|
||||
output[i + n_vectors * d] = (float)X * k_2powneg32;
|
||||
for (int i = 1; i < n_vectors; i++) {
|
||||
// x[i] = x[i-1] ^ v[c]
|
||||
// where c is the index of the rightmost zero bit in i
|
||||
// minus 1 (since C arrays count from zero)
|
||||
// In the Bratley and Fox paper this is equation (**)
|
||||
X ^= v[ffs(~(i - 1)) - 1];
|
||||
output[i + n_vectors * d] = (float)X * k_2powneg32;
|
||||
}
|
||||
|
||||
v += n_directions;
|
||||
}
|
||||
|
||||
v += n_directions;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,6 @@
|
||||
#define SOBOL_GOLD_H
|
||||
|
||||
void initSobolDirectionVectors(int n_dimensions, unsigned int *directions);
|
||||
void sobolCPU(int n_vectors, int n_dimensions, unsigned int *directions,
|
||||
float *output);
|
||||
void sobolCPU(int n_vectors, int n_dimensions, unsigned int *directions, float *output);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -54,154 +54,155 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cooperative_groups.h>
|
||||
|
||||
#include "sobol.h"
|
||||
#include "sobol_gpu.h"
|
||||
#include <cooperative_groups.h>
|
||||
|
||||
namespace cg = cooperative_groups;
|
||||
#include <helper_cuda.h>
|
||||
|
||||
#define k_2powneg32 2.3283064E-10F
|
||||
|
||||
__global__ void sobolGPU_kernel(unsigned n_vectors, unsigned n_dimensions,
|
||||
unsigned *d_directions, float *d_output) {
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
__shared__ unsigned int v[n_directions];
|
||||
__global__ void sobolGPU_kernel(unsigned n_vectors, unsigned n_dimensions, unsigned *d_directions, float *d_output)
|
||||
{
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
__shared__ unsigned int v[n_directions];
|
||||
|
||||
// Offset into the correct dimension as specified by the
|
||||
// block y coordinate
|
||||
d_directions = d_directions + n_directions * blockIdx.y;
|
||||
d_output = d_output + n_vectors * blockIdx.y;
|
||||
// Offset into the correct dimension as specified by the
|
||||
// block y coordinate
|
||||
d_directions = d_directions + n_directions * blockIdx.y;
|
||||
d_output = d_output + n_vectors * blockIdx.y;
|
||||
|
||||
// Copy the direction numbers for this dimension into shared
|
||||
// memory - there are only 32 direction numbers so only the
|
||||
// first 32 (n_directions) threads need participate.
|
||||
if (threadIdx.x < n_directions) {
|
||||
v[threadIdx.x] = d_directions[threadIdx.x];
|
||||
}
|
||||
// Copy the direction numbers for this dimension into shared
|
||||
// memory - there are only 32 direction numbers so only the
|
||||
// first 32 (n_directions) threads need participate.
|
||||
if (threadIdx.x < n_directions) {
|
||||
v[threadIdx.x] = d_directions[threadIdx.x];
|
||||
}
|
||||
|
||||
cg::sync(cta);
|
||||
cg::sync(cta);
|
||||
|
||||
// Set initial index (i.e. which vector this thread is
|
||||
// computing first) and stride (i.e. step to the next vector
|
||||
// for this thread)
|
||||
int i0 = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
int stride = gridDim.x * blockDim.x;
|
||||
// Set initial index (i.e. which vector this thread is
|
||||
// computing first) and stride (i.e. step to the next vector
|
||||
// for this thread)
|
||||
int i0 = threadIdx.x + blockIdx.x * blockDim.x;
|
||||
int stride = gridDim.x * blockDim.x;
|
||||
|
||||
// Get the gray code of the index
|
||||
// c.f. Numerical Recipes in C, chapter 20
|
||||
// http://www.nrbook.com/a/bookcpdf/c20-2.pdf
|
||||
unsigned int g = i0 ^ (i0 >> 1);
|
||||
// Get the gray code of the index
|
||||
// c.f. Numerical Recipes in C, chapter 20
|
||||
// http://www.nrbook.com/a/bookcpdf/c20-2.pdf
|
||||
unsigned int g = i0 ^ (i0 >> 1);
|
||||
|
||||
// Initialisation for first point x[i0]
|
||||
// In the Bratley and Fox paper this is equation (*), where
|
||||
// we are computing the value for x[n] without knowing the
|
||||
// value of x[n-1].
|
||||
unsigned int X = 0;
|
||||
unsigned int mask;
|
||||
// Initialisation for first point x[i0]
|
||||
// In the Bratley and Fox paper this is equation (*), where
|
||||
// we are computing the value for x[n] without knowing the
|
||||
// value of x[n-1].
|
||||
unsigned int X = 0;
|
||||
unsigned int mask;
|
||||
|
||||
for (unsigned int k = 0; k < __ffs(stride) - 1; k++) {
|
||||
// We want X ^= g_k * v[k], where g_k is one or zero.
|
||||
// We do this by setting a mask with all bits equal to
|
||||
// g_k. In reality we keep shifting g so that g_k is the
|
||||
// LSB of g. This way we avoid multiplication.
|
||||
mask = -(g & 1);
|
||||
X ^= mask & v[k];
|
||||
g = g >> 1;
|
||||
}
|
||||
for (unsigned int k = 0; k < __ffs(stride) - 1; k++) {
|
||||
// We want X ^= g_k * v[k], where g_k is one or zero.
|
||||
// We do this by setting a mask with all bits equal to
|
||||
// g_k. In reality we keep shifting g so that g_k is the
|
||||
// LSB of g. This way we avoid multiplication.
|
||||
mask = -(g & 1);
|
||||
X ^= mask & v[k];
|
||||
g = g >> 1;
|
||||
}
|
||||
|
||||
if (i0 < n_vectors) {
|
||||
d_output[i0] = (float)X * k_2powneg32;
|
||||
}
|
||||
if (i0 < n_vectors) {
|
||||
d_output[i0] = (float)X * k_2powneg32;
|
||||
}
|
||||
|
||||
// Now do rest of points, using the stride
|
||||
// Here we want to generate x[i] from x[i-stride] where we
|
||||
// don't have any of the x in between, therefore we have to
|
||||
// revisit the equation (**), this is easiest with an example
|
||||
// so assume stride is 16.
|
||||
// From x[n] to x[n+16] there will be:
|
||||
// 8 changes in the first bit
|
||||
// 4 changes in the second bit
|
||||
// 2 changes in the third bit
|
||||
// 1 change in the fourth
|
||||
// 1 change in one of the remaining bits
|
||||
//
|
||||
// What this means is that in the equation:
|
||||
// x[n+1] = x[n] ^ v[p]
|
||||
// x[n+2] = x[n+1] ^ v[q] = x[n] ^ v[p] ^ v[q]
|
||||
// ...
|
||||
// We will apply xor with v[1] eight times, v[2] four times,
|
||||
// v[3] twice, v[4] once and one other direction number once.
|
||||
// Since two xors cancel out, we can skip even applications
|
||||
// and just apply xor with v[4] (i.e. log2(16)) and with
|
||||
// the current applicable direction number.
|
||||
// Note that all these indices count from 1, so we need to
|
||||
// subtract 1 from them all to account for C arrays counting
|
||||
// from zero.
|
||||
unsigned int v_log2stridem1 = v[__ffs(stride) - 2];
|
||||
unsigned int v_stridemask = stride - 1;
|
||||
// Now do rest of points, using the stride
|
||||
// Here we want to generate x[i] from x[i-stride] where we
|
||||
// don't have any of the x in between, therefore we have to
|
||||
// revisit the equation (**), this is easiest with an example
|
||||
// so assume stride is 16.
|
||||
// From x[n] to x[n+16] there will be:
|
||||
// 8 changes in the first bit
|
||||
// 4 changes in the second bit
|
||||
// 2 changes in the third bit
|
||||
// 1 change in the fourth
|
||||
// 1 change in one of the remaining bits
|
||||
//
|
||||
// What this means is that in the equation:
|
||||
// x[n+1] = x[n] ^ v[p]
|
||||
// x[n+2] = x[n+1] ^ v[q] = x[n] ^ v[p] ^ v[q]
|
||||
// ...
|
||||
// We will apply xor with v[1] eight times, v[2] four times,
|
||||
// v[3] twice, v[4] once and one other direction number once.
|
||||
// Since two xors cancel out, we can skip even applications
|
||||
// and just apply xor with v[4] (i.e. log2(16)) and with
|
||||
// the current applicable direction number.
|
||||
// Note that all these indices count from 1, so we need to
|
||||
// subtract 1 from them all to account for C arrays counting
|
||||
// from zero.
|
||||
unsigned int v_log2stridem1 = v[__ffs(stride) - 2];
|
||||
unsigned int v_stridemask = stride - 1;
|
||||
|
||||
for (unsigned int i = i0 + stride; i < n_vectors; i += stride) {
|
||||
// x[i] = x[i-stride] ^ v[b] ^ v[c]
|
||||
// where b is log2(stride) minus 1 for C array indexing
|
||||
// where c is the index of the rightmost zero bit in i,
|
||||
// not including the bottom log2(stride) bits, minus 1
|
||||
// for C array indexing
|
||||
// In the Bratley and Fox paper this is equation (**)
|
||||
X ^= v_log2stridem1 ^ v[__ffs(~((i - stride) | v_stridemask)) - 1];
|
||||
d_output[i] = (float)X * k_2powneg32;
|
||||
}
|
||||
for (unsigned int i = i0 + stride; i < n_vectors; i += stride) {
|
||||
// x[i] = x[i-stride] ^ v[b] ^ v[c]
|
||||
// where b is log2(stride) minus 1 for C array indexing
|
||||
// where c is the index of the rightmost zero bit in i,
|
||||
// not including the bottom log2(stride) bits, minus 1
|
||||
// for C array indexing
|
||||
// In the Bratley and Fox paper this is equation (**)
|
||||
X ^= v_log2stridem1 ^ v[__ffs(~((i - stride) | v_stridemask)) - 1];
|
||||
d_output[i] = (float)X * k_2powneg32;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void sobolGPU(int n_vectors, int n_dimensions,
|
||||
unsigned int *d_directions, float *d_output) {
|
||||
const int threadsperblock = 64;
|
||||
extern "C" void sobolGPU(int n_vectors, int n_dimensions, unsigned int *d_directions, float *d_output)
|
||||
{
|
||||
const int threadsperblock = 64;
|
||||
|
||||
// Set up the execution configuration
|
||||
dim3 dimGrid;
|
||||
dim3 dimBlock;
|
||||
// Set up the execution configuration
|
||||
dim3 dimGrid;
|
||||
dim3 dimBlock;
|
||||
|
||||
int device;
|
||||
cudaDeviceProp prop;
|
||||
checkCudaErrors(cudaGetDevice(&device));
|
||||
checkCudaErrors(cudaGetDeviceProperties(&prop, device));
|
||||
int device;
|
||||
cudaDeviceProp prop;
|
||||
checkCudaErrors(cudaGetDevice(&device));
|
||||
checkCudaErrors(cudaGetDeviceProperties(&prop, device));
|
||||
|
||||
// This implementation of the generator outputs all the draws for
|
||||
// one dimension in a contiguous region of memory, followed by the
|
||||
// next dimension and so on.
|
||||
// Therefore all threads within a block will be processing different
|
||||
// vectors from the same dimension. As a result we want the total
|
||||
// number of blocks to be a multiple of the number of dimensions.
|
||||
dimGrid.y = n_dimensions;
|
||||
// This implementation of the generator outputs all the draws for
|
||||
// one dimension in a contiguous region of memory, followed by the
|
||||
// next dimension and so on.
|
||||
// Therefore all threads within a block will be processing different
|
||||
// vectors from the same dimension. As a result we want the total
|
||||
// number of blocks to be a multiple of the number of dimensions.
|
||||
dimGrid.y = n_dimensions;
|
||||
|
||||
// If the number of dimensions is large then we will set the number
|
||||
// of blocks to equal the number of dimensions (i.e. dimGrid.x = 1)
|
||||
// but if the number of dimensions is small (e.g. less than four per
|
||||
// multiprocessor) then we'll partition the vectors across blocks
|
||||
// (as well as threads).
|
||||
if (n_dimensions < (4 * prop.multiProcessorCount)) {
|
||||
dimGrid.x = 4 * prop.multiProcessorCount;
|
||||
} else {
|
||||
dimGrid.x = 1;
|
||||
}
|
||||
// If the number of dimensions is large then we will set the number
|
||||
// of blocks to equal the number of dimensions (i.e. dimGrid.x = 1)
|
||||
// but if the number of dimensions is small (e.g. less than four per
|
||||
// multiprocessor) then we'll partition the vectors across blocks
|
||||
// (as well as threads).
|
||||
if (n_dimensions < (4 * prop.multiProcessorCount)) {
|
||||
dimGrid.x = 4 * prop.multiProcessorCount;
|
||||
}
|
||||
else {
|
||||
dimGrid.x = 1;
|
||||
}
|
||||
|
||||
// Cap the dimGrid.x if the number of vectors is small
|
||||
if (dimGrid.x > (unsigned int)(n_vectors / threadsperblock)) {
|
||||
dimGrid.x = (n_vectors + threadsperblock - 1) / threadsperblock;
|
||||
}
|
||||
// Cap the dimGrid.x if the number of vectors is small
|
||||
if (dimGrid.x > (unsigned int)(n_vectors / threadsperblock)) {
|
||||
dimGrid.x = (n_vectors + threadsperblock - 1) / threadsperblock;
|
||||
}
|
||||
|
||||
// Round up to a power of two, required for the algorithm so that
|
||||
// stride is a power of two.
|
||||
unsigned int targetDimGridX = dimGrid.x;
|
||||
// Round up to a power of two, required for the algorithm so that
|
||||
// stride is a power of two.
|
||||
unsigned int targetDimGridX = dimGrid.x;
|
||||
|
||||
for (dimGrid.x = 1; dimGrid.x < targetDimGridX; dimGrid.x *= 2)
|
||||
;
|
||||
for (dimGrid.x = 1; dimGrid.x < targetDimGridX; dimGrid.x *= 2)
|
||||
;
|
||||
|
||||
// Fix the number of threads
|
||||
dimBlock.x = threadsperblock;
|
||||
// Fix the number of threads
|
||||
dimBlock.x = threadsperblock;
|
||||
|
||||
// Execute GPU kernel
|
||||
sobolGPU_kernel<<<dimGrid, dimBlock>>>(n_vectors, n_dimensions, d_directions,
|
||||
d_output);
|
||||
// Execute GPU kernel
|
||||
sobolGPU_kernel<<<dimGrid, dimBlock>>>(n_vectors, n_dimensions, d_directions, d_output);
|
||||
}
|
||||
|
||||
@@ -57,7 +57,6 @@
|
||||
#ifndef SOBOL_GPU_H
|
||||
#define SOBOL_GPU_H
|
||||
|
||||
extern "C" void sobolGPU(int n_vectors, int n_dimensions,
|
||||
unsigned int *d_directions, float *d_output);
|
||||
extern "C" void sobolGPU(int n_vectors, int n_dimensions, unsigned int *d_directions, float *d_output);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -65,8 +65,7 @@
|
||||
// The primitives are based on those generated by Stephen Joe and
|
||||
// Frances Kuo in the joe-kuo-6.10200 set.
|
||||
// c.f. http://web.maths.unsw.edu.au/~fkuo/sobol/index.html
|
||||
const struct primitive sobol_primitives[] =
|
||||
{
|
||||
const struct primitive sobol_primitives[] = {
|
||||
// First dimension is a special case so this entry is actually ignored
|
||||
{1, 0, 0, {}},
|
||||
{2, 1, 0, {1}},
|
||||
|
||||
@@ -64,11 +64,12 @@
|
||||
// degree is the degree of the polynomial
|
||||
// a is a binary word representing the coefficients
|
||||
// m is the array of m values
|
||||
struct primitive {
|
||||
unsigned int dimension;
|
||||
unsigned int degree;
|
||||
unsigned int a;
|
||||
unsigned int m[max_m];
|
||||
struct primitive
|
||||
{
|
||||
unsigned int dimension;
|
||||
unsigned int degree;
|
||||
unsigned int a;
|
||||
unsigned int m[max_m];
|
||||
};
|
||||
|
||||
extern const struct primitive sobol_primitives[];
|
||||
|
||||
Reference in New Issue
Block a user