This blog is translated from an Intel blog in Russian posted at habrahabr.ru. Original post can be found at: http://habrahabr.ru/company/intel/blog/161527/
Recently we have wrote writing multithreaded applications for Windows shop with Intel ® Threading Building Blocks ( Intel ® TBB ). It states that the use of cross-platform libraries TBB make it easy to transfer computational codes from Windows to other platforms. Android is a very good example of "other platforms", This article will give you some details on how to do it.
Intel® recently released a new version of TBB libraries ( Intel ® TBB tbb41_20121112oss), which is available for download at threadingbuildingblocks.org. This release included experimental support for applications for Android, ie building native libraries for Android applications via JNI interface.
Android SDK Tools Rev.21 and Android NDK Rev 8C will need to be used for the construction of the library. In my case here, the sample will only works for NDK Linux. Some of library was built on Linux, and the others was done on Windows. In general, there is one more advantage of cross-platform library.
So what does it take to build a simple application using Intel TBB? First we need to extract and compile, as this TBB Libraries release is distributed only in the source. It means that there is a gnu make, the command line environment. You can use NDK issue the command:
gmake tbb tbbmalloc target=android
With all of the libraries, go to the Eclipse. Using templates and create a simple application, for simplicity, as in the previous case, let's call it app1:

You can choose FullscreenActivity as Activity, In this work pattern is complete. Please note that applications are not com.example *.
You can then add a couple of buttons on the main frame. After that, this XML file to the main frame (app1/res/layout/activity_fullscreen.xml) will look like below:
<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>
A file with the lines (app1/res/values/strings.xml) will be:
<?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>
Just add a handler buttons:
// JNI functions
private 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);
}
And loading of libraries in FullscreenActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
…
System.loadLibrary("tbb");
System.loadLibrary("jni-engine");
}
The library «tbb» should be clear. Add more details in «jni-engine».
«Jni-engine» - is a C + + library that provides the computational part and exposes C interfaces for JNI calls onClickSRCall () and onClickSRCall ().
Following the rules of development within the project, and create a directory 'jni'. Add create 3 files that are specific to our library «jni-engine».
They are:
Android.mk (in <> brackets data, which should be replaced with actual values)
LOCAL_PATH := $(call my-dir) TBB_PATH := <path_to_the_package> include $(CLEAR_VARS) LOCAL_MODULE := jni-engine LOCAL_SRC_FILES := jni-engine.cpp LOCAL_CFLAGS += -DTBB_USE_GCC_BUILTINS -std=c++11 -I$(TBB_PATH)/include LOCAL_LDLIBS := -ltbb -L./ -L$(TBB_PATH)/<path_to_libtbb_so> include $(BUILD_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libtbb LOCAL_SRC_FILES := libtbb.so include $(PREBUILT_SHARED_LIBRARY)
Application.mk
APP_ABI := x86 APP_GNUSTL_FORCE_CPP_FEATURES := exceptions rtti APP_STL := system
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();
}
When building using the NDK, it will lay out the libraries in a directory, including libjni-engine.so and libtbb.so.
Now go back to eclipse and build. Apk file. Application is ready to be installed on the device or AVD. Below is how it looks on the AVD:

That's it! Our simple application written! And for those who used the codes used in Windows application ( blog above) is successfully moved to Android.
For those who are interested, you can try:
Download :
Library Intel ® Threading Building Blocks ( open-source version):
The commercial version of Intel ® TBB (functionally different):
software.intel.com / en-us / intel-tbb
English-language and Russian-language blogs about Intel ® TBB
software.intel.com/en-us/tags/17207
software.intel.com/en-us/tags/17220
And, of course, our forum
software.intel.com / en-us / forums / intel-threading-building-blocks
