Skip to content

Commit 0bcefd0

Browse files
committed
Add SVD example
1 parent 7e503d0 commit 0bcefd0

File tree

1 file changed

+161
-12
lines changed

1 file changed

+161
-12
lines changed

source-code/gpu/scikit_cuda.ipynb

Lines changed: 161 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,16 @@
1818
},
1919
{
2020
"cell_type": "code",
21-
"execution_count": 1,
21+
"execution_count": 2,
2222
"id": "f95a5430-cc82-4e33-9d25-066e558c87b8",
2323
"metadata": {},
24-
"outputs": [
25-
{
26-
"name": "stderr",
27-
"output_type": "stream",
28-
"text": [
29-
"/home/gjb/miniconda3/envs/cuda/lib/python3.9/site-packages/skcuda/cublas.py:284: UserWarning: creating CUBLAS context to get version number\n",
30-
" warnings.warn('creating CUBLAS context to get version number')\n"
31-
]
32-
}
33-
],
24+
"outputs": [],
3425
"source": [
3526
"import numpy as np\n",
3627
"import pycuda.autoinit\n",
3728
"from pycuda import gpuarray\n",
3829
"from skcuda import cublas\n",
30+
"from skcuda import linalg\n",
3931
"import sys\n",
4032
"from time import time"
4133
]
@@ -505,7 +497,9 @@
505497
{
506498
"cell_type": "markdown",
507499
"id": "89d7ce40-e25b-4f7c-ae7a-3e67b580486c",
508-
"metadata": {},
500+
"metadata": {
501+
"tags": []
502+
},
509503
"source": [
510504
"### Benchmarking"
511505
]
@@ -702,6 +696,161 @@
702696
"source": [
703697
"dev_time"
704698
]
699+
},
700+
{
701+
"cell_type": "markdown",
702+
"id": "dfb43514-7195-4292-b1df-f1a6eb4eb9e0",
703+
"metadata": {},
704+
"source": [
705+
"# CuSolver"
706+
]
707+
},
708+
{
709+
"cell_type": "markdown",
710+
"id": "d05fb7e8-8064-4159-b4de-3a230bf0e8ff",
711+
"metadata": {},
712+
"source": [
713+
"The CuSolver library contains many algorithms for linear algebra such as matrix decomposition, linear regression and solving systems of linear equations."
714+
]
715+
},
716+
{
717+
"cell_type": "markdown",
718+
"id": "133f7314-bc89-4785-b829-b4b0b5de9abd",
719+
"metadata": {},
720+
"source": [
721+
"## Singular Value Decomposition (SVD)"
722+
]
723+
},
724+
{
725+
"cell_type": "markdown",
726+
"id": "348a5c76-aa59-461a-947a-babc0b55a29a",
727+
"metadata": {},
728+
"source": [
729+
"To illustrate some aspects of the library's usage, we will perform a Singular Value Decomposition (SVD) of a large square matrix $A$."
730+
]
731+
},
732+
{
733+
"cell_type": "code",
734+
"execution_count": 26,
735+
"id": "e8c1b888-702c-4ef7-ae2b-afa3ebe92456",
736+
"metadata": {},
737+
"outputs": [],
738+
"source": [
739+
"A_nr_rows, A_nr_cols = 2_000, 2_000\n",
740+
"A = np.random.uniform(size=(A_nr_rows, A_nr_cols)).astype(np.float32)\n",
741+
"A_dev = gpuarray.to_gpu(A)"
742+
]
743+
},
744+
{
745+
"cell_type": "markdown",
746+
"id": "eb0272e4-6d23-441c-957b-53a9652bd294",
747+
"metadata": {},
748+
"source": [
749+
"### Initialization"
750+
]
751+
},
752+
{
753+
"cell_type": "markdown",
754+
"id": "eb92dd10-b5ba-422f-9969-2bb81afa026e",
755+
"metadata": {},
756+
"source": [
757+
"The `linalg` module has to be initialized before it is used to perform computations."
758+
]
759+
},
760+
{
761+
"cell_type": "code",
762+
"execution_count": 10,
763+
"id": "9ed7e255-9e73-48e5-a392-6ff9796f0390",
764+
"metadata": {},
765+
"outputs": [],
766+
"source": [
767+
"linalg.init()"
768+
]
769+
},
770+
{
771+
"cell_type": "markdown",
772+
"id": "100c5d2d-59ad-4992-be0e-4541d66341f2",
773+
"metadata": {},
774+
"source": [
775+
"### Computation"
776+
]
777+
},
778+
{
779+
"cell_type": "markdown",
780+
"id": "2a00e713-2914-463a-a19b-af7a6a9cbce4",
781+
"metadata": {},
782+
"source": [
783+
"Now we can perform the SVD of the matrix $A$ which results in two device matrices $U$ and $V^t$ as well as a vector $s$."
784+
]
785+
},
786+
{
787+
"cell_type": "code",
788+
"execution_count": 27,
789+
"id": "54715e9a-cbb6-4704-942a-00089ea6dd75",
790+
"metadata": {},
791+
"outputs": [],
792+
"source": [
793+
"U_dev, s_dev, Vt_dev = linalg.svd(A_dev, lib='cusolver')"
794+
]
795+
},
796+
{
797+
"cell_type": "markdown",
798+
"id": "1df63456-9965-4014-81aa-210e85ac3371",
799+
"metadata": {},
800+
"source": [
801+
"Getting the vector and matrices from the device, we can verify the decomposition by reconstructing $A$."
802+
]
803+
},
804+
{
805+
"cell_type": "code",
806+
"execution_count": 28,
807+
"id": "03db8ca2-a606-48e2-8550-7f624d28cc1f",
808+
"metadata": {},
809+
"outputs": [],
810+
"source": [
811+
"U, s, Vt = U_dev.get(), s_dev.get(), Vt_dev.get()"
812+
]
813+
},
814+
{
815+
"cell_type": "code",
816+
"execution_count": 29,
817+
"id": "528acf0f-8aa6-4abd-bccf-6601617810ab",
818+
"metadata": {},
819+
"outputs": [],
820+
"source": [
821+
"S = np.diag(s)"
822+
]
823+
},
824+
{
825+
"cell_type": "code",
826+
"execution_count": 30,
827+
"id": "1fbb598e-8e16-4922-b695-42cf5c903e97",
828+
"metadata": {},
829+
"outputs": [],
830+
"source": [
831+
"A_reconstructed = U @ S @ Vt"
832+
]
833+
},
834+
{
835+
"cell_type": "code",
836+
"execution_count": 32,
837+
"id": "ff57f0de-b88a-4099-b013-56d9ca656977",
838+
"metadata": {},
839+
"outputs": [
840+
{
841+
"data": {
842+
"text/plain": [
843+
"True"
844+
]
845+
},
846+
"execution_count": 32,
847+
"metadata": {},
848+
"output_type": "execute_result"
849+
}
850+
],
851+
"source": [
852+
"np.allclose(A, A_reconstructed, atol=1.0e-4)"
853+
]
705854
}
706855
],
707856
"metadata": {

0 commit comments

Comments
 (0)