Skip to content

Commit

Permalink
upgrade ndslice api (#397)
Browse files Browse the repository at this point in the history
* upgrade ndslice api

* fix dub

* fix hoffman and appveyor

* fix example

* fix circle ci

* fix circle ci

* ditto

* ditto
  • Loading branch information
9il authored Sep 9, 2018
1 parent 2000335 commit 427e0e9
Show file tree
Hide file tree
Showing 19 changed files with 182 additions and 167 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ benchmarks/glas/gemm_report
*.dylib

mir-test-unittest
examples/lda_hoffman_sparse
benchmarks/ndslice/binarization
benchmarks/ndslice/convolution
benchmarks/ndslice/euclidean_distance
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ script:
- echo "$ARCH"
- dub test --arch "$ARCH" --build=unittest-cov
- dub build --single --arch "$ARCH" examples/lda_hoffman_sparse.d
- dub fetch imageformats --version 6.1.0 && dub build --arch "$ARCH" imageformats
# - dub build --single --arch "$ARCH" examples/median_filter.d
- dub fetch imageformats --version 7.0.0 && dub build --arch "$ARCH" imageformats
- dub build --single --arch "$ARCH" examples/median_filter.d
- dub build --single --arch "$ARCH" examples/means_of_columns.d
after_success:
- bash <(curl -s https://codecov.io/bash)
Expand Down
62 changes: 42 additions & 20 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ platform: x64
environment:
matrix:
- DC: ldc
DVersion: 1.7.0
DVersion: '1.10.0'
arch: x64

skip_tags: true
Expand All @@ -14,23 +14,41 @@ branches:
install:
- ps: function SetUpDCompiler
{
echo "downloading ...";
if($env:arch -eq "x86"){
$env:DConf = "m32";
}
elseif($env:arch -eq "x64"){
$env:DConf = "m64";
}
$env:toolchain = "msvc";
$version = $env:DVersion;
Invoke-WebRequest "http://downloads.dlang.org/releases/2.x/$($version)/dmd.$($version).windows.7z" -OutFile "c:\dmd.7z";
echo "finished.";
pushd c:\\;
7z x ldc.zip > $null;
popd;
if($env:DC -eq "dmd"){
if($env:arch -eq "x86"){
$env:DConf = "m32";
}
elseif($env:arch -eq "x64"){
$env:DConf = "m64";
}
echo "downloading ...";
$env:toolchain = "msvc";
$version = $env:DVersion;
Invoke-WebRequest "http://downloads.dlang.org/releases/2.x/$($version)/dmd.$($version).windows.7z" -OutFile "c:\dmd.7z";
echo "finished.";
pushd c:\\;
7z x dmd.7z > $null;
popd;
}
elseif($env:DC -eq "ldc"){
echo "downloading ...";
if($env:arch -eq "x86"){
$env:DConf = "m32";
}
elseif($env:arch -eq "x64"){
$env:DConf = "m64";
}
$env:toolchain = "msvc";
$version = $env:DVersion;
Invoke-WebRequest "https://github.com/ldc-developers/ldc/releases/download/v$($version)/ldc2-$($version)-windows-x64.7z" -OutFile "c:\ldc.7z";
echo "finished.";
pushd c:\\;
7z x ldc.7z > $null;
popd;
}
}
- ps: SetUpDCompiler
- powershell -Command Invoke-WebRequest https://code.dlang.org/files/dub-1.7.2-windows-x86.zip -OutFile dub.zip
- powershell -Command Invoke-WebRequest https://code.dlang.org/files/dub-1.9.0-windows-x86.zip -OutFile dub.zip
- 7z x dub.zip -odub > nul
- set PATH=%CD%\%binpath%;%CD%\dub;%PATH%
- dub --version
Expand All @@ -44,10 +62,14 @@ before_build:
$env:compilersetupargs = "amd64";
$env:Darch = "x86_64";
}
- ps:
$version = $env:DVersion;
$env:PATH += ";C:\ldc2-$($version)-win64-msvc\bin";
$env:DC = "ldmd2";
- ps : if($env:DC -eq "dmd"){
$env:PATH += ";C:\dmd2\windows\bin;";
}
elseif($env:DC -eq "ldc"){
$version = $env:DVersion;
$env:PATH += ";C:\ldc2-$($version)-windows-x64\bin";
$env:DC = "ldc2";
}
- ps: $env:compilersetup = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall";
- '"%compilersetup%" %compilersetupargs%'

Expand Down
16 changes: 8 additions & 8 deletions benchmarks/ndslice/binarization.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ LDC - the LLVM D compiler (918073):
$ dub run --build=release-nobounds --compiler=ldmd2 --single binarization.d
+/
import std.datetime : benchmark, Duration;
import std.datetime.stopwatch : benchmark, Duration;
import std.stdio : writefln;
import std.conv : to;
import std.algorithm.comparison : min;
import mir.utility : min;

import mir.ndslice;
import mir.ndslice.internal : fastmath;
import mir.math.common : fastmath;

alias F = double;

void binarizationLockstep(Slice!(Contiguous, [2], F*) input, F threshold, Slice!(Contiguous, [2], F*) output)
void binarizationLockstep(Slice!(F*, 2) input, F threshold, Slice!(F*, 2) output)
{
import std.range : lockstep;
foreach(i, ref o; lockstep(input.flattened, output.flattened))
Expand All @@ -39,9 +39,9 @@ void binarizationLockstep(Slice!(Contiguous, [2], F*) input, F threshold, Slice!
}
}

void binarizationAssumeSameStructure(Slice!(Contiguous, [2], F*) input, F threshold, Slice!(Contiguous, [2], F*) output)
void binarizationAssumeSameStructure(Slice!(F*, 2) input, F threshold, Slice!(F*, 2) output)
{
import mir.ndslice.algorithm : each;
import mir.algorithm.iteration : each;
import mir.ndslice.topology : zip;

zip(input, output).each!( (p) {
Expand All @@ -51,8 +51,8 @@ void binarizationAssumeSameStructure(Slice!(Contiguous, [2], F*) input, F thresh

// __gshared is used to prevent specialized optimization for input data
__gshared n = 256; // image size
__gshared Slice!(Contiguous, [2], F*) a;
__gshared Slice!(Contiguous, [2], F*) b;
__gshared Slice!(F*, 2) a;
__gshared Slice!(F*, 2) b;
__gshared F t; // threshold

void main()
Expand Down
24 changes: 12 additions & 12 deletions benchmarks/ndslice/convolution.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}
+/
/+
Benchmark demonstrates performance superiority of using mir.ndslice.algorithm over looped code, for
Benchmark demonstrates performance superiority of using mir.algorithm.iteration over looped code, for
multidimensional processing with ndslice package.
$ ldc2 --version
Expand All @@ -20,25 +20,25 @@ LDC - the LLVM D compiler (918073):
$ dub run --build=release-nobounds --compiler=ldmd2 --single convolution.d
+/
import std.datetime : benchmark, Duration;
import std.datetime.stopwatch : benchmark, Duration;
import std.stdio : writefln;
import std.conv : to;
import std.algorithm.comparison : min;
import mir.utility : min;

import mir.ndslice;
import mir.ndslice.internal : fastmath;
import mir.math.common : fastmath;

alias F = double;

@fastmath void convLoop(Slice!(Contiguous, [2], F*) input, Slice!(Contiguous, [2], F*) output, Slice!(Contiguous, [2], F*) kernel)
@fastmath void convLoop(Slice!(F*, 2) input, Slice!(F*, 2) output, Slice!(F*, 2) kernel)
{
auto kr = kernel.length!0; // kernel row size
auto kc = kernel.length!1; // kernel column size
foreach (r; 0 .. output.length!0)
foreach (c; 0 .. output.length!1)
{
// take window to input at given pixel coordinate
Slice!(Canonical, [2], F*) window = input[r .. r + kr, c .. c + kc];
Slice!(F*, 2, Canonical) window = input[r .. r + kr, c .. c + kc];

// calculate result for current pixel
F v = 0.0f;
Expand All @@ -54,9 +54,9 @@ static @fastmath F kapply(F v, F e, F k) @safe @nogc nothrow pure
return v + (e * k);
}

void convAlgorithm(Slice!(Contiguous, [2], F*) input, Slice!(Contiguous, [2], F*) output, Slice!(Contiguous, [2], F*) kernel)
void convAlgorithm(Slice!(F*, 2) input, Slice!(F*, 2) output, Slice!(F*, 2) kernel)
{
import mir.ndslice.algorithm : reduce;
import mir.algorithm.iteration : reduce;
import mir.ndslice.topology : windows, map;

auto mapping = input
Expand All @@ -72,9 +72,9 @@ void convAlgorithm(Slice!(Contiguous, [2], F*) input, Slice!(Contiguous, [2], F*
// __gshared is used to prevent specialized optimization for input data
__gshared n = 256; // image size
__gshared m = 5; // kernel size
__gshared Slice!(Contiguous, [2], F*) a;
__gshared Slice!(Contiguous, [2], F*) b;
__gshared Slice!(Contiguous, [2], F*) k;
__gshared Slice!(F*, 2) a;
__gshared Slice!(F*, 2) b;
__gshared Slice!(F*, 2) k;

void main()
{
Expand All @@ -95,5 +95,5 @@ void main()
}

writefln("%26s = %s", "loops", bestBench[0]);
writefln("%26s = %s", "mir.ndslice.algorithm", bestBench[1]);
writefln("%26s = %s", "mir.algorithm.iteration", bestBench[1]);
}
6 changes: 3 additions & 3 deletions benchmarks/ndslice/dot_product.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $ dub run --build=release-nobounds --compiler=ldmd2 --single dot_product.d
import std.numeric : dotProduct;
import std.array;
import std.typecons;
import std.datetime;
import std.datetime.stopwatch : benchmark, Duration;
import std.stdio;
import std.conv;
import std.range: std_zip = zip;
Expand All @@ -36,8 +36,8 @@ __gshared F result;
__gshared n = 8000;
__gshared F[] a;
__gshared F[] b;
__gshared Slice!(Contiguous, [1], F*) asl;
__gshared Slice!(Contiguous, [1], F*) bsl;
__gshared Slice!(F*) asl;
__gshared Slice!(F*) bsl;

void main()
{
Expand Down
14 changes: 6 additions & 8 deletions benchmarks/ndslice/euclidean_distance.d
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@ LDC - the LLVM D compiler (918073):
$ dub run --build=release-nobounds --compiler=ldmd2 --single dot_product.d
+/
import std.numeric : euclideanDistance;
import std.array;
import std.typecons;
import std.datetime;
import std.datetime.stopwatch : benchmark, Duration;
import std.stdio;
import std.conv;
import std.conv: to;
import std.range: std_zip = zip;
import std.algorithm: std_reduce = reduce;
import std.math : sqrt;

import mir.array.allocation;
import mir.ndslice;
import mir.utility;
import mir.ndslice.internal : fastmath;
import mir.math.common : sqrt, fastmath;

alias F = double;

Expand All @@ -44,8 +42,8 @@ __gshared F result;
__gshared n = 8000;
__gshared F[] a;
__gshared F[] b;
__gshared Slice!(Contiguous, [1], F*) asl;
__gshared Slice!(Contiguous, [1], F*) bsl;
__gshared Slice!(F*) asl;
__gshared Slice!(F*) bsl;

void main()
{
Expand Down
6 changes: 3 additions & 3 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
machine:
environment:
DMD: 2.073.2 # for rdmd
LDC: 1.4.0
DUB: 1.1.0
DMD: 2.082.0 # for rdmd
LDC: 1.10.0
DUB: 1.9.0
PATH: "${HOME}/dmd2/linux/bin64:${HOME}/ldc2-$LDC-linux-x86_64/bin:${PATH}"
LD_LIBRARY_PATH: "${HOME}/dmd2/linux/lib64:${HOME}/ldc2-$LDC-linux-x86_64/lib:${LD_LIBRARY_PATH}"
checkout:
Expand Down
4 changes: 2 additions & 2 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
}
],
"dependencies": {
"mir-random": ">0.3.0 <1.0.0",
"mir-algorithm": ">0.8.0 <2.0.0"
"mir-random": ">=1.0.0 <2.0.0",
"mir-algorithm": ">=2.0.0 <2.5.0"
}
}
6 changes: 3 additions & 3 deletions examples/means_of_columns.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
* faster than the numpy version.
*/

import std.datetime;
import std.datetime.stopwatch;
import std.conv : to;
import std.stdio;
import mir.ndslice;

enum testCount = 10_000;
__gshared Slice!(Contiguous, [1], double*) means;
Slice!(Contiguous, [2], int*) sl;
__gshared Slice!(double*) means;
Slice!(int*, 2) sl;

void main() {
sl = iota!int(100, 1000).slice;
Expand Down
10 changes: 5 additions & 5 deletions examples/median_filter.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/+ dub.json:
{
"name": "median_filter",
"dependencies": {"mir": {"path": ".."}, "imageformats": "==6.1.0"},
"dependencies": {"mir": {"path": ".."}, "imageformats": "==7.0.0"},
}
+/

Expand Down Expand Up @@ -40,8 +40,8 @@ Returns:
Dense data layout is guaranteed.
+/

Slice!(Contiguous, [3], C*) movingWindowByChannel(alias filter, C)
(Slice!(Universal, [3], C*) image, size_t nr, size_t nc)
Slice!(C*, 3) movingWindowByChannel(alias filter, C)
(Slice!(C*, 3, Universal) image, size_t nr, size_t nc)
{
// 0. 3D
// The last dimension represents the color channel.
Expand All @@ -54,7 +54,7 @@ Slice!(Contiguous, [3], C*) movingWindowByChannel(alias filter, C)
.windows(nr, nc)
// 3. 5D
// Unpacks the windows.
.unpack
.unpack.unpack
// 4. 5D
// Brings the color channel dimension to the third position.
.transposed!(0, 1, 4)
Expand All @@ -74,7 +74,7 @@ Params:
Returns:
median value over the range `r`
+/
T median(SliceKind kind, Iterator, T)(Slice!(kind, [2], Iterator) sl, T[] buf)
T median(Iterator, SliceKind kind, T)(Slice!(Iterator, 2, kind) sl, T[] buf)
{
import std.algorithm.sorting : topN;
// copy sl to the buffer
Expand Down
Loading

0 comments on commit 427e0e9

Please sign in to comment.