OpenMP in NDK always uses 1 thread

228 views
Skip to first unread message

Joydeep Mitra

unread,
Dec 1, 2017, 2:25:42 PM12/1/17
to android-ndk
Hi,

I am trying to explore OpenMP in Android. However, my code always runs using 1 thread even if the max no of threads available is 6. I have tried to set the no. of threads to 6 but it still runs on only 1. I have also tried to set the environment variable to 6 but it still doesn't work. Can anyone provide some insight as to why this is happening? Here is the code I am using.

jdouble Java_edu_example_anative_myfirstnativeapp_MyNative_calcPipar(){
int i,j;
int tid;
double x;
double pi, sum = 0.0;
double start, delta;

step = 1.0/(double) steps;

sum = 0.0;

__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "Max Threads %d...\n", omp_get_max_threads());
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "No of Processors %d...\n", omp_get_num_procs());
omp_set_num_threads(omp_get_max_threads());
omp_set_dynamic(0);
start = omp_get_wtime();
#pragma omp parallel for private(x, tid) reduction(+:sum)
tid = omp_get_thread_num();
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "No. of Threads working %d \n", omp_get_num_threads());
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "Thread %d working...\n", tid);
for (i=0; i < steps; i++) {
x = (i+0.5)*step;
sum += 4.0 / (1.0+x*x);
}

delta = omp_get_wtime() - start;
// Out of the parallel region, finialize computation
pi = step * sum;
//printf("PI = %.16g computed in %.4g seconds\n", pi, delta);
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "PI = %.16g computed in %.4g seconds\n", pi, delta);
return pi;
}

Ryan Prichard

unread,
Dec 2, 2017, 3:23:01 PM12/2/17
to andro...@googlegroups.com
The code didn't compile:
 - LOG_TAG, step, and steps weren't declared.
 - The #pragma omp needs to immediately precede the for loop. Move the "for (i=0; i < steps; i++) {" line up.

Once I make those fixes, I see this output:

11-27 04:59:13.173 10356 10356 D test    : Max Threads 8...
11-27 04:59:13.173 10356 10356 D test    : No of Processors 8...
11-27 04:59:13.179 10356 10356 D test    : No. of Threads working 8 
11-27 04:59:13.179 10356 10358 D test    : No. of Threads working 8 
11-27 04:59:13.179 10356 10356 D test    : Thread 0 working...
11-27 04:59:13.179 10356 10358 D test    : Thread 2 working...
...
11-27 04:59:13.179 10356 10358 D test    : No. of Threads working 8 
11-27 04:59:13.179 10356 10358 D test    : Thread 2 working...
11-27 04:59:13.179 10356 10359 D test    : No. of Threads working 8 
11-27 04:59:13.179 10356 10359 D test    : Thread 3 working...
11-27 04:59:13.179 10356 10359 D test    : No. of Threads working 8 
11-27 04:59:13.179 10356 10359 D test    : Thread 3 working...
11-27 04:59:13.179 10356 10359 D test    : No. of Threads working 8 
11-27 04:59:13.179 10356 10359 D test    : Thread 3 working...
11-27 04:59:13.179 10356 10359 D test    : No. of Threads working 8 
11-27 04:59:13.179 10356 10359 D test    : Thread 3 working...
...
11-27 04:59:13.194 10356 10361 D test    : No. of Threads working 8 
11-27 04:59:13.194 10356 10361 D test    : Thread 5 working...
11-27 04:59:13.194 10356 10361 D test    : No. of Threads working 8 
11-27 04:59:13.194 10356 10361 D test    : Thread 5 working...
11-27 04:59:13.197 10356 10356 D test    : PI = 3.141600986923125 computed in 0.02364 seconds

-Ryan



--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/7c3fc447-1054-4f78-b077-848d5bb13da6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kenneth Geisshirt

unread,
Dec 2, 2017, 3:23:01 PM12/2/17
to andro...@googlegroups.com

Which NDK version do you use? I think you need r15 and use clang as compiler.

- Kenneth


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

Kenneth Geisshirt, M.Sc., Ph.D.
Majbøl Allé 18, DK-2770 Kastrup, +45 60 62 71 82

Arthur Araújo

unread,
Dec 26, 2017, 5:42:48 PM12/26/17
to android-ndk
Hi,

Did you get it working? I'm having a similar problem trying to create OpenMP threads in Android. I'm using Clang 3.8 on Visual Studio 2015 (Android NDK r11c). Basically, I got it all compiled and running on mobile, but there is no forking going on, I always end up with one thread only.

Arthur
Reply all
Reply to author
Forward
0 new messages