Changes

Jump to: navigation, search

GPU621/GPU Targeters

2 bytes added, 18:26, 3 December 2020
no edit summary
https://stackoverflow.com/questions/7263193/opencl-vs-openmp-performance#7263823
== Programming GPUs with OpenMP ==
<h3>Target Region</h3>
* The target region is the offloading construct in OpenMP.
<pre>int main() {
// This code executes on the host (CPU)
 
#pragma omp target
// This code executes on the device
 
}
</pre>
 
* An OpenMP program will begin executing on the host (CPU).
* When a target region is encountered the code that is within the target region will begin to execute on a device (GPU).
 
If no other construct is specified, for instance a construct to enable a parallelized region (''#pragma omp parallel'').
By default, the code within the target region will execute sequentially. The target region does not express parallelism, it only expresses where the contained code is going to be executed on.
 
There is an implied synchronization between the host and the device at the end of a target region. At the end of a target region the host thread waits for the target region to finish execution and continues executing the next statements.
 
<h3>Mapping host and device data</h3>
 
* In order to access data inside the target region it must be mapped to the device.
* The host environment and device environment have separate memory.
* Data that has been mapped to the device from the host cannot access that data until the target region (Device) has completed its execution.
 
The map clause provides the ability to control a variable over a target region.
 
''#pragma omp target map(map-type : list)''
 
* ''list'' specifies the data variables to be mapped from the host data environment to the target's device environment.
 
* ''map-type'' is one of the types '''to''', '''from''', '''tofrom''', or '''alloc'''.
 
'''to''' - copies the data to the device on execution.
 
'''from''' - copies the data to the host on exit.
 
'''tofrom''' - copies the data to the device on execution and back on exit.
 
'''alloc''' - allocated an uninitialized copy on the device (without copying from the host environment).
 
<pre>
// Offloading to the target device, but still without parallelism.
#pragma omp target map(to:A,B), map(tofrom:sum)
{
for (int i = 0; i < N; i++)
sum += A[i] + B[i];
}
</pre>
 
<h3>Dynamically allocated data</h3>
If we have dynamically allocated data in the host region that we'd like to map to the target region. Then in the map clause we'll need to specify the number of elements that we'd like to copy over. Otherwise all the compiler would have is a pointer to some region in memory. As it would require the size of allocated memory that needs to be mapped over to the target device.
 
<pre>
int* a = (int*)malloc(sizeof(int) * N);
#pragma omp target map(to: a[0:N]) // [start:length]
</pre>
 
<h3>Parallelism 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. [[File: Teams.JPG|thumb|upright=1.2|right|alt=OpenMP teams]]
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.
[[File: Distribute.JPG|thumb|upright=1.2|right|alt=OpenMP distribute]]
<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>
''Calling functions within the scope of a target region.''
 
* The ''declare target'' construct will compile a version of a function that can be called on the device.
* In order to offload a function onto the target's device region the function must be first declare on the target.
<pre>
#pragma omp declare target
int combine(int a, int b);
#pragma omp end declare target
 
#pragma omp target teams distribute parallel for \
map(to: A, B), map(tofrom:sum), reduction(+:sum)
for (int i = 0; i < N; i++) {
sum += combine(A[i], B[i])
}
</pre>
 
== Instructions for NVIDEA ==
https://rocmdocs.amd.com/en/latest/Programming_Guides/aomp.html
== Programming GPUs with OpenMP ==
<h3>Target Region</h3>
* The target region is the offloading construct in OpenMP.
<pre>int main() {
// This code executes on the host (CPU)
 
#pragma omp target
// This code executes on the device
 
}
</pre>
 
* An OpenMP program will begin executing on the host (CPU).
* When a target region is encountered the code that is within the target region will begin to execute on a device (GPU).
 
If no other construct is specified, for instance a construct to enable a parallelized region (''#pragma omp parallel'').
By default, the code within the target region will execute sequentially. The target region does not express parallelism, it only expresses where the contained code is going to be executed on.
 
There is an implied synchronization between the host and the device at the end of a target region. At the end of a target region the host thread waits for the target region to finish execution and continues executing the next statements.
 
<h3>Mapping host and device data</h3>
 
* In order to access data inside the target region it must be mapped to the device.
* The host environment and device environment have separate memory.
* Data that has been mapped to the device from the host cannot access that data until the target region (Device) has completed its execution.
 
The map clause provides the ability to control a variable over a target region.
 
''#pragma omp target map(map-type : list)''
 
* ''list'' specifies the data variables to be mapped from the host data environment to the target's device environment.
 
* ''map-type'' is one of the types '''to''', '''from''', '''tofrom''', or '''alloc'''.
 
'''to''' - copies the data to the device on execution.
 
'''from''' - copies the data to the host on exit.
 
'''tofrom''' - copies the data to the device on execution and back on exit.
 
'''alloc''' - allocated an uninitialized copy on the device (without copying from the host environment).
 
<pre>
// Offloading to the target device, but still without parallelism.
#pragma omp target map(to:A,B), map(tofrom:sum)
{
for (int i = 0; i < N; i++)
sum += A[i] + B[i];
}
</pre>
 
<h3>Dynamically allocated data</h3>
If we have dynamically allocated data in the host region that we'd like to map to the target region. Then in the map clause we'll need to specify the number of elements that we'd like to copy over. Otherwise all the compiler would have is a pointer to some region in memory. As it would require the size of allocated memory that needs to be mapped over to the target device.
 
<pre>
int* a = (int*)malloc(sizeof(int) * N);
#pragma omp target map(to: a[0:N]) // [start:length]
</pre>
 
<h3>Parallelism 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. [[File: Teams.JPG|thumb|upright=1.2|right|alt=OpenMP teams]]
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.
[[File: Distribute.JPG|thumb|upright=1.2|right|alt=OpenMP distribute]]
<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>
''Calling functions within the scope of a target region.''
 
* The ''declare target'' construct will compile a version of a function that can be called on the device.
* In order to offload a function onto the target's device region the function must be first declare on the target.
<pre>
#pragma omp declare target
int combine(int a, int b);
#pragma omp end declare target
 
#pragma omp target teams distribute parallel for \
map(to: A, B), map(tofrom:sum), reduction(+:sum)
for (int i = 0; i < N; i++) {
sum += combine(A[i], B[i])
}
</pre>
== Results and Graphs (Nathan/Elena) ==
51
edits

Navigation menu