Skip to content

Instantly share code, notes, and snippets.

@hamidb80
Last active November 2, 2023 07:30
Show Gist options
  • Save hamidb80/8ae56af2b52a8cab5df8dbc10e2feb4f to your computer and use it in GitHub Desktop.
Save hamidb80/8ae56af2b52a8cab5df8dbc10e2feb4f to your computer and use it in GitHub Desktop.
openMP parallel for loop with shared & local variables
#include <stdio.h>
#include <omp.h>
int main(){
int partial_Sum, total_Sum;
#pragma omp parallel private(partial_Sum) shared(total_Sum)
{
partial_Sum = 0;
total_Sum = 0;
#pragma omp for
{
for(int i = 1; i <= 1000; i++){
partial_Sum += i;
}
}
//Create thread safe region.
#pragma omp critical
{
//add each threads partial sum to the total sum
total_Sum += partial_Sum;
}
}
printf(“Total Sum: %d”, total_Sum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment