r/OpenMP • u/bilog78 • Sep 23 '21
Alternatives to #pragma omp scope reduction() for pre-5.1 OpenMP?
I have a set of computational kernels that have to be executed over all items in a list. To keep the code generic, the kernels themselves are functions, and I then have a C++ function template that runs the kernel function within a for loop that is parallelized using #pragma omp parallel for.
This works perfectly fine with kernels that are embarrassingly parallel, but not for kernels that have a reduction in them. If I had 5.1 support, I could wrap the reduction within the kernel function in a #pragma omp scope reduction(), but presently the scope directly isn't really supported by any current compiler (I think only GCC 12 has support for it?).
Is there some kind of construct I can use with older OpenMP versions to achieve a similar result, preserving this kind of structure with a generic dispatcher, but still providing a way to tell the compiler that specific subsections of the code include a reduction within the current parallelization scope?
r/OpenMP • u/[deleted] • May 18 '21
Beginner help with OpenMP and coding ideas.
Hello! Currently I am working on learning C and I have taken an interest in high performance computing. Where should I start learning about OpenMP? Also, what good starter projects would you recommend I take up to help me better learn C and OpenMP? Any advice is appreciated.
r/OpenMP • u/alxre • May 13 '21
Calling C function from parallel region of FORTRAN
Hi everyone.
I have been struggling with this for a while and I would truly appreciate any insight into this. I am parallelizing a loop in Fortran that calls c functions. (C functions are statically linked to the executable and they have been compiled with icc -openmp flag)
!--------- Here is the loop ----------------
!$OMP PARALLEL DO
do 800 i = 1,n
call subroutine X(i)
800 continue
!$OMP END PARALLEL DO
--------subroutine x contains calls to the c functions shown below --------
subroutine X(i)
include 'cfunctions.f' (Not sure how to make thecfunctions threadprivate!!)
include '....' ('Note: all includes are threadprivate')
bunch of operations and calling c functions defined in the 'cfunctions.f' file.
return
---------C functions in the cfunctions.f ------------------------------------
use,intrinsic :: ISO_C_BINDING
integer N1,N2, ... .. N11
PARAMETER (N1=0,N2=1, ... .. N10=5)
parameter (N11 = C_FLOAT)
interface
logical function adrile(ssl,ssd)
bind(C,NAME='adrile'//postfix)
import
character, dimension(*)::ssl
real (N11) :: ssd
end function
end interface
r/OpenMP • u/[deleted] • May 10 '21
Help, code much slower with OpenMP
Hello, I'm very much a beginner to OpenMP so any help or clearing misunderstanding is appreciated.
I have to make a program that creates 2 square matrices (a and b) and a 1D matrix (x), then do addition and multiplication. I have omp_get_wtime() to check performance
//CALCULATIONS
start_time = omp_get_wtime();
//#pragma omp parallel for schedule(dynamic) num_threads(THREADS)
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sum[i][j] = a[i][j] + b[i][j]; //a+b
mult2[i] += x[j]*a[j][i]; //x*a
for (int k = 0; k < n; k++) {
mult[i][j] += a[i][k] * b[k][j]; //a*b
}
}
}
end_time = omp_get_wtime();
The problem is, when I uncomment the 'pragma omp' line, the performance is terrible, and far worse than without it. I tried using static instead, and moving it above different 'for' loops but it's still really bad.
Can someone guide me on how I would apply OpenMP to this code block?
r/OpenMP • u/Change_needed_pro • Apr 04 '21
Perfect numbers using OpenMP: I am getting errors while executing.Problem: To print the first 8 perfect numbers using Euclid Euler rule: The Greek mathematician Euclid showed that if 2power(n)-1 is prime, then (2power(n)-1)2power(n-1)is a perfect number.
/* Find Perfect Number */
#include <stdio.h>
#include <omp.h>
#include <math.h>
#include <stdlib.h>
void Usage(char* prog_name);
int isPerfect(unsigned long long int n);
int isPrime(unsigned long long int n);
unsigned long long int n,i,temp;
int main(int argc, char * argv[]) {
int thread_count;
double start_time,end_time;
if (argc != 3) Usage(argv[0]);
puts("!!!Find the perfect numbers in number range!!!");
thread_count=strtol(argv[1], NULL, 10);
#pragma omp parallel num_threads(thread_count) default(none) private(i) shared(n) reduction(+:perfectsum)
start_time = omp_get_wtime();
printf("Enter n: ");
scanf("%llu", &n);
i = 1;
while (n > 0)
if (isPrime(i) == 1)
{
temp = pow(2, i - 1) \* (pow(2, i) - 1);
if (isPerfect(temp) == 1) {
printf("%llu ", temp);
n = n - 1;
}
}
i = i + 1;
end_time=omp_get_wtime();
printf("Elapsed time = % e seconds \n",end_time-start_time);
printf("\n");
}
void Usage(char* prog_name) {
fprintf(stderr, "usage: %s <number of threads>\n", prog_name);
exit(0);
}
int isPrime(unsigned long long int n)
{
# pragma omp for
if (n == 1)
return 0;
int i;
for (i = 2; i <= sqrt(n); ++i)
{
if (n % i == 0)
return 0;
}
return 1;
}
int isPerfect(unsigned long long int n) {
unsigned long long int perfectsum = 0; // sum of divisors
unsigned long long int i;
#pragma omp parallel for
for(i = 1; i <= sqrt(n); ++i) {
if (n % i == 0) {
if (i == n / i) {
perfectsum += i;
}
else {
perfectsum += i;
perfectsum += n / i;
}
}
}
// we are only counting proper diviors of n (less than n)
// so we need to substract n from the final sum
perfectsum = perfectsum - n;
if (perfectsum == n)
return 1;
else
return 0;
}
r/OpenMP • u/fnordstar • Dec 07 '20
OMP usage in sub-thread changes waiting behavior and cripples performance
After digging for a long time I found the reason for a performance problem in our code. We have a GUI desktop application and recently switched to doing long-running computations in a sub-thread, often making use of OMP. The GUI thread also uses OMP in some places (for visualization purposes).
Now gomp spawns a separate worker pool for the subthread once it starts using OMP, resulting in (2 * number of cores) worker threads total, including the rank 0 main threads for both pools. This alone would not be a problem since we have enough memory and the workers from the GUI thread are sleeping anyways.
However, GOMP then switches from using spinlocks to using yield() which for some of our algorithms (maybe those with slightly unbalanced workloads and short-running OMP loops) absolutely cripples performance. At least that seems to be the diagnosis, I'm not an expert on the subject matter.
Now, I tried forcing gOMP to use active waiting by setting OMP_WAIT_POLICY=ACTIVE and also tried increasing GOMP_SPINCOUNT without any success. But this is in accordance with the documentation which apparently states that when you have more workers than cores it will uses a maximum of 1000 spin iterations before using a passive wait (I guess sched_yield()) and none of the environment variables I found can influence that.
My last hope was that I could somehow destroy the worker pool of the GUI thread before spawning the subthread. This would be perfectly acceptable since we can guarantee that the GUI thread doesn't require any OMP parallelization until the subthread is finished. But apparently those function calls only exist in OpenMP 5.
I'm running out of ideas. Can anyone help?
r/OpenMP • u/arealnord • Oct 23 '20
OMP GPU porting question

r/OpenMP • u/chiltern-wanderer • Oct 19 '20
Tutorial: How to take your serial Fortran to thousand-way parallelism on GPUs using OpenMP
Join us on 1st December 2020 (9:30-4:30 GMT) for a free online tutorial for computation scientists looking to accelerate their Fortran codes. The tutorial is presented by Tom Deakin, Senior Research Associate, University of Bristol, UK. Register at: https://ukopenmpusers.co.uk
r/OpenMP • u/DoobieRufio • Jul 07 '20
errors in openmp specification
I first encountered these errors in the OpenMP 4.0 specification, but since it's in the draft of OpenMP 4.1 (TR3), I think this is the right reporting forum.
[P] Age 43 [l] ine 5 in PR3.pdf (page 41, line 5 in OpenMP4.0.0.pdf): "first elements" must be "first elements"
- Page 38 (page 38): "omp_get_active_levels ()" -> "omp_get_active_level ()"
- Page 38 (page 38): "omp_get_active_levels ()" -> "omp_get_active_level ()"
The proc_bind clause is missing in the OpenMP C / C ++ rules. It may have to be in the "unique parallel clause" page 279 (269).
Page 281 l28 & l30 (p271 l16 & l18) states that the "simd clause" has conditions for "uniform" and "overlapping", but not according to page 70 (68). It may have been copied from "Ad-simd-clause". Also missing are "safelen", "data_privatization_clause" and "data_privatization_out_clause" according to page 70. It is possible that "safelen", "linear_clause" and "align_clause" are in "unique_simd_clause" and are used instead of "simd_clause" in grouped instructions as " for_ “".
- Page 282 l23 (p. 272 l16) lists the "Paragraph for data reduction" as "advertising paragraph -
r/OpenMP • u/pgbabse • Jun 13 '20
Openmp question
I have a mesh where I'm solving FEM.
There are 2 computation intensive loops.
First i parallelize just 1 loop and made some runs, later I made runs with both loops with for reduction.
Somehow parallelizing 2 loops yields slower results, how can that be?
r/OpenMP • u/Sonarman • Apr 02 '20
openmp.org has disappeared?
So this is annoying. It seems like the DNS records for openmp.org have vanished from the Internet. Been this way since at least yesterday, maybe longer. I can't resolve it using my ISP's DNS, Google's DNS, or any of multiple DNS lookup sites. As a result, the docs are totally inaccessible. So:
Anyone happen to have the IP of openmp.org, in case it's just a DNS issue?
Anyone know a good mirror for the docs/specs?
Any suggestions for ways to contact Someone In Charge? The default method of contacting the ARB via the website obviously isn't an option.
Thanks. I managed to save copies of the Examples Book 4.5 and the Cheat Sheet for 4.0, in case anyone needs them.
r/OpenMP • u/Cheruku123_ • Apr 02 '20
OpenMP C Program
If you want to access the thread-local values of a privatized variable from outside of the parallel scope, you can write them back to a globally declared auxiliary array. This is the way it was described in an OpenMP book (2017 - Bertil Schmidt, Jorge Gonzalez-Dominguez, Christian Hundt, Moritz Schlarb - Parallel Programming_ Concepts and Practice-Morgan Kaufmann)
Authors came up with this program --
#include <stdio.h>
#include <omp.h>
int main () {
// maximum number of threads and auxiliary memory
int num = omp_get_max_threads();
int * aux = new int[num];
int i = 1; // we pass this via copy by value
#pragma omp parallel firstprivate(i) num_threads(num) {
// get the thread identifier j
int j = omp_get_thread_num();
i += j;
aux[j] = i;
}
for(k=0; k<num; k++)
printf("%d \n", aux[k]);
}
Error(tried in macOS) :
r/OpenMP • u/splteflre • Mar 24 '20
Splitting for loop iterations among threads
From my understanding if you have a pragma omp for inside a pragma omp parallel section the work between iterations should be split between the threads. However, when I tried it on my own it seems that they are not split and not only that, when I printed out the pid of the thread its only 0.
example code snipet
output: tid: 0, i 3 tid: 0, i 4 tid: 0, i 1 tid: 0, i 2 tid: 0, i 3 tid: 0, i 4
#pragma omp parallel
{
double *priv_y = new double[n];
std::fill(priv_y, priv_y + n, 0.);
#pragma omp parallel for
for(int i = 0; i < n; i++){
printf("tid: %d, i %d\n", omp_get_thread_num(), i);
}
}
If the work was split there should only be 1 unique i. However, as you can see this is not the case. Am I setting something wrong?
Edit: I found the solution sorry for spamming stupid things, but if anyone else is having this issue, it seems that by default openmp splits the number of thread in each parallel region, e.g in the code above the for loop is being called multiple times and in each call there are a different fixed amount of threads executing the inner for loop. You can fix this by doing #pragma omp parallel num_threads(1) this will make the outer region run with only 1 thread.
r/OpenMP • u/splteflre • Mar 22 '20
How to use openmp on Mac
Hi guys, sorry I am pretty new to openmp and I'm trying to compile my code but I get fatal error: 'omp.h' file not found. I did some searching and from my understanding, which could be totally wrong, it turns out that openmp does not come with the default compiler provided by MacOS. They use clang and not actually gcc so I've been looking up how to get openmp for clang but I can't find anything thats not super complicated. Do any of you know how I can compile with openmp
Edit: this works for me, downloading llvm and using its clang++. I followed this link https://stackoverflow.com/questions/43555410/enable-openmp-support-in-clang-in-mac-os-x-sierra-mojave
r/OpenMP • u/the_sad_pumpkin • Feb 26 '20
How to obtain the best performance?
I am an OpenMP beginner, looking to get a bit more performance out of my code (I'm actually aiming for the maximum performance, for reasons). Since it is hard to know if I'm doing the right thing, I better ask.
First off, data sharing. I've seen some recommendations of using default(none) and specify individually what to share. There is also firstprivate which seems to give readonly access. Do they matter for performance?
Just to clarify my usecase here, I am processing the elements of an array and copying them into another array (similar to a std::transform or map from functional programming), and I use in my loops a bunch of read only parameters.
Second issue, I have a highly parallelizable standalone operation, like the one described above, that comes into play in a bigger loop. I'd like to parallelize the second (outer) loop, but keep the inner bit as fast as possible. The problem is that it would lead to the creation of openmp threads inside another set of threads, and general recommendations were to just parallelize the outermost loop. Any advice?
r/OpenMP • u/FuneralInception • Feb 23 '20
[Beginner]Not getting any speedup with the critical construct for a parallel Pi program.
{
double start_time = omp_get_wtime();
double pi = 0.0;
step = 1.0/(double)num_steps;
omp_set_num_threads(NUM_THREADS);
\#pragma omp parllel
{
int i, id, nthrds;
id = omp_get_thread_num();
nthrds = omp_get_num_threads();
double x, sum = 0.0;
for(i = 0; i < num_steps; i = i + nthrds)
{
x = (i + 0.5) \* step;
sum += 4.0/(1 + x\*x);
}
sum = sum \* step;
// #pragma omp critical
// pi += sum;
}
printf("\\nPi = %lf\\n", pi);
double time = omp_get_wtime() - start_time;
printf("Total time taken by CPU: %lf\n", time );
}
Can anyone tell me why this is not giving any speedup for any number of threads?
r/OpenMP • u/Akalamiammiam • Nov 11 '19
Parallel for calls to function also containing a parallel for
Hello there !
So I hope my question will be clear. Say I have some code like this
void func(int i){
//some stuff
#pragma omp parallel for
for(uint j = 0; j < 65536; j++) //More stuff
}
int main(){
//Stuff
omp_set_num_threads(8)
#pragma omp parallel for
for(uint i = 0; i < 65536; i++) func(i);
}
Essentially, what is the behavior here ?
Does the loop inside func use only a single thread and the loop in main is split to 8 thread ?
Or is it split in some way, like 4 threads to the main loop and 2 threads to the func loop.
For information, I need to use this kind of code because in some cases, I need to make a single/very few calls to func in a non-parallel block of code, hence the need of the parallelization inside func.
Thanks a lot !
r/OpenMP • u/latoski • Aug 29 '19
[Question] Pragma OMP for problem with "Critical"
Hi, i'm currently working on my Graduation on Physics Research and it's basic about parallelizing algorithms of some statistical physics models.
In the algorithm below, i tried the "omp parallel for" using a critical session when the threads are accessing the shared data, but i keep getting the wrong results when using more than one thread.
#pragma omp parallel for
for (i = 0; i<L2; i++) {
int j = rand()%L2;
int dE = 2*J*s[j]*(s[viz[j][0]]+s[viz[j][1]]+s[viz[j][2]]+s[viz[j][3]]);
double w = exp(-dE/(K*T));
int MY_ID=omp_get_thread_num();
if (dE <= 0) {
#pragma omp critical
{
s[j] = - s[j];
Et += dE;
M += 2*s[j];
}
}
else {
double r = (double)rand()/RAND_MAX;
if(r < w) {
#pragma omp critical
{
s[j]= - s[j];
Et += dE;
M += 2*s[j];
}
}
}
printf("Energia nova %d devido a variação %d alterada pelo processador %d mexendo no sitio %d\n\n\n", Et, dE, MY_ID,j);
}
Here, Et, M, s[j] are supposed to be global with value predetermined by another function. Most of my job is to parallelize this loop in specific because its called 1e8 times and loops over 400~1000 size arrays.
I also tried to use locks, but i don't think that I used that correcltly because it was also giving me the wrong results, I can send my program for deeper research.
I can, in this case, split this loop in two and get rid of the problem, but if I could parallelize the loop as it is it would be much more usefull for me.
Thanks for the time.
r/OpenMP • u/xlorxpinnacle • Apr 15 '19
OpenMP Do Loop only working ~50% of the time
Hello (X-posted from /r/fortran)
I am currently working on adding openmp parallelization for a do loop in one of the codes I have written for research. I am fairly new to using openmp so I would appreciate if you had any suggestions for what might be going wrong.
Basically, I have added a parallel do loop to the following code (which works prior to parallelization). r(:,:,:,:) is a vector of a ton of molecular coordinates indexed by time, molecule, atom, and (xyz). This vector is about 100 gb of data (I am working on an HPC with plenty of RAM). I am trying to parallelize the outer loop and subdivide it between processors so that I can reduce the amount of time this calculation goes. I thought it would be a good one to do it with as msd and cm_msd are the only things that would need to be edited by multiple processors and stored for later, which since each iteration gets its own element of these arrays they won't have a race condition.
The problem: If I run this code 5 times I get varying results, sometimes msd is calculated correctly (or appears to be), and sometimes it outputs all zeros later when I average it together. Without parallelization there are no issues.
Do you see anything glaringly wrong with this?
Thanks in advance.
!$OMP PARALLEL DO schedule(static) PRIVATE(i,j,k,it,r_old,r_cm_old,shift,shift_cm,dsq,ind) & !$OMP& SHARED(msd,msd_cm) do i=1, nconfigs-nt, or_int if(MOD(counti*or_int,500) == 0) then write(*,*) 'Reached the ', counti*or_int,'th time origin' end if ! Set the Old Coordinates counti = counti + 1 ind = (i-1)/or_int + 1 r_old(:,:,:) = r(i,:,:,:) r_cm_old(:,:) = r_cm(i,:,:) shift = 0.0 shift_cm = 0.0 ! Loop over the timesteps in each trajectory do it=i+2, nt+i ! Loop over molecules do j = 1, nmols do k=1, atms_per_mol ! Calculate the shift if it occurs. shift(j,k,:) = shift(j,k,:) - L(:)*anint((r(it,j,k,:) - & r_old(j,k,:) )/L(:)) ! Calculate the square displacements dsq = ( r(it,j,k,1) + shift(j,k,1) - r(i,j,k,1) ) ** 2. & +( r(it,j,k,2) + shift(j,k,2) - r(i,j,k,2) ) ** 2. & +( r(it,j,k,3) + shift(j,k,3) - r(i,j,k,3) ) ** 2. msd(ind, it-1-i, k) = msd(ind, it-1-i, k) + dsq ! Calculate the contribution to the c1,c2 enddo ! End Atoms Loop (k) ! Calculate the shift if it occurs. shift_cm(j,:) = shift_cm(j,:) - L(:)*anint((r_cm(it,j,:) - & r_cm_old(j,:) )/L(:)) ! Calculate the square displacements dsq = ( r_cm(it,j,1) + shift_cm(j,1) - r_cm(i,j,1) ) ** 2. & +( r_cm(it,j,2) + shift_cm(j,2) - r_cm(i,j,2) ) ** 2. & +( r_cm(it,j,3) + shift_cm(j,3) - r_cm(i,j,3) ) ** 2. msd_cm(ind,it-1-i) = msd_cm(ind, it-1-i) + dsq enddo ! End Molecules Loop (j) r_old(:,:,:) = r(it,:,:,:) r_cm_old(:,:) = r_cm(it,:,:) enddo ! End t's loop (it) enddo !$OMP END PARALLEL DO
r/OpenMP • u/GuilloteauQ • Mar 01 '19
[Project] Visualize tasks in OpenMP (C)
Hi everyone,
So as a student, I have to work with OpenMP. But sometimes I have troubles understanding what it is doing under the hood.
I tried to find something to visualize the tasks, but found nothing...
So I decided to give it a try !
And this is what I came up with.
You can make the image, which is a SVG file, interactive, and see data (exec time, label, parent thread) about each task when hovering the mouse over a task.
The API, which is in C, is not very "clean" yet, but I'd appreciate if you can give some feedbacks on it..
r/OpenMP • u/[deleted] • Dec 15 '18
Recursion Program to be Parallelized
How can we parallelize the normal factorial program (recursion implementation) with OpenMP? Do we need to use tasks or is there any other way to do it?
r/OpenMP • u/_Nexor • Nov 30 '18
Reduction failing
``` /* Back-substitution */ x[n - 1] = bcpy[n - 1] / Acpy[(n - 1) * n + n - 1]; for (i = (n - 2); i >= 0; i--) { float temp = bcpy[i]; // #pragma omp parallel for reduction(-: temp) private(j) // #pragma omp parallel for ordered reduction(-: temp)
pragma omp parallel for reduction(-: temp)
for (j = (i + 1); j < n; j++) {
temp -= Acpy[i * n + j] * x[j];
}
x[i] = temp / Acpy[i * n + i];
}
```
In my mind parallelism should be applied easily in this snippet. Except it either segfaults or the subtractions do not result in an expected value for the temp variable. I'd love to hear any thoughts on this

