I've been executing some tests on Android in order to verify how good the performance of an algorithm (like FFT) can be improved if it is parallelized. I've implemented the algorithms by using pthread with JNI (FFTW) and Java threads (from JTransforms). Instead of getting a better performance by using threads as expected, I've got better results using serial algorithm. It is unclear to me why I've got those results since I'd executed those tests on multicore devices. It seems that the scheduling algorithm used by Android system is kinda different from the one used by Linux and you're out of luck if you want to use more than one CPU to do multiprocessing on Android.
Example with FFTW: The JNI code is in https://github.com/maxrosan/DspBenchmarking/blob/master/jni/fftw_jni.c and its interface is https://github.com/maxrosan/DspBenchmarking/blob/master/src/br/usp/ime/dspbenchmarking/algorithms/fftw/FFTW.java.
The method called in tests is 'execute'.
Example with pure Java: https://github.com/maxrosan/DspBenchmarking/blob/master/src/br/usp/ime/dspbenchmarking/algorithms/jtransforms/fft/DoubleFFT_1D2TAlgorithm.java
Here the method called is 'perform'.
'execute' and 'perform' are called inside another thread.
Android applications can consist of one or more processes. You can choose to run your services or activities in a process other than the main one.
Android can use multiple CPU cores for multithreading, but the kernel and JVM handle that process, not the developer himself. An internal multithreading design will improve the program's basic performance, but the device upon which it actually runs will determine its speed.
However, you can arrange for different components in your application to run in separate processes, and you can create additional threads for any process. This document discusses how processes and threads work in an Android application.
Two basic principles for Process Management: ❏ Android OS tries to maintain an application process for as long as possible ❏ Only kills process when out of memory. ❏ Remove process to reclaim memory for more important/new processes. ❏
If your program has multiple CPU-intensive threads running for a sustained period, the kernel will shift threads to separate cores. Otherwise, the kernel is motivated by two things:
Android turns cores off when possible, and only enables them when CPU demand requires them. What exactly constitutes a "sustained period" varies from device to device.
I put together two bits of sample code that demonstrate multiple cores in use (C version, Java version).
With a rooted device that has systrace support you can actually see graphically which thread is running on each core.
Update: I thought it might help to have an example, so I wrapped my MultiCore.java test inside a sample app and ran it on a 4.3 Nexus 4 under systrace. I created a page that explains the results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With