mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-13 00:08:26 +08:00
CUDA 13.2 samples update (#432)
- Added Python samples for CUDA Python 1.0 release - Renamed top-level `Samples` directory to `cpp` to accommodate Python samples.
This commit is contained in:
18
cpp/0_Introduction/simpleMPI/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp/0_Introduction/simpleMPI/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Linux",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**",
|
||||
"${workspaceFolder}/../../../Common"
|
||||
],
|
||||
"defines": [],
|
||||
"compilerPath": "/usr/local/cuda/bin/nvcc",
|
||||
"cStandard": "gnu17",
|
||||
"cppStandard": "gnu++14",
|
||||
"intelliSenseMode": "linux-gcc-x64",
|
||||
"configurationProvider": "ms-vscode.makefile-tools"
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
7
cpp/0_Introduction/simpleMPI/.vscode/extensions.json
vendored
Normal file
7
cpp/0_Introduction/simpleMPI/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"nvidia.nsight-vscode-edition",
|
||||
"ms-vscode.cpptools",
|
||||
"ms-vscode.makefile-tools"
|
||||
]
|
||||
}
|
||||
50
cpp/0_Introduction/simpleMPI/CMakeLists.txt
Normal file
50
cpp/0_Introduction/simpleMPI/CMakeLists.txt
Normal file
@@ -0,0 +1,50 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||
|
||||
project(simpleMPI LANGUAGES C CXX CUDA)
|
||||
|
||||
find_package(CUDAToolkit REQUIRED)
|
||||
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
set(CMAKE_CUDA_ARCHITECTURES 75 80 86 87 89 90 100 110 120)
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
|
||||
if(ENABLE_CUDA_DEBUG)
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G") # enable cuda-gdb (may significantly affect performance on some targets)
|
||||
else()
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -lineinfo") # add line information to all builds for debug tools (exclusive to -G option)
|
||||
endif()
|
||||
|
||||
# Include directories and libraries
|
||||
include_directories(../../../Common)
|
||||
|
||||
find_package(MPI)
|
||||
|
||||
# Source file
|
||||
if(${MPI_FOUND})
|
||||
# Add target for simpleMPI
|
||||
add_executable(simpleMPI simpleMPI.cpp simpleMPI.cu)
|
||||
|
||||
target_compile_options(simpleMPI PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(simpleMPI PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
set_target_properties(simpleMPI PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||
|
||||
target_include_directories(simpleMPI PUBLIC
|
||||
${MPI_INCLUDE_PATH}
|
||||
)
|
||||
|
||||
target_link_libraries(simpleMPI PUBLIC
|
||||
${MPI_C_LIBRARIES}
|
||||
${MPI_CXX_LIBRARIES}
|
||||
)
|
||||
|
||||
else()
|
||||
message(STATUS "MPI not found - will not build sample 'simpleMPI'")
|
||||
endif()
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
setup_samples_install()
|
||||
36
cpp/0_Introduction/simpleMPI/README.md
Normal file
36
cpp/0_Introduction/simpleMPI/README.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# simpleMPI - simpleMPI
|
||||
|
||||
## Description
|
||||
|
||||
Simple example demonstrating how to use MPI in combination with CUDA.
|
||||
|
||||
## Key Concepts
|
||||
|
||||
CUDA Systems Integration, MPI, Multithreading
|
||||
|
||||
## Supported SM Architectures
|
||||
|
||||
[SM 5.0 ](https://developer.nvidia.com/cuda-gpus) [SM 5.2 ](https://developer.nvidia.com/cuda-gpus) [SM 5.3 ](https://developer.nvidia.com/cuda-gpus) [SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
|
||||
|
||||
## Supported OSes
|
||||
|
||||
Linux, Windows
|
||||
|
||||
## Supported CPU Architecture
|
||||
|
||||
x86_64, aarch64
|
||||
|
||||
## CUDA APIs involved
|
||||
|
||||
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||
cudaMalloc, cudaGetLastError, cudaMemcpy, cudaFree
|
||||
|
||||
## Dependencies needed to build/run
|
||||
[MPI](../../../README.md#message-passing-interface)
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Download and install the [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||
Make sure the dependencies mentioned in [Dependencies]() section above are installed.
|
||||
|
||||
## References (for more details)
|
||||
129
cpp/0_Introduction/simpleMPI/simpleMPI.cpp
Normal file
129
cpp/0_Introduction/simpleMPI/simpleMPI.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* Simple example demonstrating how to use MPI with CUDA
|
||||
*
|
||||
* Generate some random numbers on one node.
|
||||
* Dispatch them to all nodes.
|
||||
* Compute their square root on each node's GPU.
|
||||
* Compute the average of the results using MPI.
|
||||
*
|
||||
* simpleMPI.cpp: main program, compiled with mpicxx on linux/Mac platforms
|
||||
* on Windows, please download the Microsoft HPC Pack SDK 2008
|
||||
*/
|
||||
|
||||
// MPI include
|
||||
#include <mpi.h>
|
||||
|
||||
// System includes
|
||||
#include <iostream>
|
||||
|
||||
using std::cerr;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
// User include
|
||||
#include "simpleMPI.h"
|
||||
|
||||
// Error handling macros
|
||||
#define MPI_CHECK(call) \
|
||||
if ((call) != MPI_SUCCESS) { \
|
||||
cerr << "MPI error calling \"" #call "\"\n"; \
|
||||
my_abort(-1); \
|
||||
}
|
||||
|
||||
// Host code
|
||||
// No CUDA here, only MPI
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// Dimensions of the dataset
|
||||
int blockSize = 256;
|
||||
int gridSize = 10000;
|
||||
int dataSizePerNode = gridSize * blockSize;
|
||||
|
||||
// Initialize MPI state
|
||||
MPI_CHECK(MPI_Init(&argc, &argv));
|
||||
|
||||
// Get our MPI node number and node count
|
||||
int commSize, commRank;
|
||||
MPI_CHECK(MPI_Comm_size(MPI_COMM_WORLD, &commSize));
|
||||
MPI_CHECK(MPI_Comm_rank(MPI_COMM_WORLD, &commRank));
|
||||
|
||||
// Generate some random numbers on the root node (node 0)
|
||||
int dataSizeTotal = dataSizePerNode * commSize;
|
||||
float *dataRoot = NULL;
|
||||
|
||||
// Are we the root node?
|
||||
if (commRank == 0) {
|
||||
cout << "Running on " << commSize << " nodes" << endl;
|
||||
dataRoot = new float[dataSizeTotal];
|
||||
initData(dataRoot, dataSizeTotal);
|
||||
}
|
||||
|
||||
// Allocate a buffer on each node
|
||||
float *dataNode = new float[dataSizePerNode];
|
||||
|
||||
// Dispatch a portion of the input data to each node
|
||||
MPI_CHECK(
|
||||
MPI_Scatter(dataRoot, dataSizePerNode, MPI_FLOAT, dataNode, dataSizePerNode, MPI_FLOAT, 0, MPI_COMM_WORLD));
|
||||
|
||||
if (commRank == 0) {
|
||||
// No need for root data any more
|
||||
delete[] dataRoot;
|
||||
}
|
||||
|
||||
// On each node, run computation on GPU
|
||||
computeGPU(dataNode, blockSize, gridSize);
|
||||
|
||||
// Reduction to the root node, computing the sum of output elements
|
||||
float sumNode = sum(dataNode, dataSizePerNode);
|
||||
float sumRoot;
|
||||
|
||||
MPI_CHECK(MPI_Reduce(&sumNode, &sumRoot, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD));
|
||||
|
||||
if (commRank == 0) {
|
||||
float average = sumRoot / dataSizeTotal;
|
||||
cout << "Average of square roots is: " << average << endl;
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
delete[] dataNode;
|
||||
MPI_CHECK(MPI_Finalize());
|
||||
|
||||
if (commRank == 0) {
|
||||
cout << "PASSED\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Shut down MPI cleanly if something goes wrong
|
||||
void my_abort(int err)
|
||||
{
|
||||
cout << "Test FAILED\n";
|
||||
MPI_Abort(MPI_COMM_WORLD, err);
|
||||
}
|
||||
104
cpp/0_Introduction/simpleMPI/simpleMPI.cu
Normal file
104
cpp/0_Introduction/simpleMPI/simpleMPI.cu
Normal file
@@ -0,0 +1,104 @@
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* Simple example demonstrating how to use MPI with CUDA
|
||||
*
|
||||
* Generate some random numbers on one node.
|
||||
* Dispatch them to all nodes.
|
||||
* Compute their square root on each node's GPU.
|
||||
* Compute the average of the results using MPI.
|
||||
*
|
||||
* simpleMPI.cu: GPU part, compiled with nvcc
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
using std::cerr;
|
||||
using std::endl;
|
||||
|
||||
#include "simpleMPI.h"
|
||||
|
||||
// Error handling macro
|
||||
#define CUDA_CHECK(call) \
|
||||
if ((call) != cudaSuccess) { \
|
||||
cudaError_t err = cudaGetLastError(); \
|
||||
cerr << "CUDA error calling \"" #call "\", code is " << err << endl; \
|
||||
my_abort(err); \
|
||||
}
|
||||
|
||||
// Device code
|
||||
// Very simple GPU Kernel that computes square roots of input numbers
|
||||
__global__ void simpleMPIKernel(float *input, float *output)
|
||||
{
|
||||
int tid = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
output[tid] = sqrt(input[tid]);
|
||||
}
|
||||
|
||||
// Initialize an array with random data (between 0 and 1)
|
||||
void initData(float *data, int dataSize)
|
||||
{
|
||||
for (int i = 0; i < dataSize; i++) {
|
||||
data[i] = (float)rand() / RAND_MAX;
|
||||
}
|
||||
}
|
||||
|
||||
// CUDA computation on each node
|
||||
// No MPI here, only CUDA
|
||||
void computeGPU(float *hostData, int blockSize, int gridSize)
|
||||
{
|
||||
int dataSize = blockSize * gridSize;
|
||||
|
||||
// Allocate data on GPU memory
|
||||
float *deviceInputData = NULL;
|
||||
CUDA_CHECK(cudaMalloc((void **)&deviceInputData, dataSize * sizeof(float)));
|
||||
|
||||
float *deviceOutputData = NULL;
|
||||
CUDA_CHECK(cudaMalloc((void **)&deviceOutputData, dataSize * sizeof(float)));
|
||||
|
||||
// Copy to GPU memory
|
||||
CUDA_CHECK(cudaMemcpy(deviceInputData, hostData, dataSize * sizeof(float), cudaMemcpyHostToDevice));
|
||||
|
||||
// Run kernel
|
||||
simpleMPIKernel<<<gridSize, blockSize>>>(deviceInputData, deviceOutputData);
|
||||
|
||||
// Copy data back to CPU memory
|
||||
CUDA_CHECK(cudaMemcpy(hostData, deviceOutputData, dataSize * sizeof(float), cudaMemcpyDeviceToHost));
|
||||
|
||||
// Free GPU memory
|
||||
CUDA_CHECK(cudaFree(deviceInputData));
|
||||
CUDA_CHECK(cudaFree(deviceOutputData));
|
||||
}
|
||||
|
||||
float sum(float *data, int size)
|
||||
{
|
||||
float accum = 0.f;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
accum += data[i];
|
||||
}
|
||||
|
||||
return accum;
|
||||
}
|
||||
45
cpp/0_Introduction/simpleMPI/simpleMPI.h
Normal file
45
cpp/0_Introduction/simpleMPI/simpleMPI.h
Normal file
@@ -0,0 +1,45 @@
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* Simple example demonstrating how to use MPI with CUDA
|
||||
*
|
||||
* Generate some random numbers on one node.
|
||||
* Dispatch them to all nodes.
|
||||
* Compute their square root on each node's GPU.
|
||||
* Compute the average of the results using MPI.
|
||||
*
|
||||
* simpleMPI.h: common header file
|
||||
*/
|
||||
|
||||
// Forward declarations
|
||||
extern "C"
|
||||
{
|
||||
void initData(float *data, int dataSize);
|
||||
void computeGPU(float *hostData, int blockSize, int gridSize);
|
||||
float sum(float *data, int size);
|
||||
void my_abort(int err);
|
||||
}
|
||||
Reference in New Issue
Block a user