mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-23 21:28:17 +08:00
add and update samples with CUDA 11.3 support
This commit is contained in:
@@ -263,6 +263,14 @@ ALL_CCFLAGS += $(EXTRA_NVCCFLAGS)
|
||||
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(CCFLAGS))
|
||||
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(EXTRA_CCFLAGS))
|
||||
|
||||
SAMPLE_ENABLED := 1
|
||||
|
||||
# This sample is not supported on QNX
|
||||
ifeq ($(TARGET_OS),qnx)
|
||||
$(info >>> WARNING - MersenneTwisterGP11213 is not supported on QNX - waiving sample <<<)
|
||||
SAMPLE_ENABLED := 0
|
||||
endif
|
||||
|
||||
ALL_LDFLAGS :=
|
||||
ALL_LDFLAGS += $(ALL_CCFLAGS)
|
||||
ALL_LDFLAGS += $(addprefix -Xlinker ,$(LDFLAGS))
|
||||
@@ -297,6 +305,10 @@ ALL_CCFLAGS += --threads 0
|
||||
|
||||
LIBRARIES += -lcurand_static -lculibos
|
||||
|
||||
ifeq ($(SAMPLE_ENABLED),0)
|
||||
EXEC ?= @echo "[@]"
|
||||
endif
|
||||
|
||||
################################################################################
|
||||
|
||||
# Target rules
|
||||
@@ -304,16 +316,23 @@ all: build
|
||||
|
||||
build: MersenneTwisterGP11213
|
||||
|
||||
check.deps:
|
||||
ifeq ($(SAMPLE_ENABLED),0)
|
||||
@echo "Sample will be waived due to the above missing dependencies"
|
||||
else
|
||||
@echo "Sample is ready - all dependencies have been met"
|
||||
endif
|
||||
|
||||
MersenneTwister.o:MersenneTwister.cpp
|
||||
$(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
|
||||
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
|
||||
|
||||
MersenneTwisterGP11213: MersenneTwister.o
|
||||
$(NVCC) $(ALL_LDFLAGS) $(GENCODE_FLAGS) -o $@ $+ $(LIBRARIES)
|
||||
mkdir -p ../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
|
||||
cp $@ ../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
|
||||
$(EXEC) $(NVCC) $(ALL_LDFLAGS) $(GENCODE_FLAGS) -o $@ $+ $(LIBRARIES)
|
||||
$(EXEC) mkdir -p ../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
|
||||
$(EXEC) cp $@ ../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
|
||||
|
||||
run: build
|
||||
./MersenneTwisterGP11213
|
||||
$(EXEC) ./MersenneTwisterGP11213
|
||||
|
||||
clean:
|
||||
rm -f MersenneTwisterGP11213 MersenneTwister.o
|
||||
|
||||
@@ -47,138 +47,134 @@
|
||||
|
||||
float compareResults(int rand_n, float *h_RandGPU, float *h_RandCPU);
|
||||
|
||||
const int DEFAULT_RAND_N = 2400000;
|
||||
const int DEFAULT_RAND_N = 2400000;
|
||||
const unsigned int DEFAULT_SEED = 777;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Main program
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
// Start logs
|
||||
printf("%s Starting...\n\n", argv[0]);
|
||||
int main(int argc, char **argv) {
|
||||
// Start logs
|
||||
printf("%s Starting...\n\n", argv[0]);
|
||||
|
||||
// initialize the GPU, either identified by --device
|
||||
// or by picking the device with highest flop rate.
|
||||
int devID = findCudaDevice(argc, (const char **)argv);
|
||||
// initialize the GPU, either identified by --device
|
||||
// or by picking the device with highest flop rate.
|
||||
int devID = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
// parsing the number of random numbers to generate
|
||||
int rand_n = DEFAULT_RAND_N;
|
||||
// parsing the number of random numbers to generate
|
||||
int rand_n = DEFAULT_RAND_N;
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **) argv, "count"))
|
||||
{
|
||||
rand_n = getCmdLineArgumentInt(argc, (const char **) argv, "count");
|
||||
}
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "count")) {
|
||||
rand_n = getCmdLineArgumentInt(argc, (const char **)argv, "count");
|
||||
}
|
||||
|
||||
printf("Allocating data for %i samples...\n", rand_n);
|
||||
printf("Allocating data for %i samples...\n", rand_n);
|
||||
|
||||
// parsing the seed
|
||||
int seed = DEFAULT_SEED;
|
||||
// parsing the seed
|
||||
int seed = DEFAULT_SEED;
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **) argv, "seed"))
|
||||
{
|
||||
seed = getCmdLineArgumentInt(argc, (const char **) argv, "seed");
|
||||
}
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "seed")) {
|
||||
seed = getCmdLineArgumentInt(argc, (const char **)argv, "seed");
|
||||
}
|
||||
|
||||
printf("Seeding with %i ...\n", seed);
|
||||
printf("Seeding with %i ...\n", seed);
|
||||
|
||||
cudaStream_t stream;
|
||||
checkCudaErrors(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
|
||||
cudaStream_t stream;
|
||||
checkCudaErrors(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
|
||||
|
||||
float *d_Rand;
|
||||
checkCudaErrors(cudaMalloc((void **)&d_Rand, rand_n * sizeof(float)));
|
||||
float *d_Rand;
|
||||
checkCudaErrors(cudaMalloc((void **)&d_Rand, rand_n * sizeof(float)));
|
||||
|
||||
curandGenerator_t prngGPU;
|
||||
checkCudaErrors(curandCreateGenerator(&prngGPU, CURAND_RNG_PSEUDO_MTGP32));
|
||||
checkCudaErrors(curandSetStream(prngGPU, stream));
|
||||
checkCudaErrors(curandSetPseudoRandomGeneratorSeed(prngGPU, seed));
|
||||
curandGenerator_t prngGPU;
|
||||
checkCudaErrors(curandCreateGenerator(&prngGPU, CURAND_RNG_PSEUDO_MTGP32));
|
||||
checkCudaErrors(curandSetStream(prngGPU, stream));
|
||||
checkCudaErrors(curandSetPseudoRandomGeneratorSeed(prngGPU, seed));
|
||||
|
||||
curandGenerator_t prngCPU;
|
||||
checkCudaErrors(curandCreateGeneratorHost(&prngCPU, CURAND_RNG_PSEUDO_MTGP32));
|
||||
checkCudaErrors(curandSetPseudoRandomGeneratorSeed(prngCPU, seed));
|
||||
curandGenerator_t prngCPU;
|
||||
checkCudaErrors(
|
||||
curandCreateGeneratorHost(&prngCPU, CURAND_RNG_PSEUDO_MTGP32));
|
||||
checkCudaErrors(curandSetPseudoRandomGeneratorSeed(prngCPU, seed));
|
||||
|
||||
//
|
||||
// Example 1: Compare random numbers generated on GPU and CPU
|
||||
float *h_RandGPU = (float *)malloc(rand_n * sizeof(float));
|
||||
//
|
||||
// Example 1: Compare random numbers generated on GPU and CPU
|
||||
float *h_RandGPU;
|
||||
checkCudaErrors(cudaMallocHost(&h_RandGPU, rand_n * sizeof(float)));
|
||||
|
||||
printf("Generating random numbers on GPU...\n\n");
|
||||
checkCudaErrors(curandGenerateUniform(prngGPU, (float *) d_Rand, rand_n));
|
||||
printf("Generating random numbers on GPU...\n\n");
|
||||
checkCudaErrors(curandGenerateUniform(prngGPU, (float *)d_Rand, rand_n));
|
||||
|
||||
printf("\nReading back the results...\n");
|
||||
checkCudaErrors(cudaMemcpyAsync(h_RandGPU, d_Rand, rand_n * sizeof(float), cudaMemcpyDeviceToHost, stream));
|
||||
printf("\nReading back the results...\n");
|
||||
checkCudaErrors(cudaMemcpyAsync(h_RandGPU, d_Rand, rand_n * sizeof(float),
|
||||
cudaMemcpyDeviceToHost, stream));
|
||||
|
||||
float *h_RandCPU = (float *)malloc(rand_n * sizeof(float));
|
||||
|
||||
float *h_RandCPU = (float *)malloc(rand_n * sizeof(float));
|
||||
printf("Generating random numbers on CPU...\n\n");
|
||||
checkCudaErrors(curandGenerateUniform(prngCPU, (float *)h_RandCPU, rand_n));
|
||||
|
||||
printf("Generating random numbers on CPU...\n\n");
|
||||
checkCudaErrors(curandGenerateUniform(prngCPU, (float *) h_RandCPU, rand_n));
|
||||
checkCudaErrors(cudaStreamSynchronize(stream));
|
||||
printf("Comparing CPU/GPU random numbers...\n\n");
|
||||
float L1norm = compareResults(rand_n, h_RandGPU, h_RandCPU);
|
||||
|
||||
checkCudaErrors(cudaStreamSynchronize(stream));
|
||||
printf("Comparing CPU/GPU random numbers...\n\n");
|
||||
float L1norm = compareResults(rand_n, h_RandGPU, h_RandCPU);
|
||||
//
|
||||
// Example 2: Timing of random number generation on GPU
|
||||
const int numIterations = 10;
|
||||
int i;
|
||||
StopWatchInterface *hTimer;
|
||||
|
||||
//
|
||||
// Example 2: Timing of random number generation on GPU
|
||||
const int numIterations = 10;
|
||||
int i;
|
||||
StopWatchInterface *hTimer;
|
||||
sdkCreateTimer(&hTimer);
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
|
||||
sdkCreateTimer(&hTimer);
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
for (i = 0; i < numIterations; i++) {
|
||||
checkCudaErrors(curandGenerateUniform(prngGPU, (float *)d_Rand, rand_n));
|
||||
}
|
||||
|
||||
for (i = 0; i < numIterations; i++)
|
||||
{
|
||||
checkCudaErrors(curandGenerateUniform(prngGPU, (float *) d_Rand, rand_n));
|
||||
}
|
||||
checkCudaErrors(cudaStreamSynchronize(stream));
|
||||
sdkStopTimer(&hTimer);
|
||||
|
||||
checkCudaErrors(cudaStreamSynchronize(stream));
|
||||
sdkStopTimer(&hTimer);
|
||||
double gpuTime = 1.0e-3 * sdkGetTimerValue(&hTimer) / (double)numIterations;
|
||||
|
||||
double gpuTime = 1.0e-3 * sdkGetTimerValue(&hTimer)/(double)numIterations;
|
||||
printf(
|
||||
"MersenneTwisterGP11213, Throughput = %.4f GNumbers/s, Time = %.5f s, "
|
||||
"Size = %u Numbers\n",
|
||||
1.0e-9 * rand_n / gpuTime, gpuTime, rand_n);
|
||||
|
||||
printf("MersenneTwisterGP11213, Throughput = %.4f GNumbers/s, Time = %.5f s, Size = %u Numbers\n",
|
||||
1.0e-9 * rand_n / gpuTime, gpuTime, rand_n);
|
||||
printf("Shutting down...\n");
|
||||
|
||||
printf("Shutting down...\n");
|
||||
checkCudaErrors(curandDestroyGenerator(prngGPU));
|
||||
checkCudaErrors(curandDestroyGenerator(prngCPU));
|
||||
checkCudaErrors(cudaStreamDestroy(stream));
|
||||
checkCudaErrors(cudaFree(d_Rand));
|
||||
sdkDeleteTimer(&hTimer);
|
||||
checkCudaErrors(cudaFreeHost(h_RandGPU));
|
||||
free(h_RandCPU);
|
||||
|
||||
checkCudaErrors(curandDestroyGenerator(prngGPU));
|
||||
checkCudaErrors(curandDestroyGenerator(prngCPU));
|
||||
checkCudaErrors(cudaStreamDestroy(stream));
|
||||
checkCudaErrors(cudaFree(d_Rand));
|
||||
sdkDeleteTimer(&hTimer);
|
||||
free(h_RandGPU);
|
||||
free(h_RandCPU);
|
||||
|
||||
exit(L1norm < 1e-6 ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
exit(L1norm < 1e-6 ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
}
|
||||
|
||||
float compareResults(int rand_n, float *h_RandGPU, float *h_RandCPU) {
|
||||
int i;
|
||||
float rCPU, rGPU, delta;
|
||||
float max_delta = 0.;
|
||||
float sum_delta = 0.;
|
||||
float sum_ref = 0.;
|
||||
|
||||
float compareResults(int rand_n, float *h_RandGPU, float *h_RandCPU)
|
||||
{
|
||||
int i;
|
||||
float rCPU, rGPU, delta;
|
||||
float max_delta = 0.;
|
||||
float sum_delta = 0.;
|
||||
float sum_ref = 0.;
|
||||
for (i = 0; i < rand_n; i++) {
|
||||
rCPU = h_RandCPU[i];
|
||||
rGPU = h_RandGPU[i];
|
||||
delta = fabs(rCPU - rGPU);
|
||||
sum_delta += delta;
|
||||
sum_ref += fabs(rCPU);
|
||||
|
||||
for (i = 0; i < rand_n; i++)
|
||||
{
|
||||
rCPU = h_RandCPU[i];
|
||||
rGPU = h_RandGPU[i];
|
||||
delta = fabs(rCPU - rGPU);
|
||||
sum_delta += delta;
|
||||
sum_ref += fabs(rCPU);
|
||||
|
||||
if (delta >= max_delta)
|
||||
{
|
||||
max_delta = delta;
|
||||
}
|
||||
if (delta >= max_delta) {
|
||||
max_delta = delta;
|
||||
}
|
||||
}
|
||||
|
||||
float L1norm = (float)(sum_delta / sum_ref);
|
||||
printf("Max absolute error: %E\n", max_delta);
|
||||
printf("L1 norm: %E\n\n", L1norm);
|
||||
float L1norm = (float)(sum_delta / sum_ref);
|
||||
printf("Max absolute error: %E\n", max_delta);
|
||||
printf("L1 norm: %E\n\n", L1norm);
|
||||
|
||||
return L1norm;
|
||||
return L1norm;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
<Import Project="$(CUDAPropsPath)\CUDA 11.2.props" />
|
||||
<Import Project="$(CUDAPropsPath)\CUDA 11.3.props" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets">
|
||||
<Import Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" />
|
||||
@@ -108,6 +108,6 @@
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
<Import Project="$(CUDAPropsPath)\CUDA 11.2.targets" />
|
||||
<Import Project="$(CUDAPropsPath)\CUDA 11.3.targets" />
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
<Import Project="$(CUDAPropsPath)\CUDA 11.2.props" />
|
||||
<Import Project="$(CUDAPropsPath)\CUDA 11.3.props" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets">
|
||||
<Import Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" />
|
||||
@@ -104,6 +104,6 @@
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
<Import Project="$(CUDAPropsPath)\CUDA 11.2.targets" />
|
||||
<Import Project="$(CUDAPropsPath)\CUDA 11.3.targets" />
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
|
||||
@@ -27,7 +27,7 @@ x86_64, ppc64le, armv7l
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Download and install the [CUDA Toolkit 11.2](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||
Download and install the [CUDA Toolkit 11.3](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||
Make sure the dependencies mentioned in [Dependencies]() section above are installed.
|
||||
|
||||
## Build and Run
|
||||
|
||||
Reference in New Issue
Block a user