Skip to content

Commit 7059770

Browse files
Add some examples
1 parent aba3401 commit 7059770

30 files changed

+8030
-0
lines changed

notebooks/ProjectMockup.ipynb

+249
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+
}

0 commit comments

Comments
 (0)