Skip to content

Commit f4531ff

Browse files
committed
Add example notebooks
1 parent 1463e97 commit f4531ff

File tree

5 files changed

+767
-0
lines changed

5 files changed

+767
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "73cbab37-71dd-477d-981b-f2ec28c01bd6",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"#include <stdio.h>\n",
11+
"#include <omp.h>"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 3,
17+
"id": "c2b754ad-9553-4a42-b990-f990a9a269ed",
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"int main() {\n",
22+
" int max_threads = omp_get_max_threads();\n",
23+
"\n",
24+
" printf(\"max threads: %d\\n\", max_threads);\n",
25+
" omp_set_num_threads(max_threads);\n",
26+
"\n",
27+
"#pragma omp parallel\n",
28+
" {\n",
29+
" int id = omp_get_thread_num();\n",
30+
" printf(\"Hello World from thread = %d with %d threads\\n\", id, omp_get_num_threads());\n",
31+
" }\n",
32+
"\n",
33+
" printf(\"all done, with hopefully %d threads\\n\", max_threads);\n",
34+
"}"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 4,
40+
"id": "a37a13d4-fc82-496e-8f42-9e718a8c2aa0",
41+
"metadata": {},
42+
"outputs": [
43+
{
44+
"name": "stdout",
45+
"output_type": "stream",
46+
"text": [
47+
"max threads: 8\n",
48+
"Hello World from thread = 0 with 8 threads\n",
49+
"Hello World from thread = 3 with 8 threads\n",
50+
"Hello World from thread = 4 with 8 threads\n",
51+
"Hello World from thread = 2 with 8 threads\n",
52+
"Hello World from thread = 7 with 8 threads\n",
53+
"Hello World from thread = 1 with 8 threads\n",
54+
"Hello World from thread = 6 with 8 threads\n",
55+
"Hello World from thread = 5 with 8 threads\n",
56+
"all done, with hopefully 8 threads\n"
57+
]
58+
}
59+
],
60+
"source": [
61+
"main();"
62+
]
63+
}
64+
],
65+
"metadata": {
66+
"kernelspec": {
67+
"display_name": "C++17 (xcpp+OpenMP)",
68+
"language": "cpp",
69+
"name": "xcpp17-omp"
70+
},
71+
"language_info": {
72+
"codemirror_mode": "text/x-c++src",
73+
"file_extension": ".cpp",
74+
"mimetype": "text/x-c++src",
75+
"name": "C++",
76+
"version": "17"
77+
}
78+
},
79+
"nbformat": 4,
80+
"nbformat_minor": 5
81+
}
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "156447d2-9279-45a0-890b-4e519d2c796b",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"#include <stdlib.h>\n",
11+
"#include <stdio.h>\n",
12+
"#include <omp.h>"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 2,
18+
"id": "c96fdeb0-817d-48c0-af8e-20a52947d60b",
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"#ifndef N\n",
23+
"#define N 5\n",
24+
"#endif\n",
25+
"#ifndef FS\n",
26+
"#define FS 38\n",
27+
"#endif"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 4,
33+
"id": "22f97c49-78d1-496e-ac7c-978aed95331a",
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"struct node {\n",
38+
" int data;\n",
39+
" int fibdata;\n",
40+
" struct node *next;\n",
41+
"};"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 5,
47+
"id": "b16b1e8a-8831-4b8d-9d57-09deeaaa88ee",
48+
"metadata": {},
49+
"outputs": [],
50+
"source": [
51+
"struct node *init_list(struct node *p);\n",
52+
"void processwork(struct node *p);\n",
53+
"int fib(int n);"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 6,
59+
"id": "0ef8af6c-1d6f-4c68-84bc-3dd1d8092b06",
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"int fib(int n) {\n",
64+
" int x, y;\n",
65+
" if (n < 2) {\n",
66+
" return (n);\n",
67+
" } else {\n",
68+
" x = fib(n - 1);\n",
69+
" y = fib(n - 2);\n",
70+
" return (x + y);\n",
71+
" }\n",
72+
"}"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": 7,
78+
"id": "1fa0307d-fdc9-4503-95cb-1c6448791354",
79+
"metadata": {},
80+
"outputs": [],
81+
"source": [
82+
"void processwork(struct node *p) {\n",
83+
" int n, temp;\n",
84+
" n = p->data;\n",
85+
" temp = fib(n);\n",
86+
"\n",
87+
" p->fibdata = temp;\n",
88+
"}"
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": 8,
94+
"id": "03acb599-9329-49ff-8aff-c0902adb6c3c",
95+
"metadata": {},
96+
"outputs": [],
97+
"source": [
98+
"struct node *init_list(struct node *p) {\n",
99+
" int i;\n",
100+
" struct node *head = NULL;\n",
101+
" struct node *temp = NULL;\n",
102+
"\n",
103+
" head = (struct node*) malloc(sizeof(struct node));\n",
104+
" p = head;\n",
105+
" p->data = FS;\n",
106+
" p->fibdata = 0;\n",
107+
" for (i = 0; i < N; i++) {\n",
108+
" temp = (struct node*) malloc(sizeof(struct node));\n",
109+
" p->next = temp;\n",
110+
" p = temp;\n",
111+
" p->data = FS + i + 1;\n",
112+
" p->fibdata = i + 1;\n",
113+
" }\n",
114+
"\n",
115+
" p->next = NULL;\n",
116+
" return head;\n",
117+
"}"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": 9,
123+
"id": "f2dfb41b-e55f-43c0-b7f6-546a1697acb1",
124+
"metadata": {},
125+
"outputs": [],
126+
"source": [
127+
"int main() {\n",
128+
" double start, end;\n",
129+
" struct node *p = NULL;\n",
130+
" struct node *temp = NULL;\n",
131+
" struct node *head = NULL;\n",
132+
"\n",
133+
" printf(\"Process linked list\\n\");\n",
134+
" printf(\" Each linked list node will be processed by function 'processwork()'\\n\");\n",
135+
" printf(\" Each ll node will compute %d fibonacci numbers beginning with %d\\n\", N, FS);\n",
136+
"\n",
137+
" omp_set_num_threads(omp_get_max_threads());\n",
138+
"\n",
139+
" p = init_list(p);\n",
140+
" head = p;\n",
141+
"\n",
142+
" start = omp_get_wtime();\n",
143+
"\n",
144+
"#pragma omp parallel\n",
145+
" {\n",
146+
"#pragma omp master\n",
147+
" printf(\"Threads: %d\\n\", omp_get_num_threads());\n",
148+
"\n",
149+
"#pragma omp single\n",
150+
" {\n",
151+
" p = head;\n",
152+
" while (p) {\n",
153+
"#pragma omp task firstprivate(p) // first private is required\n",
154+
" {\n",
155+
" processwork(p);\n",
156+
" }\n",
157+
" p = p->next;\n",
158+
" }\n",
159+
" }\n",
160+
" }\n",
161+
"\n",
162+
" end = omp_get_wtime();\n",
163+
" p = head;\n",
164+
" while (p != NULL) {\n",
165+
" printf(\"%d : %d\\n\", p->data, p->fibdata);\n",
166+
" temp = p->next;\n",
167+
" free(p);\n",
168+
" p = temp;\n",
169+
" }\n",
170+
"\n",
171+
" free(p);\n",
172+
" printf(\"Compute Time: %f seconds\\n\", end - start);\n",
173+
"\n",
174+
" return 0;\n",
175+
"}"
176+
]
177+
},
178+
{
179+
"cell_type": "code",
180+
"execution_count": 10,
181+
"id": "353e5dfd-fcae-43e6-97e3-ec98070811a1",
182+
"metadata": {},
183+
"outputs": [
184+
{
185+
"name": "stdout",
186+
"output_type": "stream",
187+
"text": [
188+
"Process linked list\n",
189+
" Each linked list node will be processed by function 'processwork()'\n",
190+
" Each ll node will compute 5 fibonacci numbers beginning with 38\n",
191+
"Threads: 8\n",
192+
"38 : 39088169\n",
193+
"39 : 63245986\n",
194+
"40 : 102334155\n",
195+
"41 : 165580141\n",
196+
"42 : 267914296\n",
197+
"43 : 433494437\n",
198+
"Compute Time: 2.617225 seconds\n"
199+
]
200+
}
201+
],
202+
"source": [
203+
"main();"
204+
]
205+
}
206+
],
207+
"metadata": {
208+
"kernelspec": {
209+
"display_name": "C++17 (xcpp+OpenMP)",
210+
"language": "cpp",
211+
"name": "xcpp17-omp"
212+
},
213+
"language_info": {
214+
"codemirror_mode": "text/x-c++src",
215+
"file_extension": ".cpp",
216+
"mimetype": "text/x-c++src",
217+
"name": "C++",
218+
"version": "17"
219+
}
220+
},
221+
"nbformat": 4,
222+
"nbformat_minor": 5
223+
}

0 commit comments

Comments
 (0)