Skip to content

Commit fb53cad

Browse files
committed
added optimizations to em++ and improved test
1 parent 449589d commit fb53cad

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ build/module.js: setup globals.bc
33
# -Os \
44
# -s TOTAL_MEMORY=100MB
55
docker run -it -v $(shell pwd):/src seveibar/emscripten em++ \
6+
-O3 -ffast-math \
67
-std=c++1z -I/usr/local/include --bind \
78
-o build/module.js ./module.cpp \
89
-s MODULARIZE=1 \
@@ -13,6 +14,7 @@ build/module.js: setup globals.bc
1314

1415
# Create separate node version
1516
docker run -it -v $(shell pwd):/src seveibar/emscripten em++ \
17+
-O3 -ffast-math \
1618
-std=c++1z -I/usr/local/include --bind \
1719
-o build/node.js ./module.cpp \
1820
-s MODULARIZE=1 \
@@ -31,6 +33,7 @@ build/module.js: setup globals.bc
3133
.PHONY: debug_build/module.js
3234
debug_build/module.js: setup globals.bc
3335
docker run -it -v $(shell pwd):/src seveibar/emscripten em++ \
36+
-O3 -ffast-math \
3437
-std=c++1z -I/usr/local/include --bind -s "EXTRA_EXPORTED_RUNTIME_METHODS=['getValue']" \
3538
-o build/module.js ./module.cpp \
3639
-s ALLOW_MEMORY_GROWTH=1 -s ASSERTIONS=1 -s SAFE_HEAP=1 -s DETERMINISTIC=1 -s DEMANGLE_SUPPORT=1 \
@@ -43,8 +46,8 @@ globals.bc: globals.cpp globals.hpp
4346

4447
test_modulejs: debug_build/module.js
4548
npx ava --verbose ./tests/modulejs/basic.test.js
46-
python util/read-bin-image.py tests/modulejs/mask.bin
47-
python util/read-bin-image.py tests/modulejs/mask-polygon-only.bin
49+
python util/read-bin-image.py tests/modulejs/mask-320x249.bin
50+
python util/read-bin-image.py tests/modulejs/mask-320x249-polygon-only.bin
4851

4952
assets/orange-320x249.bin:
5053
mkdir -p assets
@@ -87,7 +90,7 @@ build/test_polygon_fill: setup globals.o
8790
.PHONY: test_polygon_fill
8891
test_polygon_fill: build/test_polygon_fill
8992
./build/test_polygon_fill
90-
python util/read-bin-image.py ./polygon_fill.bin
93+
python util/read-bin-image.py ./polygon_fill-320x249.bin
9194

9295
.PHONY: test_min_cut
9396
test_min_cut: build/test_min_cut

super_pixel.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Implementation of SLIC-like algorithm. Some modifications:
2020
using blaze::DynamicMatrix;
2121
using blaze::StaticMatrix;
2222

23+
inline double pow2C(double v) { return (v * v); }
24+
2325
void allocateGlobals() {
2426
lmat = vector<vector<double>>(height, vector<double>(width));
2527
amat = vector<vector<double>>(height, vector<double>(width));
@@ -73,21 +75,21 @@ void selectInitialCenters() {
7375
double compute_distance(int cci, int ri, int ci) {
7476
const double centerRi = centers[cci][0];
7577
const double centerCi = centers[cci][1];
76-
double ds = sqrt(pow(centerRi - ri, 2) + pow(centerCi - ci, 2));
77-
double dc = sqrt(pow(centers[cci][2] - lmat[ri][ci], 2) +
78-
pow(centers[cci][3] - amat[ri][ci], 2) +
79-
pow(centers[cci][4] - bmat[ri][ci], 2));
80-
return sqrt(pow(dc / weightFactor, 2) + pow(ds / step, 2));
78+
double ds = sqrt(pow2C(centerRi - ri) + pow2C(centerCi - ci));
79+
double dc = sqrt(pow2C(centers[cci][2] - lmat[ri][ci]) +
80+
pow2C(centers[cci][3] - amat[ri][ci]) +
81+
pow2C(centers[cci][4] - bmat[ri][ci]));
82+
return sqrt(pow2C(dc / weightFactor) + pow2C(ds / step));
8183
}
8284

8385
double compute_distance2(int cci, int ri, int ci) {
8486
const double centerRi = centers[cci][0];
8587
const double centerCi = centers[cci][1];
86-
double ds2 = pow(centerRi - ri, 2) + pow(centerCi - ci, 2);
87-
double dc2 = pow(centers[cci][2] - lmat[ri][ci], 2) +
88-
pow(centers[cci][3] - amat[ri][ci], 2) +
89-
pow(centers[cci][4] - bmat[ri][ci], 2);
90-
return pow(dc2 / weightFactor2, 2) + pow(ds2 / step2, 2);
88+
double ds2 = pow2C(centerRi - ri) + pow2C(centerCi - ci);
89+
double dc2 = pow2C(centers[cci][2] - lmat[ri][ci]) +
90+
pow2C(centers[cci][3] - amat[ri][ci]) +
91+
pow2C(centers[cci][4] - bmat[ri][ci]);
92+
return pow2C(dc2 / weightFactor2) + pow2C(ds2 / step2);
9193
}
9294

9395
double cluster_color_similarity(int cci1, int cci2) {

test_polygon_fill.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ using namespace boost;
1919
// #define TEST_CLASSPOINTS 1
2020

2121
int main() {
22-
std::vector<BYTE> *imageFileData = readFile("assets/orange.bin");
22+
std::vector<BYTE> *imageFileData = readFile("assets/orange-320x249.bin");
2323
width = 320;
2424
height = 249;
2525

@@ -72,7 +72,7 @@ int main() {
7272
#endif
7373

7474
std::ofstream fout;
75-
fout.open("polygon_fill.bin", std::ios::binary | std::ios::out);
75+
fout.open("polygon_fill-320x249.bin", std::ios::binary | std::ios::out);
7676
for (int i = 0; i < coloredMask.size(); i++) {
7777
fout.write((char *)&coloredMask[i], 4);
7878
}

tests/modulejs/basic.test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ const range = require("lodash/range")
66

77
const f = (p) => path.join(__dirname, p)
88

9-
const imData = Uint8Array.from(fs.readFileSync(f("../../assets/orange.bin")))
9+
const imData = Uint8Array.from(
10+
fs.readFileSync(f("../../assets/orange-320x249.bin"))
11+
)
1012

1113
async function setup() {
1214
while (!m.setImageSize) {
@@ -85,7 +87,7 @@ test("should be able to get a mask", async (t) => {
8587

8688
t.assert(cppImDataUint8[0] === 255)
8789

88-
fs.writeFileSync(f("./mask.bin"), cppImDataUint8)
90+
fs.writeFileSync(f("./mask-320x249.bin"), cppImDataUint8)
8991
})
9092

9193
test("should be able to get a simple mode polygon mask", async (t) => {
@@ -98,7 +100,7 @@ test("should be able to get a simple mode polygon mask", async (t) => {
98100

99101
m.clearClassElements()
100102
m.setClassColor(0, 0xff0000ff)
101-
m.setClassColor(1, 0xffffffff)
103+
m.setClassColor(1, 0xffff00ff)
102104

103105
const pi1 = m.addPolygon(0)
104106
const pi2 = m.addPolygon(1)
@@ -132,5 +134,5 @@ test("should be able to get a simple mode polygon mask", async (t) => {
132134

133135
t.pass("TODO for now manually confirm mask content in mask-polygon-only.bin!")
134136

135-
fs.writeFileSync(f("./mask-polygon-only.bin"), cppImDataUint8)
137+
fs.writeFileSync(f("./mask-320x249-polygon-only.bin"), cppImDataUint8)
136138
})

0 commit comments

Comments
 (0)