Coder Social home page Coder Social logo

rapid-android-demoapp's Introduction

RAPID - Android Demo Application

This is part of the RAPID Project and is an ongoing work. While RAPID envisions to support heterogeneous devices, this is the demonstration of the tasks offloading on Android. This demo uses the RAPID Android Offloading Framework.
For Java and CUDA code offloading on other platforms, have a look at the generic RAPID Linux/Windows Demo Application.

In this page we will guide you on how to:

Intro

RAPID enables automatic computation offloading of heavy tasks on Android and Linux/Windows Java applications. Moreover, RAPID enables the possibility for embedding CUDA code in applications for generic Android devices and for Java Linux/Windows.
RAPID enables highly CPU- or GPU-demanding applications to be offered through physical or virtual devices with lower capabilities or resources than the applications require, potentially backed by remote accelerators of the same class (D2D) or higher classes (hierarchical mode).
RAPID supports its acceleration service through code offloading to more capable devices or devices with more resources, when this is deemed necessary or beneficial.

Terminology

  • User Device (UD): is the low-power device (phone, e.g.) that will be accelerated by code offloading. In our scenario it will be a phone running Android (version 4.1+ is recommended).
  • Acceleration Client (AC): is an Android library that enables code offloading on the Android applications.
  • Application (AP): is the Android application that will be accelerated by the framework. This application includes the AC as a library and uses the AC's API and the RAPID programming model.
  • VM: is a Virtual Machine running on virtualized software, with the same operating system as the UD. In our scenario it will be an Android-x86 instance (version 4.0+ is recommended) running on VirtualBox.
  • Acceleration Server (AS): is an Android application that runs on the VM and is responsible for executing the offloaded code by the client.

Installing and Testing the Demo

Description of the Demo App

The demo application shows three representative use case offloading scenarios:

  • Android Java method offloading.

    This is the simplest case of computation offloading, dealing with remote execution of Java methods. We have selected the N-Queens puzzle as a representative for this use case. The N-Queens puzzle is the task of arranging N chess queens in the chess keyboard so that no two queens can attack each other. The current implementation is a brute force algorithm. The user can vary the number of queens from 4 to 8, changing this way the difficulty of the problem and its duration. When pressing the button Solve Nqueens, the computation will be performed via the RAPID AC locally on the device or remotely on the VM. Cumulative statistics in terms of number of local/remote executions and average duration of local/remote executions will be shown to the user in real time. The expected result is that while increasing the number of queens, the gap between the local and remote execution should increase, with the remote executions being faster for bigger number of queens.

  • Android C/C++ native function offloading.

    Android allows developers to include native C/C++ code in their applications for increasing the performance of intensive tasks or for allowing code reusability. A normal Java method can call a native function thanks to the Java Native Interface (JNI). To show that RAPID supports offloading of native functions, we have included in the demo a simple application that simply returns the string "Hello from JNI" implemented in C++ and included as a native library in the demo application. Also in this case, the user can see cumulative statistics in terms of number and duration of local/remote execution. The expected result here is that the local execution will always be faster than the remote one, given that the native function is not computationally intensive, meaning that the remote execution is penalized by the data transmission. However, this is just a simple demo serving as a starting point for building applications that include offloadable native functions.

  • Android CUDA programming and Android CUDA offloading.

    The third showcase is the most complex one, including CUDA code offloading. The demo application in this case is a matrix multiplication performed using CUDA. Notice that CUDA development is not possible for the majority of Android devices. As such, the developer:

    • Implements her CUDA code in a development machine as if it were for another supported operating system, e.g. Linux, and generates the Parallel Thread Execution (PTX) file using the NVIDIA CUDA Compiler (nvcc).
    • Then, the PTX file has to be embedded in the assets/cuda-kernels folder of the Android application, where the RAPID framework will look for loading the file during runtime.

    When the execution of the method containing CUDA calls is performed locally, if the client device does not have a GPU, RAPID will offload the CUDA calls from the client device to RAPID AS, which will take care of running them on the physical GPU of the machine where it is deployed (i.e. the RAPID cloud). When the execution of the method containing the CUDA calls is performed remotely, because it is offloaded by the RAPID AC, the CUDA calls will be executed by RAPID on the remote GPU.

Installing

The demo shows how portion of the application's code can be run locally on the device or can be offloaded on a remote VM. Installation steps:

  1. Clone this project in Android Studio.
  2. Install the demo application in an Android device (a phone with Android 4.1+ is recommended).
  3. Install the Android-x86 VM we provide on a computer that is reachable by the phone device (i.e. the phone can ping the VM).
    • Install VirtualBox on the computer.
    • Download the Android-x86-6.0 VirtualBox image of the VM from the RAPID website here. (If you are having problems with this image then you can download the Android-x86-4.4 RAPID image from here)
    • Import the image on VirtualBox.
    • Launch the Android VM you just imported.
      • The VM will automatically start the AS, you don't have to do anything.
      • Get the IP of the VM (one way to do this is to open the Terminal app inside the Android-x86 VM and type busybox ifconfig. If this command doesn't work then try netcfg.).
            * Make sure that the phone device can ping the VM and the VM can ping the phone.
      • If you are experiencing issues with networking, you can read more about VirtualBox networking.
    • Notice: In the final release of the RAPID architecture we will provide VMs running on the RAPID cloud, meaning that you will not have to deal with these steps yourself.
  4. On the phone, select the radio button Direct connection to VM and write the IP of the VM on the text box that will open (see the first figure below).
  5. Press Start and wait until the app connects with the AS running on the VM.
    • A green text will notify that the connection with the VM was successful.
    • A red text will notify that the connection with the VM was not successful.
  6. You will be presented with an Android activity showing the three demo apps.
  7. You can select the execution location of the tasks using the radio buttons:
    • Always Local will instruct the framework to always execute the tasks locally on the device (phone).
    • Always Remote will instruct the framework to always execute the tasks remotely on the VM.
    • Energy and Delay will instruct the framework to make dynamic decisions and choose the execution location (local or remote) so that to minimize the energy and execution time of each task.
  8. The second figure below shows the N-Queens puzzle being executed locally on the device.
  9. The third figure shows the statistics of running the N-Queens puzzle in the device and remotely on the VM.
    • You can see that running it remotely is almost 10 times faster.

Developing Android Applications with RAPID Offloading Support

Normal Android/Java Method Offloading

Let's start with a simple Android/Java class Factorial.java that implements a heavy method long factorial(int n), which takes in a number n and returns the factorial of number n!:

public class Factorial {
    public long factorial(int n) { 
        long result = 1;
        for (int i = 2; i <= n; i++) {
            result *= i;
        }
        return result;
    }
}

To make this class RAPID offloadable, we simply need to perform the following steps:

  • Include the RAPID libraries in the build.gradle file (use the latest version releases):
    dependencies {
        compile 'eu.project.rapid:rapid-android-ac:0.0.9'
        compile 'eu.project.rapid:rapid-gvirtus4a:0.0.2'
    }
  • Next, modify the source code in the following way:
    • Make the Factorial class extend Remoteable, which is a RAPID abstract class.
      • The Remoteable class implements the Serializable Java interface, which means that the Factorial class should also be serializable.
      • Moreover, the abstract method copyState() should be implemented. Leave it empty for the moment.
    • Add a @Remote Java annotation to the factorial method to indicate that this method should be considered for offloading (we can also add QoS annotations for more fine-grained quality of service controls but let's keep this example simple and ignore it).
    • Declare an instance variable transient DFE controller; and initialize it on the constructor with a DFE object that will be passed to the class from the main class.
      import eu.project.rapid.ac.DFE;
      import eu.project.rapid.ac.Remoteable;
      
      public class Factorial extends Remoteable {
      
          private transient DFE controller;
      
          public Factorial(DFE controller) {
              this.controller = controller;
          }
      
          @Remote
          public long factorial(int n) { 
              long result = 1;
              for (int i = 2; i <= n; i++) {
                  result *= i;
              }
              return result;
          }
      
          @Override
          public void copyState(Remoteable state) {
          }
      }
    • Next, download the RAPID source-to-source compiler to convert the modified class on RAPID-offloadable class.
      • Download the compiler as an executable jar file from the RAPID website here.
      • Run the compiler:
        java -jar rapid-acceleration-compiler.jar [<PATH_TO_THE_PROJECT> | <PATH_TO_THE_FILE>]
        • If the command line argument is a folder, all Java files inside the folder will be processed.
        • If the command line argument is a Java file, only that file will be processed.
        • The Factorial.java will be modified as shown below:
          import eu.project.rapid.ac.DFE;
          import eu.project.rapid.ac.Remoteable;
          
          public class Factorial extends Remoteable {
          
              private transient DFE controller;
          
              public Factorial(DFE controller) {
                  this.controller = controller;
              }
          
              @Remote
              public long localfactorial(int n) {
                  long result = 1;
                  for (int i = 2; i <= n; i++) {
                      result *= i;
                  }
                  return result;
              }
          
              @Override
              public void copyState(Remoteable state) {
              }
          
              public  long factorial (int n) {
                 Method toExecute;
                 Class<?>[] paramTypes = {int.class};
                 Object[] paramValues = { n};
                 long result = null;
                 try {
                     toExecute = this.getClass().getDeclaredMethod("localfactorial", paramTypes);
                     result = (long) controller.execute(toExecute, paramValues, this);
                 } catch (SecurityException e) {
                     // Should never get here
                     e.printStackTrace();
                     throw e;
                 } catch (NoSuchMethodException e) {
                     // Should never get here
                     e.printStackTrace();
                 } catch (Throwable e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
                 return result;
             }
          }
  • Create a DFE dfe; object in the main activity of your app (have a look at the DemoActivity.java file in this repo).
    • The DFE class is implemented as Java Singleton and exposes two static public methods for getting an instance:
      • The first allows you to specify the IP of the VM where the AS is running.
      • The second performs a registration with the RAPID architecture and allocates a VM on the RAPID cloud (currently works only for RAPID internal testing).
    • Pass the DFE object to the constructor of the Factorial when you create the new object.
    • Call the dfe.onDestroy() method in the onDestroy() method of your main activity.
  • That's it, now when you run your application, the method factorial() will be executed via the DFE locally on the device or remotely on the VM.

Native C/C++ Android Code Offloading

CUDA Android Support and CUDA Code Offloading

rapid-android-demoapp's People

Contributors

raffmont avatar rapidproject avatar sokol-kosta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rapid-android-demoapp's Issues

busybox ifconfig always returns localhost

While trying to get the IP address of the VM using 'busybox ifconfig' as mentioned in the guide it always gives me the local address (127.0.0.1) and not the IP of the machine. Tried other commands as well, same problem.
I'm using Oracle's VirtualBox v5.1.22 on Windows 10.

Rapid project

Hi;
I am trying to install the rapid project.
First, I did clone the rapid project and the rapid demo android application in Android Studio.
Then, I connect my Android tablet to the Android Studio and install the demo application on the Table.
After that, I imported the Andriod-X86 on a VMBox installed on my desktop.
Now, when I try to connect my Tablet to the VM using internet setting as NAT it fields and My tablet cannot ping the VM as both IP addresses have different range.
When I change the network setting to the network bridge, Both the VM and the Tablet have same IP range and they can ping each other but when I open my android tablet and lunch the rapid application and put the VM IP address, it is not connected and always shows the red light and says not Connected to VM.
I've notice one thing and it might be the case, in the installation guide, it says when you lunch the VM image the AS will start automatically? it is not starting? So is that the case? and I did not understand how it will work automatically, is it because you embedded the AS in the Android image since it is recommended to download from rapid?
could you please advise on this issue?
Your kind respond is highly appreciated
Ali

After importing, getting error of External Native Build Issues

Build command failed.  
Error while executing process D:\Android\cmake\3.6.4111459\bin\cmake.exe with arguments {-HD:\AndroidStudioProjects\rapid-android-master\rapid-gvirtus4a -BD:\AndroidStudioProjects\rapid-android-master\rapid-gvirtus4a.externalNativeBuild\cmake\release\armeabi -GAndroid Gradle - Ninja -DANDROID_ABI=armeabi -DANDROID_NDK=D:\Android\ndk-bundle -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=D:\AndroidStudioProjects\rapid-android-master\rapid-gvirtus4a\build\intermediates\cmake\release\obj\armeabi -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=D:\Android\cmake\3.6.4111459\bin\ninja.exe -DCMAKE_TOOLCHAIN_FILE=D:\Android\ndk-bundle\build\cmake\android.toolchain.cmake -DANDROID_PLATFORM=android-19}  
D:/AndroidStudioProjects/rapid-android-DemoApp-master  
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage  
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage  
-- Configuring incomplete, errors occurred!  
Build command failed.  
Error while executing process D:\Android\cmake\3.6.4111459\bin\cmake.exe with arguments {-HD:\AndroidStudioProjects\rapid-android-master\rapid-gvirtus4a -BD:\AndroidStudioProjects\rapid-android-master\rapid-gvirtus4a.externalNativeBuild\cmake\debug\armeabi -GAndroid Gradle - Ninja -DANDROID_ABI=armeabi -DANDROID_NDK=D:\Android\ndk-bundle -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=D:\AndroidStudioProjects\rapid-android-master\rapid-gvirtus4a\build\intermediates\cmake\debug\obj\armeabi -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=D:\Android\cmake\3.6.4111459\bin\ninja.exe -DCMAKE_TOOLCHAIN_FILE=D:\Android\ndk-bundle\build\cmake\android.toolchain.cmake -DANDROID_PLATFORM=android-19}  
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage  
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage  
-- Configuring incomplete, errors occurred!  
Build command failed.  
Error while executing process D:\Android\ndk-bundle\ndk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=D:\AndroidStudioProjects\rapid-android-DemoApp-master\rapidDemoApp\src\main\jni\Android.mk NDK_APPLICATION_MK=D:\AndroidStudioProjects\rapid-android-DemoApp-master\rapidDemoApp\src\main\jni\Application.mk APP_ABI=armeabi NDK_ALL_ABIS=armeabi NDK_DEBUG=1 APP_PLATFORM=android-19 NDK_OUT=D:/AndroidStudioProjects/rapid-android-DemoApp-master/rapidDemoApp/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT=D:\AndroidStudioProjects\rapid-android-DemoApp-master\rapidDemoApp\build\intermediates\ndkBuild\debug\lib APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n}  
Android NDK: INTERNAL ERROR: The armeabi ABI should have exactly one architecture definitions. Found: ''  
process_begin: CreateProcess(NULL, "", ...) failed.  
Build command failed.  
Error while executing process D:\Android\ndk-bundle\ndk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=D:\AndroidStudioProjects\rapid-android-DemoApp-master\rapidDemoApp\src\main\jni\Android.mk NDK_APPLICATION_MK=D:\AndroidStudioProjects\rapid-android-DemoApp-master\rapidDemoApp\src\main\jni\Application.mk APP_ABI=armeabi NDK_ALL_ABIS=armeabi NDK_DEBUG=0 APP_PLATFORM=android-19 NDK_OUT=D:/AndroidStudioProjects/rapid-android-DemoApp-master/rapidDemoApp/build/intermediates/ndkBuild/release/obj NDK_LIBS_OUT=D:\AndroidStudioProjects\rapid-android-DemoApp-master\rapidDemoApp\build\intermediates\ndkBuild\release\lib APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n}  
Android NDK: INTERNAL ERROR: The armeabi ABI should have exactly one architecture definitions. Found: ''  
process_begin: CreateProcess(NULL, "", ...) failed.  

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.