Skip to content

Commit f54c721

Browse files
authored
upgrade mir-algorithm version (#400)
1 parent ed7bc8f commit f54c721

File tree

11 files changed

+279
-474
lines changed

11 files changed

+279
-474
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ examples/lda_hoffman_sparse
5252
benchmarks/ndslice/binarization
5353
benchmarks/ndslice/convolution
5454
benchmarks/ndslice/euclidean_distance
55+
mir-test-unittest.exe

circle.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,11 @@ dependencies:
1717
- ldmd2 --version
1818
- 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 -
1919
- dub --version
20-
# - wget -q -O dscanner "http://releases.dlang.io/dscanner/0.3.0-git/dscanner"
21-
# - chmod +x dscanner
22-
# - wget "https://raw.githubusercontent.com/dlang/dmd/master/src/checkwhitespace.d"
2320
test:
2421
override:
25-
# - dscanResult=$(./dscanner --config .dscanner.ini --styleCheck source 2> /dev/null ) ; if [ -z "${dscanResult}" ] ; then echo "No warnings found" ; else echo $dscanResult && $(exit 1); fi
26-
- grep -nE "(for|foreach|foreach_reverse|if|while)\(" $(find source -name '*.d'); test $? -eq 1
27-
# - ldmd2 -O -release -run ./checkwhitespace.d $(find source -name '*.d')
2822
- dub test --compiler=ldmd2
2923
- dub test --compiler=ldmd2 --build=unittest-release
24+
- git clone https://github.com/libmir/mir-core
3025
- git clone https://github.com/libmir/mir-random
3126
- git clone https://github.com/libmir/mir-algorithm
3227
- make -f doc/Makefile html

doc/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ DOC_SOURCE_DIR=doc
1616
GENERATED=.generated
1717
MIR_DIR=source
1818
RANDOM_DIR=mir-random/source
19-
ALOGORITHM_DIR=mir-algorithm/source
19+
ALGORITHM_DIR=mir-algorithm/source
20+
CORE_DIR=mir-core/source
2021
DLANGORG_DIR=$(DOC_SOURCE_DIR)/dlang.org
2122
ARTWORK_DIR=$(DOC_SOURCE_DIR)/artwork
2223

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

8889
###########################################################
8990
# Setup all other resources needed by dlang.org

dub.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,22 @@
1313
"configurations": [
1414
{
1515
"name": "source",
16-
"targetType": "sourceLibrary",
16+
"targetType": "sourceLibrary"
1717
},
1818
{
1919
"name": "static",
20-
"targetType": "staticLibrary",
20+
"targetType": "staticLibrary"
2121
},
2222
{
2323
"name": "dynamic",
24-
"targetType": "dynamicLibrary",
24+
"targetType": "dynamicLibrary"
2525
},
2626
{
2727
"name": "unittest",
28-
"versions": ["to_be_filled"],
28+
"versions": ["to_be_filled"]
2929
}
3030
],
3131
"dependencies": {
32-
"mir-random": ">=1.0.0 <2.0.0",
33-
"mir-algorithm": ">=2.0.1 <2.5.0"
32+
"mir-random": "~>2.0.0"
3433
}
3534
}

examples/means_of_columns.d

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
*/
3030

3131
import std.datetime.stopwatch;
32-
import std.conv : to;
3332
import std.stdio;
33+
import mir.conv : to;
3434
import mir.ndslice;
3535

3636
enum testCount = 10_000;
@@ -41,7 +41,6 @@ void main() {
4141
sl = iota!int(100, 1000).slice;
4242
auto r = benchmark!({
4343
means = sl
44-
.universal
4544
.transposed
4645
.pack!1
4746
.map!(col => reduce!"a + b"(0L, col) / double(col.length))

source/mir/model/lda/hoffman.d

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,15 @@ struct LdaHoffman(F)
135135
136136
Params:
137137
n = mini-batch, a collection of compressed documents.
138-
maxIterations = maximal number of iterations for single document in a batch for E-step.
138+
maxIterations = maximal number of iterations for s This implementation is optimized for sparse documents,
139+
ingle document in a batch for E-step.
139140
+/
140-
size_t putBatch(SliceKind kind, C, I, J)(Slice!(FieldIterator!(CompressedField!(C, I, J)), 1, kind) n, size_t maxIterations)
141+
size_t putBatch(SliceKind kind, C, I, J)(Slice!(ChopIterator!(J*, Series!(I*, C*)), 1, kind) n, size_t maxIterations)
141142
{
142143
return putBatchImpl(n.recompress!F, maxIterations);
143144
}
144145

145-
private size_t putBatchImpl(CompressedTensor!(F, 2) n, size_t maxIterations)
146+
private size_t putBatchImpl(Slice!(ChopIterator!(size_t*, Series!(uint*, F*))) n, size_t maxIterations)
146147
{
147148
import std.math: isFinite;
148149
import mir.sparse.blas.dot;
@@ -191,7 +192,7 @@ struct LdaHoffman(F)
191192
}
192193
if (c < maxIterations && sum > eps * K)
193194
{
194-
nd.values[] = nsave[d].values;
195+
nd.value[] = nsave[d].value;
195196
continue;
196197
}
197198
import core.atomic;
@@ -212,16 +213,13 @@ struct LdaHoffman(F)
212213
return ret;
213214
}
214215

215-
private auto saveN(CompressedTensor!(F, 2) n)
216+
private auto saveN(Slice!(ChopIterator!(size_t*, Series!(uint*, F*))) n)
216217
{
217-
import mir.ndslice.topology: universal;
218-
return
219-
CompressedField!(F)(
220-
n.iterator._field.compressedLength,
221-
n.iterator._field.values.dup,
222-
n.iterator._field.indexes,
223-
n.iterator._field.pointers)
224-
.slicedField(n.length).universal;
218+
import mir.series: series;
219+
import mir.ndslice.topology: chopped, universal;
220+
return n.iterator._sliceable.index
221+
.series(n.iterator._sliceable.value.dup)
222+
.chopped(n.iterator._iterator.sliced(n.length + 1));
225223
}
226224

227225
private static void unparameterize(Vector param, Vector posterior)

source/mir/sparse/blas/axpy.d

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module mir.sparse.blas.axpy;
88
import std.traits;
99
import mir.ndslice.slice;
1010
import mir.sparse;
11+
import mir.series;
1112

1213
/++
1314
Constant times a vector plus a vector.
@@ -21,47 +22,46 @@ Returns:
2122
+/
2223
void axpy(
2324
CR,
24-
V1 : CompressedArray!(T1, I1),
25-
T1, I1, V2)
25+
V1 : Series!(I1, T1),
26+
I1, T1, V2)
2627
(in CR alpha, V1 x, V2 y)
2728
if (isDynamicArray!V2 || isSlice!V2)
2829
in
2930
{
30-
if (x.indexes.length)
31-
assert(x.indexes[$-1] < y.length);
31+
if (x.index.length)
32+
assert(x.index[$-1] < y.length);
3233
}
3334
body
3435
{
3536
import mir.internal.utility;
3637

37-
foreach (size_t i; 0 .. x.indexes.length)
38+
foreach (size_t i; 0 .. x.index.length)
3839
{
39-
auto j = x.indexes[i];
40-
y[j] = alpha * x.values[i] + y[j];
40+
auto j = x.index[i];
41+
y[j] = alpha * x.value[i] + y[j];
4142
}
4243
}
4344

4445
///
4546
unittest
4647
{
47-
auto x = CompressedArray!(double, uint)([1.0, 3, 4, 9, 13], [0, 3, 5, 9, 10]);
48+
auto x = series([0, 3, 5, 9, 10], [1.0, 3, 4, 9, 13]);
4849
auto y = [0.0, 1.0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
4950
axpy(2.0, x, y);
5051
assert(y == [2.0, 1.0, 2, 9, 4, 13, 6, 7, 8, 27, 36, 11, 12]);
5152
}
5253

5354
unittest
5455
{
55-
auto x = CompressedArray!(double, uint)([1.0, 3, 4, 9, 13], [0, 3, 5, 9, 10]);
56+
auto x = series([0, 3, 5, 9, 10], [1.0, 3, 4, 9, 13]);
5657
auto y = [0.0, 1.0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
5758
axpy(2.0, x, y.sliced);
5859
assert(y == [2.0, 1.0, 2, 9, 4, 13, 6, 7, 8, 27, 36, 11, 12]);
5960
}
6061

6162
unittest
6263
{
63-
import std.typecons: No;
64-
auto x = CompressedArray!(double, uint)([1.0, 3, 4, 9, 13], [0, 3, 5, 9, 10]);
64+
auto x = series([0, 3, 5, 9, 10], [1.0, 3, 4, 9, 13]);
6565
auto y = [0.0, 1.0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
6666
axpy(2.0, x, y.slicedField);
6767
assert(y == [2.0, 1.0, 2, 9, 4, 13, 6, 7, 8, 27, 36, 11, 12]);

source/mir/sparse/blas/dot.d

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module mir.sparse.blas.dot;
88
import std.traits;
99
import mir.ndslice.slice;
1010
import mir.sparse;
11+
import mir.series;
1112

1213
/++
1314
Dot product of two vectors
@@ -19,8 +20,8 @@ Returns:
1920
scalar `xᵀ × y`
2021
+/
2122
Unqual!(CommonType!(T1, T2)) dot(
22-
V1 : CompressedArray!(T1, I1),
23-
V2 : CompressedArray!(T2, I2),
23+
V1 : Series!(I1*, T1*),
24+
V2 : Series!(I2*, T2*),
2425
T1, T2, I1, I2)
2526
(V1 x, V2 y)
2627
{
@@ -30,8 +31,8 @@ Unqual!(CommonType!(T1, T2)) dot(
3031
/// ditto
3132
D dot(
3233
D,
33-
V1 : CompressedArray!(T1, I1),
34-
V2 : CompressedArray!(T2, I2),
34+
V1 : Series!(I1*, T1*),
35+
V2 : Series!(I2*, T2*),
3536
T1, T2, I1, I2)
3637
(V1 x, V2 y)
3738
{
@@ -42,40 +43,38 @@ D dot(
4243
Unqual!I1 ai0 = void;
4344
Unqual!I2 bi0 = void;
4445

45-
if (x.indexes.length && y.indexes.length) for (;;)
46+
if (x.length && y.length) for (;;)
4647
{
47-
bi0 = y.indexes[0];
48-
if (x.indexes[0] < bi0)
48+
bi0 = y.index[0];
49+
if (x.index[0] < bi0)
4950
{
5051
do
5152
{
52-
x.values = x.values[1 .. $];
53-
x.indexes = x.indexes[1 .. $];
54-
if (x.indexes.length == 0)
53+
x.popFront;
54+
if (x.length == 0)
5555
{
5656
break;
5757
}
5858
}
59-
while (x.indexes[0] < bi0);
59+
while (x.index[0] < bi0);
6060
done = 2;
6161
}
6262
if (--done == 0)
6363
{
6464
goto L;
6565
}
66-
ai0 = x.indexes[0];
67-
if (y.indexes[0] < ai0)
66+
ai0 = x.index[0];
67+
if (y.index[0] < ai0)
6868
{
6969
do
7070
{
71-
y.values = y.values[1 .. $];
72-
y.indexes = y.indexes[1 .. $];
73-
if (y.indexes.length == 0)
71+
y.popFront;
72+
if (y.length == 0)
7473
{
7574
break;
7675
}
7776
}
78-
while (y.indexes[0] < ai0);
77+
while (y.index[0] < ai0);
7978
done = 2;
8079
}
8180
if (--done == 0)
@@ -84,19 +83,17 @@ D dot(
8483
}
8584
continue;
8685
L:
87-
s = x.values[0] * y.values[0] + s;
88-
x.indexes = x.indexes[1 .. $];
89-
if (x.indexes.length == 0)
86+
s = x.value[0] * y.value[0] + s;
87+
x.popFront;
88+
if (x.length == 0)
9089
{
9190
break;
9291
}
93-
y.indexes = y.indexes[1 .. $];
94-
if (y.indexes.length == 0)
92+
y.popFront;
93+
if (y.length == 0)
9594
{
9695
break;
9796
}
98-
x.values = x.values[1 .. $];
99-
y.values = y.values[1 .. $];
10097
}
10198

10299
return s;
@@ -105,8 +102,8 @@ D dot(
105102
///
106103
unittest
107104
{
108-
auto x = CompressedArray!(int, uint)([1, 3, 4, 9, 10], [0, 3, 5, 9, 100]);
109-
auto y = CompressedArray!(int, uint)([1, 10, 100, 1000], [1, 3, 4, 9]);
105+
auto x = series([0u, 3, 5, 9, 100], [1, 3, 4, 9, 10]);
106+
auto y = series([1u, 3, 4, 9], [1, 10, 100, 1000]);
110107
// x = [1, 0, 0, 3, 0, 4, 0, 0, 0, 9, ... ,10]
111108
// y = [0, 1, 0, 10, 0, 0, 0, 0, 0, 1000]
112109
assert(dot(x, y) == 9030);
@@ -122,7 +119,7 @@ Returns:
122119
scalar `x × y`
123120
+/
124121
Unqual!(CommonType!(T1, ForeachType!V2)) dot(
125-
V1 : CompressedArray!(T1, I1),
122+
V1 : Series!(I1*, T1*),
126123
T1, I1, V2)
127124
(V1 x, V2 y)
128125
if (isDynamicArray!V2 || isSlice!V2)
@@ -133,14 +130,14 @@ Unqual!(CommonType!(T1, ForeachType!V2)) dot(
133130
///ditto
134131
D dot(
135132
D,
136-
V1 : CompressedArray!(T1, I1),
133+
V1 : Series!(I1*, T1*),
137134
T1, I1, V2)
138135
(V1 x, V2 y)
139136
if (isDynamicArray!V2 || isSlice!V2)
140137
in
141138
{
142-
if (x.indexes.length)
143-
assert(x.indexes[$-1] < y.length);
139+
if (x.length)
140+
assert(x.index[$-1] < y.length);
144141
}
145142
body
146143
{
@@ -151,9 +148,9 @@ body
151148

152149
alias F = Unqual!(CommonType!(T1, T2));
153150
F s = 0;
154-
foreach (size_t i; 0 .. x.indexes.length)
151+
foreach (size_t i; 0 .. x.index.length)
155152
{
156-
s = y[x.indexes[i]] * x.values[i] + s;
153+
s = y[x.index[i]] * x.value[i] + s;
157154
}
158155

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

0 commit comments

Comments
 (0)