Evaluate OpenCV performance with asm.js (compiled by Emscripten)

1,810 views
Skip to first unread message

tk...@mozilla.com

unread,
Dec 31, 2014, 1:51:22 AM12/31/14
to emscripte...@googlegroups.com, Chia-Hung Tai
HI,

I recently evaluate the performance of OpenCV with asm.js. 
Thanks to the OpenCV team, there are already lots of performance tests in each OpenCV module.
What I have done is that compile OpenCV library and its performance tests both in native and with asm.js.
Run the native performance test and the javascript performance test (with the Firefox javascript engine).
And the statistics data could be found in the following Google Sheet link: https://docs.google.com/spreadsheets/d/1w8gr1_q_dQclTaGyiMtAhlwj_5_HPVKbHWfwUszPU40/edit?usp=sharing In summary, there are about 20% tests are faster or equal to the native performance and about 60% tests are faster than 3x native performance. Some patterns are observed.
First, tests with operations which convert floating point values into integer values are much slower.
Second, in native environment, single operation is much faster when it is dealing with integer type data comparing to dealing with floating point values;
however, in javascript environment, the same operation performs equally in each data type, which leads to relatively larger performance drop in non-floating point data type.

By traversing old posts, I found several people have tried to use OpenCV in their own projects, however, there is few performance information.
I post this message here is intending to open a discussion forum which could collect information about OpenCV with asm.js and especially the performance.
This message is also posted as a discussion bug in Mozilla Bugzilla, the link is, https://bugzilla.mozilla.org/show_bug.cgi?id=1116674.

Tzuhao

Android

unread,
Mar 19, 2015, 8:19:33 PM3/19/15
to emscripte...@googlegroups.com, ct...@mozilla.com
Hi Kuo, 

How did you compile OpenCV for JS? I'm trying to use Emscripten. Could you please share your process? 

Thanks

Tzu-Hao Kuo (Kaku)

unread,
Mar 23, 2015, 6:24:39 AM3/23/15
to emscripte...@googlegroups.com, ct...@mozilla.com
Hi,

You can refer to my github, I have a step-by-step guide in the readme.
https://github.com/kakukogou/opencv/tree/opencvjs

I also put two demos for using the compiled bytecode.
https://github.com/kakukogou/opencvjs_demo_facedetection
https://github.com/kakukogou/opencvjs_demo_segmentation

- Kaku

uel...@vicomtech.org

unread,
Apr 8, 2015, 11:41:16 AM4/8/15
to emscripte...@googlegroups.com, ct...@mozilla.com
Hi everybody: 

I am trying to compile kakukogou emscripten (https://github.com/kakukogou/opencv/tree/opencvjs)  opencv library and it compiles fine except these warnings.

WARNING  root: ignoring dynamic library libopencv_core.so.3.0.0 because not compiling to JS or HTML, remember to link it when compiling to JS or HTML at the end
WARNING  root: ignoring dynamic library libopencv_imgproc.so.3.0.0 because not compiling to JS or HTML, remember to link it when compiling to JS or HTML at the end
(there are more warnings with the same type, but I write a few).

I think this is emscripten issue because there is no any output to js or html output.

But the problem, is when I try to link opencv libraries in facedetection example, I get this error: 

fUse:   %umul9 = tail call { i16, i1 } @llvm.umul.with.overflow.i16(i16 %61, i16 %59)
LLVM ERROR: ExpandArithWithOverflow: At least one argument of *.with.overflow must be a constant
Traceback (most recent call last):
  File "/home/VICOMTECH/uelordi/SDK/emsdk_portable/emscripten/master/emcc", line 1301, in <module>
    shared.Building.llvm_opt(final, link_opts)
  File "/home/VICOMTECH/uelordi/SDK/emsdk_portable/emscripten/master/tools/shared.py", line 1461, in llvm_opt
    assert os.path.exists(target), 'Failed to run llvm optimizations: ' + output
AssertionError: Failed to run llvm optimizations:

My script is, the same which is in faceDetection example, and I add --llvm-opts 0 option,  because it seems a clang optimization problem, but I have the same error. 

Thanks in advance. 

Message has been deleted

Tzu-Hao Kuo (Kaku)

unread,
Apr 8, 2015, 10:38:46 PM4/8/15
to emscripte...@googlegroups.com, ct...@mozilla.com
Hi,
May I know your environments? (Linux? Emscripten version?), I would like to see if I can reproduce your situation.

uel...@vicomtech.org

unread,
Apr 9, 2015, 3:10:05 AM4/9/15
to emscripte...@googlegroups.com, ct...@mozilla.com
Sorry I forgot to put the environment. 

Im working in ubuntu 14.04 LTS, 
Linux kernel 3.13.0-46-generic

emcc (Emscripten GCC-like replacement + linker emulating GNU ld ) 1.30.0
clang version 3.5.0 
Target: x86_64-unknown-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.1
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Candidate multilib: .;@m64
Selected multilib: .;@m64
INFO     root: (Emscripten: Running sanity checks)

Unai Elordi

unread,
Apr 16, 2015, 4:03:04 AM4/16/15
to emscripte...@googlegroups.com, ct...@mozilla.com
Hi, Finally I solved the opencv and face detector issue setting up the environmet on windows.

Besides, I think I solved face detector chrome compatibility on google chrome.

There was a javascript error in this line:

resultImageData = new ImageData(temp1, width, height); //for chrome this line is an illegal constructor.

resultImageData= ctx.createImageData(width, height);

In the kaku_helper function I put this resultImageData.data.set(temp1); before to   ctx.putImageData(resultImageData, 0, 0);

The .data of ImageData instances invocated by new(), maintains the pointer of temp1, that's why the canvas is rendering fine, but only in firefox. Using ctx.createImageData, the contex has reserved other memory address, so resultImageData.data.set(temp1) only copies the value of temp1.

Is a little bit unstable, sometimes is working at 25 fps, rarely works in 3 fps, and in few cases doesn't open the camera.
About the performance, I know that this is not the best way to do it, but I haven't found any better solution.

Unai.


--
You received this message because you are subscribed to a topic in the Google Groups "emscripten-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/emscripten-discuss/uaRH2R62WOk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Merlange

unread,
Jul 31, 2015, 8:36:31 AM7/31/15
to emscripten-discuss, uel...@vicomtech.org
Hi,
I'm facing the same issue. Do you remember what settings did you fix please?
Thanks,
Michaël
To unsubscribe from this group and all its topics, send an email to emscripten-discuss+unsub...@googlegroups.com.

Michael Merlange

unread,
Jul 31, 2015, 8:46:45 AM7/31/15
to emscripten-discuss, uel...@vicomtech.org, mmer...@gmail.com
stupid spelling mistake emcmake instead of emmake :)

Gunjan Shrivastava

unread,
Jun 14, 2016, 3:11:34 AM6/14/16
to emscripten-discuss, ct...@mozilla.com

Hello Kuo,


I am trying to compile OpenCV library in Emscripten using https://github.com/kakukogou/opencv/tree/opencvjs. I successfully completed the steps till:


emcmake cmake -DBUILD_CUDA_STUBS=OFF -DBUILD_DOCS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_JASPER=OFF -DBUILD_JPEG=OFF -DBUILD_OPENEXR=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_PNG=OFF -DBUILD_TBB=OFF -DBUILD_TESTS=OFF -DBUILD_TIFF=OFF -DBUILD_WITH_DEBUG_INFO=OFF -DBUILD_ZLIB=OFF -DBUILD_opencv_apps=OFF -DBUILD_opencv_ts=OFF -DBUILD_opencv_world=OFF -DWITH_1394=OFF -DWITH_CLP=OFF -DWITH_CUBLAS=OFF -DWITH_CUDA=OFF -DWITH_CUFFT=OFF -DWITH_FFMPEG=OFF -DWITH_GDAL=OFF -DWITH_GIGEAPI=OFF -DWITH_GSTREAMER=OFF -DWITH_GSTREAMER_0_10=OFF -DWITH_GTK=OFF -DWITH_GTK_2_X=OFF -DWITH_JASPER=OFF -DWITH_JPEG=OFF -DWITH_LIBV4L=OFF -DWITH_NVCUVID=OFF -DWITH_OPENCL=OFF -DWITH_OPENCLAMDBLAS=OFF -DWITH_OPENCLAMDFFT=OFF -DWITH_OPENEXR=OFF -DWITH_OPENGL=OFF -DWITH_OPENMP=OFF -DWITH_OPENNI=OFF -DWITH_OPENNI2=OFF -DWITH_PNG=OFF -DWITH_PVAPI=OFF -DWITH_QT=OFF -DWITH_TBB=OFF -DWITH_TIFF=OFF -DWITH_UNICAP=OFF -DWITH_V4L=OFF -DWITH_VTK=OFF -DWITH_WEBP=OFF -DWITH_XIMEA=OFF -DWITH_XINE=OFF -DBUILD_SHARED_LIBS=ON -DBUILD_PACKAGE=ON -DCMAKE_COLOR_MAKEFILE=ON -DWITH_EIGEN=ON -DCMAKE_BUILD_TYPE=RELEASE  -DCMAKE_CXX_FLAGS_RELEASE="-DNDEBUG -O3 -s PRECISE_F32=1 -Wno-warn-absolute-paths" -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O3 -s PRECISE_F32=1" -DCMAKE_C_FLAGS_RELEASE="-DNDEBUG -O3 -s PRECISE_F32=1" -DCMAKE_C_FLAGS_RELWITHDEBINFO="-O3 -s PRECISE_F32=1" -DCMAKE_EXE_LINKER_FLAGS_RELEASE="-O3 -s PRECISE_F32=1" -DCMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO="-O3 -g -s PRECISE_F32=1" -DCMAKE_MODULE_LINKER_FLAGS_RELEASE="-O3 -s PRECISE_F32=1" -DCMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO="-O3 -g -s PRECISE_F32=1" -DCMAKE_SHARED_LINKER_FLAGS_RELEASE="-O3 -s PRECISE_F32=1" -DCMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO="-O3 -g -s PRECISE_F32=1" ..


but I am getting following error on ‘emmake make’: 


[  5%] Built target zlib

[  5%] Generating opencl_kernels_core.cpp, opencl_kernels_core.hpp

Scanning dependencies of target opencv_core

[  7%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/rand.cpp.o

In file included from /home/gunjan/Downloads/emsdk_portable/opencv/modules/core/src/rand.cpp:49:

In file included from /home/gunjan/Downloads/emsdk_portable/opencv/modules/core/src/precomp.hpp:55:

In file included from /home/gunjan/Downloads/emsdk_portable/opencv/modules/core/include/opencv2/core/private.hpp:58:

In file included from /usr/include/eigen3/Eigen/Core:263:

/usr/include/eigen3/Eigen/src/Core/arch/SSE/PacketMath.h:333:76: error: no matching function for call to '_mm_prefetch'

template<> EIGEN_STRONG_INLINE void prefetch<float>(const float*   addr) { _mm_prefetch((const char*)(addr), _MM_HINT_T0); }

                                                                           ^~~~~~~~~~~~

/home/gunjan/Downloads/emsdk_portable/emscripten/master/system/include/emscripten/xmmintrin.h:129:1: note: candidate function not viable: no known conversion from 'const char *' to 'void *' for 1st

      argument

_mm_prefetch(void *__p, int __i)


Please refer the screenshot for additional information.

Any help in this regard would be greatly beneficial for my project. Please let me know if you need any other detail.

 

Thank you.

-Gunjan

error.jpg

juj j

unread,
Jun 21, 2016, 8:13:47 AM6/21/16
to emscripte...@googlegroups.com
Thanks for reporting,

I think this is fixed by https://github.com/kripken/emscripten/commit/2e56e7764fcfac0dc617723283cf064dd7115cbd, which is currently in the incoming branch (./emsdk install sdk-incoming-64bit && ./emsdk activate sdk-incoming-64bit)

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

Gunjan Shrivastava

unread,
Aug 9, 2016, 7:42:16 PM8/9/16
to emscripten-discuss
Thanks jj,

I was able to successfully compile opencv (https://github.com/kakukogou/opencv/tree/opencvjs) after installing the incoming branch but still there are errors coming when I tried to run the segmentation demo(https://github.com/kakukogou/opencvjs_demo_segmentation).

When I build 'make', I am getting the following error:

mkdir -p ./bin
/home/gunjan/gsoc/emsdk-07-26-2016/emsdk_portable/emscripten/incoming/emcc \
../src/segmentation.cpp \
../src/export.cpp \
-I /home/gunjan/gsoc/emsdk-07-26-2016/emsdk_portable/emscripten/incoming/opencv/release_asm/install/include \
/home/gunjan/gsoc/emsdk-07-26-2016/emsdk_portable/emscripten/incoming/opencv/release_asm/install/lib/libopencv_core.so \
/home/gunjan/gsoc/emsdk-07-26-2016/emsdk_portable/emscripten/incoming/opencv/release_asm/install/lib/libopencv_imgproc.so \
-s EXPORTED_FUNCTIONS="['_on_mouse', '_on_init', '_on_process']" \
-O3 \
--llvm-lto 1 \
--closure 1 \
-s PRECISE_F32=1 \

  %703 = extractelement <2 x i64> %bc.i.i135, i32 0
0  llc             0x0000000000e38d98 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 56
1  llc             0x0000000000e37026 llvm::sys::RunSignalHandlers() + 54
2  llc             0x0000000000e37189
3  libpthread.so.0 0x00007f291f9c43d0
4  llc             0x0000000000b65091 llvm::CallInst::init(llvm::FunctionType*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*> >, llvm::Twine const&) + 177
5  llc             0x0000000000ef3982
6  llc             0x0000000000ef6cb0
7  llc             0x0000000000b8677f llvm::legacy::PassManagerImpl::run(llvm::Module&) + 783
8  llc             0x0000000000569e5b
9  llc             0x000000000054bb10 main + 256
10 libc.so.6       0x00007f291ed69830 __libc_start_main + 240
11 llc             0x0000000000562349 _start + 41
Stack dump:
0.    Program arguments: /home/gunjan/gsoc/emsdk-07-26-2016/emsdk_portable/clang/fastcomp/build_incoming_64/bin/llc /tmp/tmpPJYtDm/a.out.bc -march=js -filetype=asm -o /tmp/tmpdIDpbR.4.js -emscripten-precise-f32 -emscripten-global-base=8 -O3
1.    Running pass 'Expand and lower illegal >i32 operations into 32-bit chunks' on module '/tmp/tmpPJYtDm/a.out.bc'.

Traceback (most recent call last):
  File "/home/gunjan/gsoc/emsdk-07-26-2016/emsdk_portable/emscripten/incoming/emcc", line 13, in <module>
    emcc.run()
  File "/home/gunjan/gsoc/emsdk-07-26-2016/emsdk_portable/emscripten/incoming/emcc.py", line 1490, in run
    final = shared.Building.emscripten(final, append_ext=False, extra_args=extra_args)
  File "/home/gunjan/gsoc/emsdk-07-26-2016/emsdk_portable/emscripten/incoming/tools/shared.py", line 1691, in emscripten
    call_emscripten(cmdline)
  File "/home/gunjan/gsoc/emsdk-07-26-2016/emsdk_portable/emscripten/incoming/emscripten.py", line 1721, in _main
    temp_files.run_and_clean(lambda: main(
  File "/home/gunjan/gsoc/emsdk-07-26-2016/emsdk_portable/emscripten/incoming/tools/tempfiles.py", line 64, in run_and_clean
    return func()
  File "/home/gunjan/gsoc/emsdk-07-26-2016/emsdk_portable/emscripten/incoming/emscripten.py", line 1726, in <lambda>
    DEBUG=DEBUG,
  File "/home/gunjan/gsoc/emsdk-07-26-2016/emsdk_portable/emscripten/incoming/emscripten.py", line 1627, in main
    temp_files=temp_files, DEBUG=DEBUG)
  File "/home/gunjan/gsoc/emsdk-07-26-2016/emsdk_portable/emscripten/incoming/emscripten.py", line 86, in emscript
    funcs, metadata, mem_init = get_and_parse_backend(infile, settings, temp_files, DEBUG)
  File "/home/gunjan/gsoc/emsdk-07-26-2016/emsdk_portable/emscripten/incoming/emscripten.py", line 146, in get_and_parse_backend
    backend_output = open(temp_js).read()
IOError: [Errno 2] No such file or directory: '/tmp/tmpdIDpbR.4.js'
Makefile:7: recipe for target 'segmentation' failed
make: *** [segmentation] Error 1


Can someone help me here, anyone who may have faced a similar issue?

Thanks,
Gunjan.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.

Jukka Jylänki

unread,
Aug 16, 2016, 1:24:28 PM8/16/16
to emscripte...@googlegroups.com
That looks like this bug https://github.com/kripken/emscripten/issues/4395. Unfortunately we don't currently have a fix for that, but for OpenCV, you can try editing the CMakeLists.txt to remove SIMD support to see if that works around the issue.

To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages