Commit 90a9f070 authored by mohamadbashar.disoki's avatar mohamadbashar.disoki

Update OpenMP

parent 4104e57c
File deleted
#include "omp.h"
#include "stdlib.h"
#include "stdio.h"
void simpleParalleloop(int n, float *a, float *b, float *res)
{
int index;
#pragma omp parallel for num_threads(4)
for (index = 1; index < n; index++) /* index is private by default */
{
res[index] = (a[index] + b[index]);
}
}
void main(int count, char *arg[])
{
int n = count > 1 ? atoi(arg[1]) : 10;
float a[n], b[n], res[n];
//set array a values
for (int i = 0; i < n; i++)
{
a[i] = i * i;
b[i] = i;
}
simpleParalleloop(n, a, b, res);
printf("\nThe Result Is: \r\n");
for (int i = 0; i < n; i++)
printf("%f, ", res[i]);
printf("\n");
}
\ No newline at end of file
...@@ -6,24 +6,18 @@ ...@@ -6,24 +6,18 @@
void start() void start()
{ {
int x = 2; int x = 2;
#pragma omp parallel num_threads(20) #pragma omp parallel num_threads(6)
{ {
int threadId = omp_get_thread_num(); int threadId = omp_get_thread_num();
if (threadId == 5) if (threadId == 0)
x = 5; x = 5;
else else
printf("I am Thread %d and for me x=%d \n", threadId, x); printf("I am Thread %d and for me x=%d \n", threadId, x);
#pragma omp barrier
printf("After barrier => I'am thread %d and x=%d\n",threadId,x);
} }
} }
void main(int count, char *arg[]) void main(int count, char *arg[])
{ {
start(); start();
} }
\ No newline at end of file
...@@ -5,6 +5,10 @@ ...@@ -5,6 +5,10 @@
//simple example to demonstrate race condition in parallel programs and barrier //simple example to demonstrate race condition in parallel programs and barrier
void start() void start()
{ {
#pragma omp parallel num_threads(12)
{
int threadId = omp_get_thread_num();
int i=0, length = 10; int i=0, length = 10;
int array[length]; int array[length];
for(i = 0; i < length; i++) for(i = 0; i < length; i++)
...@@ -18,11 +22,17 @@ void start() ...@@ -18,11 +22,17 @@ void start()
sum += array[index]; sum += array[index];
//calculate average //calculate average
avg = sum/(double)length; #pragma omp barrier
#pragma omp single
{
avg = sum/(double)length;
printf("Average is avg = %f\n", avg); printf("Average is avg = %f\n", avg);
} }
}
}
void main(int count, char *arg[]) void main(int count, char *arg[])
{ {
start(); start();
} }
\ No newline at end of file
#include <stdio.h>
#include <omp.h>
//simple example to demonstrate race condition in parallel programs and barrier
void start()
{
int x = 2;
#pragma omp parallel num_threads(6)
{
int threadId = omp_get_thread_num();
if (threadId == 0)
x = 5;
else
printf("I am Thread %d and for me x=%d \n", threadId, x);
#pragma omp barrier
{
#pragma ordered
printf("After Barrier => I am Thread %d and for me x=%d \n", threadId, x);
#pragma ordered
printf("After Order => I am Thread %d and for me x=%d \n", threadId, x);
}
}
}
void main(int count, char *arg[])
{
start();
}
\ No newline at end of file
...@@ -29,11 +29,11 @@ void start() ...@@ -29,11 +29,11 @@ void start()
} }
omp_destroy_lock(&lock);
} }
} }
void main(int count, char *arg[]) void main(int count, char *arg[])
{ {
start(); start();
} }
\ No newline at end of file
#include <omp.h>
#include <stdio.h>
void main(int count, char *arg[])
{
int flag = 0;
#pragma omp parallel num_threads(3)
{
int threadId = omp_get_thread_num();
if (threadId == 0)
{
/* Set flag to release thread 1 */
#pragma omp atomic update
flag++;
/* Flush of flag is implied by the atomic directive */
}
else if (threadId == 1)
{
/* Loop until we see that flag reaches 1*/
#pragma omp flush(flag)
while (flag < 1)
{
#pragma omp flush(flag)
}
printf("Thread 1 awoken\n");
/* Set flag to release thread 2 */
#pragma omp atomic update
flag++;
/* Flush of flag is implied by the atomic directive */
}
else if (threadId == 2)
{
/* Loop until we see that flag reaches 2 */
#pragma omp flush(flag)
while (flag < 2)
{
#pragma omp flush(flag)
}
printf("Thread 2 awoken\n");
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment