Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-core Android

I have run simple parallel algorithm drawing the mandelbrot set to test parallel computations on a Nexus 7 (Tegra 3, 4+1 cores). After running several times I get 1.5 seconds for serial and 1.0 for parallel, but parallel and serial come really close to each other at 1.3 seconds.

The square is 700x700 pixels, and the mandelbrot code I use is from

http://rosettacode.org/wiki/Mandelbrot_set#Java

The parallel implementation runs two halves of mandelbrot like this

    public void mandelbrotParallel() {
    Thread t1 = new Thread(new Runnable() {
        public void run() {
            mandelbrotOne();
        }
    });
    Thread t2 = new Thread(new Runnable() {
        public void run() {
            mandelbrotTwo();
        }
    });
    t1.start();
    t2.start();
    try {
        t1.join();
        t2.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mHandler.post(new Runnable() {
        public void run() {
            v.setBmp(bmp);
            v.invalidate();
        }
    });
}

I have run a simple vector addition before and found similar anecdotal results (no scientific rigor). So I wonder if there is anything special one has to do to get Android to fire up multiple cores to get a task done.

Based on quick conversations with Google, it could be that the cores are dormant and wait for the computation to be truly long running (multiple seconds) before the cores are turned on... Is this true ? If so, are there API calls from Java (no JNI) that can be done to preemptively wake up the cores ?

like image 548
jeremyvillalobos Avatar asked Oct 04 '22 11:10

jeremyvillalobos


1 Answers

This sounds like a candidate for RenderScript. In a nutshell, it allows you to do computationally expensive operations that take advantage of all available acceleration resources (multiple cores, GPU compute, dsp, etc). From the docs:

Renderscript gives your apps the ability to run operations with automatic parallelization across all available processor cores. It also supports different types of processors such as the CPU, GPU or DSP. Renderscript is useful for apps that do image processing, mathematical modeling, or any operations that require lots of mathematical computation.

You'd have to rewrite your Mandelbrot code in C, but you won't have to break it apart into pieces since parallelization will be taken care of for you.

Using RenderScript from Android code is straightforward as described here.

like image 109
Brett Duncavage Avatar answered Oct 10 '22 03:10

Brett Duncavage