Skip to content

Replace tuples for arrays #627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
288 changes: 149 additions & 139 deletions benches/bench1.rs

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions benches/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use ndarray::NdProducer;
#[bench]
fn chunk2x2_iter_sum(bench: &mut Bencher)
{
let a = Array::<f32, _>::zeros((256, 256));
let chunksz = (2, 2);
let a = Array::<f32, _>::zeros([256, 256]);
let chunksz = [2, 2];
let mut sum = Array::zeros(a.exact_chunks(chunksz).raw_dim());
bench.iter(|| {
azip!(ref a (a.exact_chunks(chunksz)), mut sum in {
Expand All @@ -23,8 +23,8 @@ fn chunk2x2_iter_sum(bench: &mut Bencher)
#[bench]
fn chunk2x2_sum(bench: &mut Bencher)
{
let a = Array::<f32, _>::zeros((256, 256));
let chunksz = (2, 2);
let a = Array::<f32, _>::zeros([256, 256]);
let chunksz = [2, 2];
let mut sum = Array::zeros(a.exact_chunks(chunksz).raw_dim());
bench.iter(|| {
azip!(ref a (a.exact_chunks(chunksz)), mut sum in {
Expand All @@ -36,11 +36,11 @@ fn chunk2x2_sum(bench: &mut Bencher)
#[bench]
fn chunk2x2_sum_get1(bench: &mut Bencher)
{
let a = Array::<f32, _>::zeros((256, 256));
let chunksz = (2, 2);
let a = Array::<f32, _>::zeros([256, 256]);
let chunksz = [2, 2];
let mut sum = Array::<f32, _>::zeros(a.exact_chunks(chunksz).raw_dim());
bench.iter(|| {
let (m, n) = a.dim();
let [m, n] = a.dim();
for i in 0..m {
for j in 0..n {
sum[[i/2, j/2]] += a[[i, j]];
Expand All @@ -52,11 +52,11 @@ fn chunk2x2_sum_get1(bench: &mut Bencher)
#[bench]
fn chunk2x2_sum_uget1(bench: &mut Bencher)
{
let a = Array::<f32, _>::zeros((256, 256));
let chunksz = (2, 2);
let a = Array::<f32, _>::zeros([256, 256]);
let chunksz = [2, 2];
let mut sum = Array::<f32, _>::zeros(a.exact_chunks(chunksz).raw_dim());
bench.iter(|| {
let (m, n) = a.dim();
let [m, n] = a.dim();
for i in 0..m {
for j in 0..n {
unsafe {
Expand All @@ -70,11 +70,11 @@ fn chunk2x2_sum_uget1(bench: &mut Bencher)
#[bench]
fn chunk2x2_sum_get2(bench: &mut Bencher)
{
let a = Array::<f32, _>::zeros((256, 256));
let chunksz = (2, 2);
let a = Array::<f32, _>::zeros([256, 256]);
let chunksz = [2, 2];
let mut sum = Array::<f32, _>::zeros(a.exact_chunks(chunksz).raw_dim());
bench.iter(|| {
let (m, n) = sum.dim();
let [m, n] = sum.dim();
for i in 0..m {
for j in 0..n {
sum[[i, j]] += a[[i*2 + 0, j*2 + 0]];
Expand Down
8 changes: 4 additions & 4 deletions benches/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ use ndarray::prelude::*;
#[bench]
fn default_f64(bench: &mut Bencher) {
bench.iter(|| {
Array::<f64, _>::default((128, 128))
Array::<f64, _>::default([128, 128])
})
}

#[bench]
fn zeros_f64(bench: &mut Bencher) {
bench.iter(|| {
Array::<f64, _>::zeros((128, 128))
Array::<f64, _>::zeros([128, 128])
})
}

#[bench]
fn map_regular(bench: &mut test::Bencher)
{
let a = Array::linspace(0., 127., 128).into_shape((8, 16)).unwrap();
let a = Array::linspace(0., 127., 128).into_shape([8, 16]).unwrap();
bench.iter(|| {
a.map(|&x| 2. * x)
});
Expand All @@ -33,7 +33,7 @@ fn map_regular(bench: &mut test::Bencher)
#[bench]
fn map_stride(bench: &mut test::Bencher)
{
let a = Array::linspace(0., 127., 256).into_shape((8, 32)).unwrap();
let a = Array::linspace(0., 127., 256).into_shape([8, 32]).unwrap();
let av = a.slice(s![.., ..;2]);
bench.iter(|| {
av.map(|&x| 2. * x)
Expand Down
24 changes: 12 additions & 12 deletions benches/gemv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ use ndarray::linalg::general_mat_vec_mul;

#[bench]
fn gemv_64_64c(bench: &mut Bencher) {
let a = Array::zeros((64, 64));
let (m, n) = a.dim();
let x = Array::zeros(n);
let mut y = Array::zeros(m);
let a = Array::zeros([64, 64]);
let [m, n] = a.dim();
let x = Array::zeros([n]);
let mut y = Array::zeros([m]);
bench.iter(|| {
general_mat_vec_mul(1.0, &a, &x, 1.0, &mut y);
});
}

#[bench]
fn gemv_64_64f(bench: &mut Bencher) {
let a = Array::zeros((64, 64).f());
let (m, n) = a.dim();
let x = Array::zeros(n);
let mut y = Array::zeros(m);
let a = Array::zeros([64, 64].f());
let [m, n] = a.dim();
let x = Array::zeros([n]);
let mut y = Array::zeros([m]);
bench.iter(|| {
general_mat_vec_mul(1.0, &a, &x, 1.0, &mut y);
});
}

#[bench]
fn gemv_64_32(bench: &mut Bencher) {
let a = Array::zeros((64, 32));
let (m, n) = a.dim();
let x = Array::zeros(n);
let mut y = Array::zeros(m);
let a = Array::zeros([64, 32]);
let [m, n] = a.dim();
let x = Array::zeros([n]);
let mut y = Array::zeros([m]);
bench.iter(|| {
general_mat_vec_mul(1.0, &a, &x, 1.0, &mut y);
});
Expand Down
2 changes: 1 addition & 1 deletion benches/higher-order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Y: usize = 16;
#[bench]
fn map_regular(bench: &mut Bencher)
{
let a = Array::linspace(0., 127., N).into_shape((X, Y)).unwrap();
let a = Array::linspace(0., 127., N).into_shape([X, Y]).unwrap();
bench.iter(|| {
a.map(|&x| 2. * x)
});
Expand Down
54 changes: 27 additions & 27 deletions benches/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ndarray::Slice;
#[bench]
fn iter_sum_2d_regular(bench: &mut Bencher)
{
let a = Array::<i32, _>::zeros((64, 64));
let a = Array::<i32, _>::zeros([64, 64]);
bench.iter(|| {
a.iter().fold(0, |acc, &x| acc + x)
});
Expand All @@ -23,7 +23,7 @@ fn iter_sum_2d_regular(bench: &mut Bencher)
#[bench]
fn iter_sum_2d_cutout(bench: &mut Bencher)
{
let a = Array::<i32, _>::zeros((66, 66));
let a = Array::<i32, _>::zeros([66, 66]);
let av = a.slice(s![1..-1, 1..-1]);
let a = av;
bench.iter(|| {
Expand All @@ -34,7 +34,7 @@ fn iter_sum_2d_cutout(bench: &mut Bencher)
#[bench]
fn iter_all_2d_cutout(bench: &mut Bencher)
{
let a = Array::<i32, _>::zeros((66, 66));
let a = Array::<i32, _>::zeros([66, 66]);
let av = a.slice(s![1..-1, 1..-1]);
let a = av;
bench.iter(|| {
Expand All @@ -45,7 +45,7 @@ fn iter_all_2d_cutout(bench: &mut Bencher)
#[bench]
fn iter_sum_2d_transpose(bench: &mut Bencher)
{
let a = Array::<i32, _>::zeros((66, 66));
let a = Array::<i32, _>::zeros([66, 66]);
let a = a.t();
bench.iter(|| {
a.iter().fold(0, |acc, &x| acc + x)
Expand All @@ -55,7 +55,7 @@ fn iter_sum_2d_transpose(bench: &mut Bencher)
#[bench]
fn iter_filter_sum_2d_u32(bench: &mut Bencher)
{
let a = Array::linspace(0., 1., 256).into_shape((16, 16)).unwrap();
let a = Array::linspace(0., 1., 256).into_shape([16, 16]).unwrap();
let b = a.mapv(|x| (x * 100.) as u32);
bench.iter(|| {
b.iter().filter(|&&x| x < 75).fold(0, |acc, &x| acc + x)
Expand All @@ -65,7 +65,7 @@ fn iter_filter_sum_2d_u32(bench: &mut Bencher)
#[bench]
fn iter_filter_sum_2d_f32(bench: &mut Bencher)
{
let a = Array::linspace(0., 1., 256).into_shape((16, 16)).unwrap();
let a = Array::linspace(0., 1., 256).into_shape([16, 16]).unwrap();
let b = a * 100.;
bench.iter(|| {
b.iter().filter(|&&x| x < 75.).fold(0., |acc, &x| acc + x)
Expand All @@ -75,7 +75,7 @@ fn iter_filter_sum_2d_f32(bench: &mut Bencher)
#[bench]
fn iter_filter_sum_2d_stride_u32(bench: &mut Bencher)
{
let a = Array::linspace(0., 1., 256).into_shape((16, 16)).unwrap();
let a = Array::linspace(0., 1., 256).into_shape([16, 16]).unwrap();
let b = a.mapv(|x| (x * 100.) as u32);
let b = b.slice(s![.., ..;2]);
bench.iter(|| {
Expand All @@ -86,7 +86,7 @@ fn iter_filter_sum_2d_stride_u32(bench: &mut Bencher)
#[bench]
fn iter_filter_sum_2d_stride_f32(bench: &mut Bencher)
{
let a = Array::linspace(0., 1., 256).into_shape((16, 16)).unwrap();
let a = Array::linspace(0., 1., 256).into_shape([16, 16]).unwrap();
let b = a * 100.;
let b = b.slice(s![.., ..;2]);
bench.iter(|| {
Expand Down Expand Up @@ -235,8 +235,8 @@ const I2DSZ: usize = 64;

#[bench]
fn indexed_iter_1d_ix1(bench: &mut Bencher) {
let mut a = Array::<f64, _>::zeros(I2DSZ * I2DSZ);
for (i, elt) in a.indexed_iter_mut() {
let mut a = Array::<f64, _>::zeros([I2DSZ * I2DSZ]);
for ([i], elt) in a.indexed_iter_mut() {
*elt = i as _;
}

Expand All @@ -250,8 +250,8 @@ fn indexed_iter_1d_ix1(bench: &mut Bencher) {

#[bench]
fn indexed_zip_1d_ix1(bench: &mut Bencher) {
let mut a = Array::<f64, _>::zeros(I2DSZ * I2DSZ);
for (i, elt) in a.indexed_iter_mut() {
let mut a = Array::<f64, _>::zeros([I2DSZ * I2DSZ]);
for ([i], elt) in a.indexed_iter_mut() {
*elt = i as _;
}

Expand All @@ -266,8 +266,8 @@ fn indexed_zip_1d_ix1(bench: &mut Bencher) {

#[bench]
fn indexed_iter_2d_ix2(bench: &mut Bencher) {
let mut a = Array::<f64, _>::zeros((I2DSZ, I2DSZ));
for ((i, j), elt) in a.indexed_iter_mut() {
let mut a = Array::<f64, _>::zeros([I2DSZ, I2DSZ]);
for ([i, j], elt) in a.indexed_iter_mut() {
*elt = (i + 100 * j) as _;
}

Expand All @@ -280,8 +280,8 @@ fn indexed_iter_2d_ix2(bench: &mut Bencher) {
}
#[bench]
fn indexed_zip_2d_ix2(bench: &mut Bencher) {
let mut a = Array::<f64, _>::zeros((I2DSZ, I2DSZ));
for ((i, j), elt) in a.indexed_iter_mut() {
let mut a = Array::<f64, _>::zeros([I2DSZ, I2DSZ]);
for ([i, j], elt) in a.indexed_iter_mut() {
*elt = (i + 100 * j) as _;
}

Expand All @@ -298,8 +298,8 @@ fn indexed_zip_2d_ix2(bench: &mut Bencher) {

#[bench]
fn indexed_iter_3d_ix3(bench: &mut Bencher) {
let mut a = Array::<f64, _>::zeros((ISZ, ISZ, ISZ));
for ((i, j, k), elt) in a.indexed_iter_mut() {
let mut a = Array::<f64, _>::zeros([ISZ, ISZ, ISZ]);
for ([i, j, k], elt) in a.indexed_iter_mut() {
*elt = (i + 100 * j + 10000 * k) as _;
}

Expand All @@ -313,8 +313,8 @@ fn indexed_iter_3d_ix3(bench: &mut Bencher) {

#[bench]
fn indexed_zip_3d_ix3(bench: &mut Bencher) {
let mut a = Array::<f64, _>::zeros((ISZ, ISZ, ISZ));
for ((i, j, k), elt) in a.indexed_iter_mut() {
let mut a = Array::<f64, _>::zeros([ISZ, ISZ, ISZ]);
for ([i, j, k], elt) in a.indexed_iter_mut() {
*elt = (i + 100 * j + 10000 * k) as _;
}

Expand All @@ -329,8 +329,8 @@ fn indexed_zip_3d_ix3(bench: &mut Bencher) {

#[bench]
fn indexed_iter_3d_dyn(bench: &mut Bencher) {
let mut a = Array::<f64, _>::zeros((ISZ, ISZ, ISZ));
for ((i, j, k), elt) in a.indexed_iter_mut() {
let mut a = Array::<f64, _>::zeros([ISZ, ISZ, ISZ]);
for ([i, j, k], elt) in a.indexed_iter_mut() {
*elt = (i + 100 * j + 10000 * k) as _;
}
let a = a.into_shape(&[ISZ; 3][..]).unwrap();
Expand All @@ -346,7 +346,7 @@ fn indexed_iter_3d_dyn(bench: &mut Bencher) {
#[bench]
fn iter_sum_1d_strided_fold(bench: &mut Bencher)
{
let mut a = Array::<u64, _>::ones(10240);
let mut a = Array::<u64, _>::ones([10240]);
a.slice_axis_inplace(Axis(0), Slice::new(0, None, 2));
bench.iter(|| {
a.iter().fold(0, |acc, &x| acc + x)
Expand All @@ -356,7 +356,7 @@ fn iter_sum_1d_strided_fold(bench: &mut Bencher)
#[bench]
fn iter_sum_1d_strided_rfold(bench: &mut Bencher)
{
let mut a = Array::<u64, _>::ones(10240);
let mut a = Array::<u64, _>::ones([10240]);
a.slice_axis_inplace(Axis(0), Slice::new(0, None, 2));
bench.iter(|| {
a.iter().rfold(0, |acc, &x| acc + x)
Expand All @@ -367,7 +367,7 @@ fn iter_sum_1d_strided_rfold(bench: &mut Bencher)
#[bench]
fn iter_axis_iter_sum(bench: &mut Bencher)
{
let a = Array::<f32, _>::zeros((64, 64));
let a = Array::<f32, _>::zeros([64, 64]);
bench.iter(|| {
a.axis_iter(Axis(0)).map(|plane| plane.sum()).sum::<f32>()
});
Expand All @@ -376,7 +376,7 @@ fn iter_axis_iter_sum(bench: &mut Bencher)
#[bench]
fn iter_axis_chunks_1_iter_sum(bench: &mut Bencher)
{
let a = Array::<f32, _>::zeros((64, 64));
let a = Array::<f32, _>::zeros([64, 64]);
bench.iter(|| {
a.axis_chunks_iter(Axis(0), 1).map(|plane| plane.sum()).sum::<f32>()
});
Expand All @@ -385,7 +385,7 @@ fn iter_axis_chunks_1_iter_sum(bench: &mut Bencher)
#[bench]
fn iter_axis_chunks_5_iter_sum(bench: &mut Bencher)
{
let a = Array::<f32, _>::zeros((64, 64));
let a = Array::<f32, _>::zeros([64, 64]);
bench.iter(|| {
a.axis_chunks_iter(Axis(0), 5).map(|plane| plane.sum()).sum::<f32>()
});
Expand Down
Loading