Skip to content

Commit b0ecd0e

Browse files
committed
Added material for the Intro to OpenMP session
1 parent c98b216 commit b0ecd0e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1126
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ The programme specifically addresses the needs of scientists using, writing, or
4848
**Interfacing Multiple Programming Languages** [[Slides]](day2/multi-language.pdf) [[Code]](day2/multi-language)
4949

5050
**Floating Point Math and Errors** [[Slides]](day2/floating-point) [[Code]](day2/floating-point-code/)
51+
52+
#### Day 3
53+
54+
**Introduction to OpenMP** [[Slides]](day3/introduction-to-openmp.pdf) [[Code]](day3/introduction-to-openmp/)

day3/introduction-to-openmp.pdf

1.17 MB
Binary file not shown.

day3/introduction-to-openmp/OMP-hello

8.21 KB
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdio.h>
2+
#include <omp.h>
3+
int main() {
4+
5+
char hn[600];
6+
int thi=10;
7+
int thid=200;
8+
int thid2=20;
9+
int thid3=15;
10+
11+
printf("before Thread shared %d private %d firstprivate %d lastprivate %d \n", thi, thid, thid2, thid3);
12+
#pragma omp parallel shared(thi) private(thid) firstprivate(thid2)
13+
{
14+
gethostname(hn,600);
15+
printf("hello from hostname %s shared %d private %d firstprivate %d lastprivate %d \n",hn, thi, thid, thid2, thid3);
16+
thi = omp_get_thread_num();
17+
thid = omp_get_thread_num();
18+
thid2 = omp_get_thread_num();
19+
thid3 = omp_get_thread_num();
20+
printf("hello from hostname %s shared %d private %d firstprivate %d lastprivate %d \n",hn, thi, thid, thid2, thid2);
21+
}
22+
23+
printf("Outside Thread shared %d private %d firstprivate %d lastprivate %d \n", thi, thid, thid2, thid3);
24+
return(0);
25+
}
12.8 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
#include <omp.h>
3+
4+
int main() {
5+
6+
printf(" omp_get_max_threads %d", omp_get_max_threads() );
7+
printf(" omp_get_thread_limit %d", omp_get_thread_limit() );
8+
9+
char hn[600];
10+
int ID = 0;
11+
#pragma omp parallel
12+
{
13+
ID = omp_get_thread_num();
14+
gethostname(hn,600);
15+
16+
printf("hello from hostname %s Thread Number: %d\n",hn, ID);
17+
}
18+
19+
printf("Executing with 4 threads");
20+
21+
#pragma omp parallel num_threads(4)
22+
{
23+
ID = omp_get_thread_num();
24+
gethostname(hn,600);
25+
printf("\nhello from hostname %s Thread Number: %d\n",hn, ID);
26+
}
27+
28+
printf("Executing with 8 threads");
29+
omp_set_num_threads(8);
30+
#pragma omp parallel
31+
{
32+
ID = omp_get_thread_num();
33+
gethostname(hn,600);
34+
printf("\nhello from hostname %s Thread Number: %d\n",hn, ID);
35+
}
36+
37+
return(0);
38+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
5+
char hn[600];
6+
7+
#pragma omp parallel
8+
{
9+
gethostname(hn,600);
10+
printf("hello from hostname %s\n",hn);
11+
}
12+
return(0);
13+
}
8.53 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <time.h>
4+
#include <omp.h>
5+
#include <math.h>
6+
7+
int main() {
8+
9+
double *a;
10+
double *b;
11+
double randV;
12+
double lastx;
13+
int i =0;
14+
int msize = 0;
15+
16+
//msize = 987456213; //16000;
17+
msize = 1000; //16000;
18+
randV = 0;
19+
20+
srand(time(NULL));
21+
randV=rand();
22+
randV=randV;
23+
24+
a = (double*) malloc(msize * sizeof(double));
25+
for ( i = 0; i < msize; i++) {
26+
a[i] = i/randV;
27+
}
28+
29+
randV=rand();
30+
randV=randV;
31+
printf("randV %f\n", randV);
32+
33+
b = (double*) malloc(msize * sizeof(double));
34+
35+
int totthreads;
36+
37+
totthreads=omp_get_num_threads();
38+
totthreads=11;
39+
40+
int id=0;
41+
int stepPThread;
42+
int stepLastThread;
43+
int begin;
44+
int end;
45+
46+
#pragma omp parallel shared(totthreads) private(id, stepPThread, stepLastThread, begin, end)
47+
{
48+
id=omp_get_thread_num();
49+
stepPThread=msize/totthreads;
50+
stepLastThread=msize % totthreads;
51+
begin = id*stepPThread;
52+
end = (begin + stepPThread)-1;
53+
54+
int cmp = totthreads-1;
55+
56+
if (id == cmp) {
57+
end = end + stepLastThread;
58+
//printf("\n id %d cmp %d \n", id, cmp);
59+
}
60+
//else
61+
//printf("\n id %d cmp %d \n", id, cmp);
62+
63+
printf("totthreads %d id %d stepPThread %d stepLastThread %d begin %d end %d \n", totthreads, id, stepPThread, stepLastThread, begin, end);
64+
65+
for ( i = begin; i < end; i++) {
66+
b[i] = i/randV;
67+
}
68+
}
69+
70+
#pragma omp parallel
71+
{
72+
#pragma omp for
73+
for ( i = 0; i < msize; i++) {
74+
b[i] = i/randV;
75+
}
76+
}
77+
78+
#pragma omp parallel for
79+
for ( i = 0; i < msize; i++) {
80+
a[i] = a[i]+b[i];
81+
}
82+
83+
lastx = 0;
84+
for ( i = 0; i < msize; i++) {
85+
lastx = a[i]+b[i] ;
86+
}
87+
88+
printf("lastx SERIAL %f\n", lastx);
89+
90+
lastx = 0;
91+
#pragma omp parallel for lastprivate(lastx)
92+
for ( i = 0; i < msize; i++) {
93+
lastx = a[i]+b[i] ;
94+
}
95+
96+
printf("lastx lastprivate %f\n", lastx);
97+
98+
lastx = 0;
99+
#pragma omp parallel for
100+
for ( i = 0; i < msize; i++) {
101+
lastx = a[i]+b[i] ;
102+
}
103+
104+
printf("lastx omp parallel for %f\n", lastx);
105+
106+
return(0);
107+
}
12.7 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int main(){
5+
6+
int nn, i;
7+
nn = 2000000000;
8+
double* a;
9+
double* b;
10+
double* c;
11+
12+
a = (double*) malloc(nn*sizeof(double));
13+
b = (double*) malloc(nn*sizeof(double));
14+
c = (double*) malloc(nn*sizeof(double));
15+
16+
#pragma omp parallel for
17+
for( i=0; i<nn; i++) {
18+
a[i] = i+i;
19+
b[i] = i-i;
20+
c[i] = i*i;
21+
}
22+
23+
#pragma omp parallel for
24+
for( i=0; i<nn; i++) {
25+
a[i] = b[i] + c[i];
26+
}
27+
28+
#pragma omp parallel for
29+
for( i=0; i<nn; i++) {
30+
b[i] = c[i] + a[i];
31+
}
32+
33+
#pragma omp parallel for
34+
for( i=0; i<nn; i++) {
35+
c[i] = b[i] + a[i];
36+
}
37+
38+
}
12.7 KB
Binary file not shown.
8.12 KB
Binary file not shown.

day3/introduction-to-openmp/OMP-race

8.63 KB
Binary file not shown.
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int main(){
5+
int* input;
6+
int snum, i, sum;
7+
8+
sum = 0;
9+
snum=40000000;
10+
11+
input = (int*) malloc (sizeof(int)*snum);
12+
13+
for(i=0;i<snum;i++) {
14+
input[i] = i+1;
15+
}
16+
17+
#pragma omp parallel for
18+
for(i=0;i<snum;i++)
19+
{
20+
int* tmpsum = input+i;
21+
22+
//#pragma omp critical
23+
//{
24+
// sum += *tmpsum;
25+
//}
26+
#pragma omp atomic
27+
sum += *tmpsum;
28+
29+
}
30+
printf("sum %d", sum);
31+
}
8.63 KB
Binary file not shown.
8.73 KB
Binary file not shown.

day3/introduction-to-openmp/OMP-sync

8.58 KB
Binary file not shown.
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <omp.h>
2+
#include <stdio.h>
3+
4+
int main ()
5+
{
6+
int thid;
7+
8+
#pragma omp parallel private(thid)
9+
{
10+
#pragma omp master
11+
{
12+
thid= omp_get_thread_num();
13+
printf("master thread only: thread %d \n", thid);
14+
}
15+
16+
thid= omp_get_thread_num();
17+
printf("ALL threads: BE CAREFULL! thread %d \n", thid);
18+
19+
#pragma omp barrier
20+
21+
#pragma omp single
22+
{
23+
thid= omp_get_thread_num();
24+
printf("some thread execute this part (only one): thread %d \n", thid);
25+
}
26+
27+
#pragma omp barrier
28+
29+
thid= omp_get_thread_num();
30+
printf("after omp barrier! thread %d \n", thid);
31+
}
32+
33+
return 0;
34+
}

day3/introduction-to-openmp/OMP-task

8.11 KB
Binary file not shown.
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdio.h>
2+
int main() {
3+
#pragma omp parallel​
4+
{
5+
#pragma omp single​
6+
{
7+
#pragma omp task
8+
printf("hello \n");
9+
10+
#pragma omp task
11+
printf("again \n");
12+
}
13+
}
14+
return(0);
15+
}
8.17 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
4+
#define N 30
5+
6+
int main ( ) {
7+
unsigned long long fibs[N];
8+
9+
fibs[0] = fibs[1] = 1;
10+
11+
int i;
12+
unsigned long long sum=2;
13+
#pragma omp parallel for
14+
for (i = 2; i < N; i++) {
15+
fibs[i] = fibs[i - 1] + fibs[i - 2];
16+
printf("%llu \n", fibs[i]);
17+
}
18+
19+
return 0;
20+
}
8.59 KB
Binary file not shown.

day3/introduction-to-openmp/fib-task

8.14 KB
Binary file not shown.
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
4+
unsigned long long fib(int n) {
5+
6+
unsigned long long i, j, res;
7+
8+
if (n < 2) return n;
9+
10+
#pragma omp task shared(i)
11+
i = fib(n-1);
12+
#pragma omp task shared(j)
13+
j = fib(n-2);
14+
15+
#pragma omp taskwait
16+
return (i+j);
17+
}
18+
19+
int main(){
20+
int n = 80;
21+
int c = 1;
22+
#pragma omp parallel
23+
{
24+
#pragma omp single
25+
{
26+
for (c = 1; c <= n; c++) {
27+
printf("%llu\n", fib(c));
28+
}
29+
}
30+
}
31+
}
8.72 KB
Binary file not shown.

0 commit comments

Comments
 (0)