CUDA Heterogeneous API
Parallel Algorithms
David Mayerich
Scalable Tissue Imaging and Modeling (STIM) Laboratory
Department of Electrical and Computer Engineering
Cullen College of Engineering
University of Houston
David Mayerich
STIM Laboratory, University of Houston

Enumerating Multiple Devices
-
cudaGetDeviceCount(int* count)
returns the number of devices with compute capability \(> 1.0\) -
cudaGetDeviceProperties(struct cudaDeviceProp* p, int device)
fills a structure with information about an enumerated device
David Mayerich
STIM Laboratory, University of Houston
int nd; // stores the number of devices
cudaGetDeviceCount(&nd); // get the number of CUDA devices
for(int di=0; di<nd; di++) {
cudaDeviceProp p; // create a property structure
cudaGetDeviceProperties(&p, d); // get properties
// output the device ID, compute capability, and name
std::cout<<"("<<d<<"): "<<p.major<<"."<<p.minor<<" "<<p.name<<std::endl;
}Device Selection
-
cudaSetDevice(int d)
specifies the device that will be sent commands
David Mayerich
STIM Laboratory, University of Houston
size_t n;
float* f;
// ... // specify a value for N and fill fDevice Selection
-
cudaSetDevice(int d)
specifies the device that will be sent commands
David Mayerich
STIM Laboratory, University of Houston
size_t n;
float* f;
// ... // specify a value for N and fill f
cudaSetDevice(0); // set the current device to 0
float* gpu_f0; // create a gpu pointer
cudaMalloc(&gpu_f0, N*sizeof(float)); // allocate memory on device 0Device Selection
-
cudaSetDevice(int d)
specifies the device that will be sent commands
David Mayerich
STIM Laboratory, University of Houston
size_t n;
float* f;
// ... // specify a value for N and fill f
cudaSetDevice(0); // set the current device to 0
float* gpu_f0; // create a gpu pointer
cudaMalloc(&gpu_f0, N*sizeof(float)); // allocate memory on device 0
// copy data from the host to device 0
cudaMemcpy(gpu_f0, f, N*sizeof(float), cudaMemcpyHostToDevice);Device Selection
-
cudaSetDevice(int d)
specifies the device that will be sent commands
David Mayerich
STIM Laboratory, University of Houston
size_t n;
float* f;
// ... // specify a value for N and fill f
cudaSetDevice(0); // set the current device to 0
float* gpu_f0; // create a gpu pointer
cudaMalloc(&gpu_f0, N*sizeof(float)); // allocate memory on device 0
// copy data from the host to device 0
cudaMemcpy(gpu_f0, f, N*sizeof(float), cudaMemcpyHostToDevice);
cudaSetDevice(1); // set the current device to 1
float* gpu_f1; // create a pointerDevice Selection
-
cudaSetDevice(int d)
specifies the device that will be sent commands
David Mayerich
STIM Laboratory, University of Houston
size_t n;
float* f;
// ... // specify a value for N and fill f
cudaSetDevice(0); // set the current device to 0
float* gpu_f0; // create a gpu pointer
cudaMalloc(&gpu_f0, N*sizeof(float)); // allocate memory on device 0
// copy data from the host to device 0
cudaMemcpy(gpu_f0, f, N*sizeof(float), cudaMemcpyHostToDevice);
cudaSetDevice(1); // set the current device to 1
float* gpu_f1; // create a pointer
cudaMalloc(&gpu_f1, N*sizeof(float)); // allocate memory on device 1
cudaMemset(gpu_f1, 0, N*sizeof(float)); // allocate memory and set it to 0Concurrency
-
CUDA specifies that the following actions can be done concurrently:
-
host computation
-
device computation (kernels)
-
memory copies (host-to-device and device-to-host)
-
memory copies on a device
-
memory copies between devices
-
-
Available concurrency can be queried using
cudaGetDeviceProperties() -
Concurrency with the host is the default behavior for kernels
-
CUDA kernel calls return execution to the host thread
-
control is handled asynchronously through the CUDA library
-
multiple kernels sent to the GPU will run concurrently with the host thread that sends them
-
David Mayerich
STIM Laboratory, University of Houston
CUDA Streams
-
A stream is a series of CUDA commands executed in serial
-
Concurrent operations are placed in different streams
-
CUDA streams are executed concurrently
-
The order of execution between streams is not defined
-
kernel
0Awill execute before kernel0B -
0Amay or may not execute before1C(which will execute before1D)
-
David Mayerich
STIM Laboratory, University of Houston
host > device
kernel 0A
host > device
kernel 0B
device > host
stream 0
host > device
kernel 1D
device > host
memset 0
kernel 1C
stream 1
Creating and Destroying CUDA Streams
-
Data structure used to store a stream:
cudaStream_t -
Create a stream:
cudaStreamCreate( cudaStream_t* pStream ) -
Destroy a stream:
cudaStreamDestroy( cudaStream_t pStream )
-
Streams can be synchronized with the CPU:
cudaStreamSynchronize( cudaStream_t pStream ) -
A stream can be specified at kernel launch in the configuration:
myKernel <<< blocks, threads, share_bytes, stream_id >>> (/*...*/)
David Mayerich
STIM Laboratory, University of Houston
Asynchronous Capability
-
asyncEngineCountproperty specifies the number of memory copies that can be performed concurrently while computing-
asyncEngineCount == 0can't copy while executing a kernel -
asyncEngineCount == 1can copy to/from memory while executing -
asyncEngineCount == 2can copy to and from memory while executing
-
-
Concurrent copies with the host
-
memory copies to/from the host aren't concurrent with the host thread by default
-
cudaMemcpy()includes a synchronization barrier - host code doesn't continue until the copy is resolved -
the host program can change memory
-
the host memory can be paged (may not actually be in memory)
-
David Mayerich
STIM Laboratory, University of Houston
Concurrent Memory Copies
-
The operating system optimizes perofrmance using paging, which copies unused data from main memory to secondary storage (ex. SSD)
-
Concurrent copies to/from the host are possible
-
the host memory used in the copy must be page locked
-
the user should not change values in memory while the copy occurs
-
-
Page locked memory can be allocated through the CUDA API
David Mayerich
STIM Laboratory, University of Houston
size_t n = 10000;
float* c_alloc = (float*) malloc(n * sizeof(float)); // allocation in C
float* cpp_alloc = new float[n]; // allocation in C++
// CUDA API allocation
float* hostPtr;
cudaMallocHost( &hostPtr, n * sizeof( float ));Concurrent Memory Copies
-
Copy data asynchronously between host and device memory:
David Mayerich
STIM Laboratory, University of Houston
cudaMemcpyAsync( void* dst,
void* src,
size_t bytes,
cudaMemcpyKind kind,
cudaStream_t stream);-
Make sure:
-
host memory isn't being paged
-
device memory isn't being written to
-
Concurrent Memory Copies
David Mayerich
STIM Laboratory, University of Houston
size_t n = 10000;
const size_t ns = 2;
float* ptr[ns];
float* gpu_ptr[ns];
cudaStream_t streams[ns];Concurrent Memory Copies
David Mayerich
STIM Laboratory, University of Houston
size_t n = 10000;
const size_t ns = 2;
float* ptr[ns];
float* gpu_ptr[ns];
cudaStream_t streams[ns];
for(int si=0; si<ns; si++) {
cudaStreamCreate(&streams[si]);
cudaMallocHost( &ptr[si], n * sizeof(float));
cudaMalloc( &gpu_ptr[si], n * sizeof(float));
}
/* ... */Concurrent Memory Copies
David Mayerich
STIM Laboratory, University of Houston
size_t n = 10000;
const size_t ns = 2;
float* ptr[ns];
float* gpu_ptr[ns];
cudaStream_t streams[ns];
for(int si=0; si<ns; si++) {
cudaStreamCreate(&streams[si]);
cudaMallocHost( &ptr[si], n * sizeof(float));
cudaMalloc( &gpu_ptr[si], n * sizeof(float));
}
/* ... */
for(int i=0; i<ns; i++) {
cudaMemcpyAsync( gpu_ptr[i], ptr[i], n * sizeof(float),
cudaMemcpyHostToDevice, streams[i]);
kernel<<<blocks, threads, 0, streams[i]>>>(gpu_ptr[i], n);
cudaMemcpyAsync(ptr[i], gpu_ptr[i], n*sizeof(float),
cudaMemcpyDeviceToHost, streams[i]);
}Concurrent Memory Copies
David Mayerich
STIM Laboratory, University of Houston
asyncEngineCount == 0
host > device
kernel 0A
device > host
host > device
kernel 1A
device > host
host > device
kernel 0A
device > host
stream 0
host > device
kernel 1A
device > host
stream 1
asyncEngineCount == 1
host > device
kernel 0A
device > host
host > device
kernel 1A
device > host
Synchronization and Clean-Up
-
Use cudaStreamDestroy() to destroy the CUDA streams
-
applies a synchronization barrier:
cudaStreamDestroy()doesn't return until all streams have finished
-
-
Explicit synchronization
-
wait for all streams on the device to finish
cudaDeviceSynchronize() -
wait for a stream to finish
cudaStreamSynchronize(cudaStream_t s) -
wait for an event
cudaStreamWaitEvent( cudaStream_t s, cudaEvent_t e, 0 )
-
David Mayerich
STIM Laboratory, University of Houston
for( int si = 0; si < ns; si++) {
cudaStreamDestroy( streams[si] );
}Synchronization Across Streams
Calculate \(C[i] = e^{A[i]} + \sqrt{B[i]}\) where \(i=[0, N)\)
David Mayerich
STIM Laboratory, University of Houston
How do we guarantee that kernel 2 runs after kernel 0 is completed?
kernel 0
exp(A)
host B ->
device B
host A ->
device A
kernel 1
sqrt(B)
kernel 2
A + B
device B ->
host C
stream 0
stream 1
kernel 0
exp(A)
host A ->
device A
e 0
stream 0
cudaEventCreate(&e0);
cudaEventRecord(e0);host B ->
device B
kernel 2
A + B
device B ->
host C
stream 1
kernel 1
sqrt(B)
Peer-to-Peer Memory Access
-
Using a pointer to memory on one device to access data from another device:
-
cudaDeviceCanAccessPeer(int* canAccessPeer, int dev, int peer) -
canAccessPeer == 1if device peer can access memory on device dev
-
-
Enable peer-to-peer memory access
-
cudaDeviceEnablePeerAccess(int device, unsigned int flags)
(flags should be0for now) -
creates a unified address space for both devices
-
David Mayerich
STIM Laboratory, University of Houston
Peer-to-Peer Memory Access
David Mayerich
STIM Laboratory, University of Houston
cudaSetDevice(0); // set the current device to 0
float f0; // create a pointer for device 0
size_t n = 1024; // size of an array
cudaMalloc(&f0, n * sizeof(float)); // allocate memory on device 0Peer-to-Peer Memory Access
David Mayerich
STIM Laboratory, University of Houston
cudaSetDevice(0); // set the current device to 0
float f0; // create a pointer for device 0
size_t n = 1024; // size of an array
cudaMalloc(&f0, n * sizeof(float)); // allocate memory on device 0
kernelA<<<blocks, threads>>>(f0); // launch a kernel on 0Peer-to-Peer Memory Access
David Mayerich
STIM Laboratory, University of Houston
cudaSetDevice(0); // set the current device to 0
float f0; // create a pointer for device 0
size_t n = 1024; // size of an array
cudaMalloc(&f0, n * sizeof(float)); // allocate memory on device 0
kernelA<<<blocks, threads>>>(f0); // launch a kernel on 0
// launch a kernel on the second device if possible
int p2p;
cudaDeviceCanAccessPeer(&p2p, 1, 0); // can device 1 access memory on 0?Peer-to-Peer Memory Access
David Mayerich
STIM Laboratory, University of Houston
cudaSetDevice(0); // set the current device to 0
float f0; // create a pointer for device 0
size_t n = 1024; // size of an array
cudaMalloc(&f0, n * sizeof(float)); // allocate memory on device 0
kernelA<<<blocks, threads>>>(f0); // launch a kernel on 0
// launch a kernel on the second device if possible
int p2p;
cudaDeviceCanAccessPeer(&p2p, 1, 0); // can device 1 access memory on 0?
if(p2p) {
cudaSetDevice(1); // activate device 1
cudaDeviceEnablePeerAccess(0, 0); // enable peer access
kernelB<<<blocks, threads>>>(f0); // thread on 1 can access memory on 0
}Unified Virtual Address Spaces
-
Allows a single address space to be used for all host and device pointers
-
Virtual pointers can be acquired for any memory allocated using the API
-
Device information is available through the device pointer:
-
cudaPointerGetAttributes(struct cudaPtrAttributes* att, void* ptr) -
fills an attribute structure describing pointer ptr
-
David Mayerich
STIM Laboratory, University of Houston
struct cudaPointerAttributes {
int device; // device where memory is allocated
void* devicePointer; // pointer used to access memory on this device
void* hostPointer; // pointer to use if accessing memory on a host
cudaMemoryType memoryType; // cudaMemoryTypeHost or cudaMemoryTypeDevice
}E.7 Heterogeneous Programming
By STIM Laboratory
E.7 Heterogeneous Programming
- 118