Android OpenCV并行化循环 [英] Android OpenCV parallelize loops
问题描述
我知道NDK中包含OpenMP(此处使用示例: http ://recursify.com/blog/2013/08/09/openmp-on-android ).我已经完成了该页面上的说明,但是当我在简单的for循环上使用 #pragma omp for 扫描矢量时,应用程序崩溃,并出现了著名的致命信号11".>
我在这里想念什么?顺便说一句,我使用了一个来自Android示例的修改示例,它是Tutorial 2 Mixed Processing.我想要的是使用OpenCV并行化(多线程)一些jni c ++文件中的for循环和嵌套的for循环.
感谢您的帮助/建议!
添加了示例代码:
#pragma omp parallel for
Mat tmp(iheight, iwidth, CV_8UC1);
for (int x = 0; x < iheight; x++) {
for (int y = 0; y < iwidth; y++) {
int value = (int) buffer[x * iwidth + y];
tmp.at<uchar>(x, y) = value;
}
}
基于此: http://www. slideshare.net/noritsuna/如何在本地活动中使用openmp 谢谢!
我认为这是GOMP中的已知问题,请参见臭虫52738 .
如果您尝试在非主线程上使用OpenMP指令或函数,则应用程序将崩溃,并且可以追溯到gomp_thread()
函数(请参见在这里,您可以阅读有关Android TLS的相同答案.
GOMP创建工作线程时,会在函数gomp_thread_start()
(第72行)中设置线程特定的数据:
#ifdef HAVE_TLS
thr = &gomp_tls_data;
#else
struct gomp_thread local_thr;
thr = &local_thr;
pthread_setspecific (gomp_tls_key, thr);
#endif
但是,当应用程序独立创建线程时,未设置线程特定的数据,因此gomp_thread()
函数将返回NULL
.这会导致崩溃,并且在支持TLS的情况下这不是问题,因为使用的全局变量将始终可用
我记得这个问题已得到解决,android-ndk-r10d
,但仅适用于后台进程(无Java).这意味着当您启用OpenMP并从JNI(从Java Android调用)创建本地线程时,您的应用程序将崩溃.
I know that OpenMP is included in NDK (usage example here: http://recursify.com/blog/2013/08/09/openmp-on-android ). I've done what it says on that page but when I use: #pragma omp for on a simple for loop that scans a vector, the app crashes with the famous "fatal signal 11".
What am I missing here? Btw I use a modified example from the Android samples, it's Tutorial 2 Mixed Processing. All I want is to parallelize (multithread) some of the for loops and nested for loops that I have in the jni c++ file while using OpenCV.
Any help/suggestion is appreciated!
Edit: sample code added:
#pragma omp parallel for
Mat tmp(iheight, iwidth, CV_8UC1);
for (int x = 0; x < iheight; x++) {
for (int y = 0; y < iwidth; y++) {
int value = (int) buffer[x * iwidth + y];
tmp.at<uchar>(x, y) = value;
}
}
Based on this: http://www.slideshare.net/noritsuna/how-to-use-openmp-on-native-activity Thanks!
I think this is a known issue in GOMP, see Bug 42616 and Bug 52738.
It's about your app will crash if you try to use OpenMP directives or functions on a non-main thread, and can be traced back to the gomp_thread()
function (see libgomp/libgomp.h @ line 362 and 368) which returns NULL
for threads you create:
#ifdef HAVE_TLS
extern __thread struct gomp_thread gomp_tls_data;
static inline struct gomp_thread *gomp_thread (void)
{
return &gomp_tls_data;
}
#else
extern pthread_key_t gomp_tls_key;
static inline struct gomp_thread *gomp_thread (void)
{
return pthread_getspecific (gomp_tls_key);
}
#endif
As you can see GOMP uses different implementation depending on whether or not thread-local storage (TLS) is available.
- If it is available, then
HAVE_TLS
flag is set, and a global variable is used to track the state of each thread, - Otherwise, thread-local data will be managed via the function
pthread_setspecific
.
In the earlier version of NDKs the thread-local storage (the __thread
keyword) isn't supported so HAVE_TLS
won't be defined, therefore pthread_setspecific
will be used.
Remark: I'm not sure whether __thread
is supported or not in the last version of NDK, but here you can read the same answers about Android TLS.
When GOMP creates a worker thread, it sets up the thread specific data in the function gomp_thread_start()
(line 72):
#ifdef HAVE_TLS
thr = &gomp_tls_data;
#else
struct gomp_thread local_thr;
thr = &local_thr;
pthread_setspecific (gomp_tls_key, thr);
#endif
But, when the application creates a thread independently, the thread specific data isn't set, and so the gomp_thread()
function returns NULL
. This causes the crash and this isn't a problem when TLS is supported, since the global variable that's used will always be available
I remember that this issue had been fixed android-ndk-r10d
, but it only works with background processes (no Java). It means when you enable OpenMP and create a native thread from JNI (what is called from Java Android) then your app will crash remains.
这篇关于Android OpenCV并行化循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!