Sync v13.5-public from internal repo

This commit is contained in:
cuda-samples sync
2026-11-04 20:44:00 +00:00
parent 5443602d89
commit a9315c55fb
53 changed files with 1750 additions and 183 deletions

View File

@@ -2,7 +2,7 @@
## Description
This sample demonstrates efficient all-pairs simulation of a gravitational n-body simulation in CUDA. This sample accompanies the GPU Gems 3 chapter "Fast N-Body Simulation with CUDA". With CUDA 5.5, performance on Tesla K20c has increased to over 1.8TFLOP/s single precision. Double Performance has also improved on all Kepler and Fermi GPU architectures as well. Starting in CUDA 4.0, the nBody sample has been updated to take advantage of new features to easily scale the n-body simulation across multiple GPUs in a single PC. Adding "-numbodies=<bodies>" to the command line will allow users to set # of bodies for simulation. Adding “-numdevices=<N>” to the command line option will cause the sample to use N devices (if available) for simulation. In this mode, the position and velocity data for all bodies are read from system memory using “zero copy” rather than from device memory. For a small number of devices (4 or fewer) and a large enough number of bodies, bandwidth is not a bottleneck so we can achieve strong scaling across these devices.
This sample demonstrates efficient all-pairs simulation of a gravitational n-body simulation in CUDA. This sample accompanies the GPU Gems 3 chapter "Fast N-Body Simulation with CUDA". With CUDA 5.5, performance on Tesla K20c has increased to over 1.8TFLOP/s single precision. Double Performance has also improved on all Kepler and Fermi GPU architectures as well. Starting in CUDA 4.0, the nBody sample has been updated to take advantage of new features to easily scale the n-body simulation across multiple GPUs in a single PC. Adding "-numbodies=<bodies>" to the command line will allow users to set # of bodies for simulation. Adding “-numdevices=<N>” to the command line option will cause the sample to use N devices (if available) for simulation. In this mode, GPU 0 holds the authoritative position and velocity buffers. Each other device keeps a local scratch copy of the read-only position array and refreshes it with one peer-to-peer copy per simulation step, so the all-pairs kernel reads every tile from local memory instead of re-fetching it over the peer link on every tile access; each device still writes its own share of results directly back to GPU 0 over the peer link. Each device issues that copy and its kernel on its own non-blocking stream so the GPUs overlap. This keeps per-step peer traffic proportional to the number of devices instead of the number of thread blocks, so multi-GPU runs scale on both NVLink and PCIe-only (no NVLink) systems.
## Key Concepts
@@ -23,7 +23,7 @@ x86_64, armv7l
## CUDA APIs involved
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
cudaGraphicsUnmapResources, cudaSetDeviceFlags, cudaGraphicsResourceSetMapFlags, cudaGraphicsResourceGetMappedPointer, cudaGraphicsMapResources, cudaSetDevice, cudaEventSynchronize, cudaGetDeviceCount, cudaGetDeviceProperties, cudaDeviceSynchronize, cudaEventRecord, cudaGetDevice, cudaMemcpyToSymbol, cudaStreamQuery, cudaEventDestroy, cudaEventElapsedTime, cudaDeviceCanAccessPeer, cudaEventCreate
cudaGraphicsUnmapResources, cudaSetDeviceFlags, cudaGraphicsResourceSetMapFlags, cudaGraphicsResourceGetMappedPointer, cudaGraphicsMapResources, cudaSetDevice, cudaEventSynchronize, cudaGetDeviceCount, cudaGetDeviceProperties, cudaDeviceSynchronize, cudaEventRecord, cudaGetDevice, cudaMemcpyToSymbol, cudaEventDestroy, cudaEventElapsedTime, cudaDeviceCanAccessPeer, cudaDeviceEnablePeerAccess, cudaMemcpyPeerAsync, cudaStreamCreateWithFlags, cudaStreamDestroy, cudaStreamSynchronize, cudaEventCreate
## Dependencies needed to build/run
[X11](../../../README.md#x11), [OpenGL](../../../README.md#opengl), [Freeglut](../../../README.md#freeglut), [GLEW](../../../README.md#glew)

View File

@@ -89,8 +89,10 @@ template <> __device__ double getSofteningSquared<double>() { return soften
template <typename T> struct DeviceData
{
T *dPos[2]; // mapped host pointers
T *dReadScratch;
T *dVel;
cudaEvent_t event;
cudaStream_t stream;
unsigned int offset;
unsigned int numBodies;
};
@@ -210,14 +212,22 @@ void integrateNbodySystem(DeviceData<T> *deviceData,
bool bUsePBO)
{
if (bUsePBO) {
if (numDevices > 1) {
checkCudaErrors(cudaSetDevice(0));
}
checkCudaErrors(cudaGraphicsResourceSetMapFlags(pgres[currentRead], cudaGraphicsMapFlagsReadOnly));
checkCudaErrors(cudaGraphicsResourceSetMapFlags(pgres[1 - currentRead], cudaGraphicsMapFlagsWriteDiscard));
checkCudaErrors(cudaGraphicsMapResources(2, pgres, 0));
checkCudaErrors(cudaGraphicsMapResources(2, pgres, deviceData[0].stream));
size_t bytes;
checkCudaErrors(cudaGraphicsResourceGetMappedPointer(
(void **)&(deviceData[0].dPos[currentRead]), &bytes, pgres[currentRead]));
checkCudaErrors(cudaGraphicsResourceGetMappedPointer(
(void **)&(deviceData[0].dPos[1 - currentRead]), &bytes, pgres[1 - currentRead]));
// Peer copies and non-owner writes use deviceData[0].dPos directly.
// Those pointers are only valid after this map; other devices' streams
// do not wait for it.
checkCudaErrors(cudaStreamSynchronize(deviceData[0].stream));
}
for (unsigned int dev = 0; dev != numDevices; dev++) {
@@ -228,21 +238,33 @@ void integrateNbodySystem(DeviceData<T> *deviceData,
int numBlocks = (deviceData[dev].numBodies + blockSize - 1) / blockSize;
int numTiles = (numBodies + blockSize - 1) / blockSize;
int sharedMemSize = blockSize * 4 * sizeof(T); // 4 floats for pos
// GPU0 owns both ping-pong position buffers and velocity. Always pass
// this map's GPU0 pointers into the kernel so a non-owner never writes
// through a pre-map alias. Local scratch is the only per-device buffer.
T *readPos = deviceData[0].dPos[currentRead];
integrateBodies<T>
<<<numBlocks, blockSize, sharedMemSize>>>((typename vec4<T>::Type *)deviceData[dev].dPos[1 - currentRead],
(typename vec4<T>::Type *)deviceData[dev].dPos[currentRead],
(typename vec4<T>::Type *)deviceData[dev].dVel,
deviceData[dev].offset,
deviceData[dev].numBodies,
deltaTime,
damping,
numTiles);
if (deviceData[dev].dReadScratch != nullptr) {
checkCudaErrors(cudaMemcpyPeerAsync(deviceData[dev].dReadScratch,
dev,
deviceData[0].dPos[currentRead],
0,
numBodies * 4 * sizeof(T),
deviceData[dev].stream));
readPos = deviceData[dev].dReadScratch;
}
integrateBodies<T><<<numBlocks, blockSize, sharedMemSize, deviceData[dev].stream>>>(
(typename vec4<T>::Type *)deviceData[0].dPos[1 - currentRead],
(typename vec4<T>::Type *)readPos,
(typename vec4<T>::Type *)deviceData[0].dVel,
deviceData[dev].offset,
deviceData[dev].numBodies,
deltaTime,
damping,
numTiles);
if (numDevices > 1) {
checkCudaErrors(cudaEventRecord(deviceData[dev].event));
// MJH: Hack on older driver versions to force kernel launches to flush!
cudaStreamQuery(0);
checkCudaErrors(cudaEventRecord(deviceData[dev].event, deviceData[dev].stream));
}
// check if kernel invocation generated an error
@@ -250,13 +272,23 @@ void integrateNbodySystem(DeviceData<T> *deviceData,
}
if (numDevices > 1) {
// Single-GPU stays on the legacy default stream (see _initialize())
// and keeps queuing kernels without a host wait here. Multi-GPU
// kernels run on per-device non-blocking streams; wait so the next
// step's peer copy and any PBO unmap see completed writes.
for (unsigned int dev = 0; dev < numDevices; dev++) {
checkCudaErrors(cudaEventSynchronize(deviceData[dev].event));
}
}
if (bUsePBO) {
checkCudaErrors(cudaGraphicsUnmapResources(2, pgres, 0));
if (numDevices > 1) {
checkCudaErrors(cudaSetDevice(0));
}
checkCudaErrors(cudaGraphicsUnmapResources(2, pgres, deviceData[0].stream));
// GL (windowed display) may use the PBO as soon as update() returns.
checkCudaErrors(cudaStreamSynchronize(deviceData[0].stream));
}
}

View File

@@ -33,8 +33,10 @@
template <typename T> struct DeviceData
{
T *dPos[2]; // mapped host pointers
T *dReadScratch;
T *dVel;
cudaEvent_t event;
cudaStream_t stream;
unsigned int offset;
unsigned int numBodies;
};
@@ -66,6 +68,12 @@ public:
virtual unsigned int getNumBodies() const { return m_numBodies; }
// Stream that GPU0 kernels (and PBO map/unmap) are issued on. Single-GPU
// leaves this as the legacy default stream (0); multi-GPU uses a
// non-blocking stream. Benchmark / FPS timing must record on this stream
// rather than hard-coding 0.
cudaStream_t getComputeStream() const { return m_deviceData[0].stream; }
protected: // methods
BodySystemCUDA() {}

View File

@@ -116,6 +116,9 @@ template <typename T> void BodySystemCUDA<T>::_initialize(int numBodies)
unsigned int remaining = m_numBodies;
for (unsigned int i = 0; i < m_numDevices; i++) {
m_deviceData[i].dReadScratch = 0;
m_deviceData[i].stream = 0;
unsigned int count = (int)((weights[i] / total) * m_numBodies);
// Rounding up to numSms[i]*256 leads to better GPU utilization _per_ GPU
// but when using multiple devices, it will lead to the last GPUs not having
@@ -172,6 +175,13 @@ template <typename T> void BodySystemCUDA<T>::_initialize(int numBodies)
checkCudaErrors(cudaSetDevice(m_devID));
checkCudaErrors(cudaEventCreate(&m_deviceData[0].event));
// A non-blocking stream is only needed to let multiple devices' work
// overlap. Single-GPU keeps m_deviceData[0].stream == 0 (set above)
// and launches on the legacy default stream.
if (m_numDevices > 1) {
checkCudaErrors(cudaStreamCreateWithFlags(&m_deviceData[0].stream, cudaStreamNonBlocking));
}
if (m_bUsePBO) {
// create the position pixel buffer objects for rendering
// we will actually compute directly from this memory in CUDA too
@@ -217,12 +227,22 @@ template <typename T> void BodySystemCUDA<T>::_initialize(int numBodies)
}
checkCudaErrors(cudaEventCreate(&m_deviceData[i].event));
checkCudaErrors(cudaStreamCreateWithFlags(&m_deviceData[i].stream, cudaStreamNonBlocking));
// Local scratch for this device's copy of the read-only position
// array: integrateNbodySystem() refreshes it once per step so the
// all-pairs kernel reads every tile from local memory instead of
// re-fetching over the P2P link on every tile access.
checkCudaErrors(cudaMalloc((void **)&m_deviceData[i].dReadScratch, memSize));
// Point all GPUs to the memory allocated on gpu0
m_deviceData[i].dPos[0] = m_deviceData[0].dPos[0];
m_deviceData[i].dPos[1] = m_deviceData[0].dPos[1];
m_deviceData[i].dVel = m_deviceData[0].dVel;
}
// Restore m_devID: callers create CUDA handles on whatever device is current right after this returns.
checkCudaErrors(cudaSetDevice(m_devID));
}
}
@@ -247,6 +267,13 @@ template <typename T> void BodySystemCUDA<T>::_finalize()
delete[] m_hPos[1];
delete[] m_hVel;
// integrateNbodySystem() can leave a non-zero device current when
// m_numDevices > 1 and m_bUsePBO is false. The handles below were
// created on m_devID, so restore it before touching them.
if (m_numDevices > 1) {
checkCudaErrors(cudaSetDevice(m_devID));
}
checkCudaErrors(cudaFree((void **)m_deviceData[0].dVel));
if (m_bUsePBO) {
@@ -257,14 +284,21 @@ template <typename T> void BodySystemCUDA<T>::_finalize()
else {
checkCudaErrors(cudaFree((void **)m_deviceData[0].dPos[0]));
checkCudaErrors(cudaFree((void **)m_deviceData[0].dPos[1]));
}
checkCudaErrors(cudaEventDestroy(m_deviceData[0].event));
checkCudaErrors(cudaEventDestroy(m_deviceData[0].event));
if (m_numDevices > 1) {
checkCudaErrors(cudaStreamDestroy(m_deviceData[0].stream));
}
if (m_bUseP2P) {
for (unsigned int i = 1; i < m_numDevices; i++) {
checkCudaErrors(cudaEventDestroy(m_deviceData[i].event));
}
if (m_bUseP2P) {
for (unsigned int i = 1; i < m_numDevices; i++) {
checkCudaErrors(cudaSetDevice(i));
checkCudaErrors(cudaFree(m_deviceData[i].dReadScratch));
checkCudaErrors(cudaEventDestroy(m_deviceData[i].event));
checkCudaErrors(cudaStreamDestroy(m_deviceData[i].stream));
}
checkCudaErrors(cudaSetDevice(m_devID));
}
}
@@ -304,6 +338,11 @@ template <typename T> void BodySystemCUDA<T>::setSoftening(T softening)
checkCudaErrors(setSofteningSquared(softeningSq));
}
// Restore m_devID; the constructor calls this right after _initialize()'s own restore.
if (m_numDevices > 1) {
checkCudaErrors(cudaSetDevice(m_devID));
}
}
template <typename T> void BodySystemCUDA<T>::setDamping(T damping) { m_damping = damping; }
@@ -322,6 +361,14 @@ template <typename T> void BodySystemCUDA<T>::update(T deltaTime)
m_blockSize,
m_bUsePBO);
// The launch loop leaves the last device current. Host-memory multi-GPU
// has no explicit stream, so a later cudaEventRecord(timingEvent, 0) uses
// the current device's default stream. Timing events are created on
// m_devID, and each device has its own default stream.
if (m_numDevices > 1) {
checkCudaErrors(cudaSetDevice(m_devID));
}
std::swap(m_currentRead, m_currentWrite);
}
@@ -357,15 +404,18 @@ template <typename T> T *BodySystemCUDA<T>::getArray(BodyArray array)
if (!m_bUseSysMem) {
if (pgres) {
checkCudaErrors(cudaGraphicsResourceSetMapFlags(pgres, cudaGraphicsMapFlagsReadOnly));
checkCudaErrors(cudaGraphicsMapResources(1, &pgres, 0));
checkCudaErrors(cudaGraphicsMapResources(1, &pgres, m_deviceData[0].stream));
size_t bytes;
checkCudaErrors(cudaGraphicsResourceGetMappedPointer((void **)&ddata, &bytes, pgres));
}
checkCudaErrors(cudaMemcpy(hdata, ddata, m_numBodies * 4 * sizeof(T), cudaMemcpyDeviceToHost));
checkCudaErrors(cudaMemcpyAsync(
hdata, ddata, m_numBodies * 4 * sizeof(T), cudaMemcpyDeviceToHost, m_deviceData[0].stream));
checkCudaErrors(cudaStreamSynchronize(m_deviceData[0].stream));
if (pgres) {
checkCudaErrors(cudaGraphicsUnmapResources(1, &pgres, 0));
checkCudaErrors(cudaGraphicsUnmapResources(1, &pgres, m_deviceData[0].stream));
checkCudaErrors(cudaStreamSynchronize(m_deviceData[0].stream));
}
}

View File

@@ -162,6 +162,8 @@ static int fpsLimit = 5;
cudaEvent_t startEvent, stopEvent;
cudaEvent_t hostMemSyncEvent;
cudaStream_t nbodyTimingStream();
template <typename T> class NBodyDemo
{
public:
@@ -188,6 +190,11 @@ public:
static void runBenchmark(int iterations) { m_singleton->_runBenchmark(iterations); }
static cudaStream_t timingStream()
{
return (m_singleton && m_singleton->m_nbodyCuda) ? m_singleton->m_nbodyCuda->getComputeStream() : 0;
}
static void updateParams()
{
m_singleton->m_nbody->setSoftening(activeParams.m_softening);
@@ -440,7 +447,7 @@ private:
sdkStartTimer(&timer);
}
else {
checkCudaErrors(cudaEventRecord(startEvent, 0));
checkCudaErrors(cudaEventRecord(startEvent, nbodyTimingStream()));
}
for (int i = 0; i < iterations; ++i) {
@@ -455,7 +462,7 @@ private:
sdkStartTimer(&timer);
}
else {
checkCudaErrors(cudaEventRecord(stopEvent, 0));
checkCudaErrors(cudaEventRecord(stopEvent, nbodyTimingStream()));
checkCudaErrors(cudaEventSynchronize(stopEvent));
checkCudaErrors(cudaEventElapsedTime(&milliseconds, startEvent, stopEvent));
}
@@ -473,6 +480,19 @@ private:
}
};
cudaStream_t nbodyTimingStream()
{
if (useCpu) {
return 0;
}
if (fp64) {
return NBodyDemo<double>::timingStream();
}
return NBodyDemo<float>::timingStream();
}
void finalize()
{
if (!useCpu) {
@@ -642,7 +662,7 @@ void display()
if (!useCpu) {
cudaEventRecord(hostMemSyncEvent,
0); // insert an event to wait on before rendering
nbodyTimingStream()); // insert an event to wait on before rendering
}
}
@@ -720,7 +740,7 @@ void display()
sdkResetTimer(&timer);
}
else {
checkCudaErrors(cudaEventRecord(stopEvent, 0));
checkCudaErrors(cudaEventRecord(stopEvent, nbodyTimingStream()));
checkCudaErrors(cudaEventSynchronize(stopEvent));
checkCudaErrors(cudaEventElapsedTime(&milliseconds, startEvent, stopEvent));
}
@@ -748,7 +768,7 @@ void display()
// restart timer
if (!useCpu) {
checkCudaErrors(cudaEventRecord(startEvent, 0));
checkCudaErrors(cudaEventRecord(startEvent, nbodyTimingStream()));
}
}
@@ -1086,26 +1106,13 @@ int main(int argc, char **argv)
}
if (!useCpu) {
// Now choose the CUDA Device
// Either without GL interop:
if (benchmark || compareToCPU || useHostMem) {
// Note if we are using host memory for the body system, we
// don't use CUDA-GL interop.
if (checkCmdLineFlag(argc, (const char **)argv, "device")) {
customGPU = true;
}
devID = findCudaDevice(argc, (const char **)argv);
// Device selection does not depend on GL interop. Benchmark, compare,
// and host memory skip interop by leaving the PBO argument false below.
if (checkCmdLineFlag(argc, (const char **)argv, "device")) {
customGPU = true;
}
else // or with GL interop:
{
if (checkCmdLineFlag(argc, (const char **)argv, "device")) {
customGPU = true;
}
devID = findCudaDevice(argc, (const char **)argv);
}
devID = findCudaDevice(argc, (const char **)argv);
checkCudaErrors(cudaGetDevice(&devID));
checkCudaErrors(cudaGetDeviceProperties(&props, devID));
@@ -1147,10 +1154,7 @@ int main(int argc, char **argv)
exit(EXIT_SUCCESS);
}
if (numDevsRequested > 1) {
checkCudaErrors(cudaSetDevice(i));
}
checkCudaErrors(cudaSetDevice(i));
checkCudaErrors(cudaSetDeviceFlags(cudaDeviceMapHost));
#else
fprintf(stderr,
@@ -1322,7 +1326,7 @@ int main(int argc, char **argv)
glutIdleFunc(idle);
if (!useCpu) {
checkCudaErrors(cudaEventRecord(startEvent, 0));
checkCudaErrors(cudaEventRecord(startEvent, nbodyTimingStream()));
}
glutMainLoop();
@@ -1349,7 +1353,7 @@ int main(int argc, char **argv)
glutIdleFunc(idle);
if (!useCpu) {
checkCudaErrors(cudaEventRecord(startEvent, 0));
checkCudaErrors(cudaEventRecord(startEvent, nbodyTimingStream()));
}
glutMainLoop();