Skip to content

Commit ea7b23a

Browse files
committed
Improve benchmarks and add Fourier method to bench's
1 parent 199fbdb commit ea7b23a

File tree

3 files changed

+133
-25
lines changed

3 files changed

+133
-25
lines changed

benches/compare_cython_rust.py

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88
import gstools as gs
99
from gstools.field.generator import RandMeth
10-
from gstools.field.summator import summate, summate_incompr
10+
from gstools.field.summator import summate, summate_incompr, summate_fourier
1111
from gstools.krige.krigesum import calc_field_krige_and_variance, calc_field_krige
1212
from gstools.variogram.estimator import (
1313
structured,
@@ -26,31 +26,70 @@
2626
x = np.loadtxt(path / 'field_x.txt')
2727
y = np.loadtxt(path / 'field_y.txt')
2828
z = np.loadtxt(path / 'field_z.txt')
29-
cov_samples = np.loadtxt(path / 'field_cov_samples.txt')
29+
cov_samples_2d = np.loadtxt(path / 'field_cov_samples_2d.txt')
30+
cov_samples_3d = np.loadtxt(path / 'field_cov_samples_3d.txt')
3031
z_1 = np.loadtxt(path / 'field_z_1.txt')
3132
z_2 = np.loadtxt(path / 'field_z_2.txt')
32-
pos = np.array((x, y, z))
33+
spectrum_factor_2d = np.loadtxt(path / 'field_fourier_factor_2d.txt')
34+
modes_2d = np.loadtxt(path / 'field_fourier_modes_2d.txt')
35+
z_1_fourier = np.loadtxt(path / 'field_fourier_z_1.txt')
36+
z_2_fourier = np.loadtxt(path / 'field_fourier_z_2.txt')
37+
pos_2d = np.array((x, y))
38+
pos_3d = np.array((x, y, z))
3339

34-
print(f'SUMMATE')
40+
print(f'SUMMATE 2D')
3541
print(f'\tCYTHON IMPLEMENTATION')
3642
cProfile.run(
37-
'summate(cov_samples, z_1, z_2, pos)'
43+
'summate(cov_samples_2d, z_1, z_2, pos_2d)'
3844
)
3945

4046
print(f'\tRUST IMPLEMENTATION')
4147
cProfile.run(
42-
'gsc.summate(cov_samples, z_1, z_2, pos)'
48+
'gsc.summate(cov_samples_2d, z_1, z_2, pos_2d)'
4349
)
4450

45-
print(f'SUMMATE INCOMPR')
51+
print(f'SUMMATE 3D')
4652
print(f'\tCYTHON IMPLEMENTATION')
4753
cProfile.run(
48-
'summate_incompr(cov_samples, z_1, z_2, pos)'
54+
'summate(cov_samples_3d, z_1, z_2, pos_3d)'
4955
)
5056

5157
print(f'\tRUST IMPLEMENTATION')
5258
cProfile.run(
53-
'gsc.summate_incompr(cov_samples, z_1, z_2, pos)'
59+
'gsc.summate(cov_samples_3d, z_1, z_2, pos_3d)'
60+
)
61+
62+
print(f'SUMMATE INCOMPR 2D')
63+
print(f'\tCYTHON IMPLEMENTATION')
64+
cProfile.run(
65+
'summate_incompr(cov_samples_2d, z_1, z_2, pos_2d)'
66+
)
67+
68+
print(f'\tRUST IMPLEMENTATION')
69+
cProfile.run(
70+
'gsc.summate_incompr(cov_samples_2d, z_1, z_2, pos_2d)'
71+
)
72+
73+
print(f'SUMMATE INCOMPR 3D')
74+
print(f'\tCYTHON IMPLEMENTATION')
75+
cProfile.run(
76+
'summate_incompr(cov_samples_3d, z_1, z_2, pos_3d)'
77+
)
78+
79+
print(f'\tRUST IMPLEMENTATION')
80+
cProfile.run(
81+
'gsc.summate_incompr(cov_samples_3d, z_1, z_2, pos_3d)'
82+
)
83+
84+
print(f'SUMMATE FOURIER')
85+
print(f'\tCYTHON IMPLEMENTATION')
86+
cProfile.run(
87+
'summate_fourier(spectrum_factor_2d, modes_2d, z_1_fourier, z_2_fourier, pos_2d)'
88+
)
89+
90+
print(f'\tRUST IMPLEMENTATION')
91+
cProfile.run(
92+
'gsc.summate_fourier(spectrum_factor_2d, modes_2d, z_1_fourier, z_2_fourier, pos_2d)'
5493
)
5594

5695
##########################################################################

benches/gen_benchmark_inputs.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,33 @@
44
from pathlib import Path
55
import numpy as np
66
import gstools as gs
7-
from gstools.field.generator import RandMeth, IncomprRandMeth
7+
from gstools.field.generator import RandMeth, IncomprRandMeth, Fourier
88

99

1010
def gen_field_summate(path, seed):
11-
pos_no = 100_000
11+
pos_no = 10_000
1212
mode_no = 1_000
1313
x = np.linspace(0.0, 10.0, pos_no)
1414
y = np.linspace(-5.0, 5.0, pos_no)
1515
z = np.linspace(-6.0, 8.0, pos_no)
1616

17-
model = gs.Gaussian(dim=3, var=1.0, len_scale=1.0)
17+
model_2d = gs.Gaussian(dim=2, var=1.0, len_scale=1.0)
18+
model_3d = gs.Gaussian(dim=3, var=1.0, len_scale=1.0)
1819

19-
rm = RandMeth(model, mode_no, seed)
20+
rm_2d = RandMeth(model_2d, mode_no=mode_no, seed=seed)
21+
rm_3d = RandMeth(model_3d, mode_no=mode_no, seed=seed)
22+
f_2d = Fourier(model_2d, period=[10.0, 10.0] , seed=seed)
2023
np.savetxt(path / 'field_x.txt', x)
2124
np.savetxt(path / 'field_y.txt', y)
2225
np.savetxt(path / 'field_z.txt', z)
23-
np.savetxt(path / 'field_cov_samples.txt', rm._cov_sample)
24-
np.savetxt(path / 'field_z_1.txt', rm._z_1)
25-
np.savetxt(path / 'field_z_2.txt', rm._z_2)
26+
np.savetxt(path / 'field_cov_samples_2d.txt', rm_2d._cov_sample)
27+
np.savetxt(path / 'field_cov_samples_3d.txt', rm_3d._cov_sample)
28+
np.savetxt(path / 'field_z_1.txt', rm_2d._z_1)
29+
np.savetxt(path / 'field_z_2.txt', rm_2d._z_2)
30+
np.savetxt(path / 'field_fourier_z_1.txt', f_2d._z_1)
31+
np.savetxt(path / 'field_fourier_z_2.txt', f_2d._z_2)
32+
np.savetxt(path / 'field_fourier_factor_2d.txt', f_2d._spectrum_factor)
33+
np.savetxt(path / 'field_fourier_modes_2d.txt', f_2d._modes)
2634

2735
def gen_krige(path, seed):
2836
def prepare_data(pos_no, cond_no):

benches/main.rs

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ use ndarray_rand::{
1010
RandomExt,
1111
};
1212

13-
use gstools_core::field::{summator, summator_incompr};
13+
use gstools_core::field::{summator, summator_fourier, summator_incompr};
1414
use gstools_core::krige::{calculator_field_krige, calculator_field_krige_and_variance};
1515
use gstools_core::variogram::{
1616
variogram_directional, variogram_ma_structured, variogram_structured, variogram_unstructured,
1717
};
1818

1919
fn read_1d_from_file(file_path: &Path) -> Array1<f64> {
20-
let file = File::open(file_path).expect("File wasn't found");
20+
let file = File::open(file_path).expect("File wasn't found, run `gen_benchmark_inputs.py`");
2121
let reader = BufReader::new(file);
2222

2323
reader
@@ -27,7 +27,7 @@ fn read_1d_from_file(file_path: &Path) -> Array1<f64> {
2727
}
2828

2929
fn read_2d_from_file(file_path: &Path) -> Array2<f64> {
30-
let file = File::open(file_path).expect("File wasn't found");
30+
let file = File::open(file_path).expect("File wasn't found, run `gen_benchmark_inputs.py`");
3131
let reader = BufReader::new(file);
3232

3333
let mut vec = Vec::new();
@@ -54,19 +54,80 @@ pub fn field_benchmark(c: &mut Criterion) {
5454
let y = read_1d_from_file(&path.join("field_y.txt"));
5555
let z = read_1d_from_file(&path.join("field_z.txt"));
5656

57-
let pos = stack![Axis(0), x, y, z];
57+
let pos_2d = stack![Axis(0), x, y];
58+
let pos_3d = stack![Axis(0), x, y, z];
5859

59-
let cov_samples = read_2d_from_file(&path.join("field_cov_samples.txt"));
60+
let cov_samples_2d = read_2d_from_file(&path.join("field_cov_samples_2d.txt"));
61+
let cov_samples_3d = read_2d_from_file(&path.join("field_cov_samples_3d.txt"));
6062

6163
let z_1 = read_1d_from_file(&path.join("field_z_1.txt"));
6264
let z_2 = read_1d_from_file(&path.join("field_z_2.txt"));
65+
let z_1_fourier = read_1d_from_file(&path.join("field_fourier_z_1.txt"));
66+
let z_2_fourier = read_1d_from_file(&path.join("field_fourier_z_2.txt"));
6367

64-
c.bench_function("field summate", |b| {
65-
b.iter(|| summator(cov_samples.view(), z_1.view(), z_2.view(), pos.view(), None))
68+
let spectrum_factor_2d = read_1d_from_file(&path.join("field_fourier_factor_2d.txt"));
69+
70+
let modes_2d = read_2d_from_file(&path.join("field_fourier_modes_2d.txt"));
71+
72+
c.bench_function("field summate 2d", |b| {
73+
b.iter(|| {
74+
summator(
75+
cov_samples_2d.view(),
76+
z_1.view(),
77+
z_2.view(),
78+
pos_2d.view(),
79+
None,
80+
)
81+
})
82+
});
83+
84+
c.bench_function("field summate 3d", |b| {
85+
b.iter(|| {
86+
summator(
87+
cov_samples_3d.view(),
88+
z_1.view(),
89+
z_2.view(),
90+
pos_3d.view(),
91+
None,
92+
)
93+
})
94+
});
95+
96+
c.bench_function("field summate incompr 2d", |b| {
97+
b.iter(|| {
98+
summator_incompr(
99+
cov_samples_2d.view(),
100+
z_1.view(),
101+
z_2.view(),
102+
pos_2d.view(),
103+
None,
104+
)
105+
})
106+
});
107+
108+
c.bench_function("field summate incompr 3d", |b| {
109+
b.iter(|| {
110+
summator_incompr(
111+
cov_samples_3d.view(),
112+
z_1.view(),
113+
z_2.view(),
114+
pos_3d.view(),
115+
None,
116+
)
117+
})
66118
});
67119

68-
c.bench_function("field summate incompr", |b| {
69-
b.iter(|| summator_incompr(cov_samples.view(), z_1.view(), z_2.view(), pos.view(), None))
120+
c.bench_function("field summate Fourier 2d", |b| {
121+
b.iter(|| {
122+
summator_fourier(
123+
spectrum_factor_2d.view(),
124+
modes_2d.view(),
125+
z_1_fourier.view(),
126+
z_2_fourier.view(),
127+
pos_2d.view(),
128+
None,
129+
)
130+
})
70131
});
71132
}
72133

0 commit comments

Comments
 (0)