Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple noob example for AOT android #8508

Closed
ko-work opened this issue Apr 11, 2024 · 2 comments
Closed

Simple noob example for AOT android #8508

ko-work opened this issue Apr 11, 2024 · 2 comments
Labels
question Question on using Taichi

Comments

@ko-work
Copy link

ko-work commented Apr 11, 2024

Hi! Thanks for your work.

I would like to use Taichi with android. Based on the tests I am trying to create a very simple initial example, but I am getting all zeros.
https://github.com/taichi-dev/taichi/blob/52b24f3e09c093610b1ecf69b5e33cbc66b7bd6d/tests/cpp/aot/python_scripts/shared_array_aot_test_.py
and
https://github.com/taichi-dev/taichi/blob/52b24f3e09c093610b1ecf69b5e33cbc66b7bd6d/c_api/tests/c_api_aot_test.cpp

ubuntu 22.04
ndk version 25.1.8937393
taichi commit 52b24f3

I have built android taichi with the scripts/build-taichi-android.sh (-DTI_WITH_VULKAN=ON).

Here is the code:

app.py

import numpy as np
import taichi as ti

ti.init(arch=ti.vulkan)
v_arr = np.zeros(100).astype(np.float32)

@ti.kernel
def update_values(v: ti.types.ndarray(ndim=1)):
    for i in range(100):
        v[i] = 15

m = ti.aot.Module()
m.add_kernel(update_values, template_args={"v": v_arr})
m.archive("update_values.tcm")

app.cpp

#include <taichi/cpp/taichi.hpp>

int main(int argc, const char** argv) {
  uint32_t N = 100;
  ti::Runtime runtime(TI_ARCH_VULKAN);
  ti::AotModule aot_mod = runtime.load_aot_module("update_values.tcm");
  ti::Kernel k_run = aot_mod.get_kernel("run");

  ti::NdArray<float> v_array =
      runtime.allocate_ndarray<float>({N}, {}, true);
    
  k_run.push_arg(v_array);
  k_run.launch();
  runtime.wait();

  float *res = reinterpret_cast<float *>(v_array.map());
  for (int i = 0; i < N; ++i) {
    std::cout << res[i];
  }
  v_array.unmap();

  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.17)

set(APP_NAME update_val)
project(${APP_NAME} LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(${APP_NAME} app.cpp)

include_directories(${PROJECT_SOURCE_DIR}/third_party/taichi/include)
add_library(taichi SHARED IMPORTED)
set_target_properties(taichi PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/third_party/taichi/lib/libtaichi_c_api.so)

target_link_libraries(${APP_NAME} taichi)

the command:

  • creating module archive: python app.py
  • configure: cmake -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_TOOLCHAIN_FILE=$NDK_TOOLCHAIN_FILE \ -DANDROID_PLATFORM=33 \ -DANDROID_ABI=$ANDROID_ABI \ -Bbuild \ -S.
  • build: cmake --build build -j 4
  • upload and run:
DEVICE_PATH=/data/local/tmp/taichi2
DEVICE_ENV="LD_LIBRARY_PATH=${DEVICE_PATH}:/vendor/lib64"
adb shell mkdir -p ${DEVICE_PATH}
adb push libtaichi_c_api.so ${DEVICE_PATH}
adb push update_val ${DEVICE_PATH}
adb shell ${DEVICE_ENV} ./${DEVICE_PATH}/update_val

the log of running python and cmake:

[Taichi] version 1.7.0, llvm 15.0.4, commit 2fd24490, linux, python 3.10.12
[Taichi] Starting on arch=x64
-- The C compiler identification is Clang 14.0.6
-- The CXX compiler identification is Clang 14.0.6
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /path/to/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /path/to/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /path/to/taichi_example/test_aot_issue/build
[ 50%] Building CXX object CMakeFiles/update_val.dir/app.cpp.o
[100%] Linking CXX executable update_val
[100%] Built target update_val

On device in the folder there is the so and the executable.

The output:

LD_LIBRARY_PATH=. ./update_val
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

The result is the same with cpu gles and vulkan (changed in both python and cpp)

@ko-work ko-work added the question Question on using Taichi label Apr 11, 2024
@github-project-automation github-project-automation bot moved this to Untriaged in Taichi Lang Apr 11, 2024
@ko-work
Copy link
Author

ko-work commented Apr 15, 2024

I forgot to upload tcm archive

@ko-work
Copy link
Author

ko-work commented Apr 15, 2024

And I was using inconsistent architectures which caused bad keys when reading the module did not see at first in the log, also module names were wrong.

here is an updated example just in case someone would came across this
test_aot_issue.zip

@ko-work ko-work closed this as completed Apr 15, 2024
@github-project-automation github-project-automation bot moved this from Untriaged to Done in Taichi Lang Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question on using Taichi
Projects
Status: Done
Development

No branches or pull requests

1 participant