Skip to content

Commit

Permalink
upgrade mir-algorithm version (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
9il authored Oct 25, 2018
1 parent ed7bc8f commit f54c721
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 474 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ examples/lda_hoffman_sparse
benchmarks/ndslice/binarization
benchmarks/ndslice/convolution
benchmarks/ndslice/euclidean_distance
mir-test-unittest.exe
7 changes: 1 addition & 6 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@ dependencies:
- ldmd2 --version
- curl -fsSL --retry 3 http://code.dlang.org/files/dub-${DUB}-linux-x86_64.tar.gz | tar -C ~/ldc2-$LDC-linux-x86_64/bin -zxf -
- dub --version
# - wget -q -O dscanner "http://releases.dlang.io/dscanner/0.3.0-git/dscanner"
# - chmod +x dscanner
# - wget "https://raw.githubusercontent.com/dlang/dmd/master/src/checkwhitespace.d"
test:
override:
# - dscanResult=$(./dscanner --config .dscanner.ini --styleCheck source 2> /dev/null ) ; if [ -z "${dscanResult}" ] ; then echo "No warnings found" ; else echo $dscanResult && $(exit 1); fi
- grep -nE "(for|foreach|foreach_reverse|if|while)\(" $(find source -name '*.d'); test $? -eq 1
# - ldmd2 -O -release -run ./checkwhitespace.d $(find source -name '*.d')
- dub test --compiler=ldmd2
- dub test --compiler=ldmd2 --build=unittest-release
- git clone https://github.com/libmir/mir-core
- git clone https://github.com/libmir/mir-random
- git clone https://github.com/libmir/mir-algorithm
- make -f doc/Makefile html
Expand Down
5 changes: 3 additions & 2 deletions doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ DOC_SOURCE_DIR=doc
GENERATED=.generated
MIR_DIR=source
RANDOM_DIR=mir-random/source
ALOGORITHM_DIR=mir-algorithm/source
ALGORITHM_DIR=mir-algorithm/source
CORE_DIR=mir-core/source
DLANGORG_DIR=$(DOC_SOURCE_DIR)/dlang.org
ARTWORK_DIR=$(DOC_SOURCE_DIR)/artwork

Expand Down Expand Up @@ -83,7 +84,7 @@ ADDSOURCE=$(if $(subst index.d,,$1),$(MIR_DIR)/$1,$1)
# ../web/phobos/std_conv.html : std/conv.d $(STDDOC) ; ...
$(foreach p,$(SRC_DOCUMENTABLES),$(eval \
$(DOC_OUTPUT_DIR)/$(call D2HTML,$p) : $(call ADDSOURCE,$p) $(STDDOC) ;\
$(DDOC) $(STDDOC) -I$(MIR_DIR) -I$(RANDOM_DIR) -I$(ALOGORITHM_DIR) -Df$$@ $$<))
$(DDOC) $(STDDOC) -I$(MIR_DIR) -I$(RANDOM_DIR) -I$(CORE_DIR) -I$(ALGORITHM_DIR) -Df$$@ $$<))

###########################################################
# Setup all other resources needed by dlang.org
Expand Down
11 changes: 5 additions & 6 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@
"configurations": [
{
"name": "source",
"targetType": "sourceLibrary",
"targetType": "sourceLibrary"
},
{
"name": "static",
"targetType": "staticLibrary",
"targetType": "staticLibrary"
},
{
"name": "dynamic",
"targetType": "dynamicLibrary",
"targetType": "dynamicLibrary"
},
{
"name": "unittest",
"versions": ["to_be_filled"],
"versions": ["to_be_filled"]
}
],
"dependencies": {
"mir-random": ">=1.0.0 <2.0.0",
"mir-algorithm": ">=2.0.1 <2.5.0"
"mir-random": "~>2.0.0"
}
}
3 changes: 1 addition & 2 deletions examples/means_of_columns.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
*/

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

enum testCount = 10_000;
Expand All @@ -41,7 +41,6 @@ void main() {
sl = iota!int(100, 1000).slice;
auto r = benchmark!({
means = sl
.universal
.transposed
.pack!1
.map!(col => reduce!"a + b"(0L, col) / double(col.length))
Expand Down
24 changes: 11 additions & 13 deletions source/mir/model/lda/hoffman.d
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,15 @@ struct LdaHoffman(F)
Params:
n = mini-batch, a collection of compressed documents.
maxIterations = maximal number of iterations for single document in a batch for E-step.
maxIterations = maximal number of iterations for s This implementation is optimized for sparse documents,
ingle document in a batch for E-step.
+/
size_t putBatch(SliceKind kind, C, I, J)(Slice!(FieldIterator!(CompressedField!(C, I, J)), 1, kind) n, size_t maxIterations)
size_t putBatch(SliceKind kind, C, I, J)(Slice!(ChopIterator!(J*, Series!(I*, C*)), 1, kind) n, size_t maxIterations)
{
return putBatchImpl(n.recompress!F, maxIterations);
}

private size_t putBatchImpl(CompressedTensor!(F, 2) n, size_t maxIterations)
private size_t putBatchImpl(Slice!(ChopIterator!(size_t*, Series!(uint*, F*))) n, size_t maxIterations)
{
import std.math: isFinite;
import mir.sparse.blas.dot;
Expand Down Expand Up @@ -191,7 +192,7 @@ struct LdaHoffman(F)
}
if (c < maxIterations && sum > eps * K)
{
nd.values[] = nsave[d].values;
nd.value[] = nsave[d].value;
continue;
}
import core.atomic;
Expand All @@ -212,16 +213,13 @@ struct LdaHoffman(F)
return ret;
}

private auto saveN(CompressedTensor!(F, 2) n)
private auto saveN(Slice!(ChopIterator!(size_t*, Series!(uint*, F*))) n)
{
import mir.ndslice.topology: universal;
return
CompressedField!(F)(
n.iterator._field.compressedLength,
n.iterator._field.values.dup,
n.iterator._field.indexes,
n.iterator._field.pointers)
.slicedField(n.length).universal;
import mir.series: series;
import mir.ndslice.topology: chopped, universal;
return n.iterator._sliceable.index
.series(n.iterator._sliceable.value.dup)
.chopped(n.iterator._iterator.sliced(n.length + 1));
}

private static void unparameterize(Vector param, Vector posterior)
Expand Down
22 changes: 11 additions & 11 deletions source/mir/sparse/blas/axpy.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module mir.sparse.blas.axpy;
import std.traits;
import mir.ndslice.slice;
import mir.sparse;
import mir.series;

/++
Constant times a vector plus a vector.
Expand All @@ -21,47 +22,46 @@ Returns:
+/
void axpy(
CR,
V1 : CompressedArray!(T1, I1),
T1, I1, V2)
V1 : Series!(I1, T1),
I1, T1, V2)
(in CR alpha, V1 x, V2 y)
if (isDynamicArray!V2 || isSlice!V2)
in
{
if (x.indexes.length)
assert(x.indexes[$-1] < y.length);
if (x.index.length)
assert(x.index[$-1] < y.length);
}
body
{
import mir.internal.utility;

foreach (size_t i; 0 .. x.indexes.length)
foreach (size_t i; 0 .. x.index.length)
{
auto j = x.indexes[i];
y[j] = alpha * x.values[i] + y[j];
auto j = x.index[i];
y[j] = alpha * x.value[i] + y[j];
}
}

///
unittest
{
auto x = CompressedArray!(double, uint)([1.0, 3, 4, 9, 13], [0, 3, 5, 9, 10]);
auto x = series([0, 3, 5, 9, 10], [1.0, 3, 4, 9, 13]);
auto y = [0.0, 1.0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
axpy(2.0, x, y);
assert(y == [2.0, 1.0, 2, 9, 4, 13, 6, 7, 8, 27, 36, 11, 12]);
}

unittest
{
auto x = CompressedArray!(double, uint)([1.0, 3, 4, 9, 13], [0, 3, 5, 9, 10]);
auto x = series([0, 3, 5, 9, 10], [1.0, 3, 4, 9, 13]);
auto y = [0.0, 1.0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
axpy(2.0, x, y.sliced);
assert(y == [2.0, 1.0, 2, 9, 4, 13, 6, 7, 8, 27, 36, 11, 12]);
}

unittest
{
import std.typecons: No;
auto x = CompressedArray!(double, uint)([1.0, 3, 4, 9, 13], [0, 3, 5, 9, 10]);
auto x = series([0, 3, 5, 9, 10], [1.0, 3, 4, 9, 13]);
auto y = [0.0, 1.0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
axpy(2.0, x, y.slicedField);
assert(y == [2.0, 1.0, 2, 9, 4, 13, 6, 7, 8, 27, 36, 11, 12]);
Expand Down
63 changes: 30 additions & 33 deletions source/mir/sparse/blas/dot.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module mir.sparse.blas.dot;
import std.traits;
import mir.ndslice.slice;
import mir.sparse;
import mir.series;

/++
Dot product of two vectors
Expand All @@ -19,8 +20,8 @@ Returns:
scalar `xᵀ × y`
+/
Unqual!(CommonType!(T1, T2)) dot(
V1 : CompressedArray!(T1, I1),
V2 : CompressedArray!(T2, I2),
V1 : Series!(I1*, T1*),
V2 : Series!(I2*, T2*),
T1, T2, I1, I2)
(V1 x, V2 y)
{
Expand All @@ -30,8 +31,8 @@ Unqual!(CommonType!(T1, T2)) dot(
/// ditto
D dot(
D,
V1 : CompressedArray!(T1, I1),
V2 : CompressedArray!(T2, I2),
V1 : Series!(I1*, T1*),
V2 : Series!(I2*, T2*),
T1, T2, I1, I2)
(V1 x, V2 y)
{
Expand All @@ -42,40 +43,38 @@ D dot(
Unqual!I1 ai0 = void;
Unqual!I2 bi0 = void;

if (x.indexes.length && y.indexes.length) for (;;)
if (x.length && y.length) for (;;)
{
bi0 = y.indexes[0];
if (x.indexes[0] < bi0)
bi0 = y.index[0];
if (x.index[0] < bi0)
{
do
{
x.values = x.values[1 .. $];
x.indexes = x.indexes[1 .. $];
if (x.indexes.length == 0)
x.popFront;
if (x.length == 0)
{
break;
}
}
while (x.indexes[0] < bi0);
while (x.index[0] < bi0);
done = 2;
}
if (--done == 0)
{
goto L;
}
ai0 = x.indexes[0];
if (y.indexes[0] < ai0)
ai0 = x.index[0];
if (y.index[0] < ai0)
{
do
{
y.values = y.values[1 .. $];
y.indexes = y.indexes[1 .. $];
if (y.indexes.length == 0)
y.popFront;
if (y.length == 0)
{
break;
}
}
while (y.indexes[0] < ai0);
while (y.index[0] < ai0);
done = 2;
}
if (--done == 0)
Expand All @@ -84,19 +83,17 @@ D dot(
}
continue;
L:
s = x.values[0] * y.values[0] + s;
x.indexes = x.indexes[1 .. $];
if (x.indexes.length == 0)
s = x.value[0] * y.value[0] + s;
x.popFront;
if (x.length == 0)
{
break;
}
y.indexes = y.indexes[1 .. $];
if (y.indexes.length == 0)
y.popFront;
if (y.length == 0)
{
break;
}
x.values = x.values[1 .. $];
y.values = y.values[1 .. $];
}

return s;
Expand All @@ -105,8 +102,8 @@ D dot(
///
unittest
{
auto x = CompressedArray!(int, uint)([1, 3, 4, 9, 10], [0, 3, 5, 9, 100]);
auto y = CompressedArray!(int, uint)([1, 10, 100, 1000], [1, 3, 4, 9]);
auto x = series([0u, 3, 5, 9, 100], [1, 3, 4, 9, 10]);
auto y = series([1u, 3, 4, 9], [1, 10, 100, 1000]);
// x = [1, 0, 0, 3, 0, 4, 0, 0, 0, 9, ... ,10]
// y = [0, 1, 0, 10, 0, 0, 0, 0, 0, 1000]
assert(dot(x, y) == 9030);
Expand All @@ -122,7 +119,7 @@ Returns:
scalar `x × y`
+/
Unqual!(CommonType!(T1, ForeachType!V2)) dot(
V1 : CompressedArray!(T1, I1),
V1 : Series!(I1*, T1*),
T1, I1, V2)
(V1 x, V2 y)
if (isDynamicArray!V2 || isSlice!V2)
Expand All @@ -133,14 +130,14 @@ Unqual!(CommonType!(T1, ForeachType!V2)) dot(
///ditto
D dot(
D,
V1 : CompressedArray!(T1, I1),
V1 : Series!(I1*, T1*),
T1, I1, V2)
(V1 x, V2 y)
if (isDynamicArray!V2 || isSlice!V2)
in
{
if (x.indexes.length)
assert(x.indexes[$-1] < y.length);
if (x.length)
assert(x.index[$-1] < y.length);
}
body
{
Expand All @@ -151,9 +148,9 @@ body

alias F = Unqual!(CommonType!(T1, T2));
F s = 0;
foreach (size_t i; 0 .. x.indexes.length)
foreach (size_t i; 0 .. x.index.length)
{
s = y[x.indexes[i]] * x.values[i] + s;
s = y[x.index[i]] * x.value[i] + s;
}

return s;
Expand All @@ -163,7 +160,7 @@ body
unittest
{
import std.typecons: No;
auto x = CompressedArray!(double, uint)([1.0, 3, 4, 9, 13], [0, 3, 5, 9, 10]);
auto x = [0u, 3, 5, 9, 10].series([1.0, 3, 4, 9, 13]);
auto y = [0.0, 1.0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
// x: [1, 0, 0, 3, 0, 4, 0, 0, 0, 9, 13, 0, 0, 0]
// y: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
Expand Down
Loading

0 comments on commit f54c721

Please sign in to comment.