diff --git a/notebooks/ProjectMockup.ipynb b/notebooks/ProjectMockup.ipynb new file mode 100644 index 00000000..e1070353 --- /dev/null +++ b/notebooks/ProjectMockup.ipynb @@ -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": [ + ":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& 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 +} diff --git a/notebooks/image_CUDA_demo/img_in.jpg b/notebooks/image_CUDA_demo/img_in.jpg new file mode 100644 index 00000000..08fc8254 Binary files /dev/null and b/notebooks/image_CUDA_demo/img_in.jpg differ diff --git a/notebooks/image_CUDA_demo/img_out.jpg b/notebooks/image_CUDA_demo/img_out.jpg new file mode 100644 index 00000000..10ed414f Binary files /dev/null and b/notebooks/image_CUDA_demo/img_out.jpg differ diff --git a/notebooks/gaussian_CUDA_demo/gaussian_test.ipynb b/notebooks/image_CUDA_demo/threshold_test.ipynb similarity index 77% rename from notebooks/gaussian_CUDA_demo/gaussian_test.ipynb rename to notebooks/image_CUDA_demo/threshold_test.ipynb index fbd26037..9e59283c 100644 --- a/notebooks/gaussian_CUDA_demo/gaussian_test.ipynb +++ b/notebooks/image_CUDA_demo/threshold_test.ipynb @@ -13,83 +13,29 @@ "#include\n" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "34ab442f-c7bd-45d5-8678-adad2179f1f3", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "code", "execution_count": 2, - "id": "c126a964-07c2-4563-9a57-5e21ed7ebd93", - "metadata": {}, - "outputs": [], - "source": [ - "const int kernelWidth = 3;\n", - "float kernel[] = {1, 2, 1, 2, 4, 2, 1, 2, 1};\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "497d50c2-f26b-4c1c-b7b2-d3bb9c49a6be", - "metadata": {}, - "outputs": [], - "source": [ - "// const int half = kernelWidth / 2;\n", - "// float blur = 0.0;\n", - "// for(int i = -half; i <= half; i++) {\n", - "// for(int j = -half; j <= half; j++) {\n", - "\n", - "// const unsigned int y = max(0, min(height - 1, row + i));\n", - "// const unsigned int x = max(0, min(width - 1, col + j));\n", - "\n", - "// const float w = kernel[(j + half) + (i + half) * kernelWidth];\n", - "// blur += w * input[x + y * width];\n", - "// }\n", - "// }" - ] - }, - { - "cell_type": "code", - "execution_count": 4, "id": "c63a0cf7-c79b-4281-8b76-e0ccad6c7b93", "metadata": {}, "outputs": [], "source": [ - "__global__ void gaussianBlur(float* input, float* output, const int width, const int height, const float *kernel, const int kernelWidth) {\n", + "__global__ void thresholdKernel(float* input, float* output, const int width, const int height) {\n", " const unsigned int col = threadIdx.x + blockIdx.x * blockDim.x;\n", " const unsigned int row = threadIdx.y + blockIdx.y * blockDim.y;\n", - "\n", - " if(row < height && col < width) {\n", - "// float sum = 0.0f;\n", - "// int halfKernelWidth = kernelWidth / 2;\n", - "\n", - "// // Convolve the pixel with the kernel\n", - "// for(int i = -halfKernelWidth; i <= halfKernelWidth; i++) {\n", - "// for(int j = -halfKernelWidth; j <= halfKernelWidth; j++) {\n", - "// int curRow = min(max(row + i, 0), height - 1);\n", - "// int curCol = min(max(col + j, 0), width - 1);\n", - "\n", - "// float pixelValue = input[curCol + curRow * width];\n", - "// float kernelValue = kernel[(j + halfKernelWidth) + (i + halfKernelWidth) * kernelWidth];\n", - "\n", - "// sum += pixelValue * kernelValue;\n", - "// }\n", - "// }\n", - "\n", - " output[col + row * width] = input[col + row * width];\n", + " if (row < height && col < width) {\n", + " \n", + " if(input[col + row * width] > 200)\n", + " output[col + row * width] = input[col + row * width] * 2;\n", + " else\n", + " output[col + row * width] = input[col + row * width] * 0.4;\n", " }\n", "}" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "id": "c7454511-0b12-4022-ab97-c73f50c3c1f8", "metadata": {}, "outputs": [], @@ -100,7 +46,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 4, "id": "c639e359-7481-4fe0-8dfd-860885fd9044", "metadata": {}, "outputs": [], @@ -111,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 5, "id": "bf055a82-9dd8-4961-a271-b3e5a144c909", "metadata": {}, "outputs": [ @@ -129,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 6, "id": "d7c9c2b7-45be-4d22-90f9-4d61042e353f", "metadata": {}, "outputs": [ @@ -147,7 +93,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "id": "ac00db7b-867a-4ff0-8afa-2c13006b9f32", "metadata": {}, "outputs": [ @@ -175,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 8, "id": "a872a8cd-117b-4238-a4ea-e31e9dac48f3", "metadata": {}, "outputs": [], @@ -186,7 +132,7 @@ "import numpy as np\n", "\n", "\n", - "image = Image.open('test1.jpg') \n", + "image = Image.open('img_in.jpg') \n", "image = image.resize((512, 512))\n", "\n", "image_array = np.array(image)\n", @@ -196,7 +142,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 9, "id": "6a1cd914-db44-4e34-a9fb-5d2100b298b8", "metadata": {}, "outputs": [ @@ -222,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 10, "id": "7545b5c9-c901-4599-abde-3ae5ac17e160", "metadata": {}, "outputs": [], @@ -242,7 +188,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 11, "id": "a3c74a47-399e-4454-89f6-f4e8b0376a68", "metadata": {}, "outputs": [], @@ -257,7 +203,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 12, "id": "ab6c16bd-c44a-4e4e-985a-12c5ca3256a8", "metadata": {}, "outputs": [], @@ -278,7 +224,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 13, "id": "a168bbab-fc67-41f5-85c1-f9980d75a00b", "metadata": {}, "outputs": [], @@ -294,7 +240,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 14, "id": "2eec8b62-5500-45e4-9c02-9bc587288206", "metadata": {}, "outputs": [], @@ -306,7 +252,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 15, "id": "fd1bef06-d14e-4c82-8fa5-da14df696fe5", "metadata": {}, "outputs": [ @@ -324,7 +270,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 16, "id": "ea90464f-ed8f-44f1-9336-99aa3dc85149", "metadata": {}, "outputs": [], @@ -340,7 +286,7 @@ "dim3 dimBlock(16, 16);\n", "dim3 dimGrid((width + dimBlock.x - 1) / dimBlock.x, (height + dimBlock.y - 1) / dimBlock.y);\n", "\n", - "gaussianBlur<<>>(d_input, d_output, width, height, kernel, kernelWidth);\n", + "thresholdKernel<<>>(d_input, d_output, width, height);\n", "\n", "cudaMemcpy(h_output, d_output, width * height * sizeof(float), cudaMemcpyDeviceToHost);\n", "\n", @@ -350,7 +296,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 17, "id": "7bf09ac1-ac6b-45e9-93c2-d4ae50897c2d", "metadata": {}, "outputs": [ @@ -358,7 +304,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "126 126 125 ... 5 16 16 " + "50.4 50.4 50 ... 2 6.4 6.4 " ] } ], @@ -368,7 +314,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 18, "id": "3a97a53f-c2b7-4e24-a299-a6c0ec81b16e", "metadata": {}, "outputs": [], @@ -378,7 +324,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 19, "id": "2586f437-295d-4b33-b18e-6f28628a7f81", "metadata": {}, "outputs": [], @@ -391,31 +337,24 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 20, "id": "a8104074-7398-4653-a81a-827dd81a16b3", "metadata": {}, "outputs": [], "source": [ "%%python\n", "\n", - "Image.fromarray(k).save(\"test_out.png\")" + "Image.fromarray(k).save(\"img_out.jpg\")" ] }, { "cell_type": "markdown", - "id": "eda04921-db73-418c-98d6-84b8c0d72222", + "id": "d0d2e15b-5a5f-4ed8-8f17-3fd1f3fc4daf", "metadata": {}, "source": [ - "" + "\n", + "" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f5ae07c0-639f-4241-b063-4403e9677434", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {