Android AsyncTask, DeepBeliefSDK e SISSEGV

(Article copied from my old blog)

Lately I have been playing with the DeepBeliefSDK released by JetPac, a startup acquired by Google in 2014.

I have encountered a lot of problems when I have tried to bring the usage of the library from the main application thread (as it is used in the provided examples) to a background thread (called Worker in many different lnaguages and named AsyncTask in Android).

The library has a huge CPU usage, so I guess it HAS TO be used on a background thread, or the application will experience lag (on a Galaxy S5 it keeps 650ms to classify an image, on my device much more). During my tests with the AsyncTask, the application was costantly crashing, with a nice Segmentation Fault (SISSEGV error on the LogCat window) and no useful stacktrace (as expected from native C++ code).

When all seemed lost, I found this article. DeepBeliefSDK is using Open MP (from wikipedia: OpenMP (Open Multi-Processing) is an API that supports multi-platform shared memory multiprocessing programming in C, C++, and Fortran, on most processor architectures and operating systems […] It consists of a set of compiler directives, library routines, and environment variables that influence run-time behavior.), but there’s currently a bug in the way GOMP (GCC’s implementation of the OpenMP specification) handles data when thread-local storage isn’t available.

So when a non-main thread (the AsyncTask) attempts to use the Thread Local Storage, it crashes. There is a workaround, you should recompile the DeepBeliefSDK with a patched GCC toolchain.

Here you can find the recompiled SDK ready for the use on AsyncTasks. Enjoy! :)