Android * Tutorial: use intel & #174; thread build module to Write multi-threaded applications

Source: Internet
Author: User

By Vladimir Polin

Recently, we have released the "Windows * 8 Tutorial: using the Intel thread building module to Write multi-threaded applications for the Windows App Store ". We mentioned that parallel computing engines can easily be imported to other mobile or desktop platforms. Android is a typical example of such a mobile platform.

In the latest stable version of the Intel thread building module (Intel TBB), we have added experimental support for Android applications, that is, building the Intel TBB library, the JNI interface is used in Android applications. You can download this version from threadingbuildingblocks.org.

To start the process on a Linux * Host, open the Intel TBB source release, find the <unpacked_dir>/build/android_setup.csh script and build the library. You must build a library because the development version can only be released in the source form. The <unpacked_dir>/build/index.android.html file describes how to configure the environment and build libraries on Linux.

Assume that gnu make 3.81 is available in % PATH % (on Microsoft Windows * Host platform) and $ PATH (on Linux platform, we need to execute the following commands in the NDK environment to build the Intel TBB library for Android:
gmake tbb tbbmalloc target=android

The above is the operation required to build the library. Now we can use Eclipse * to build an example. For the following examples, I will use the Windows * Android SDK tool Rev.21 and Android NDK Rev 8C to describe the cross-platform development process.

Use the default template New Android Application to create a New object. For simplicity, we name it "app1", which is the same as the above release:

Select FullscreenActivity as the "activity ". The content of this template ends here. Please note that you cannot use com. example * to name Google Play *, but it can help us better understand it.

Then add several buttons to the master frame. After adding, the XML file of the master frame (app1/res/layout/activity_fullscreen.xml) is as follows:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#0099cc"    tools:context=".FullscreenActivity" >    <TextView        android:id="@+id/fullscreen_content"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:gravity="center"        android:keepScreenOn="true"        android:text="@string/dummy_content"        android:textColor="#33b5e5"        android:textSize="50sp"        android:textStyle="bold" />    <FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:fitsSystemWindows="true" >        <LinearLayout            android:id="@+id/fullscreen_content_controls"            style="?buttonBarStyle"            android:layout_width="match_parent"            android:layout_height="74dp"            android:layout_gravity="bottom|center_horizontal"            android:background="@color/black_overlay"            android:orientation="horizontal"            tools:ignore="UselessParent" >            <Button                android:id="@+id/dummy_button1"                style="?buttonBarButtonStyle"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="@string/dummy_button1"                android:onClick="onClickSR" />            <Button                android:id="@+id/dummy_button2"                style="?buttonBarButtonStyle"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="@string/dummy_button2"                android:onClick="onClickDR" />        </LinearLayout>    </FrameLayout></FrameLayout>

The string file (app1/res/values/strings. xml) is as follows:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">Sample</string>    <string name="dummy_content">Reduce sample</string>    <string name="dummy_button1">Simple Reduce</string>    <string name="dummy_button2">Deterministic Reduce</string></resources>

Then add the button processor:

// JNI functionsprivate native float onClickDRCall();private native float onClickSRCall();      public void onClickDR(View myView) {            TextView tv=(TextView)(this.findViewById(R.id.fullscreen_content));            float res=onClickDRCall();            tv.setText("Result DR is n" + res);      }      public void onClickSR(View myView) {            TextView tv=(TextView)(this.findViewById(R.id.fullscreen_content));            float res=onClickSRCall();            tv.setText("Result SR is n" + res);      }

Then, load the Library to the FullscreenActivity. java file:

       @Override      protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);…            System.loadLibrary("gnustl_shared");            System.loadLibrary("tbb");            System.loadLibrary("jni-engine");             }

When using the "tbb" library, all content should be cleared, and the "gnustl_shared" library must be used to support the C ++ language features of TBB. However, for the "jni-engine" library, we need more information.

"Jni-engine" is a compute ++ library that can run computing engines and call and Output C interfaces for JNI named onClickSRCall () and onClickSRCall.

Create the "jni" folder in the workspace according to the NDK development rules and create three files for the "jni-engine" library in this folder.

These files are:

Android. mk (<> Replace the text in parentheses with the actual value)

LOCAL_PATH := $(call my-dir)TBB_PATH := <path_to_the_package>include $(CLEAR_VARS)LOCAL_MODULE    := jni-engineLOCAL_SRC_FILES := jni-engine.cpp LOCAL_CFLAGS += -DTBB_USE_GCC_BUILTINS -std=c++11 -I$(TBB_PATH)/includeLOCAL_LDLIBS := -ltbb -L./ -L$(TBB_PATH)/<path_to_libtbb_so>include $(BUILD_SHARED_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE    := libtbbLOCAL_SRC_FILES := libtbb.soinclude $(PREBUILT_SHARED_LIBRARY)

 

Application. mk

APP_ABI := x86APP_GNUSTL_FORCE_CPP_FEATURES := exceptions rttiAPP_STL := gnustl_shared

 

Jni-engine.cpp:

 #include <jni.h>#include "tbb/parallel_reduce.h"#include "tbb/blocked_range.h"float SR_Click(){    int N=10000000;    float fr = 1.0f/(float)N;    float sum = tbb::parallel_reduce(        tbb::blocked_range<int>(0,N), 0.0f,        [=](const tbb::blocked_range<int>& r, float sum)->float         {            for( int i=r.begin(); i!=r.end(); ++i )                sum += fr;            return sum;        },        []( float x, float y )->float         {            return x+y;        }    );    return sum;   }float DR_Click(){    int N=10000000;    float fr = 1.0f/(float)N;    float sum = tbb::parallel_deterministic_reduce(        tbb::blocked_range<int>(0,N), 0.0f,        [=](const tbb::blocked_range<int>& r, float sum)->float         {            for( int i=r.begin(); i!=r.end(); ++i )                sum += fr;            return sum;        },        []( float x, float y )->float         {            return x+y;        }    );          return sum;   } extern "C" JNIEXPORT jfloat JNICALL Java_com_example_app1_FullscreenActivity_onClickDRCall(JNIEnv *env, jobject obj){    return DR_Click();}extern "C" JNIEXPORT jfloat JNICALL Java_com_example_app1_FullscreenActivity_onClickSRCall(JNIEnv *env, jobject obj){    return SR_Click();}

 

We used the algorithm in the previous blog.

When we build with NDK, it writes the Library to the desired folder, including our libjni-engine.so, libgnustl_shared.so, and libtbb. so libraries.

Next, switch back to Eclipse and build the app1.apk file. You can now install the application on AVD or the actual hardware. For AVD, it should be shown in

This is the end of the operation! This simple application is now available and can be used as the basis for compiling more complex parallel applications for Android. For users who use the code in the previous blog, the application has been successfully transplanted to Android.

* Other names and brands may be assets of other owners.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.