Skip to content

Commit 968aa48

Browse files
committed
profiling power method
1 parent 8ce49e0 commit 968aa48

File tree

5 files changed

+144
-7
lines changed

5 files changed

+144
-7
lines changed

scripts/plot_scalability.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
import os
4+
5+
6+
csv_file = "tmp_data/timings.csv"
7+
8+
if not os.path.exists(csv_file):
9+
print(
10+
f"Error: {csv_file} not found!\nMake sure you have executed the script 'submit.sbatch' in the 'shell' folder."
11+
)
12+
exit(1)
13+
14+
data = np.genfromtxt(csv_file, delimiter=",", dtype=str, skip_header=1)
15+
16+
17+
dims = data[:, 0].astype(int)
18+
func_names = data[:, 1]
19+
times = data[:, 2].astype(float)
20+
21+
22+
unique_funcs = np.unique(func_names)
23+
24+
plt.figure(figsize=(8, 6))
25+
26+
27+
for func in unique_funcs:
28+
mask = func_names == func
29+
plt.plot(dims[mask], times[mask], marker="o", linestyle="-", label=func)
30+
31+
32+
plt.xlabel(r"$\log(d)$")
33+
plt.ylabel("$t$")
34+
plt.xscale("log")
35+
plt.yscale("log")
36+
plt.title("Scalability")
37+
38+
plt.legend()
39+
plt.grid(True)
40+
plt.tight_layout()
41+
42+
43+
output_dir = "logs"
44+
os.makedirs(output_dir, exist_ok=True)
45+
output_path = os.path.join(output_dir, "scalability.png")
46+
plt.savefig(output_path)
47+
print(f"Plot saved to {output_path}")
48+
49+
plt.show()
50+
plt.close()
51+
plt.clf()

scripts/run.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import cupyx.scipy.sparse as cpsp
2+
import cupy as cp
13
from pyclassify import (
24
eigenvalues_np,
35
eigenvalues_sp,
6+
eigenvalues_cp,
47
power_method,
58
power_method_numba,
9+
power_method_cp,
610
)
711
from pyclassify.utils import make_symmetric, read_config
812
import numpy as np
@@ -27,20 +31,29 @@
2731
kwargs = read_config(filename)
2832
dim = kwargs["dim"]
2933
density = kwargs["density"]
30-
# also tol and max iter
34+
tol = kwargs["tol"]
35+
max_iter = kwargs["max_iter"]
3136

3237

3338
matrix = sp.random(dim, dim, density=density, format="csr")
3439
matrix = make_symmetric(matrix)
40+
cp_symm_matrix = cpsp.csr_matrix(matrix)
41+
3542

3643
eigs_np = eigenvalues_np(matrix.toarray(), symmetric=True)
3744
eigs_sp = eigenvalues_sp(matrix, symmetric=True)
45+
eigs_cp = eigenvalues_cp(cp_symm_matrix)
46+
3847

3948
index_np = np.argmax(np.abs(eigs_np))
4049
index_sp = np.argmax(np.abs(eigs_sp))
50+
index_cp = np.argmax(np.abs(eigs_cp))
51+
4152

4253
biggest_eigenvalue_np = eigs_np[index_np]
4354
biggest_eigenvalue_sp = eigs_sp[index_sp]
55+
biggest_eigenvalue_cp = eigs_cp[index_cp]
4456

4557
biggest_eigenvalue_pm = power_method(matrix)
4658
biggest_eigenvalue_pm_numba = power_method_numba(matrix.toarray())
59+
biggest_eigenvalue_cp = power_method_cp(cp_symm_matrix)

shell/submit.sbatch

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#SBATCH --gpus=1
77
#SBATCH --ntasks-per-node=1
88
#SBATCH --gpus-per-task=1
9-
#SBATCH --mem=2000
10-
#SBATCH --time=01:00:00
9+
#SBATCH --mem=10000
10+
#SBATCH --time=12:00:00
1111
#SBATCH --mail-user=ltomada@sissa.it
1212
#SBATCH --output=%x.o%j.%N
1313
#SBATCH --error=%x.e%j.%N
@@ -29,6 +29,40 @@ export SLURM_NTASKS_PER_NODE=1 # due to Ulysses's bug
2929
module load cuda/12.1
3030
conda init
3131
source ~/.bashrc
32-
conda activate /scratch/ltomada/miniconda3/envs/devtools_scicomp
33-
python -m pytest -v
34-
#python scripts/run.py fit --config=experiments/config.yaml
32+
conda activate /scratch/ltomada/miniconda3/envs/dtsc
33+
34+
mkdir -p tmp_data experiments logs
35+
touch tmp_data/timings.csv
36+
37+
export LINE_PROFILE=0
38+
39+
density=0.1
40+
tol=0.0001
41+
max_iter=1000
42+
43+
44+
for dim in 100 500 1000; do
45+
echo "d: $dim" # for readability
46+
echo "dim: $dim" > experiments/config.yaml
47+
echo "density: $density" >> experiments/config.yaml
48+
echo "tol: $tol" >> experiments/config.yaml
49+
echo "max_iter: $max_iter" >> experiments/config.yaml
50+
51+
python -m kernprof -l -o tmp_data/profile.dat scripts/run.py --config=experiments/config
52+
53+
if [ ! -f tmp_data/timings.csv ]; then
54+
echo "dim,function_name,time" > tmp_data/timings.csv
55+
fi
56+
57+
python -m line_profiler -rmt tmp_data/profile.dat | awk -v dim="$dim" '
58+
/Function: / {func_name=$2}
59+
/Total time:/ {if (func_name != "") print dim, func_name, $3}' OFS=',' >> tmp_data/timings.csv
60+
61+
echo "dim: $dim - times saved in tmp_data/timings.csv"
62+
echo '--------------------------------------------------'
63+
done
64+
65+
66+
python scripts/plot_scalability.py
67+
echo "Experiment completed!"
68+
#rm -rf tmp_data

shell/test.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
#SBATCH --partition=gpu2
4+
#SBATCH --job-name=dtsc
5+
#SBATCH --nodes=1
6+
#SBATCH --gpus=1
7+
#SBATCH --ntasks-per-node=1
8+
#SBATCH --gpus-per-task=1
9+
#SBATCH --mem=10000
10+
#SBATCH --time=02:00:00
11+
#SBATCH --mail-user=ltomada@sissa.it
12+
#SBATCH --output=%x.o%j.%N
13+
#SBATCH --error=%x.e%j.%N
14+
15+
# Print job details
16+
NOW=`date +%H:%M-%a-%d/%b/%Y`
17+
echo '------------------------------------------------------'
18+
echo 'This job is allocated on '$SLURM_JOB_CPUS_PER_NODE' cpu(s)'
19+
echo 'Job is running on node(s): '
20+
echo $SLURM_JOB_NODELIST
21+
echo '------------------------------------------------------'
22+
#
23+
# ==== End of Info part (say things) ===== #
24+
#
25+
26+
cd $SLURM_SUBMIT_DIR
27+
export SLURM_NTASKS_PER_NODE=1 # due to Ulysses's bug
28+
29+
module load cuda/12.1
30+
conda init
31+
source ~/.bashrc
32+
conda activate /scratch/ltomada/miniconda3/envs/dtsc
33+
34+
mkdir -p tmp_data experiments logs
35+
36+
python -m pytest -v > logs/test.log
37+
38+
touch tmp_data/timings.csv
39+
40+
export LINE_PROFILE=0

src/pyclassify/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def check_square_matrix(A):
3333
raise ValueError("Matrix must be square!")
3434

3535

36-
@profile
3736
def make_symmetric(A):
3837
"""
3938
Ensures the input matrix is symmetric by averaging it with its transpose.

0 commit comments

Comments
 (0)