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

Lines changed: 4 additions & 0 deletions
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.
8.21 KB
Binary file not shown.
8.59 KB
Binary file not shown.
Lines changed: 25 additions & 0 deletions
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.
Lines changed: 38 additions & 0 deletions
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+
}
Lines changed: 13 additions & 0 deletions
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.
Lines changed: 107 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)