Changes

Jump to: navigation, search

GPU621/To Be Announced

2,238 bytes added, 17:18, 30 November 2020
Programming GPUs with OpenMP
</pre>
<h3>Target data regionsParallelism on the GPU</h3>GPUs contain many single stream multiprocessors (SM), each of which can run multiple threads within them.
OpenMP still allows us to use the traditional OpenMP constructs inside the target region to create and use threads on a device. However a parallel region executing inside a target region will only execute on one single stream multiprocessor (SM). So parallelization will work but will only be executed on one single stream multiprocessor (SM), leaving most of the cores on the GPU idle.
 
Within a single stream multiprocessor no synchronization is possible between SMs, since GPU's are not able to support a full threading model outside of a single stream multiprocessor (SM).
 
<pre>
// This will only execute one single stream multiprocessor.
// Threads are still created but the iteration can be distributed across more SMs.
 
#pragma omp target map(to:A,B), map(tofrom:sum)
#pragma omp parallel for reduction(+:sum)
for (int i = 0; i < N; i++) {
sum += A[i] + B[i];
}
</pre>
<h3>Teams construct</h3>
In order to provide parallelization within the GPU architectures there is an additional construct known as the ''teams'' construct, which creates multiple master threads on the device.
Each master thread can spawn a team of its own threads within a parallel region. But threads from different teams cannot synchronize with other threads outside of their own team.
<pre>
int main() {
 
#pragma omp target // Offload to device
#pragma omp teams // Create teams of master threads
#pragma omp parallel // Create parallel region for each team
{
// Code to execute on GPU
}
 
}
</pre>
 
<h3> Distribute construct </h3>
The ''distribute'' construct allows us to distribute iterations. This means if we offload a parallel loop to the device, we will be able to distribute the iterations of the loop across all of the created teams, and across the threads within the teams.
 
Similar to how the ''for'' construct works, but ''distribute'' assigns the iterations to different teams (single stream multiprocessors).
<pre>
// Distributes iterations to SMs, and across threads within each SM.
 
#pragma omp target teams distribute parallel for\
map(to: A,B), map(tofrom:sum) reduction(+:sum)
for (int i = 0; i < N; i++) {
sum += A[i] + B[i];
}
</pre>
<h3>Declare Target</h3>
24
edits

Navigation menu