Skip to content

Commit

Permalink
Add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-penev committed Oct 29, 2023
1 parent aeb6b00 commit 5ff07e8
Show file tree
Hide file tree
Showing 30 changed files with 8,030 additions and 0 deletions.
249 changes: 249 additions & 0 deletions notebooks/ProjectMockup.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "1c4fd5b5-1b9e-4a8e-82b3-6ab214c203ea",
"metadata": {},
"source": [
"## Project Mockup"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "54a52e27-3d9d-4574-990b-329a47d4bf13",
"metadata": {},
"outputs": [],
"source": [
"struct S { double val = 1.0; };"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b9e80eda-8924-4f3c-bbc0-7b74ba9b9a36",
"metadata": {},
"outputs": [],
"source": [
"%%python\n",
"\n",
"python_vec = cppyy.gbl.std.vector(cppyy.gbl.S)(5)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "697760dd-200e-4fa6-85b2-f1786215eda3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.0\n"
]
}
],
"source": [
"%%python\n",
"\n",
"print(python_vec[3].val)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d581f6cc-2737-4c11-b37a-25bb5296936a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.Derived object at 0x7fffdf639e40>\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"<string>:1: RuntimeWarning: class \"S\" has no virtual destructor\n"
]
}
],
"source": [
"%%python\n",
"\n",
"class Derived(cppyy.gbl.S):\n",
" def __init__(self):\n",
" val = 0\n",
"res = Derived()\n",
"print(res)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "f20feaa6-62a2-4805-853b-268b1c1832ac",
"metadata": {},
"outputs": [],
"source": [
"__global__ void arr_sum(int n, double *x, double *sum) {\n",
" for(int i = 0; i < n; i++)\n",
" *sum +=x[i];\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "cfd92edb-41e0-4bb8-b7a1-852782964423",
"metadata": {},
"outputs": [],
"source": [
"int n = 5;\n",
"double h_sum;\n",
"double *x = new double[n];"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "90480051-7a57-4145-85c4-ca3bdc8e101f",
"metadata": {},
"outputs": [],
"source": [
"void setData(const std::vector<S>& a) {\n",
" int i = 0;\n",
" for(auto &s : a) {\n",
" x[i] = s.val;\n",
" i++;\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "9d9913b7-db39-4c76-8939-efbca9256143",
"metadata": {},
"outputs": [],
"source": [
"%%python\n",
"\n",
"data_list = [1.0, 2.0, 3.0, 4.0, 5.0]\n",
"for c, i in enumerate(data_list):\n",
" python_vec[c].val = i\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "846901c7-0cbb-4978-9efe-7f5870b939a7",
"metadata": {},
"outputs": [],
"source": [
"%%python\n",
"\n",
"cppyy.gbl.setData(python_vec)\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "f57c46fa-addb-4d80-8cc3-29a464911fdc",
"metadata": {},
"outputs": [],
"source": [
"double *d_x, *d_sum;\n",
"cudaMalloc((void **)&d_x, n * sizeof(double));\n",
"cudaMalloc((void **)&d_sum, sizeof(double));"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d617ee2c-d9b7-4c4f-8ef2-051df37b2737",
"metadata": {},
"outputs": [],
"source": [
"cudaMemcpy(d_x, x, n * sizeof(double), cudaMemcpyHostToDevice);\n",
"cudaMemcpy(d_sum, &h_sum, sizeof(double), cudaMemcpyHostToDevice);"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "8417b211-e7f9-448d-969a-27fc0eb32697",
"metadata": {},
"outputs": [],
"source": [
"arr_sum<<<1, 1>>>(n, d_x, d_sum);"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "4374b687-31a5-4b85-8d66-e738956814b4",
"metadata": {},
"outputs": [],
"source": [
"cudaMemcpy(&h_sum, d_sum, sizeof(double), cudaMemcpyDeviceToHost);"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "8198bfdf-0ff7-4e27-ba6b-087833055794",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sum: 15\n"
]
}
],
"source": [
"std::cout << \"Sum: \" << h_sum << std::endl;"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "33ffcf05-58cc-4212-aef7-4a5813fdc178",
"metadata": {},
"outputs": [],
"source": [
"delete[] x;\n",
"cudaFree(d_x);\n",
"cudaFree(d_sum);"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "add835ef-6196-47d8-be75-14b872438de1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "CUDA (C++17)",
"language": "CUDA",
"name": "cuda-xcpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "17"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 5ff07e8

Please sign in to comment.