Skip to content

Commit c84187f

Browse files
committed
Update docs
1 parent 5a0233b commit c84187f

File tree

4 files changed

+73
-14
lines changed

4 files changed

+73
-14
lines changed

docs/Documentation.ipynb

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@
399399
"cell_type": "markdown",
400400
"metadata": {},
401401
"source": [
402-
"\n",
403402
"# Tradeoff accuracy vs time\n",
404403
"\n",
405404
"The pipeline for computing the eigenvalues and eigenvectors of a symmetric matrix using our proposed method is as follows:\n",
@@ -757,6 +756,65 @@
757756
"<!-- ![Time profiling](images/plot_time.png) -->"
758757
]
759758
},
759+
{
760+
"cell_type": "markdown",
761+
"metadata": {},
762+
"source": [
763+
"# A note on the code in C++\n",
764+
"As anticipated in the `README.md` file, a few functions have been written in `C++` and have then been exposed to `Python` using `pybind11`.\n",
765+
"The reason for this choice is the following one: either compiling them with `numba` offered no major performance advantage, or it was simply not possible as a result of the fact that some types that we used (e.g. `SciPy`'s sparse matrices) are not compatible with `numba`.\n",
766+
"\n",
767+
"The cell below provides a comparison of the execution time of the original `Python` implementation and the `C++` one."
768+
]
769+
},
770+
{
771+
"cell_type": "code",
772+
"execution_count": null,
773+
"metadata": {},
774+
"outputs": [],
775+
"source": [
776+
"from pyclassify.cxx_utils import secular_solver_cxx\n",
777+
"from pyclassify.zero_finder import secular_solver_python\n",
778+
"\n",
779+
"seed = 2206\n",
780+
"np.seed(seed)\n",
781+
"\n",
782+
"n = 1000\n",
783+
"d = np.arange(n)\n",
784+
"rho = 2.\n",
785+
"D = np.diag(d)\n",
786+
"v = np.random.rand(n)\n",
787+
"\n",
788+
"indices = range(len(d))\n",
789+
"rk_1_update = rho * np.outer(v, v)\n",
790+
"L = D + rk_1_update\n",
791+
"\n",
792+
"begin_cxx = time()\n",
793+
"computed_eigs_cxx, _, __ = secular_solver_cxx(rho, d, v, indices)\n",
794+
"end_cxx = time()\n",
795+
"duration_cxx = end_cxx - begin_cxx\n",
796+
"\n",
797+
"begin_python = time()\n",
798+
"computed_eigs_python = secular_solver_python(rho, d, v) \n",
799+
"end_python = time()\n",
800+
"duration_python = end_python - begin_python\n",
801+
"\n",
802+
"print(f'Speedup: {duration_python/duration_cxx}')\n",
803+
"\n",
804+
"exact_eigs, _ = np.linalg.eig(L)\n",
805+
"exact_eigs = np.sort(exact_eigs)\n",
806+
"\n",
807+
"for i in range(len(exact_eigs)):\n",
808+
" # Assert that the C++ eigenvalues are correct\n",
809+
" assert (\n",
810+
" np.abs(computed_eigs_cxx[i] - exact_eigs[i]) < 1e-8\n",
811+
" ), \"Error. The eigenvalues were not computed correctly.\"\n",
812+
" # Also assert that they are close to the Python ones\n",
813+
" assert (\n",
814+
" np.abs(computed_eigs_cxx[i] - computed_eigs_python[i]) < 1e-8\n",
815+
" ), \"Error. The eigenvalues were not computed correctly.\""
816+
]
817+
},
760818
{
761819
"cell_type": "markdown",
762820
"metadata": {
@@ -781,11 +839,6 @@
781839
"\n",
782840
"[7] [Arbenz, Peter. Lecture Notes on Solving Large Scale Eigenvalue Problems - Chapter 5-6. Computer Science Department, ETH Zürich, Spring semester 2016.](https://sissa-my.sharepoint.com/my?FolderCTID=0x012000B3941AA86D63224F9CC9A5B1FEDCCF3B&id=%2Fpersonal%2Fglicausi%5Fsissa%5Fit%2FDocuments%2FPhD%2FDevelopment%20Tools%20for%20Scientific%20Computing%20%2D%20Project%20material%20and%20references%2Fchapters5%2D6%2Epdf&parent=%2Fpersonal%2Fglicausi%5Fsissa%5Fit%2FDocuments%2FPhD%2FDevelopment%20Tools%20for%20Scientific%20Computing%20%2D%20Project%20material%20and%20references)"
783841
]
784-
},
785-
{
786-
"cell_type": "markdown",
787-
"metadata": {},
788-
"source": []
789842
}
790843
],
791844
"metadata": {

docs/plots/memory_profiling.png

60.1 KB
Loading

scripts/mpi_running.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from pyclassify.eigenvalues import Lanczos_PRO
1111

1212

13-
seed = 10
14-
np.random.seed(seed)
13+
seed = 100
14+
# np.random.seed(seed)
1515

1616

1717
def parallel_eig(diag, off_diag, nprocs):
@@ -85,3 +85,7 @@ def compute_eigvals(A, n_procs):
8585

8686
if max_error < 1e-8:
8787
print("Pretty small, huh?")
88+
else:
89+
print(
90+
"Please notice that no seed has been set, so it might be that this was an unlucky case. Please try again"
91+
)

scripts/profiling_memory_and_time.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from mpi4py import MPI
2121

2222

23-
seed = 10
23+
seed = 1000
2424
np.random.seed(seed)
2525

2626

@@ -85,7 +85,7 @@
8585
gc.collect()
8686
mem_after_lanczos = proc.memory_info().rss / 1024 / 1024 # MB
8787
delta_mem_lanczos = mem_after_lanczos - mem_before_lanczos
88-
delta_t_lanzos = end_lanczos - begin_lanczos
88+
delta_t_lanczos = end_lanczos - begin_lanczos
8989

9090
print("Done. Now computing eigenvalues...")
9191
else:
@@ -116,13 +116,15 @@
116116

117117
# Collect the information across all ranks
118118
if rank == 0:
119-
total_mem_all = delta_mem_lanczos
120-
total_time_all = delta_t_lanzos + total_time_children
119+
total_mem_all = delta_mem_lanczos + total_mem_children
120+
total_time_all = delta_t_lanczos + total_time_children
121121
print("Eigenvalues computed.")
122122
process = psutil.Process()
123123

124124
print(f"[D&I] Total memory across all processes: {total_mem_all:.4f} MB")
125-
print(f"[D&I] Total time: across all processes: {total_time_all:.4f} s")
125+
print(
126+
f"[D&I] Total time (rank 0, which also performs Lanczos): {total_time_all:.4f} s"
127+
)
126128
# We also profile numpy and scipy memory consumption
127129
mem_np, time_np = profile_numpy_eigvals(A_np)
128130
print(f"[NumPy] eig memory usage: {mem_np:.4f} MB")
@@ -164,7 +166,7 @@
164166
"mem_total_mb": round(total_mem_all, 2),
165167
"mem_numpy_mb": round(mem_np, 2),
166168
"mem_scipy_mb": round(mem_sp, 2),
167-
"time_lanczos": round(delta_t_lanzos, 2),
169+
"time_lanczos": round(delta_t_lanczos, 2),
168170
"time_tridiag": round(total_time_children, 2),
169171
"time_total": round(total_time_all, 2),
170172
"time_numpy": round(time_np, 2),

0 commit comments

Comments
 (0)