Skip to content

Commit 55998bb

Browse files
committed
TEST: Test non-contig arrays in Zip benchmarks, also with splits
Using split tests performance of the Zip in parallelization, so that we can see if there are benefits to splitting arrays better.
1 parent 2a44fb6 commit 55998bb

File tree

2 files changed

+58
-48
lines changed

2 files changed

+58
-48
lines changed

benches/iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,12 @@ pub fn zip_mut_with(data: &Array3<f32>, out: &mut Array3<f32>) {
381381
fn zip_mut_with_cc(b: &mut Bencher) {
382382
let data: Array3<f32> = Array3::zeros((ISZ, ISZ, ISZ));
383383
let mut out = Array3::zeros(data.dim());
384-
b.iter(|| black_box(zip_mut_with(&data, &mut out)));
384+
b.iter(|| zip_mut_with(&data, &mut out));
385385
}
386386

387387
#[bench]
388388
fn zip_mut_with_ff(b: &mut Bencher) {
389389
let data: Array3<f32> = Array3::zeros((ISZ, ISZ, ISZ).f());
390390
let mut out = Array3::zeros(data.dim().f());
391-
b.iter(|| black_box(zip_mut_with(&data, &mut out)));
391+
b.iter(|| zip_mut_with(&data, &mut out));
392392
}

benches/zip.rs

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,119 @@
11
#![feature(test)]
22
extern crate test;
3-
use test::{black_box, Bencher};
3+
use test::{Bencher};
44
use ndarray::{Array3, ShapeBuilder, Zip};
5-
6-
pub fn zip_copy(data: &Array3<f32>, out: &mut Array3<f32>) {
5+
use ndarray::s;
6+
use ndarray::IntoNdProducer;
7+
8+
pub fn zip_copy<'a, A, P, Q>(data: P, out: Q)
9+
where P: IntoNdProducer<Item = &'a A>,
10+
Q: IntoNdProducer<Item = &'a mut A, Dim = P::Dim>,
11+
A: Copy + 'a
12+
{
713
Zip::from(data).and(out).apply(|&i, o| {
814
*o = i;
915
});
1016
}
1117

12-
pub fn zip_indexed(data: &Array3<f32>, out: &mut Array3<f32>) {
13-
Zip::indexed(data).and(out).apply(|idx, &i, o| {
14-
*o = i;
15-
});
18+
pub fn zip_copy_split<'a, A, P, Q>(data: P, out: Q)
19+
where P: IntoNdProducer<Item = &'a A>,
20+
Q: IntoNdProducer<Item = &'a mut A, Dim = P::Dim>,
21+
A: Copy + 'a
22+
{
23+
let z = Zip::from(data).and(out);
24+
let (z1, z2) = z.split();
25+
let (z11, z12) = z1.split();
26+
let (z21, z22) = z2.split();
27+
let f = |&i: &A, o: &mut A| *o = i;
28+
z11.apply(f);
29+
z12.apply(f);
30+
z21.apply(f);
31+
z22.apply(f);
1632
}
1733

18-
pub fn zip_mut_with(data: &Array3<f32>, out: &mut Array3<f32>) {
19-
out.zip_mut_with(&data, |o, &i| {
34+
pub fn zip_indexed(data: &Array3<f32>, out: &mut Array3<f32>) {
35+
Zip::indexed(data).and(out).apply(|idx, &i, o| {
2036
*o = i;
2137
});
2238
}
2339

2440
// array size in benchmarks
25-
const SZ3: (usize, usize, usize) = (137, 171, 151);
41+
const SZ3: (usize, usize, usize) = (100, 110, 100);
2642

2743
#[bench]
28-
fn zip_cf(b: &mut Bencher) {
44+
fn zip_cc(b: &mut Bencher) {
2945
let data: Array3<f32> = Array3::zeros(SZ3);
30-
let mut out = Array3::zeros(data.dim().f());
31-
b.iter(|| black_box(zip_copy(&data, &mut out)));
46+
let mut out = Array3::zeros(data.dim());
47+
b.iter(|| zip_copy(&data, &mut out));
3248
}
3349

3450
#[bench]
35-
fn zip_cc(b: &mut Bencher) {
51+
fn zip_cf(b: &mut Bencher) {
3652
let data: Array3<f32> = Array3::zeros(SZ3);
37-
let mut out = Array3::zeros(data.dim());
38-
b.iter(|| black_box(zip_copy(&data, &mut out)));
53+
let mut out = Array3::zeros(data.dim().f());
54+
b.iter(|| zip_copy(&data, &mut out));
3955
}
4056

4157
#[bench]
4258
fn zip_fc(b: &mut Bencher) {
4359
let data: Array3<f32> = Array3::zeros(SZ3.f());
4460
let mut out = Array3::zeros(data.dim());
45-
b.iter(|| black_box(zip_copy(&data, &mut out)));
61+
b.iter(|| zip_copy(&data, &mut out));
4662
}
4763

4864
#[bench]
4965
fn zip_ff(b: &mut Bencher) {
5066
let data: Array3<f32> = Array3::zeros(SZ3.f());
5167
let mut out = Array3::zeros(data.dim().f());
52-
b.iter(|| black_box(zip_copy(&data, &mut out)));
53-
}
54-
55-
#[bench]
56-
fn zip_indexed_cf(b: &mut Bencher) {
57-
let data: Array3<f32> = Array3::zeros(SZ3);
58-
let mut out = Array3::zeros(data.dim().f());
59-
b.iter(|| black_box(zip_indexed(&data, &mut out)));
68+
b.iter(|| zip_copy(&data, &mut out));
6069
}
6170

6271
#[bench]
6372
fn zip_indexed_cc(b: &mut Bencher) {
6473
let data: Array3<f32> = Array3::zeros(SZ3);
6574
let mut out = Array3::zeros(data.dim());
66-
b.iter(|| black_box(zip_indexed(&data, &mut out)));
67-
}
68-
69-
#[bench]
70-
fn zip_indexed_fc(b: &mut Bencher) {
71-
let data: Array3<f32> = Array3::zeros(SZ3.f());
72-
let mut out = Array3::zeros(data.dim());
73-
b.iter(|| black_box(zip_indexed(&data, &mut out)));
75+
b.iter(|| zip_indexed(&data, &mut out));
7476
}
7577

7678
#[bench]
7779
fn zip_indexed_ff(b: &mut Bencher) {
7880
let data: Array3<f32> = Array3::zeros(SZ3.f());
7981
let mut out = Array3::zeros(data.dim().f());
80-
b.iter(|| black_box(zip_indexed(&data, &mut out)));
82+
b.iter(|| zip_indexed(&data, &mut out));
8183
}
8284

8385
#[bench]
84-
fn zip_mut_with_cf(b: &mut Bencher) {
86+
fn slice_zip_cc(b: &mut Bencher) {
8587
let data: Array3<f32> = Array3::zeros(SZ3);
86-
let mut out = Array3::zeros(data.dim().f());
87-
b.iter(|| black_box(zip_mut_with(&data, &mut out)));
88+
let mut out = Array3::zeros(data.dim());
89+
let data = data.slice(s![1.., 1.., 1..]);
90+
let mut out = out.slice_mut(s![1.., 1.., 1..]);
91+
b.iter(|| zip_copy(&data, &mut out));
8892
}
8993

9094
#[bench]
91-
fn zip_mut_with_cc(b: &mut Bencher) {
92-
let data: Array3<f32> = Array3::zeros(SZ3);
93-
let mut out = Array3::zeros(data.dim());
94-
b.iter(|| black_box(zip_mut_with(&data, &mut out)));
95+
fn slice_zip_ff(b: &mut Bencher) {
96+
let data: Array3<f32> = Array3::zeros(SZ3.f());
97+
let mut out = Array3::zeros(data.dim().f());
98+
let data = data.slice(s![1.., 1.., 1..]);
99+
let mut out = out.slice_mut(s![1.., 1.., 1..]);
100+
b.iter(|| zip_copy(&data, &mut out));
95101
}
96102

97103
#[bench]
98-
fn zip_mut_with_fc(b: &mut Bencher) {
99-
let data: Array3<f32> = Array3::zeros(SZ3.f());
104+
fn slice_split_zip_cc(b: &mut Bencher) {
105+
let data: Array3<f32> = Array3::zeros(SZ3);
100106
let mut out = Array3::zeros(data.dim());
101-
b.iter(|| black_box(zip_mut_with(&data, &mut out)));
107+
let data = data.slice(s![1.., 1.., 1..]);
108+
let mut out = out.slice_mut(s![1.., 1.., 1..]);
109+
b.iter(|| zip_copy_split(&data, &mut out));
102110
}
103111

104112
#[bench]
105-
fn zip_mut_with_ff(b: &mut Bencher) {
113+
fn slice_split_zip_ff(b: &mut Bencher) {
106114
let data: Array3<f32> = Array3::zeros(SZ3.f());
107115
let mut out = Array3::zeros(data.dim().f());
108-
b.iter(|| black_box(zip_mut_with(&data, &mut out)));
116+
let data = data.slice(s![1.., 1.., 1..]);
117+
let mut out = out.slice_mut(s![1.., 1.., 1..]);
118+
b.iter(|| zip_copy_split(&data, &mut out));
109119
}

0 commit comments

Comments
 (0)