How to run OpenMP parallel in android native code? - android-ndk

I am currently using OpenMP's #pragma's to distribute Android app's native code for loop to parallel threads as follows:
#pragma omp parallel shared(variable1) shared(variable2) shared(variable3) private(array_1) private(array_1) private(array_1)
{
LOG(INFO) << "Running inside thread: " << omp_get_thread_num();
#pragma omp for
for (int c = 1; c <= itrloopLim; c++) {
...
}
}
I am compiling with -fopenmp, but after installing the .apk, the app crashes on runtime with the below error:
12-06 23:30:58.700 32133 32314 W System.err: TensorFlowLite: failed to load native library: dlopen failed: cannot locate symbol "__kmpc_end_critical" referenced by "/data/app/org.mlperf.inference-aKoP_FhySRDbKCU5EeEcmQ==/lib/arm64/libmlperf_jni.so"...
12-06 23:30:58.700 32133 32314 E lperf.inferenc: No implementation found for void org.mlperf.inference.MLPerfDriverWrapper.nativeDeleteBackend(long) (tried Java_org_mlperf_inference_MLPerfDriverWrapper_nativeDeleteBackend and Java_org_mlperf_inference_MLPerfDriverWrapper_nativeDeleteBackend__J)
12-06 23:30:58.701 32133 32314 E AndroidRuntime: FATAL EXCEPTION: MLPerf.Worker
12-06 23:30:58.701 32133 32314 E AndroidRuntime: Process: org.mlperf.inference, PID: 32133
12-06 23:30:58.701 32133 32314 E AndroidRuntime: java.lang.UnsatisfiedLinkError: No implementation found for void org.mlperf.inference.MLPerfDriverWrapper.nativeDeleteBackend(long) (tried Java_org_mlperf_inference_MLPerfDriverWrapper_nativeDeleteBackend and Java_org_mlperf_inference_MLPerfDriverWrapper_nativeDeleteBackend__J)
12-06 23:30:58.701 32133 32314 E AndroidRuntime: at org.mlperf.inference.MLPerfDriverWrapper.nativeDeleteBackend(Native Method)
12-06 23:30:58.701 32133 32314 E AndroidRuntime: at org.mlperf.inference.MLPerfDriverWrapper.access$000(MLPerfDriverWrapper.java:23)
12-06 23:30:58.701 32133 32314 E AndroidRuntime: at org.mlperf.inference.MLPerfDriverWrapper$Builder.useMLBackend(MLPerfDriverWrapper.java:205)
12-06 23:30:58.701 32133 32314 E AndroidRuntime: at org.mlperf.inference.RunMLPerfWorker.handleMessage(RunMLPerfWorker.java:102)
12-06 23:30:58.701 32133 32314 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
12-06 23:30:58.701 32133 32314 E AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
12-06 23:30:58.701 32133 32314 E AndroidRuntime: at android.os.HandlerThread.run(HandlerThread.java:65)
12-06 23:30:58.703 1439 1467 W ActivityManager: Force finishing activity org.mlperf.inference/.MLPerfEvaluation
12-06 23:30:58.707 1439 1585 I ActivityManager: Showing crash dialog for package org.mlperf.inference u0
12-06 23:30:58.715 1439 1584 W BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.DROPBOX_ENTRY_ADDED flg=0x10 (has extras) } to com.google.android.gms/.stats.service.DropBoxEntryAddedReceiver

I encountered the exact same issue, no error in compilation but the app crashes immediately on startup.
It turns out you need to use the "-static-openmp -fopenmp" flags. Your build gradle has to look like this basically :
externalNativeBuild {
cmake {
arguments "-DANDROID_ARM_NEON=ON"
cppFlags "-static-openmp -fopenmp"
}

Related

Running mtouch with --embeddinator flag on a dll giving error Undefined symbols for architecture arm64: "_xamarin_embeddinator_initialize"

I am trying to create a .framework from .net library using monotouch option --embeddinator. In the process I am getting below error
Undefined symbols for architecture arm64:
"_xamarin_embeddinator_initialize", referenced from:
native_to_managed_trampoline_1(objc_object*, objc_selector*, _MonoMethod**, int, int, unsigned int) in registrar.o
native_to_managed_trampoline_2(objc_object*, objc_selector*, _MonoMethod**, void*, unsigned int) in registrar.o
native_to_managed_trampoline_3(objc_object*, objc_selector*, _MonoMethod**, bool*, unsigned int) in registrar.o
native_to_managed_trampoline_4(objc_object*, objc_selector*, _MonoMethod**, objc_object*, unsigned int) in registrar.o ld: symbol(s) not found for architecture arm64
Here is the command I am trying to run
/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/bin/mtouch --dev /Users/mihirdas/Working/XamEmbed/XamEmbed/XamEmbed/bin/Release --abi=arm64 --sdkroot /Applications/Xcode.app --targetver 8.0 --dsym:false --msym:false --embeddinator XamEmbed.dll --reference=/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll --sdk 15.2 --linksdkonly --registrar:static --cache /Users/mihirdas/Working/XamEmbed/XamEmbed/XamEmbed/bin/Release/iOS/iPhoneOS/build-cache --assembly-build-target=#all=framework=XamEmbed.iOS.framework --target-framework Xamarin.iOS,v1.0
Any help would be highly appreciated. Thanks!!!

sql.js FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory

We are trying to insert (1200 records) into sqlite database (existing database of size 106 MB) from node js, using sqlite-chiper node module and
getting the below error.
We have even increased the --max-old-space-size=10192 till 10GB still we are getting the below error.
<--- Last few GCs --->
[16316:00000292A20A0C10] 15038 ms: Scavenge 1178.9 (1213.6) -> 1178.9 (1213.6) MB, 115.4 / 0.0 ms (average mu = 0.999, current mu = 0.999) allocation failure
[16316:00000292A20A0C10] 16143 ms: Mark-sweep 1752.7 (1787.4) -> 1743.6 (1779.3) MB, 517.7 / 0.0 ms (+ 5.4 ms in 12 steps since start of marking, biggest step 0.9 ms, walltime since start of marking 2513 ms) (average mu = 0.945, current mu = 0.933) al
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 00007FF7BDBA2C6D]
Security context: 0x02fbb1f408a1 <JSObject>
1: write [0000029FB013E4D9] [E:\Product\Trunk\node_modules\sql.js\js\sql.js:~2114] [pc=00000379B65AE651](this=0x029fb01375f9 <Object map = 0000014672038741>,0x038bd5a3ff79 <JSObject>,0x032891c17b29 <Int8Array map = 0000014672002781>,6378736,4096,112812032,0x028694c804a9 <undefined>)
2: write [0000032891C1CAB9] [E:\Product\Trunk\node_modules\s...
FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory
Writing Node.js report to file: report.20200831.233420.16316.0.001.json
Node.js report completed
1: 00007FF7BCFD094F napi_wrap+124431
2: 00007FF7BCF72696 public: bool __cdecl v8::base::CPU::has_sse(void)const __ptr64+34502
3: 00007FF7BCF73356 public: bool __cdecl v8::base::CPU::has_sse(void)const __ptr64+37766
4: 00007FF7BD776F4E private: void __cdecl v8::Isolate::ReportExternalAllocationLimitReached(void) __ptr64+94
5: 00007FF7BD75EF91 public: class v8::SharedArrayBuffer::Contents __cdecl v8::SharedArrayBuffer::Externalize(void) __ptr64+833
6: 00007FF7BD62C85C public: static void __cdecl v8::internal::Heap::EphemeronKeyWriteBarrierFromCode(unsigned __int64,unsigned __int64,class v8::internal::Isolate * __ptr64)+1436
7: 00007FF7BD64287D private: class v8::internal::HeapObject __cdecl v8::internal::Factory::AllocateRawFixedArray(int,enum v8::internal::AllocationType) __ptr64+61
8: 00007FF7BD649C34 private: class v8::internal::Handle<class v8::internal::FixedArray> __cdecl v8::internal::Factory::NewFixedArrayWithFiller(enum v8::internal::RootIndex,int,class v8::internal::Object,enum v8::internal::AllocationType) __ptr64+52
9: 00007FF7BD649BF1 public: class v8::internal::Handle<class v8::internal::FixedArray> __cdecl v8::internal::Factory::NewFixedArray(int,enum v8::internal::AllocationType) __ptr64+65
10: 00007FF7BD52AD9F public: class v8::Isolate * __ptr64 __cdecl v8::Message::GetIsolate(void)const __ptr64+7663
11: 00007FF7BD3DE55A public: class v8::internal::Object __cdecl v8::internal::OrderedHashMap::ValueAt(int) __ptr64+61274
12: 00007FF7BDBA2C6D public: virtual bool __cdecl v8::internal::SetupIsolateDelegate::SetupHeap(class v8::internal::Heap * __ptr64) __ptr64+567949
13: 00000379B65AE651
Any suggestion on how to resolve this problem ?
Since we are getting memory error in sql.js, we have moved to sqlite3 for node js to resolve this problem

(cocos2d-x) How to debug android native crash with anonymous and unknown backtrace?

I use cocos2d-x and ndk-build to build app on arm64. But when i run it on 64bit device, the app crash randomly with error signal 11 (SIGSEGV), and the backtrace shows anonymous and unknown.
I use cocos2d-x 3.17.1, ndk 16, Android Studio 3.4.1, gradle tools 3.2.0 and gradle wrapper 4.6.
I tried ndk-stack but it didn't show me more useful information.
This is the log in the Logcat.
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'Xiaomi/chiron/chiron:8.0.0/OPR1.170623.027/V10.3.1.0.ODECNXM:user/release-keys'
Revision: '0'
ABI: 'arm64'
pid: 17667, tid: 17711, name: GLThread 135726 >>> com.test.myapp <<<
signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x72adf0f460
x0 00000072bc089378 x1 0000000000000000 x2 fffd8072bc08bc18 x3 fffd8072bb3f4950
x4 00000000ee1763de x5 fffd8072bc096c88 x6 ff687373604f6d64 x7 7f7f7f7f7f7f7f7f
x8 00000072b09a9f08 x9 00000072b09a9f00 x10 fffffffffffffffb x11 00000072a9bcc6c8
x12 00000072aceade40 x13 0000000000000000 x14 0000000000697474 x15 00000072a9ac6c10
x16 0000000000000001 x17 fffa0072a9ac58c8 x18 0000000000000012 x19 00000072b09a9e98
x20 fffd8072a9bc31e0 x21 00000072a9bcfa00 x22 00000072bc0893d8 x23 00000072ac48ab40
x24 00000072b162cca8 x25 00000072ade90978 x26 00000072a94d0c20 x27 00000072a99953e0
x28 00000072a9995070 x29 00000072b0da8080 x30 fffd8072a9ac48a0
sp 00000072b0da8060 pc 00000072adf0f460 pstate 0000000080000000
backtrace:
#00 pc 0000000000078460 <anonymous:00000072ade97000>
#01 pc fffd8072a9ac489c <unknown>
This is the log with ndk-stack
********** Crash dump: **********
Build fingerprint: 'Xiaomi/chiron/chiron:8.0.0/OPR1.170623.027/V10.3.1.0.ODECNXM:user/release-keys'
pid: 17667, tid: 17711, name: GLThread 135726 >>> com.test.myapp <<<
signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x72adf0f460
Stack frame #00 pc 0000000000078460 <anonymous:00000072ade97000>
Stack frame #01 pc fffd8072a9ac489c <unknown>
I expect the backtrace and ndk-stack can show me where the problem is, but it shows only anonymous and unknown.

get assert() failure message in android ndk sigaction crash handler

I am using sigaction() to install a crash handler in my app and print private version information.
In case of a failed assert() the abort message is super useful and I'd like my sigaction handler to print it. How can I get that string? Below is some analysis I've done thus far:
I noticed in ndk debuggerd/tombstone.c engrave_tombstone() that there is a argument uintptr_t abort_msg_address which contains the abort message, typically a failed assert. But no clue where that is fetched from. Noticing the default crash logs of android seem to run in a different process and debuggerd imples it's a daemon, I am not sure this is the right way to go.
I furthermore see in bionic libc/stdlib/assert.c there is just a call to __libc_android_log_print(ANDROID_LOG_FATAL, ...). Also not very helpful. But in bionic linker_main.cpp there is abort_msg_t* g_abort_message = nullptr; and other exciting stuff in android_set_abort_message.cpp. Again, not sure this is the right way, feels very hackish.
This is what the crash handler of android prints by default. Note how the first message in in the crashed pid/tid, but the others are some other random process (presumably, debuggerd?).
10-09 16:49:28.551 12084 12127 F libc : Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 12127 (applyRouting), pid 12084 (com.android.nfc)
10-09 16:49:28.691 12203 12203 F DEBUG : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
10-09 16:49:28.692 12203 12203 F DEBUG : Build fingerprint: 'Android/aosp_marlin/marlin:Q/OC-MR1/summit07191458:userdebug/test-keys'
10-09 16:49:28.692 12203 12203 F DEBUG : Revision: '0'
10-09 16:49:28.692 12203 12203 F DEBUG : ABI: 'arm64'
10-09 16:49:28.692 12203 12203 F DEBUG : pid: 12084, tid: 12127, name: applyRouting >>> com.android.nfc <<<
10-09 16:49:28.692 12203 12203 F DEBUG : signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
10-09 16:49:28.692 12203 12203 F DEBUG : Abort message: 'jni_internal.cc:622] JNI FatalError called: applyRouting'
10-09 16:49:28.692 12203 12203 F DEBUG : x0 0000000000000000 x1 0000000000002f5f x2 0000000000000006 x3 0000000000000008

Call Matlab from Intel Fortran on Linux

Editted:
This is building on my previous question.
With the answer, I am able to compile and run with gfortran however, I am still unable to run with ifort. The commands I am using to compile in tcsh are:
set PATH=${PATH}:${matlabroot}/bin/glnxa64/:${matlabroot}/bin
set LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${matlabroot}/bin/glnxa64/:${matlabroot}/sys/os/glnxa64/
${FC} ${matlabroot}/extern/examples/eng_mat/fengdemo.F -I${matlabroot}/extern/include/ -L${matlabroot}/bin/glnxa64/ -leng -lmx -cpp -o FEngDemo
Do I need to make changes to the libraries I use? To the Fortran code? Or to the Matlab code?
Any help would be appreciated.
The Fortran code is:
#include "fintrf.h"
C
#if 0
C
C fengdemo.F
C .F file need to be preprocessed to generate .for equivalent
C
#endif
C
C fengdemo.f
C
C This is a simple program that illustrates how to call the MATLAB
C Engine functions from a FORTRAN program.
C
C Copyright 1984-2011 The MathWorks, Inc.
C======================================================================
C
program main
C Declarations
implicit none
mwPointer engOpen, engGetVariable, mxCreateDoubleMatrix
mwPointer mxGetPr
mwPointer ep, T, D
double precision time(10), dist(10)
integer engPutVariable, engEvalString, engClose
integer temp, status
mwSize i
data time / 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 /
mwSize M, N
parameter(M=1)
parameter(N=10)
C
ep = engOpen('matlab ')
C
if (ep .eq. 0) then
write(6,*) 'Can''t start MATLAB engine'
stop
endif
C
T = mxCreateDoubleMatrix(M, N, 0)
call mxCopyReal8ToPtr(time, mxGetPr(T), N)
C
C
C Place the variable T into the MATLAB workspace
C
status = engPutVariable(ep, 'T', T)
C
if (status .ne. 0) then
write(6,*) 'engPutVariable failed'
stop
endif
C
C
C Evaluate a function of time, distance = (1/2)g.*t.^2
C (g is the acceleration due to gravity)
C
if (engEvalString(ep, 'D = .5.*(-9.8).*T.^2;') .ne. 0) then
write(6,*) 'engEvalString failed'
stop
endif
C
C
C Plot the result
C
if (engEvalString(ep, 'plot(T,D);') .ne. 0) then
write(6,*) 'engEvalString failed'
stop
endif
if (engEvalString(ep, 'title(''Position vs. Time'')') .ne. 0) then
write(6,*) 'engEvalString failed'
stop
endif
if (engEvalString(ep, 'xlabel(''Time (seconds)'')') .ne. 0) then
write(6,*) 'engEvalString failed'
stop
endif
if (engEvalString(ep, 'ylabel(''Position (meters)'')') .ne. 0)then
write(6,*) 'engEvalString failed'
stop
endif
C
C
C read from console to make sure that we pause long enough to be
C able to see the plot
C
print *, 'Type 0 <return> to Exit'
print *, 'Type 1 <return> to continue'
read(*,*) temp
C
if (temp.eq.0) then
print *, 'EXIT!'
status = engClose(ep)
if (status .ne. 0) then
write(6,*) 'engClose failed'
endif
stop
end if
C
if (engEvalString(ep, 'close;') .ne. 0) then
write(6,*) 'engEvalString failed'
stop
endif
C
D = engGetVariable(ep, 'D')
call mxCopyPtrToReal8(mxGetPr(D), dist, N)
print *, 'MATLAB computed the following distances:'
print *, ' time(s) distance(m)'
do 10 i=1,10
print 20, time(i), dist(i)
20 format(' ', G10.3, G10.3)
10 continue
C
C
call mxDestroyArray(T)
call mxDestroyArray(D)
status = engClose(ep)
C
if (status .ne. 0) then
write(6,*) 'engClose failed'
stop
endif
C
stop
end
With the -g and -traceback flags, the error I get at runtime is:
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image PC Routine Line Source
FEngDemo 0000000000477619 Unknown Unknown Unknown
FEngDemo 0000000000475EEE Unknown Unknown Unknown
FEngDemo 000000000044D8F2 Unknown Unknown Unknown
FEngDemo 0000000000431573 Unknown Unknown Unknown
FEngDemo 00000000004041FB Unknown Unknown Unknown
libpthread.so.0 0000003E1D80F790 Unknown Unknown Unknown
libmx.so 00007F5F077FCED7 Unknown Unknown Unknown
FEngDemo 00000000004032ED MAIN__ 46 fengdemo.F
FEngDemo 0000000000403186 Unknown Unknown Unknown
libc.so.6 0000003E1D01ED5D Unknown Unknown Unknown
FEngDemo 0000000000403009 Unknown Unknown Unknown

Resources