Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7059770

Browse files
committedOct 29, 2023
Add some examples
1 parent aba3401 commit 7059770

30 files changed

+8030
-0
lines changed
 

‎notebooks/ProjectMockup.ipynb

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "1c4fd5b5-1b9e-4a8e-82b3-6ab214c203ea",
6+
"metadata": {},
7+
"source": [
8+
"## Project Mockup"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"id": "54a52e27-3d9d-4574-990b-329a47d4bf13",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"struct S { double val = 1.0; };"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 2,
24+
"id": "b9e80eda-8924-4f3c-bbc0-7b74ba9b9a36",
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"%%python\n",
29+
"\n",
30+
"python_vec = cppyy.gbl.std.vector(cppyy.gbl.S)(5)"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 3,
36+
"id": "697760dd-200e-4fa6-85b2-f1786215eda3",
37+
"metadata": {},
38+
"outputs": [
39+
{
40+
"name": "stdout",
41+
"output_type": "stream",
42+
"text": [
43+
"1.0\n"
44+
]
45+
}
46+
],
47+
"source": [
48+
"%%python\n",
49+
"\n",
50+
"print(python_vec[3].val)"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": 4,
56+
"id": "d581f6cc-2737-4c11-b37a-25bb5296936a",
57+
"metadata": {},
58+
"outputs": [
59+
{
60+
"name": "stdout",
61+
"output_type": "stream",
62+
"text": [
63+
"<__main__.Derived object at 0x7fffdf639e40>\n"
64+
]
65+
},
66+
{
67+
"name": "stderr",
68+
"output_type": "stream",
69+
"text": [
70+
"<string>:1: RuntimeWarning: class \"S\" has no virtual destructor\n"
71+
]
72+
}
73+
],
74+
"source": [
75+
"%%python\n",
76+
"\n",
77+
"class Derived(cppyy.gbl.S):\n",
78+
" def __init__(self):\n",
79+
" val = 0\n",
80+
"res = Derived()\n",
81+
"print(res)"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 5,
87+
"id": "f20feaa6-62a2-4805-853b-268b1c1832ac",
88+
"metadata": {},
89+
"outputs": [],
90+
"source": [
91+
"__global__ void arr_sum(int n, double *x, double *sum) {\n",
92+
" for(int i = 0; i < n; i++)\n",
93+
" *sum +=x[i];\n",
94+
"}"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": 6,
100+
"id": "cfd92edb-41e0-4bb8-b7a1-852782964423",
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"int n = 5;\n",
105+
"double h_sum;\n",
106+
"double *x = new double[n];"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 7,
112+
"id": "90480051-7a57-4145-85c4-ca3bdc8e101f",
113+
"metadata": {},
114+
"outputs": [],
115+
"source": [
116+
"void setData(const std::vector<S>& a) {\n",
117+
" int i = 0;\n",
118+
" for(auto &s : a) {\n",
119+
" x[i] = s.val;\n",
120+
" i++;\n",
121+
" }\n",
122+
"}"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": 8,
128+
"id": "9d9913b7-db39-4c76-8939-efbca9256143",
129+
"metadata": {},
130+
"outputs": [],
131+
"source": [
132+
"%%python\n",
133+
"\n",
134+
"data_list = [1.0, 2.0, 3.0, 4.0, 5.0]\n",
135+
"for c, i in enumerate(data_list):\n",
136+
" python_vec[c].val = i\n"
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": 9,
142+
"id": "846901c7-0cbb-4978-9efe-7f5870b939a7",
143+
"metadata": {},
144+
"outputs": [],
145+
"source": [
146+
"%%python\n",
147+
"\n",
148+
"cppyy.gbl.setData(python_vec)\n"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": 10,
154+
"id": "f57c46fa-addb-4d80-8cc3-29a464911fdc",
155+
"metadata": {},
156+
"outputs": [],
157+
"source": [
158+
"double *d_x, *d_sum;\n",
159+
"cudaMalloc((void **)&d_x, n * sizeof(double));\n",
160+
"cudaMalloc((void **)&d_sum, sizeof(double));"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": 11,
166+
"id": "d617ee2c-d9b7-4c4f-8ef2-051df37b2737",
167+
"metadata": {},
168+
"outputs": [],
169+
"source": [
170+
"cudaMemcpy(d_x, x, n * sizeof(double), cudaMemcpyHostToDevice);\n",
171+
"cudaMemcpy(d_sum, &h_sum, sizeof(double), cudaMemcpyHostToDevice);"
172+
]
173+
},
174+
{
175+
"cell_type": "code",
176+
"execution_count": 12,
177+
"id": "8417b211-e7f9-448d-969a-27fc0eb32697",
178+
"metadata": {},
179+
"outputs": [],
180+
"source": [
181+
"arr_sum<<<1, 1>>>(n, d_x, d_sum);"
182+
]
183+
},
184+
{
185+
"cell_type": "code",
186+
"execution_count": 13,
187+
"id": "4374b687-31a5-4b85-8d66-e738956814b4",
188+
"metadata": {},
189+
"outputs": [],
190+
"source": [
191+
"cudaMemcpy(&h_sum, d_sum, sizeof(double), cudaMemcpyDeviceToHost);"
192+
]
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": 14,
197+
"id": "8198bfdf-0ff7-4e27-ba6b-087833055794",
198+
"metadata": {},
199+
"outputs": [
200+
{
201+
"name": "stdout",
202+
"output_type": "stream",
203+
"text": [
204+
"Sum: 15\n"
205+
]
206+
}
207+
],
208+
"source": [
209+
"std::cout << \"Sum: \" << h_sum << std::endl;"
210+
]
211+
},
212+
{
213+
"cell_type": "code",
214+
"execution_count": 15,
215+
"id": "33ffcf05-58cc-4212-aef7-4a5813fdc178",
216+
"metadata": {},
217+
"outputs": [],
218+
"source": [
219+
"delete[] x;\n",
220+
"cudaFree(d_x);\n",
221+
"cudaFree(d_sum);"
222+
]
223+
},
224+
{
225+
"cell_type": "code",
226+
"execution_count": null,
227+
"id": "add835ef-6196-47d8-be75-14b872438de1",
228+
"metadata": {},
229+
"outputs": [],
230+
"source": []
231+
}
232+
],
233+
"metadata": {
234+
"kernelspec": {
235+
"display_name": "CUDA (C++17)",
236+
"language": "CUDA",
237+
"name": "cuda-xcpp17"
238+
},
239+
"language_info": {
240+
"codemirror_mode": "text/x-c++src",
241+
"file_extension": ".cpp",
242+
"mimetype": "text/x-c++src",
243+
"name": "c++",
244+
"version": "17"
245+
}
246+
},
247+
"nbformat": 4,
248+
"nbformat_minor": 5
249+
}
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "ca6bccaa",
6+
"metadata": {},
7+
"source": [
8+
"## Declaring variables in C++ "
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"id": "879a6503",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"extern \"C\" int printf(const char*,...);"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 2,
24+
"id": "2063086d",
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"int new_var1 = 12;\n",
29+
"int new_var2 = 25;\n",
30+
"int new_var3 = 64;"
31+
]
32+
},
33+
{
34+
"cell_type": "markdown",
35+
"id": "6e42ad3d",
36+
"metadata": {},
37+
"source": [
38+
"## Running Python with C++ variables"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": 3,
44+
"id": "7edde9fb",
45+
"metadata": {
46+
"scrolled": true
47+
},
48+
"outputs": [
49+
{
50+
"name": "stdout",
51+
"output_type": "stream",
52+
"text": [
53+
"This is printed from Python: Today is Tue Oct 25 11:38:08 2022\n",
54+
"[1, 2, 12, 25, 64]\n"
55+
]
56+
}
57+
],
58+
"source": [
59+
"%%python\n",
60+
"\n",
61+
"from time import time,ctime\n",
62+
"print('This is printed from Python: Today is', ctime(time()))\n",
63+
"python_array = [1, 2, new_var1, new_var2, new_var3]\n",
64+
"print(python_array)"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 4,
70+
"id": "579f1d82",
71+
"metadata": {},
72+
"outputs": [],
73+
"source": [
74+
"%%python \n",
75+
"\n",
76+
"new_python_var = 1327\n"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": 5,
82+
"id": "43391a7e",
83+
"metadata": {},
84+
"outputs": [
85+
{
86+
"name": "stdout",
87+
"output_type": "stream",
88+
"text": [
89+
"new_python_var = 1327\n"
90+
]
91+
}
92+
],
93+
"source": [
94+
"auto k = printf(\"new_python_var = %d\\n\", new_python_var);"
95+
]
96+
},
97+
{
98+
"cell_type": "markdown",
99+
"id": "9030df20",
100+
"metadata": {},
101+
"source": [
102+
"## Working with a vector type"
103+
]
104+
},
105+
{
106+
"cell_type": "code",
107+
"execution_count": 6,
108+
"id": "400e42d3",
109+
"metadata": {},
110+
"outputs": [],
111+
"source": [
112+
"#include <iostream>\n",
113+
"#include <vector>"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 7,
119+
"id": "92b2bd05",
120+
"metadata": {},
121+
"outputs": [],
122+
"source": [
123+
"std::vector<int> fill_vector(std::vector<int> g) { for (int i = 1; i <= 5; i++){ g.push_back(i);} return g; }"
124+
]
125+
},
126+
{
127+
"cell_type": "code",
128+
"execution_count": 8,
129+
"id": "ea7f2d92",
130+
"metadata": {},
131+
"outputs": [],
132+
"source": [
133+
"int size_of_vector (std::vector<int> g) { std::cout << \"Size : \" << g.size(); return g.size(); }"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": 9,
139+
"id": "760237a2",
140+
"metadata": {},
141+
"outputs": [],
142+
"source": [
143+
"int capacity_of_vector (std::vector<int> g) { std::cout << \"Capacity : \" << g.capacity(); return g.capacity(); }"
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": 10,
149+
"id": "1c02ee6c",
150+
"metadata": {},
151+
"outputs": [],
152+
"source": [
153+
"std::vector<int> cpp_vector = fill_vector(cpp_vector);"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 11,
159+
"id": "a7fb4cb5",
160+
"metadata": {},
161+
"outputs": [
162+
{
163+
"name": "stdout",
164+
"output_type": "stream",
165+
"text": [
166+
"Size : 5"
167+
]
168+
}
169+
],
170+
"source": [
171+
"int size = size_of_vector(cpp_vector);"
172+
]
173+
},
174+
{
175+
"cell_type": "code",
176+
"execution_count": 12,
177+
"id": "95f8e6f7",
178+
"metadata": {},
179+
"outputs": [
180+
{
181+
"name": "stdout",
182+
"output_type": "stream",
183+
"text": [
184+
"Capacity : 5"
185+
]
186+
}
187+
],
188+
"source": [
189+
"int capacity = capacity_of_vector(cpp_vector);"
190+
]
191+
},
192+
{
193+
"cell_type": "markdown",
194+
"id": "781feea0",
195+
"metadata": {},
196+
"source": [
197+
"## Plotting data from C++ and Python"
198+
]
199+
},
200+
{
201+
"cell_type": "code",
202+
"execution_count": 13,
203+
"id": "7ccb6fe7",
204+
"metadata": {},
205+
"outputs": [
206+
{
207+
"name": "stdout",
208+
"output_type": "stream",
209+
"text": [
210+
"[1, 2, 3, 4, 5]\n",
211+
"[1, 2, 12, 25, 64]\n"
212+
]
213+
}
214+
],
215+
"source": [
216+
"%%python\n",
217+
"\n",
218+
"print(cpp_vector)\n",
219+
"print(python_array)"
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": 14,
225+
"id": "531e00ff",
226+
"metadata": {},
227+
"outputs": [],
228+
"source": [
229+
"%%python\n",
230+
"\n",
231+
"import matplotlib.pyplot as plt\n",
232+
"import numpy as np\n",
233+
"\n",
234+
"plt.plot(python_array, cpp_vector,linewidth=3)\n",
235+
"plt.xlabel('Python Array')\n",
236+
"plt.ylabel('Cpp Array')\n",
237+
"plt.savefig('python_cpp_plot.png')"
238+
]
239+
},
240+
{
241+
"cell_type": "markdown",
242+
"id": "b1842530",
243+
"metadata": {},
244+
"source": [
245+
"<img src=\"python_cpp_plot.png\" align=left width=\"400\">"
246+
]
247+
},
248+
{
249+
"cell_type": "code",
250+
"execution_count": null,
251+
"id": "7bf12bfb",
252+
"metadata": {},
253+
"outputs": [],
254+
"source": []
255+
}
256+
],
257+
"metadata": {
258+
"kernelspec": {
259+
"display_name": "C++11",
260+
"language": "C++11",
261+
"name": "xcpp11"
262+
},
263+
"language_info": {
264+
"codemirror_mode": "text/x-c++src",
265+
"file_extension": ".cpp",
266+
"mimetype": "text/x-c++src",
267+
"name": "c++",
268+
"version": "11"
269+
}
270+
},
271+
"nbformat": 4,
272+
"nbformat_minor": 5
273+
}

‎notebooks/audio/audio.wav

501 KB
Binary file not shown.

‎notebooks/clad-demo.ipynb

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "74f52cc2-bfda-4fee-9cd9-94d683a8b7e1",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"#include \"clad/Differentiator/Differentiator.h\""
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"id": "55453d37-e8af-4cb7-8f82-550e9cf88b3c",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"// Rosenbrock function declaration\n",
21+
"double rosenbrock_func(double x, double y) {\n",
22+
"return (x - 1) * (x - 1) + 100 * (y - x * x) * (y - x * x);\n",
23+
"}"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 3,
29+
"id": "9098b6e4-87a7-497d-9a1b-dddeb813dc56",
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"double rosenbrock_forward(double x[], int size) {\n",
34+
" double sum = 0;\n",
35+
" auto rosenbrockX = clad::differentiate(rosenbrock_func, 0);\n",
36+
" auto rosenbrockY = clad::differentiate(rosenbrock_func, 1);\n",
37+
" for (int i = 0; i < size-1; i++) {\n",
38+
" double one = rosenbrockX.execute(x[i], x[i + 1]);\n",
39+
" double two = rosenbrockY.execute(x[i], x[i + 1]);\n",
40+
" sum = sum + one + two;\n",
41+
" }\n",
42+
" return sum;\n",
43+
"}"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 4,
49+
"id": "e771bbdc-3979-43df-9ed9-3429ee713b5d",
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"const int size = 100000000;\n",
54+
"double Xarray[size];\n",
55+
"for(int i=0;i<size;i++)\n",
56+
" Xarray[i]=((double)rand()/RAND_MAX);"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 5,
62+
"id": "e74f85c2-a3bc-4562-a9ae-321eb306e8ff",
63+
"metadata": {},
64+
"outputs": [],
65+
"source": [
66+
"double r = rosenbrock_forward(Xarray, size);"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 6,
72+
"id": "184cebf3-a98f-4c07-97c6-c878be3740c8",
73+
"metadata": {},
74+
"outputs": [
75+
{
76+
"name": "stdout",
77+
"output_type": "stream",
78+
"text": [
79+
"3232947666.004954\n"
80+
]
81+
}
82+
],
83+
"source": [
84+
"printf(\"%f\\n\", r);"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": null,
90+
"id": "3f68f1fe-24ca-40fb-997b-6f47a6b81ee7",
91+
"metadata": {},
92+
"outputs": [],
93+
"source": []
94+
}
95+
],
96+
"metadata": {
97+
"kernelspec": {
98+
"display_name": "C++17 (+Clad)",
99+
"language": "C++17",
100+
"name": "clad-xcpp17"
101+
},
102+
"language_info": {
103+
"codemirror_mode": "text/x-c++src",
104+
"file_extension": ".cpp",
105+
"mimetype": "text/x-c++src",
106+
"name": "C++",
107+
"version": "17"
108+
}
109+
},
110+
"nbformat": 4,
111+
"nbformat_minor": 5
112+
}
113+
}

‎notebooks/clad-smallpt.ipynb

Lines changed: 729 additions & 0 deletions
Large diffs are not rendered by default.

‎notebooks/cu-smallpt.ipynb

Lines changed: 1121 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "1170e2e2-0b77-4d70-afab-3069af8d0528",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"#include<iostream>\n",
11+
"#include<vector>"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 2,
17+
"id": "9af566fb-a31f-4d0b-ad7b-69383b2b0940",
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"namespace VoidPtrArray {\n",
22+
" typedef struct _name {\n",
23+
" _name() { p[0] = (void*)0x1; p[1] = (void*)0x2; p[2] = (void*)0x3; }\n",
24+
" void* p[3];\n",
25+
" } name;\n",
26+
" }"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 3,
32+
"id": "ce6a3448-50ee-483c-8c67-b3e3fd9f6c8a",
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"%%python\n",
37+
"\n",
38+
"n = cppyy.gbl.VoidPtrArray.name()\n",
39+
"assert n.p[0] == 0x1\n",
40+
"assert n.p[1] == 0x2\n",
41+
"assert n.p[2] == 0x3"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 4,
47+
"id": "9dde9a47-48a7-4cae-90db-d489d61a1c51",
48+
"metadata": {},
49+
"outputs": [],
50+
"source": [
51+
"namespace VectorConstCharStar {\n",
52+
" std::vector<const char*> test = {\"hello\"};\n",
53+
" }"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 5,
59+
"id": "4c45ea0c-3634-43a3-bae9-c1972e6b4762",
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"%%python\n",
64+
"\n",
65+
"import cppyy\n",
66+
"ns = cppyy.gbl.VectorConstCharStar\n",
67+
"\n",
68+
"assert len(ns.test) == 1\n",
69+
"assert ns.test[0] == \"hello\"\n",
70+
"\n",
71+
"ns.test.push_back(\"world\")\n",
72+
"assert len(ns.test) == 2\n",
73+
"assert ns.test[0] == \"hello\"\n",
74+
"assert ns.test[1] == \"world\""
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 6,
80+
"id": "d5699a3b-94ce-4b1a-96f5-2b3264e0cabb",
81+
"metadata": {},
82+
"outputs": [],
83+
"source": [
84+
"namespace ArrayLike {\n",
85+
"struct __attribute__((__packed__)) Vector3f {\n",
86+
" float x, y, z;\n",
87+
"}; }\n"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 7,
93+
"id": "8bff7898-1265-4341-941e-3d1810b06abd",
94+
"metadata": {},
95+
"outputs": [],
96+
"source": [
97+
"%%python\n",
98+
"\n",
99+
"N = 5\n",
100+
"\n",
101+
"v = cppyy.gbl.std.vector['ArrayLike::Vector3f'](N)\n",
102+
"\n",
103+
"for i in range(N):\n",
104+
" d = v[i]\n",
105+
" d.x, d.y, d.z = i, i*N, i*N**2\n"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 8,
111+
"id": "bbc3c5a0-7b0b-46ae-a576-7e5721b0883a",
112+
"metadata": {},
113+
"outputs": [],
114+
"source": [
115+
"%%python\n",
116+
"\n",
117+
"for i in range(N):\n",
118+
" d = v[i]\n",
119+
" assert d.x == float(i)\n",
120+
" assert d.y == float(i*N)\n",
121+
" assert d.z == float(i*N**2)\n"
122+
]
123+
},
124+
{
125+
"cell_type": "markdown",
126+
"id": "15727205-0392-4fbd-a7e5-8cdb73f37ab1",
127+
"metadata": {},
128+
"source": [
129+
"#### STL-like class with preinc by-ref returns"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": 10,
135+
"id": "65ec9f7d-40a6-44c4-a944-6182ea7f8b02",
136+
"metadata": {},
137+
"outputs": [],
138+
"source": [
139+
"namespace PreIncrement {\n",
140+
" struct Token {\n",
141+
" int value;\n",
142+
" };\n",
143+
"\n",
144+
"struct Iterator {\n",
145+
" Token current;\n",
146+
"\n",
147+
" bool operator!=(const Iterator& rhs) {\n",
148+
" return rhs.current.value != current.value; }\n",
149+
" const Token& operator*() { return current; }\n",
150+
" Iterator& operator++() {\n",
151+
" current.value++;\n",
152+
" return *this;\n",
153+
" }\n",
154+
"};\n",
155+
"\n",
156+
"struct Stream {\n",
157+
" Iterator begin() { return Iterator(); }\n",
158+
" Iterator end() { return Iterator{10}; }\n",
159+
"}; }"
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": 15,
165+
"id": "c4ed4321-6b9b-4531-8d79-ee00f7d46e3a",
166+
"metadata": {},
167+
"outputs": [
168+
{
169+
"name": "stderr",
170+
"output_type": "stream",
171+
"text": [
172+
"Traceback (most recent call last):\n",
173+
" File \"<string>\", line 5, in <module>\n",
174+
"TypeError: PreIncrement::Iterator PreIncrement::Stream::beginIterator PreIncrement::Stream::begin() =>\n",
175+
" TypeError: unbound method Stream::begin must be called with a Stream instance as first argument\n"
176+
]
177+
}
178+
],
179+
"source": [
180+
"%%python\n",
181+
"\n",
182+
"import cppyy\n",
183+
"ns = cppyy.gbl.PreIncrement\n",
184+
"\n",
185+
"stream = ns.Stream\n",
186+
"stream.begin(stream)"
187+
]
188+
}
189+
],
190+
"metadata": {
191+
"kernelspec": {
192+
"display_name": "CUDA (C++17)",
193+
"language": "CUDA",
194+
"name": "cuda-xcpp17"
195+
},
196+
"language_info": {
197+
"codemirror_mode": "text/x-c++src",
198+
"file_extension": ".cpp",
199+
"mimetype": "text/x-c++src",
200+
"name": "c++",
201+
"version": "17"
202+
}
203+
},
204+
"nbformat": 4,
205+
"nbformat_minor": 5
206+
}

‎notebooks/eigen_demo/eigen_demo.ipynb

Lines changed: 343 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,343 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "6c9eda5c-bf2f-4bcd-8dee-bc7db08dd801",
6+
"metadata": {},
7+
"source": [
8+
"### Here we include the **conda** installed library using Cppyy"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"id": "b172dcbe-7a72-4d1a-8e70-5955dec48735",
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"name": "stdout",
19+
"output_type": "stream",
20+
"text": [
21+
"Collecting package metadata (current_repodata.json): ...working... done\n",
22+
"Solving environment: ...working... done\n",
23+
"\n",
24+
"\n",
25+
"==> WARNING: A newer version of conda exists. <==\n",
26+
" current version: 22.9.0\n",
27+
" latest version: 23.7.4\n",
28+
"\n",
29+
"Please update conda by running\n",
30+
"\n",
31+
" $ conda update -n base -c conda-forge conda\n",
32+
"\n",
33+
"\n",
34+
"\n",
35+
"# All requested packages already installed.\n",
36+
"\n",
37+
"Retrieving notices: ...working... done\n"
38+
]
39+
}
40+
],
41+
"source": [
42+
"!conda install -y -c conda-forge eigen"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 2,
48+
"id": "01d6e223-c6bc-4255-bd2f-a2deb68ea06d",
49+
"metadata": {},
50+
"outputs": [],
51+
"source": [
52+
"#include <clang/Interpreter/CppInterOp.h>\n",
53+
"Cpp::AddIncludePath(\"/opt/conda/envs/.venv/include\");"
54+
]
55+
},
56+
{
57+
"cell_type": "markdown",
58+
"id": "22e7b3ed-747e-4bd5-8b89-4a6e5661dfbf",
59+
"metadata": {},
60+
"source": [
61+
"### We are immediately able to use the `Eigen::Dense` namespaces in our C++ code cell"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": 3,
67+
"id": "815bd42b-024a-49c7-9557-fa5bfd8b4496",
68+
"metadata": {},
69+
"outputs": [],
70+
"source": [
71+
"#include \"/opt/conda/envs/.venv/include/eigen3/Eigen/Dense\"\n",
72+
"\n",
73+
"typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> MatrixXd;\n",
74+
"\n",
75+
"Eigen::Vector2d a;\n",
76+
"Eigen::Vector3d b;\n",
77+
"Eigen::Vector4d c;"
78+
]
79+
},
80+
{
81+
"cell_type": "markdown",
82+
"id": "0766e5c0-2d3b-4ae4-96bd-4a78f3b02cc2",
83+
"metadata": {},
84+
"source": [
85+
"### We can define a templated class with a set of vector operations offered by Eigen "
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": 4,
91+
"id": "bca71004-dec4-4571-8a89-c9c9a54aaeee",
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"template <typename VectorType>\n",
96+
"class EigenOperations {\n",
97+
"public:\n",
98+
" static VectorType PerformOperations(const VectorType& v) {\n",
99+
" VectorType result = v;\n",
100+
" \n",
101+
" // Using Eigen::Dense object methods\n",
102+
" \n",
103+
" result.normalize(); // eigen vector normalisation\n",
104+
"\n",
105+
" double dot = v.dot(v); // dot product\n",
106+
" for (int i = 0; i < result.size(); i++) {\n",
107+
" result[i] += dot;\n",
108+
" }\n",
109+
" \n",
110+
" double squaredNorm = v.squaredNorm(); // vector squared norm \n",
111+
" for (int i = 0; i < result.size(); i++) {\n",
112+
" result[i] -= squaredNorm;\n",
113+
" }\n",
114+
"\n",
115+
" return result;\n",
116+
" }\n",
117+
"};\n"
118+
]
119+
},
120+
{
121+
"cell_type": "markdown",
122+
"id": "de00d39d-8156-4ecb-a932-924661e7b80e",
123+
"metadata": {},
124+
"source": [
125+
"### Accessing the declared Eigen vectors on the Python side using Cppyy"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": 5,
131+
"id": "9b7e7fee-1edd-427c-a738-0cdf941200c1",
132+
"metadata": {},
133+
"outputs": [],
134+
"source": [
135+
"%%python\n",
136+
"\n",
137+
"import cppyy\n",
138+
"\n",
139+
"a = cppyy.gbl.a\n",
140+
"b = cppyy.gbl.b\n",
141+
"c = cppyy.gbl.c\n",
142+
"\n",
143+
"def display_eigen(vec, name):\n",
144+
" print(\"Vector : \", name, \", Dimensions : \", len(vec))\n",
145+
" for x in vec:\n",
146+
" print(x)"
147+
]
148+
},
149+
{
150+
"cell_type": "markdown",
151+
"id": "07cfc277-4202-47b6-9859-dd4a651bd4a1",
152+
"metadata": {},
153+
"source": [
154+
"#### Setting values to the C++ Eigen Vector"
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": 6,
160+
"id": "3775e315-652d-4dea-8729-e36549524b62",
161+
"metadata": {},
162+
"outputs": [],
163+
"source": [
164+
"%%python\n",
165+
"\n",
166+
"for i in range(len(a)):\n",
167+
" a[i] = i - 1\n",
168+
"\n",
169+
"for i in range(len(b)):\n",
170+
" b[i] = i + 1\n",
171+
"\n",
172+
"for i in range(len(c)):\n",
173+
" c[i] = i - 1"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": 7,
179+
"id": "ca77d6ff-3428-4977-9072-9f2f2a904973",
180+
"metadata": {},
181+
"outputs": [
182+
{
183+
"name": "stdout",
184+
"output_type": "stream",
185+
"text": [
186+
"Vector : A , Dimensions : 2\n",
187+
"-1.0\n",
188+
"0.0\n",
189+
"Vector : B , Dimensions : 3\n",
190+
"1.0\n",
191+
"2.0\n",
192+
"3.0\n",
193+
"Vector : C , Dimensions : 4\n",
194+
"-1.0\n",
195+
"0.0\n",
196+
"1.0\n",
197+
"2.0\n"
198+
]
199+
}
200+
],
201+
"source": [
202+
"%%python\n",
203+
"\n",
204+
"display_eigen(a, \"A\")\n",
205+
"display_eigen(b, \"B\")\n",
206+
"display_eigen(c, \"C\")"
207+
]
208+
},
209+
{
210+
"cell_type": "markdown",
211+
"id": "e8c29a51-9897-4e16-9f6e-bacf918129cb",
212+
"metadata": {},
213+
"source": [
214+
"### Calling the Templated method of the C++ class from the Python side"
215+
]
216+
},
217+
{
218+
"cell_type": "code",
219+
"execution_count": 8,
220+
"id": "a01b4c6d-88ea-4cb1-a17e-716e1d813b1b",
221+
"metadata": {},
222+
"outputs": [],
223+
"source": [
224+
"%%python\n",
225+
"\n",
226+
"res2d = cppyy.gbl.EigenOperations[\"Eigen::Vector2d\"].PerformOperations(a)\n",
227+
"res3d = cppyy.gbl.EigenOperations[\"Eigen::Vector3d\"].PerformOperations(b)\n",
228+
"res4d = cppyy.gbl.EigenOperations[\"Eigen::Vector4d\"].PerformOperations(c)"
229+
]
230+
},
231+
{
232+
"cell_type": "code",
233+
"execution_count": 9,
234+
"id": "680cc0d4-3d7b-441c-8f71-3acdf1b9b3e2",
235+
"metadata": {},
236+
"outputs": [
237+
{
238+
"name": "stdout",
239+
"output_type": "stream",
240+
"text": [
241+
"Requirement already satisfied: matplotlib in /opt/conda/envs/.venv/lib/python3.10/site-packages (3.8.0)\n",
242+
"Requirement already satisfied: contourpy>=1.0.1 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (1.1.1)\n",
243+
"Requirement already satisfied: cycler>=0.10 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (0.11.0)\n",
244+
"Requirement already satisfied: fonttools>=4.22.0 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (4.42.1)\n",
245+
"Requirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (1.4.5)\n",
246+
"Requirement already satisfied: numpy<2,>=1.21 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (1.26.0)\n",
247+
"Requirement already satisfied: packaging>=20.0 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (23.1)\n",
248+
"Requirement already satisfied: pillow>=6.2.0 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (10.0.1)\n",
249+
"Requirement already satisfied: pyparsing>=2.3.1 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (3.1.1)\n",
250+
"Requirement already satisfied: python-dateutil>=2.7 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (2.8.2)\n",
251+
"Requirement already satisfied: six>=1.5 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)\n"
252+
]
253+
}
254+
],
255+
"source": [
256+
"!pip install matplotlib"
257+
]
258+
},
259+
{
260+
"cell_type": "code",
261+
"execution_count": 10,
262+
"id": "876beb2f-113e-4889-bd10-3e460d17181e",
263+
"metadata": {},
264+
"outputs": [],
265+
"source": [
266+
"%%python\n",
267+
"\n",
268+
"import matplotlib.pyplot as plt\n",
269+
"import numpy as np\n",
270+
"\n",
271+
"origin = [0, 0]\n",
272+
"\n",
273+
"np_res2 = np.array(list(res2d))\n",
274+
"np_res3 = np.array(list(res3d)) \n",
275+
"np_res4 = np.array(list(res4d)) \n",
276+
"\n",
277+
"plt.quiver(*origin, *np_res2, color=['r'], scale=5)\n",
278+
"plt.quiver(*origin, *np_res3, color=['b'], scale=5)\n",
279+
"\n",
280+
"\n",
281+
"plt.savefig(\"test.jpg\")\n",
282+
"plt.show()"
283+
]
284+
},
285+
{
286+
"cell_type": "markdown",
287+
"id": "fc77ec01-bd62-4a87-b2e1-76141623fc5c",
288+
"metadata": {},
289+
"source": [
290+
"<img src=\"./test.jpg\"/>"
291+
]
292+
},
293+
{
294+
"cell_type": "code",
295+
"execution_count": 12,
296+
"id": "4078ec69-0b4d-4350-8e6b-2d793bc27491",
297+
"metadata": {},
298+
"outputs": [
299+
{
300+
"name": "stdout",
301+
"output_type": "stream",
302+
"text": [
303+
"Vector : A , Dimensions : 2\n",
304+
"-1.0\n",
305+
"0.0\n",
306+
"Vector : B , Dimensions : 3\n",
307+
"0.26726124191242384\n",
308+
"0.5345224838248495\n",
309+
"0.8017837257372733\n",
310+
"Vector : C , Dimensions : 4\n",
311+
"-0.40824829046386313\n",
312+
"0.0\n",
313+
"0.40824829046386313\n",
314+
"0.8164965809277263\n"
315+
]
316+
}
317+
],
318+
"source": [
319+
"%%python\n",
320+
"\n",
321+
"display_eigen(res2d, \"A\")\n",
322+
"display_eigen(res3d, \"B\")\n",
323+
"display_eigen(res4d, \"C\")"
324+
]
325+
}
326+
],
327+
"metadata": {
328+
"kernelspec": {
329+
"display_name": "C++17",
330+
"language": "C++17",
331+
"name": "xcpp17"
332+
},
333+
"language_info": {
334+
"codemirror_mode": "text/x-c++src",
335+
"file_extension": ".cpp",
336+
"mimetype": "text/x-c++src",
337+
"name": "c++",
338+
"version": "17"
339+
}
340+
},
341+
"nbformat": 4,
342+
"nbformat_minor": 5
343+
}

‎notebooks/eigen_demo/test.jpg

13 KB
Loading

‎notebooks/image_CUDA_demo/img_in.jpg

512 KB
Loading

‎notebooks/image_CUDA_demo/img_out.jpg

34.9 KB
Loading
Lines changed: 376 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,376 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "9ba0128f-ef46-4b86-a4cd-02fba18d6516",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"#include <iostream>\n",
11+
"#include <cmath>\n",
12+
"#include <cuda_runtime.h>\n",
13+
"#include<vector>\n"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": 2,
19+
"id": "c63a0cf7-c79b-4281-8b76-e0ccad6c7b93",
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"__global__ void thresholdKernel(float* input, float* output, const int width, const int height) {\n",
24+
" const unsigned int col = threadIdx.x + blockIdx.x * blockDim.x;\n",
25+
" const unsigned int row = threadIdx.y + blockIdx.y * blockDim.y;\n",
26+
" if (row < height && col < width) {\n",
27+
" \n",
28+
" if(input[col + row * width] > 200)\n",
29+
" output[col + row * width] = input[col + row * width] * 2;\n",
30+
" else\n",
31+
" output[col + row * width] = input[col + row * width] * 0.4;\n",
32+
" }\n",
33+
"}"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": 3,
39+
"id": "c7454511-0b12-4022-ab97-c73f50c3c1f8",
40+
"metadata": {},
41+
"outputs": [],
42+
"source": [
43+
"const int width = 512; \n",
44+
"const int height = 512;"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 4,
50+
"id": "c639e359-7481-4fe0-8dfd-860885fd9044",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"float* h_input = new float[width * height];\n",
55+
"float* h_output = new float[width * height];"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 5,
61+
"id": "bf055a82-9dd8-4961-a271-b3e5a144c909",
62+
"metadata": {},
63+
"outputs": [
64+
{
65+
"name": "stdout",
66+
"output_type": "stream",
67+
"text": [
68+
"Requirement already satisfied: Pillow in /opt/conda/envs/.venv/lib/python3.10/site-packages (10.0.1)\n"
69+
]
70+
}
71+
],
72+
"source": [
73+
"!pip install Pillow"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": 6,
79+
"id": "d7c9c2b7-45be-4d22-90f9-4d61042e353f",
80+
"metadata": {},
81+
"outputs": [
82+
{
83+
"name": "stdout",
84+
"output_type": "stream",
85+
"text": [
86+
"Requirement already satisfied: numpy in /opt/conda/envs/.venv/lib/python3.10/site-packages (1.26.0)\n"
87+
]
88+
}
89+
],
90+
"source": [
91+
"!pip install numpy"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": 7,
97+
"id": "ac00db7b-867a-4ff0-8afa-2c13006b9f32",
98+
"metadata": {},
99+
"outputs": [
100+
{
101+
"name": "stdout",
102+
"output_type": "stream",
103+
"text": [
104+
"Requirement already satisfied: matplotlib in /opt/conda/envs/.venv/lib/python3.10/site-packages (3.8.0)\n",
105+
"Requirement already satisfied: contourpy>=1.0.1 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (1.1.1)\n",
106+
"Requirement already satisfied: cycler>=0.10 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (0.11.0)\n",
107+
"Requirement already satisfied: fonttools>=4.22.0 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (4.42.1)\n",
108+
"Requirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (1.4.5)\n",
109+
"Requirement already satisfied: numpy<2,>=1.21 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (1.26.0)\n",
110+
"Requirement already satisfied: packaging>=20.0 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (23.1)\n",
111+
"Requirement already satisfied: pillow>=6.2.0 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (10.0.1)\n",
112+
"Requirement already satisfied: pyparsing>=2.3.1 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (3.1.1)\n",
113+
"Requirement already satisfied: python-dateutil>=2.7 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from matplotlib) (2.8.2)\n",
114+
"Requirement already satisfied: six>=1.5 in /opt/conda/envs/.venv/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)\n"
115+
]
116+
}
117+
],
118+
"source": [
119+
"!pip install matplotlib"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": 8,
125+
"id": "a872a8cd-117b-4238-a4ea-e31e9dac48f3",
126+
"metadata": {},
127+
"outputs": [],
128+
"source": [
129+
"%%python\n",
130+
"\n",
131+
"from PIL import Image\n",
132+
"import numpy as np\n",
133+
"\n",
134+
"\n",
135+
"image = Image.open('img_in.jpg') \n",
136+
"image = image.resize((512, 512))\n",
137+
"\n",
138+
"image_array = np.array(image)\n",
139+
"if len(image_array.shape) == 3 and image_array.shape[2] == 3:\n",
140+
" image_array = np.array(image.convert('L'))"
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": 9,
146+
"id": "6a1cd914-db44-4e34-a9fb-5d2100b298b8",
147+
"metadata": {},
148+
"outputs": [
149+
{
150+
"name": "stdout",
151+
"output_type": "stream",
152+
"text": [
153+
"[[126 126 125 ... 84 84 84]\n",
154+
" [126 126 125 ... 84 84 84]\n",
155+
" [127 126 126 ... 84 84 84]\n",
156+
" ...\n",
157+
" [ 58 55 63 ... 19 15 8]\n",
158+
" [ 57 56 53 ... 23 17 16]\n",
159+
" [ 55 50 58 ... 5 16 16]]\n"
160+
]
161+
}
162+
],
163+
"source": [
164+
"%%python\n",
165+
"\n",
166+
"print(image_array)"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": 10,
172+
"id": "7545b5c9-c901-4599-abde-3ae5ac17e160",
173+
"metadata": {},
174+
"outputs": [],
175+
"source": [
176+
"void displayImgArray(float* input) {\n",
177+
" for (int i = 0; i < 3; i++) {\n",
178+
" std::cout << input[i] << \" \"; \n",
179+
" }\n",
180+
"\n",
181+
" std::cout << \" ... \";\n",
182+
"\n",
183+
" for (int i = width * height - 3; i < width * height; i++) {\n",
184+
" std::cout << input[i] << \" \"; \n",
185+
" }\n",
186+
"}"
187+
]
188+
},
189+
{
190+
"cell_type": "code",
191+
"execution_count": 11,
192+
"id": "a3c74a47-399e-4454-89f6-f4e8b0376a68",
193+
"metadata": {},
194+
"outputs": [],
195+
"source": [
196+
"%%python\n",
197+
"\n",
198+
"import cppyy\n",
199+
"\n",
200+
"img_list = image_array.flatten().tolist()\n",
201+
"img_vector = cppyy.gbl.std.vector['float'](img_list)"
202+
]
203+
},
204+
{
205+
"cell_type": "code",
206+
"execution_count": 12,
207+
"id": "ab6c16bd-c44a-4e4e-985a-12c5ca3256a8",
208+
"metadata": {},
209+
"outputs": [],
210+
"source": [
211+
"\n",
212+
"void setImg(const std::vector<float>& input) {\n",
213+
" if (h_input != nullptr) {\n",
214+
" delete[] h_input;\n",
215+
" }\n",
216+
"\n",
217+
" h_input = new float[input.size()];\n",
218+
"\n",
219+
" for (size_t i = 0; i < input.size(); i++) {\n",
220+
" h_input[i] = input[i]; // No casting needed\n",
221+
" }\n",
222+
"}\n"
223+
]
224+
},
225+
{
226+
"cell_type": "code",
227+
"execution_count": 13,
228+
"id": "a168bbab-fc67-41f5-85c1-f9980d75a00b",
229+
"metadata": {},
230+
"outputs": [],
231+
"source": [
232+
"std::vector<float> getOutput() {\n",
233+
" std::vector<float> res;\n",
234+
" for (size_t i = 0; i < width * height; i++) {\n",
235+
" res.push_back(h_output[i]);\n",
236+
" }\n",
237+
" return res;\n",
238+
"}"
239+
]
240+
},
241+
{
242+
"cell_type": "code",
243+
"execution_count": 14,
244+
"id": "2eec8b62-5500-45e4-9c02-9bc587288206",
245+
"metadata": {},
246+
"outputs": [],
247+
"source": [
248+
"%%python\n",
249+
"\n",
250+
"cppyy.gbl.setImg(img_vector)"
251+
]
252+
},
253+
{
254+
"cell_type": "code",
255+
"execution_count": 15,
256+
"id": "fd1bef06-d14e-4c82-8fa5-da14df696fe5",
257+
"metadata": {},
258+
"outputs": [
259+
{
260+
"name": "stdout",
261+
"output_type": "stream",
262+
"text": [
263+
"126 126 125 ... 5 16 16 "
264+
]
265+
}
266+
],
267+
"source": [
268+
"displayImgArray(h_input);"
269+
]
270+
},
271+
{
272+
"cell_type": "code",
273+
"execution_count": 16,
274+
"id": "ea90464f-ed8f-44f1-9336-99aa3dc85149",
275+
"metadata": {},
276+
"outputs": [],
277+
"source": [
278+
"float* d_input;\n",
279+
"float* d_output;\n",
280+
"\n",
281+
"cudaMalloc((void**)&d_input, width * height * sizeof(float));\n",
282+
"cudaMalloc((void**)&d_output, width * height * sizeof(float));\n",
283+
"\n",
284+
"cudaMemcpy(d_input, h_input, width * height * sizeof(float), cudaMemcpyHostToDevice);\n",
285+
"\n",
286+
"dim3 dimBlock(16, 16);\n",
287+
"dim3 dimGrid((width + dimBlock.x - 1) / dimBlock.x, (height + dimBlock.y - 1) / dimBlock.y);\n",
288+
"\n",
289+
"thresholdKernel<<<dimGrid, dimBlock>>>(d_input, d_output, width, height);\n",
290+
"\n",
291+
"cudaMemcpy(h_output, d_output, width * height * sizeof(float), cudaMemcpyDeviceToHost);\n",
292+
"\n",
293+
"cudaFree(d_input);\n",
294+
"cudaFree(d_output)"
295+
]
296+
},
297+
{
298+
"cell_type": "code",
299+
"execution_count": 17,
300+
"id": "7bf09ac1-ac6b-45e9-93c2-d4ae50897c2d",
301+
"metadata": {},
302+
"outputs": [
303+
{
304+
"name": "stdout",
305+
"output_type": "stream",
306+
"text": [
307+
"50.4 50.4 50 ... 2 6.4 6.4 "
308+
]
309+
}
310+
],
311+
"source": [
312+
"displayImgArray(h_output);"
313+
]
314+
},
315+
{
316+
"cell_type": "code",
317+
"execution_count": 18,
318+
"id": "3a97a53f-c2b7-4e24-a299-a6c0ec81b16e",
319+
"metadata": {},
320+
"outputs": [],
321+
"source": [
322+
"std::vector<float> blurredRes = getOutput();"
323+
]
324+
},
325+
{
326+
"cell_type": "code",
327+
"execution_count": 19,
328+
"id": "2586f437-295d-4b33-b18e-6f28628a7f81",
329+
"metadata": {},
330+
"outputs": [],
331+
"source": [
332+
"%%python\n",
333+
"\n",
334+
"k = np.array(cppyy.gbl.blurredRes, dtype = np.uint8)\n",
335+
"k.resize(512, 512)"
336+
]
337+
},
338+
{
339+
"cell_type": "code",
340+
"execution_count": 20,
341+
"id": "a8104074-7398-4653-a81a-827dd81a16b3",
342+
"metadata": {},
343+
"outputs": [],
344+
"source": [
345+
"%%python\n",
346+
"\n",
347+
"Image.fromarray(k).save(\"img_out.jpg\")"
348+
]
349+
},
350+
{
351+
"cell_type": "markdown",
352+
"id": "d0d2e15b-5a5f-4ed8-8f17-3fd1f3fc4daf",
353+
"metadata": {},
354+
"source": [
355+
"<img src=\"img_out.jpg\" align=right width=\"400\">\n",
356+
"<img src=\"img_in.jpg\" align=left width=\"400\">"
357+
]
358+
}
359+
],
360+
"metadata": {
361+
"kernelspec": {
362+
"display_name": "CUDA (C++17)",
363+
"language": "CUDA",
364+
"name": "cuda-xcpp17"
365+
},
366+
"language_info": {
367+
"codemirror_mode": "text/x-c++src",
368+
"file_extension": ".cpp",
369+
"mimetype": "text/x-c++src",
370+
"name": "c++",
371+
"version": "17"
372+
}
373+
},
374+
"nbformat": 4,
375+
"nbformat_minor": 5
376+
}

‎notebooks/images/xeus-cpp.png

12.4 KB
Loading

‎notebooks/index.ipynb

Lines changed: 721 additions & 0 deletions
Large diffs are not rendered by default.
23.3 KB
Loading
39.3 KB
Loading
Loading
51.1 KB
Loading
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
data :
2+
- 1.04202710058
3+
- 1.10726790452
4+
- 1.2913511148
5+
- 1.48485250951
6+
- 1.72825901034
7+
- 1.74216489744
8+
- 2.11672039768
9+
- 2.14529225112
10+
- 2.16029641405
11+
- 2.21269371128
12+
- 2.57709350237
13+
- 2.6682215744
14+
- 2.51641839428
15+
- 2.76034056782
16+
- 2.88131780617
17+
- 2.88373786518
18+
- 2.9448468727
19+
- 2.82866600131
20+
- 3.0006601946
21+
- 3.12920591669
22+
- 2.858361783
23+
- 2.83808170354
24+
- 2.68975330958
25+
- 2.66533185589
26+
- 2.81613499531
27+
- 2.81003612051
28+
- 2.88321849354
29+
- 2.69789264832
30+
- 2.4342229249
31+
- 2.23464791825
32+
- 2.30278776224
33+
- 2.02069770395
34+
- 1.94393985809
35+
- 1.82498398739
36+
- 1.52526230354
37+
- 1.86967808173
38+
- 1.18073207847
39+
- 1.10729605087
40+
- 0.916168349913
41+
- 0.678547664519
42+
- 0.562381751596
43+
- 0.355468474885
44+
- -0.155607486619
45+
- -0.287198661013
46+
- -0.602973173813
47+
- -0.292648661013
48+
- -0.602973173813
49+
- -0.924197686613
50+
- -1.2563221994129998
51+
- -1.5993467122129998
52+
- -1.9532712250129998
53+
- -2.3180957378129996
54+
- -2.6938202506129993
55+
- -3.0804447634129994
56+
- -3.4779692762129994
57+
- -3.8863937890129994
58+
- -4.305718301812999
59+
- -4.735942814612999
60+
- -5.177067327412999
61+
- -5.629091840212999
62+
- -6.0920163530129985
63+
- -6.565840865812999
64+
- -7.0505653786129985
65+
- -7.546189891412999
66+
- -8.052714404212999
67+
- -8.570138917012999
68+
- -9.098463429812998
69+
- -9.637687942612999
70+
- -10.187812455412999
71+
- -10.748836968212998
72+
- -11.320761481013
73+
- -11.903585993813
74+
- -12.497310506613
75+
- -13.101935019413
76+
- -13.717459532213
77+
- -14.343884045013
78+
- -14.981208557813002
79+
- -15.629433070613002
80+
- -16.288557583413002
81+
- -16.958582096213004
82+
- -17.639506609013004
83+
- -18.331331121813005
84+
- -19.034055634613004
85+
- -19.747680147413003
86+
- -20.472204660213006
87+
- -21.207629173013007
88+
- -21.95395368581301
89+
- -22.71117819861301
90+
- -23.47930271141301
91+
- -24.25832722421301
92+
- -25.04825173701301
93+
- -25.849076249813013
94+
- -26.660800762613015
95+
- -27.483425275413015
96+
- -28.316949788213016
97+
- -29.161374301013016
98+
- -30.01669881381302
99+
- -30.88292332661302
100+
- -31.760047839413023
101+
- -32.64807235221303
102+
- -33.54699686501303
103+
- -34.45682137781303
104+
- -35.37754589061303
105+
- -36.30917040341303
106+
- -37.251694916213026
107+
- -38.20511942901303
108+
- -39.169443941813036
109+
- -40.14466845461304
110+
- -41.13079296741304
111+
- -42.12781748021305
112+
- -43.13574199301305
113+
- -44.15456650581305
114+
- -45.18429101861305
115+
- -46.22491553141305
116+
- -47.276440044213054
117+
- -48.33886455701305
118+
- -49.41218906981305
119+
- -50.49641358261306
120+
- -51.591538095413064
121+
- -52.69756260821307
122+
- -53.81448712101307
123+
- -54.94231163381308
124+
- -56.08103614661308
125+
- -57.23066065941308
126+
- -58.391185172213085
127+
- -59.562609685013086
128+
- -60.74493419781309
129+
- -61.93815871061309
130+
- -63.14228322341309
131+
- -64.35730773621309
132+
- -65.5832322490131
133+
- -66.82005676181309
134+
- -68.0677812746131
135+
- -69.3264057874131
136+
- -70.5959303002131
137+
- -71.8763548130131
138+
- -73.1676793258131
139+
- -74.46990383861309
140+
- -75.7830283514131
141+
- -77.1070528642131
142+
- -78.4419773770131
143+
- -79.78780188981311
144+
- -81.14452640261311
145+
- -82.51215091541312
146+
- -83.89067542821311
18.7 KB
Loading
19.8 KB
Loading

‎notebooks/kalman_CUDA_demo/run_kf.ipynb

Lines changed: 1503 additions & 0 deletions
Large diffs are not rendered by default.

‎notebooks/omp/hello_world.ipynb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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>\n",
12+
"#include <clang/Interpreter/CppInterOp.h>"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 2,
18+
"id": "ef1cd58a-672c-4a6f-843a-6c88fc4911f3",
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"Cpp::LoadLibrary(\"libomp\");\n",
23+
" "
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 3,
29+
"id": "c2b754ad-9553-4a42-b990-f990a9a269ed",
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"int main() {\n",
34+
" int max_threads = omp_get_max_threads();\n",
35+
"\n",
36+
" printf(\"max threads: %d\\n\", max_threads);\n",
37+
" omp_set_num_threads(max_threads);\n",
38+
"\n",
39+
"#pragma omp parallel\n",
40+
" {\n",
41+
" int id = omp_get_thread_num();\n",
42+
" printf(\"Hello World from thread = %d with %d threads\\n\", id, omp_get_num_threads());\n",
43+
" }\n",
44+
"\n",
45+
" printf(\"all done, with hopefully %d threads\\n\", max_threads);\n",
46+
"}"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 4,
52+
"id": "a37a13d4-fc82-496e-8f42-9e718a8c2aa0",
53+
"metadata": {},
54+
"outputs": [
55+
{
56+
"name": "stdout",
57+
"output_type": "stream",
58+
"text": [
59+
"max threads: 8\n",
60+
"Hello World from thread = 2 with 8 threads\n",
61+
"Hello World from thread = 0 with 8 threads\n",
62+
"Hello World from thread = 1 with 8 threads\n",
63+
"Hello World from thread = 7 with 8 threads\n",
64+
"Hello World from thread = 4 with 8 threads\n",
65+
"Hello World from thread = 5 with 8 threads\n",
66+
"Hello World from thread = 3 with 8 threads\n",
67+
"Hello World from thread = 6 with 8 threads\n",
68+
"all done, with hopefully 8 threads\n"
69+
]
70+
}
71+
],
72+
"source": [
73+
"main();"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"id": "af54945c-2412-4aee-bbfc-a2f6fc72d23f",
80+
"metadata": {},
81+
"outputs": [],
82+
"source": []
83+
}
84+
],
85+
"metadata": {
86+
"kernelspec": {
87+
"display_name": "C++17 (+OpenMP)",
88+
"language": "C++17",
89+
"name": "omp-xcpp17"
90+
},
91+
"language_info": {
92+
"codemirror_mode": "text/x-c++src",
93+
"file_extension": ".cpp",
94+
"mimetype": "text/x-c++src",
95+
"name": "c++",
96+
"version": "17"
97+
}
98+
},
99+
"nbformat": 4,
100+
"nbformat_minor": 5
101+
}

‎notebooks/omp/linked_list.ipynb

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

‎notebooks/omp/mandel.ipynb

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "5059dbdd-821d-498a-8716-eb0fcf8a8f5f",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"#include <stdio.h>\n",
11+
"#include <stdlib.h>\n",
12+
"#include <math.h>\n",
13+
"#include <omp.h>"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"id": "8b66f96a-14ef-4f23-8024-bcfc42b31e4e",
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"#define NPOINTS 1000\n",
24+
"#define MAXITER 1000"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"id": "d89dd57c-fe19-4233-a33a-df9b24fae98a",
31+
"metadata": {},
32+
"outputs": [],
33+
"source": [
34+
"int numoutside = 0;"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": null,
40+
"id": "5c35c479-2f79-46b7-bc66-24be6b1694e0",
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"void testpoint(double creal, double cimag) {\n",
45+
" // iterate z=z*z+c, until |z| > 2 when point is known to be outside set\n",
46+
" // If loop count reaches MAXITER, point is considered to be inside the set\n",
47+
"\n",
48+
" double zreal, zimag, temp;\n",
49+
" int iter;\n",
50+
" zreal = creal;\n",
51+
" zimag = cimag;\n",
52+
"\n",
53+
" for (iter = 0; iter < MAXITER; iter++) {\n",
54+
" temp = (zreal * zreal) - (zimag * zimag) + creal;\n",
55+
" zimag = zreal * zimag * 2 + cimag;\n",
56+
" zreal = temp;\n",
57+
" if ((zreal * zreal + zimag * zimag) > 4.0) {\n",
58+
" numoutside++;\n",
59+
" break;\n",
60+
" }\n",
61+
" }\n",
62+
"}"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": null,
68+
"id": "ea116fef-7d05-4e29-97a1-55c85c7241d8",
69+
"metadata": {},
70+
"outputs": [],
71+
"source": [
72+
"int main() {\n",
73+
" int i, j;\n",
74+
" double area, error, eps = 1.0e-5;\n",
75+
" double cimag, creal;\n",
76+
" // Loop over grid of points in the complex plane which contains the Mandelbrot set,\n",
77+
" // testing each point to see whether it is inside or outside the set.\n",
78+
"\n",
79+
"#pragma omp parallel for private(eps)\n",
80+
" for (i = 0; i < NPOINTS; i++) {\n",
81+
" for (j = 0; j < NPOINTS; j++) {\n",
82+
" creal = -2.0 + 2.5 * (double) (i) / (double) (NPOINTS) + eps;\n",
83+
" cimag = 1.125 * (double) (j) / (double) (NPOINTS) + eps;\n",
84+
" testpoint(creal, cimag);\n",
85+
" }\n",
86+
" }\n",
87+
"\n",
88+
" // Calculate area of set and error estimate and output the results\n",
89+
" area = 2.0 * 2.5 * 1.125 * (double) (NPOINTS * NPOINTS - numoutside) / (double) (NPOINTS * NPOINTS);\n",
90+
" error = area / (double) NPOINTS;\n",
91+
"\n",
92+
" printf(\"Area of Mandlebrot set = %12.8f +/- %12.8f\\n\", area, error);\n",
93+
" printf(\"Correct answer should be around 1.510659\\n\");\n",
94+
"}"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": null,
100+
"id": "39cf129c-8106-4e67-a2f1-1a7fff17cd38",
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"main();"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"id": "7bcf082a-e0a5-4260-8729-cbfab515cc6a",
111+
"metadata": {},
112+
"outputs": [],
113+
"source": []
114+
}
115+
],
116+
"metadata": {
117+
"kernelspec": {
118+
"display_name": "C++14",
119+
"language": "C++14",
120+
"name": "xcpp14"
121+
},
122+
"language_info": {
123+
"codemirror_mode": "text/x-c++src",
124+
"file_extension": ".cpp",
125+
"mimetype": "text/x-c++src",
126+
"name": "c++",
127+
"version": "14"
128+
}
129+
},
130+
"nbformat": 4,
131+
"nbformat_minor": 5
132+
}

‎notebooks/omp/pi_integral.ipynb

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "d8967ce2-994c-441e-b796-4099c6c6853c",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"#include <omp.h>\n",
11+
"#include <stdio.h>\n",
12+
"#include </home/jovyan/CppInterOp/include/clang/Interpreter/CppInterOp.h>"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": null,
18+
"id": "93b89565-44fe-4729-980b-d4f897161b0b",
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"Cpp::LoadLibrary(\"libomp\");"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": null,
28+
"id": "9078ac79-ca50-4fef-b785-37f35fec3cab",
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"static long num_steps = 100000000;\n",
33+
"double step;"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"id": "f3c10995-6f29-4d71-9e61-1993ca9d1cc9",
40+
"metadata": {},
41+
"outputs": [],
42+
"source": [
43+
"int main() {\n",
44+
" int i, j, num_threads_allocated;\n",
45+
" double x, pi, sum = 0.0;\n",
46+
" double start_time, run_time;\n",
47+
"\n",
48+
" step = 1.0 / (double)num_steps;\n",
49+
" printf(\"Num threads available: %d\\n\", omp_get_max_threads());\n",
50+
" for (i = 1; i <= 4; i++) {\n",
51+
" sum = 0.0;\n",
52+
" omp_set_num_threads(i);\n",
53+
" start_time = omp_get_wtime();\n",
54+
"#pragma omp parallel\n",
55+
" {\n",
56+
" num_threads_allocated = omp_get_num_threads();\n",
57+
"#pragma omp single\n",
58+
" printf(\"Num threads allocated for this run: %d\\n\", num_threads_allocated);\n",
59+
"\n",
60+
"#pragma omp for reduction(+ : sum)\n",
61+
" for (j = 1; j <= num_steps; j++) {\n",
62+
" x = (j - 0.5) * step;\n",
63+
" sum = sum + 4.0 / (1.0 + x * x);\n",
64+
" }\n",
65+
" }\n",
66+
"\n",
67+
" pi = step * sum;\n",
68+
" run_time = omp_get_wtime() - start_time;\n",
69+
" printf(\"pi is %f in %f seconds using %d threads\\n\\n\", pi, run_time, num_threads_allocated);\n",
70+
" }\n",
71+
"}"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"id": "0f84442a-d947-4860-bd3c-aeeea963b419",
78+
"metadata": {},
79+
"outputs": [],
80+
"source": [
81+
"main();"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": null,
87+
"id": "a7066da1-729b-4230-980f-9be9430c19d6",
88+
"metadata": {},
89+
"outputs": [],
90+
"source": []
91+
}
92+
],
93+
"metadata": {
94+
"kernelspec": {
95+
"display_name": "C++14",
96+
"language": "C++14",
97+
"name": "xcpp14"
98+
},
99+
"language_info": {
100+
"codemirror_mode": "text/x-c++src",
101+
"file_extension": ".cpp",
102+
"mimetype": "text/x-c++src",
103+
"name": "c++",
104+
"version": "14"
105+
}
106+
},
107+
"nbformat": 4,
108+
"nbformat_minor": 5
109+
}

‎notebooks/openmp-demo.ipynb

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "b0c15570-ee24-42ed-b61f-11a3fc858b2d",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"#include <iostream>\n",
11+
"#include \"omp.h\"\n",
12+
"#include </home/jovyan/CppInterOp/include/clang/Interpreter/CppInterOp.h>"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": null,
18+
"id": "1b4dac8e-3ad2-46eb-b801-ba717e664b93",
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"Cpp::LoadLibrary(\"libomp\");"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": null,
28+
"id": "5001e441-1fa5-4bdc-9fa5-2ca103ae484f",
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"void example1() {\n",
33+
" std::cout << \"Hello World!\" << std::endl;\n",
34+
"}\n",
35+
"example1();"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"id": "53fb7656-b72e-42bc-ade7-2ae2077142da",
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"void example2() {\n",
46+
" #pragma omp parallel\n",
47+
" {\n",
48+
" std::cout << \"Hello World!\" << std::endl;\n",
49+
" }\n",
50+
"}\n",
51+
"example2();"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"id": "efcdfdb6-a60b-46af-8194-75ef9cc0e27f",
58+
"metadata": {},
59+
"outputs": [],
60+
"source": [
61+
"void example3() {\n",
62+
" #pragma omp parallel\n",
63+
" {\n",
64+
" std::cout << \"Hello World! (\" << omp_get_thread_num() << \")\" << std::endl;\n",
65+
" }\n",
66+
"}\n",
67+
"example3();"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": null,
73+
"id": "d86a9efa-ba28-4cb6-bbfc-abc00ee63506",
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"void example4() {\n",
78+
" #pragma omp parallel\n",
79+
" {\n",
80+
" std::cout << \"Hello World! (\" << omp_get_thread_num() << \")\" << std::endl;\n",
81+
" }\n",
82+
"\n",
83+
" std::cout << \"This is another message! (\" << omp_get_thread_num() << \")\" << std::endl;\n",
84+
"\n",
85+
" #pragma omp parallel num_threads(2)\n",
86+
" {\n",
87+
" std::cout << \"Goodbye World! (\" << omp_get_thread_num() << \")\" << std::endl;\n",
88+
" }\n",
89+
"}\n",
90+
"example4();"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"id": "5557e01a-7c7d-4b54-8545-962ad11027df",
97+
"metadata": {},
98+
"outputs": [],
99+
"source": [
100+
"void example5() {\n",
101+
" double start_time = omp_get_wtime();\n",
102+
" double start_loop;\n",
103+
" \n",
104+
" const int N = 1000000000;\n",
105+
" int* a = new int[N];\n",
106+
" int* b = new int[N];\n",
107+
" \n",
108+
" start_loop = omp_get_wtime();\n",
109+
" #pragma omp parallel for\n",
110+
" for (int i=0; i<N; i++) {\n",
111+
" a[i] = 1.0;\n",
112+
" }\n",
113+
" std::cout << \"Initialize a[] time: \" << omp_get_wtime()-start_loop << std::endl;\n",
114+
"\n",
115+
" start_loop = omp_get_wtime();\n",
116+
" #pragma omp parallel for\n",
117+
" for (int i=0; i<N; i++) {\n",
118+
" b[i] = 1.0 + double(i);\n",
119+
" }\n",
120+
" std::cout << \"Initialize b[] time: \" << omp_get_wtime()-start_loop << std::endl;\n",
121+
"\n",
122+
" start_loop = omp_get_wtime();\n",
123+
" #pragma omp parallel for\n",
124+
" for (int i=0; i<N; i++) {\n",
125+
" a[i] = a[i] + b[i];\n",
126+
" }\n",
127+
" std::cout << \"Add arrays time: \" << omp_get_wtime()-start_loop << std::endl;\n",
128+
" \n",
129+
" start_loop = omp_get_wtime();\n",
130+
" double average = 0.0;\n",
131+
" #pragma omp parallel for reduction(+:average)\n",
132+
" for (int i=0; i<N; i++) {\n",
133+
" average += a[i];\n",
134+
" }\n",
135+
" average = average/double(N);\n",
136+
" std::cout << \"Average result time: \" << omp_get_wtime()-start_loop << std::endl;\n",
137+
" \n",
138+
" std::cout << \"Average: \" << average << std::endl;\n",
139+
"\n",
140+
" std::cout << \"Total time: \" << omp_get_wtime()-start_time << std::endl;\n",
141+
"}\n",
142+
"example5();"
143+
]
144+
}
145+
],
146+
"metadata": {
147+
"kernelspec": {
148+
"display_name": "C++11 (with OpenMP)",
149+
"language": "C++11",
150+
"name": "xcpp11"
151+
},
152+
"language_info": {
153+
"codemirror_mode": "text/x-c++src",
154+
"file_extension": ".cpp",
155+
"mimetype": "text/x-c++src",
156+
"name": "c++",
157+
"version": "11"
158+
}
159+
},
160+
"nbformat": 4,
161+
"nbformat_minor": 5
162+
}

‎notebooks/python_cpp_plot.png

20.4 KB
Loading

‎notebooks/smallptCuda.ipynb

Lines changed: 506 additions & 0 deletions
Large diffs are not rendered by default.

‎notebooks/vectorAddCuda.ipynb

Lines changed: 1016 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.