From 0d7d2d7e2ca82a3beb6ff9f8b46b15ad625e5608 Mon Sep 17 00:00:00 2001 From: jim19930609 Date: Tue, 12 Jul 2022 18:29:01 +0800 Subject: [PATCH 01/12] [build] Enabled C-API compilation on CI --- .github/workflows/scripts/unix_build.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/scripts/unix_build.sh b/.github/workflows/scripts/unix_build.sh index 0ae68786eac7d..4aab79780ced7 100755 --- a/.github/workflows/scripts/unix_build.sh +++ b/.github/workflows/scripts/unix_build.sh @@ -65,7 +65,9 @@ build_taichi_wheel() { fi fi python3 misc/make_changelog.py origin/master ./ True - python3 setup.py $PROJECT_TAGS bdist_wheel $EXTRA_ARGS + + CMAKE_ARGS="TAICHI_CMAKE_ARGS=\"-DTI_WITH_C_API=ON\"" + $CMAKE_ARGS python3 setup.py $PROJECT_TAGS bdist_wheel $EXTRA_ARGS sccache -s } From 6e03f9c4d0a9e83502e197be9f8c7601fdfc7468 Mon Sep 17 00:00:00 2001 From: jim19930609 Date: Wed, 13 Jul 2022 11:14:40 +0800 Subject: [PATCH 02/12] Fixed minor issue --- .github/workflows/scripts/unix_build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/scripts/unix_build.sh b/.github/workflows/scripts/unix_build.sh index 4aab79780ced7..cf9a2d8feb4ae 100755 --- a/.github/workflows/scripts/unix_build.sh +++ b/.github/workflows/scripts/unix_build.sh @@ -67,7 +67,7 @@ build_taichi_wheel() { python3 misc/make_changelog.py origin/master ./ True CMAKE_ARGS="TAICHI_CMAKE_ARGS=\"-DTI_WITH_C_API=ON\"" - $CMAKE_ARGS python3 setup.py $PROJECT_TAGS bdist_wheel $EXTRA_ARGS + env $CMAKE_ARGS python3 setup.py $PROJECT_TAGS bdist_wheel $EXTRA_ARGS sccache -s } From d890dee16c881baf5f4457bd3aa4cad4adccc2aa Mon Sep 17 00:00:00 2001 From: jim19930609 Date: Wed, 13 Jul 2022 11:19:10 +0800 Subject: [PATCH 03/12] Enabled CAPI compilation on windows build bots --- .github/workflows/scripts/win_build.ps1 | 1 + .github/workflows/scripts/win_build_test_cpu.ps1 | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/scripts/win_build.ps1 b/.github/workflows/scripts/win_build.ps1 index edb5cd4797a9e..a5dd202903a62 100644 --- a/.github/workflows/scripts/win_build.ps1 +++ b/.github/workflows/scripts/win_build.ps1 @@ -71,6 +71,7 @@ if ($installVulkan) { $env:TAICHI_CMAKE_ARGS += " -DTI_WITH_VULKAN:BOOL=ON" } +$env:TAICHI_CMAKE_ARGS += " -DTI_WITH_C_API:BOOL=ON" $env:TAICHI_CMAKE_ARGS += " -DTI_WITH_DX11:BOOL=ON" Pop-Location diff --git a/.github/workflows/scripts/win_build_test_cpu.ps1 b/.github/workflows/scripts/win_build_test_cpu.ps1 index 9d482fd31bd4b..36b35cfd0d392 100644 --- a/.github/workflows/scripts/win_build_test_cpu.ps1 +++ b/.github/workflows/scripts/win_build_test_cpu.ps1 @@ -38,7 +38,7 @@ WriteInfo("Setting the env vars") $env:LLVM_DIR = "C://taichi_llvm" #TODO enable build test -$env:TAICHI_CMAKE_ARGS = "-DTI_WITH_OPENGL:BOOL=OFF -DTI_WITH_CC:BOOL=OFF -DTI_WITH_VULKAN:BOOL=OFF -DTI_WITH_CUDA:BOOL=OFF -DTI_BUILD_TESTS:BOOL=OFF" +$env:TAICHI_CMAKE_ARGS = "-DTI_WITH_C_API:BOOL=ON -DTI_WITH_OPENGL:BOOL=OFF -DTI_WITH_CC:BOOL=OFF -DTI_WITH_VULKAN:BOOL=OFF -DTI_WITH_CUDA:BOOL=OFF -DTI_BUILD_TESTS:BOOL=OFF" #TODO: For now we need to hard code the compiler path from build tools 2019 $env:TAICHI_CMAKE_ARGS +=' -DCMAKE_CXX_COMPILER=C:/Program\ Files\ (x86)/Microsoft\ Visual\ Studio/2019/BuildTools/vc/Tools/Llvm/x64/bin/clang++.exe -DCMAKE_C_COMPILER=C:/Program\ Files\ (x86)/Microsoft\ Visual\ Studio/2019/BuildTools/vc/Tools/Llvm/x64/bin/clang.exe' From 630b4bad683988ec6a327aad11c5a4af856a4107 Mon Sep 17 00:00:00 2001 From: jim19930609 Date: Wed, 13 Jul 2022 13:28:52 +0800 Subject: [PATCH 04/12] Fixed minor issue --- .github/workflows/scripts/unix_build.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/scripts/unix_build.sh b/.github/workflows/scripts/unix_build.sh index cf9a2d8feb4ae..e9737c58506a3 100755 --- a/.github/workflows/scripts/unix_build.sh +++ b/.github/workflows/scripts/unix_build.sh @@ -66,8 +66,8 @@ build_taichi_wheel() { fi python3 misc/make_changelog.py origin/master ./ True - CMAKE_ARGS="TAICHI_CMAKE_ARGS=\"-DTI_WITH_C_API=ON\"" - env $CMAKE_ARGS python3 setup.py $PROJECT_TAGS bdist_wheel $EXTRA_ARGS + TAICHI_CMAKE_ARGS="${TAICHI_CMAKE_ARGS} -DTI_WITH_C_API=ON" + exec env TAICHI_CMAKE_ARGS="${TAICHI_CMAKE_ARGS}" python3 setup.py $PROJECT_TAGS bdist_wheel $EXTRA_ARGS sccache -s } From 2b51bc31ad34fecafcc98fc99efd2632ff088b91 Mon Sep 17 00:00:00 2001 From: jim19930609 Date: Wed, 13 Jul 2022 15:01:19 +0800 Subject: [PATCH 05/12] Removed LLVM-15 test on windows --- .github/workflows/scripts/win_build_test_cpu.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/scripts/win_build_test_cpu.ps1 b/.github/workflows/scripts/win_build_test_cpu.ps1 index 38ccdc6abe1d9..6fdb04f72064c 100644 --- a/.github/workflows/scripts/win_build_test_cpu.ps1 +++ b/.github/workflows/scripts/win_build_test_cpu.ps1 @@ -55,7 +55,7 @@ if (!$llvmVer.CompareTo("10")) { } #TODO enable build test -$env:TAICHI_CMAKE_ARGS = "-DTI_WITH_C_API:BOOL=ON -DTI_WITH_OPENGL:BOOL=OFF -DTI_WITH_CC:BOOL=OFF -DTI_WITH_VULKAN:BOOL=OFF -DTI_WITH_CUDA:BOOL=OFF -DTI_BUILD_TESTS:BOOL=OFF" +$env:TAICHI_CMAKE_ARGS = "-DTI_WITH_OPENGL:BOOL=OFF -DTI_WITH_CC:BOOL=OFF -DTI_WITH_VULKAN:BOOL=OFF -DTI_WITH_CUDA:BOOL=OFF -DTI_BUILD_TESTS:BOOL=OFF" #TODO: For now we need to hard code the compiler path from build tools 2019 $env:TAICHI_CMAKE_ARGS +=' -DCMAKE_CXX_COMPILER=C:/Program\ Files\ (x86)/Microsoft\ Visual\ Studio/2019/BuildTools/vc/Tools/Llvm/x64/bin/clang++.exe -DCMAKE_C_COMPILER=C:/Program\ Files\ (x86)/Microsoft\ Visual\ Studio/2019/BuildTools/vc/Tools/Llvm/x64/bin/clang.exe' From ef9519fcf09ad0923a3224d7a0dc027cf66e5f03 Mon Sep 17 00:00:00 2001 From: jim19930609 Date: Thu, 14 Jul 2022 10:47:49 +0800 Subject: [PATCH 06/12] [llvm] [aot] Add LLVM to CAPI part 4: Enabled C-API tests on CI & Add basic C-API interface test cases --- CMakeLists.txt | 3 +++ c_api/src/taichi_core_impl.cpp | 16 +++++++++---- c_api/src/taichi_llvm_impl.cpp | 2 +- cmake/TaichiCAPITests.cmake | 35 +++++++++++++++++++++++++++ tests/run_tests.py | 43 ++++++++++++++++++++-------------- tests/test_utils.py | 7 ++++++ 6 files changed, 83 insertions(+), 23 deletions(-) create mode 100644 cmake/TaichiCAPITests.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 69d5ed5d6b845..401c903c1b624 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -189,4 +189,7 @@ option(TI_WITH_C_API "build taichi runtime c-api library" OFF) if (TI_WITH_C_API) include(cmake/TaichiCAPI.cmake) + if (TI_BUILD_TESTS) + include(cmake/TaichiCAPITests.cmake) + endif() endif() diff --git a/c_api/src/taichi_core_impl.cpp b/c_api/src/taichi_core_impl.cpp index eebfea2422ba3..fd7182f696dd6 100644 --- a/c_api/src/taichi_core_impl.cpp +++ b/c_api/src/taichi_core_impl.cpp @@ -2,6 +2,7 @@ #include "taichi_vulkan_impl.h" #include "taichi_llvm_impl.h" #include "taichi/program/ndarray.h" +#include Runtime::Runtime(taichi::Arch arch) : arch(arch) { } @@ -51,23 +52,28 @@ Runtime &AotModule::runtime() { TiRuntime ti_create_runtime(TiArch arch) { switch (arch) { #ifdef TI_WITH_VULKAN - case TI_ARCH_VULKAN: + case TI_ARCH_VULKAN: { return (TiRuntime)(static_cast(new VulkanRuntimeOwned)); + } #endif // TI_WITH_VULKAN #ifdef TI_WITH_LLVM - case TI_ARCH_X64: + case TI_ARCH_X64: { return (TiRuntime)(static_cast( new capi::LlvmRuntime(taichi::Arch::x64))); - case TI_ARCH_ARM64: + } + case TI_ARCH_ARM64: { return (TiRuntime)(static_cast( new capi::LlvmRuntime(taichi::Arch::arm64))); - case TI_ARCH_CUDA: + } + case TI_ARCH_CUDA: { return (TiRuntime)(static_cast( new capi::LlvmRuntime(taichi::Arch::cuda))); + } #endif // TI_WITH_LLVM - default: + default: { TI_WARN("ignored attempt to create runtime on unknown arch"); return TI_NULL_HANDLE; + } } return TI_NULL_HANDLE; } diff --git a/c_api/src/taichi_llvm_impl.cpp b/c_api/src/taichi_llvm_impl.cpp index 86496a7588ae3..3bca5950065bb 100644 --- a/c_api/src/taichi_llvm_impl.cpp +++ b/c_api/src/taichi_llvm_impl.cpp @@ -83,7 +83,7 @@ void LlvmRuntime::submit() { } void LlvmRuntime::wait() { - executor_->sychronize(); + executor_->synchronize(); } } // namespace capi diff --git a/cmake/TaichiCAPITests.cmake b/cmake/TaichiCAPITests.cmake new file mode 100644 index 0000000000000..7c94deeef10fd --- /dev/null +++ b/cmake/TaichiCAPITests.cmake @@ -0,0 +1,35 @@ +cmake_minimum_required(VERSION 3.0) + +set(C_API_TESTS_NAME taichi_c_api_tests) +if (WIN32) + # Prevent overriding the parent project's compiler/linker + # settings on Windows + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +endif() + +# TODO(#2195): +# 1. "cpp" -> "cpp_legacy", "cpp_new" -> "cpp" +# 2. Re-implement the legacy CPP tests using googletest +file(GLOB_RECURSE TAICHI_C_API_TESTS_SOURCE + "c_api/tests/*.cpp") + +add_executable(${C_API_TESTS_NAME} ${TAICHI_C_API_TESTS_SOURCE}) +if (WIN32) + # Output the executable to bin/ instead of build/Debug/... + set(TESTS_OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/bin") + set_target_properties(${C_API_TESTS_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${TESTS_OUTPUT_DIR}) + set_target_properties(${C_API_TESTS_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${TESTS_OUTPUT_DIR}) + set_target_properties(${C_API_TESTS_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${TESTS_OUTPUT_DIR}) + set_target_properties(${C_API_TESTS_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${TESTS_OUTPUT_DIR}) + set_target_properties(${C_API_TESTS_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${TESTS_OUTPUT_DIR}) +endif() +target_link_libraries(${C_API_TESTS_NAME} PRIVATE taichi_c_api) +target_link_libraries(${C_API_TESTS_NAME} PRIVATE gtest_main) + +target_include_directories(${C_API_TESTS_NAME} + PRIVATE + ${PROJECT_SOURCE_DIR} + ${PROJECT_SOURCE_DIR}/c_api/include + ) + +add_test(NAME ${C_API_TESTS_NAME} COMMAND ${C_API_TESTS_NAME}) diff --git a/tests/run_tests.py b/tests/run_tests.py index 52171475fe838..3466d30acf5ec 100644 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -7,25 +7,17 @@ import tempfile import warnings -from test_utils import __aot_test_cases, print_aot_test_guide +from test_utils import (__aot_test_cases, __capi_aot_test_cases, + print_aot_test_guide) import taichi as ti -def _run_cpp_test(gtest_option="", extra_env=None): +def _run_cpp_test(test_filename, build_dir, gtest_option="", extra_env=None): ti.reset() print("Running C++ tests...") ti_lib_dir = os.path.join(ti.__path__[0], '_lib', 'runtime') - - curr_dir = os.path.dirname(os.path.abspath(__file__)) - if platform.system() == "Windows": - cpp_test_filename = 'taichi_cpp_tests.exe' - build_dir = os.path.join(curr_dir, '../bin') - else: - cpp_test_filename = 'taichi_cpp_tests' - build_dir = os.path.join(curr_dir, '../build') - - fullpath = os.path.join(build_dir, cpp_test_filename) + fullpath = os.path.join(build_dir, test_filename) if os.path.exists(fullpath): env_copy = os.environ.copy() @@ -38,9 +30,9 @@ def _run_cpp_test(gtest_option="", extra_env=None): subprocess.check_call(cmd, env=env_copy, cwd=build_dir) -def _test_cpp_aot(): +def _test_cpp_aot(test_filename, build_dir, test_info): tests_visited = [] - for cpp_test_name, (python_rpath, args) in __aot_test_cases.items(): + for cpp_test_name, (python_rpath, args) in test_info.items(): # Temporary folder will be removed upon handle destruction temp_handle = tempfile.TemporaryDirectory() temp_folderpath = temp_handle.name @@ -59,7 +51,8 @@ def _test_cpp_aot(): env=env_copy) # Run AOT C++ codes - _run_cpp_test(f"--gtest_filter={cpp_test_name}", extra_env) + _run_cpp_test(test_filename, build_dir, + f"--gtest_filter={cpp_test_name}", extra_env) tests_visited.append(cpp_test_name) exclude_tests_cmd = "--gtest_filter=-" + ":".join(tests_visited) @@ -67,11 +60,27 @@ def _test_cpp_aot(): def _test_cpp(): + curr_dir = os.path.dirname(os.path.abspath(__file__)) + if platform.system() == "Windows": + cpp_test_filename = 'taichi_cpp_tests.exe' + capi_test_filename = 'taichi_c_api_tests.exe' + build_dir = os.path.join(curr_dir, '../bin') + else: + cpp_test_filename = 'taichi_cpp_tests' + capi_test_filename = 'taichi_c_api_tests' + build_dir = os.path.join(curr_dir, '../build') + + # Run C-API test cases + exclude_tests_cmd = _test_cpp_aot(capi_test_filename, build_dir, + __capi_aot_test_cases) + _run_cpp_test(capi_test_filename, build_dir, exclude_tests_cmd) + # Run AOT test cases - exclude_tests_cmd = _test_cpp_aot() + exclude_tests_cmd = _test_cpp_aot(cpp_test_filename, build_dir, + __aot_test_cases) # Run rest of the cpp tests - _run_cpp_test(exclude_tests_cmd) + _run_cpp_test(cpp_test_filename, build_dir, exclude_tests_cmd) def _test_python(args): diff --git a/tests/test_utils.py b/tests/test_utils.py index 9e62a9b0f3566..e79b6748349da 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -33,6 +33,13 @@ [os.path.join('cpp', 'aot', 'mpm88_graph_aot.py'), "--arch=vulkan"], } +__capi_aot_test_cases = { + "CapiDryRun.CpuAotModule": + [os.path.join('cpp', 'aot', 'llvm', 'kernel_aot_test.py'), "--arch=cpu"], + "CapiDryRun.CudaAotModule": + [os.path.join('cpp', 'aot', 'llvm', 'kernel_aot_test.py'), "--arch=cuda"], +} + def print_aot_test_guide(): message = f""" From cdb72575aca16436139d8f817516888859c6df1a Mon Sep 17 00:00:00 2001 From: jim19930609 Date: Thu, 14 Jul 2022 10:51:35 +0800 Subject: [PATCH 07/12] Removed debug info --- c_api/src/taichi_core_impl.cpp | 1 - c_api/tests/c_api_interface_test.cpp | 89 ++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 c_api/tests/c_api_interface_test.cpp diff --git a/c_api/src/taichi_core_impl.cpp b/c_api/src/taichi_core_impl.cpp index 30caaadde095c..b4cd9e583d4d8 100644 --- a/c_api/src/taichi_core_impl.cpp +++ b/c_api/src/taichi_core_impl.cpp @@ -2,7 +2,6 @@ #include "taichi_vulkan_impl.h" #include "taichi_llvm_impl.h" #include "taichi/program/ndarray.h" -#include Runtime::Runtime(taichi::Arch arch) : arch(arch) { } diff --git a/c_api/tests/c_api_interface_test.cpp b/c_api/tests/c_api_interface_test.cpp new file mode 100644 index 0000000000000..3998580de6f44 --- /dev/null +++ b/c_api/tests/c_api_interface_test.cpp @@ -0,0 +1,89 @@ +#include "gtest/gtest.h" +#include "taichi/taichi_core.h" + +#include + +TEST(CapiDryRun, Runtime) { + { + // CPU Runtime + TiArch arch = TiArch::TI_ARCH_X64; + TiRuntime runtime = ti_create_runtime(arch); + ti_destroy_runtime(runtime); + } + + { + // CUDA Runtime + TiArch arch = TiArch::TI_ARCH_CUDA; + TiRuntime runtime = ti_create_runtime(arch); + ti_destroy_runtime(runtime); + } +} + +TEST(CapiDryRun, MemoryAllocation) { + TiMemoryAllocateInfo alloc_info; + alloc_info.size = 100; + alloc_info.host_write = false; + alloc_info.host_read = false; + alloc_info.export_sharing = false; + alloc_info.usage = TiMemoryUsageFlagBits::TI_MEMORY_USAGE_STORAGE_BIT; + + { + // CPU Runtime + TiArch arch = TiArch::TI_ARCH_X64; + TiRuntime runtime = ti_create_runtime(arch); + + ti_allocate_memory(runtime, &alloc_info); + + // Unfortunately, memory deallocation for + // CPU backend has not been implemented yet... + + ti_destroy_runtime(runtime); + } + + { + // CUDA Runtime + TiArch arch = TiArch::TI_ARCH_CUDA; + TiRuntime runtime = ti_create_runtime(arch); + + TiMemory memory = ti_allocate_memory(runtime, &alloc_info); + ti_free_memory(runtime, memory); + + ti_destroy_runtime(runtime); + } +} + +TEST(CapiDryRun, CpuAotModule) { + const auto folder_dir = getenv("TAICHI_AOT_FOLDER_PATH"); + + std::stringstream aot_mod_ss; + aot_mod_ss << folder_dir; + + { + // CPU Runtime + TiArch arch = TiArch::TI_ARCH_X64; + TiRuntime runtime = ti_create_runtime(arch); + + TiAotModule aot_mod = ti_load_aot_module(runtime, aot_mod_ss.str().c_str()); + ti_destroy_aot_module(aot_mod); + + ti_destroy_runtime(runtime); + } +} + +TEST(CapiDryRun, CudaAotModule) { + const auto folder_dir = getenv("TAICHI_AOT_FOLDER_PATH"); + + std::stringstream aot_mod_ss; + aot_mod_ss << folder_dir; + + { + // CUDA Runtime + TiArch arch = TiArch::TI_ARCH_CUDA; + TiRuntime runtime = ti_create_runtime(arch); + + TiAotModule aot_mod = ti_load_aot_module(runtime, aot_mod_ss.str().c_str()); + ti_destroy_aot_module(aot_mod); + + ti_destroy_runtime(runtime); + } +} From f08033fe135be4f38613c4116640a16f421b4bb3 Mon Sep 17 00:00:00 2001 From: jim19930609 Date: Thu, 14 Jul 2022 16:02:09 +0800 Subject: [PATCH 08/12] Fixed CI issues --- c_api/src/taichi_core_impl.cpp | 6 +++- c_api/src/taichi_core_impl.h | 2 ++ c_api/src/taichi_llvm_impl.cpp | 19 ++++++++++++ c_api/src/taichi_llvm_impl.h | 1 + c_api/tests/c_api_interface_test.cpp | 32 ++++++++++----------- tests/cpp/aot/llvm/field_aot_test.cpp | 7 ++++- tests/cpp/aot/llvm/graph_aot_test.cpp | 7 ++++- tests/cpp/aot/llvm/kernel_aot_test.cpp | 5 ++++ tests/cpp/aot/llvm/mpm88_graph_aot_test.cpp | 7 ++++- 9 files changed, 66 insertions(+), 20 deletions(-) diff --git a/c_api/src/taichi_core_impl.cpp b/c_api/src/taichi_core_impl.cpp index b4cd9e583d4d8..d8064d3290ed6 100644 --- a/c_api/src/taichi_core_impl.cpp +++ b/c_api/src/taichi_core_impl.cpp @@ -23,6 +23,10 @@ taichi::lang::DeviceAllocation Runtime::allocate_memory( return devalloc; } +void Runtime::deallocate_memory(TiMemory devmem) { + this->get().dealloc_memory(devmem2devalloc(*this, devmem)); +} + AotModule::AotModule(Runtime &runtime, std::unique_ptr &&aot_module) : runtime_(&runtime), aot_module_(std::move(aot_module)) { @@ -128,7 +132,7 @@ void ti_free_memory(TiRuntime runtime, TiMemory devmem) { } Runtime *runtime2 = (Runtime *)runtime; - runtime2->get().dealloc_memory(devmem2devalloc(*runtime2, devmem)); + runtime2->deallocate_memory(devmem); } void *ti_map_memory(TiRuntime runtime, TiMemory devmem) { diff --git a/c_api/src/taichi_core_impl.h b/c_api/src/taichi_core_impl.h index 4bc630a7eb142..da170062f5667 100644 --- a/c_api/src/taichi_core_impl.h +++ b/c_api/src/taichi_core_impl.h @@ -29,6 +29,8 @@ class Runtime { virtual TiAotModule load_aot_module(const char *module_path) = 0; virtual taichi::lang::DeviceAllocation allocate_memory( const taichi::lang::Device::AllocParams ¶ms); + virtual void deallocate_memory(TiMemory devmem); + virtual void buffer_copy(const taichi::lang::DevicePtr &dst, const taichi::lang::DevicePtr &src, size_t size) = 0; diff --git a/c_api/src/taichi_llvm_impl.cpp b/c_api/src/taichi_llvm_impl.cpp index 3bca5950065bb..fc6e71c2cd5f5 100644 --- a/c_api/src/taichi_llvm_impl.cpp +++ b/c_api/src/taichi_llvm_impl.cpp @@ -5,7 +5,10 @@ #include "taichi/runtime/llvm/llvm_runtime_executor.h" #include "taichi/runtime/cpu/aot_module_loader_impl.h" + +#ifdef TI_WITH_CUDA #include "taichi/runtime/cuda/aot_module_loader_impl.h" +#endif namespace capi { @@ -49,6 +52,18 @@ taichi::lang::DeviceAllocation LlvmRuntime::allocate_memory( llvm_runtime, result_buffer}); } +void LlvmRuntime::deallocate_memory(TiMemory devmem) { + taichi::lang::CompileConfig *config = executor_->get_config(); + if (taichi::arch_is_cpu(config->arch)) { + // For memory allocated through Device::allocate_memory_runtime(), + // the corresponding Device::deallocate_memory() interface has not been + // implemented yet... + TI_NOT_IMPLEMENTED; + } + + Runtime::deallocate_memory(devmem); +} + TiAotModule LlvmRuntime::load_aot_module(const char *module_path) { auto *config = executor_->get_config(); std::unique_ptr aot_module{nullptr}; @@ -60,11 +75,15 @@ TiAotModule LlvmRuntime::load_aot_module(const char *module_path) { aot_module = taichi::lang::cpu::make_aot_module(aot_params); } else { +#ifdef TI_WITH_CUDA TI_ASSERT(config->arch == taichi::Arch::cuda); taichi::lang::cuda::AotModuleParams aot_params; aot_params.executor_ = executor_.get(); aot_params.module_path = module_path; aot_module = taichi::lang::cuda::make_aot_module(aot_params); +#else + TI_NOT_IMPLEMENTED; +#endif } // Insert LLVMRuntime to RuntimeContext diff --git a/c_api/src/taichi_llvm_impl.h b/c_api/src/taichi_llvm_impl.h index 7867d613b066d..fbb77cc9c60e3 100644 --- a/c_api/src/taichi_llvm_impl.h +++ b/c_api/src/taichi_llvm_impl.h @@ -23,6 +23,7 @@ class LlvmRuntime : public Runtime { TiAotModule load_aot_module(const char *module_path) override; taichi::lang::DeviceAllocation allocate_memory( const taichi::lang::Device::AllocParams ¶ms) override; + void deallocate_memory(TiMemory devmem) override; void buffer_copy(const taichi::lang::DevicePtr &dst, const taichi::lang::DevicePtr &src, diff --git a/c_api/tests/c_api_interface_test.cpp b/c_api/tests/c_api_interface_test.cpp index 3998580de6f44..e2ac5ad383d21 100644 --- a/c_api/tests/c_api_interface_test.cpp +++ b/c_api/tests/c_api_interface_test.cpp @@ -1,8 +1,6 @@ #include "gtest/gtest.h" #include "taichi/taichi_core.h" -#include - TEST(CapiDryRun, Runtime) { { // CPU Runtime @@ -11,12 +9,12 @@ TEST(CapiDryRun, Runtime) { ti_destroy_runtime(runtime); } - { - // CUDA Runtime - TiArch arch = TiArch::TI_ARCH_CUDA; - TiRuntime runtime = ti_create_runtime(arch); - ti_destroy_runtime(runtime); - } +#ifdef TI_WITH_CUDA + // CUDA Runtime + TiArch arch = TiArch::TI_ARCH_CUDA; + TiRuntime runtime = ti_create_runtime(arch); + ti_destroy_runtime(runtime); +#endif } TEST(CapiDryRun, MemoryAllocation) { @@ -40,16 +38,16 @@ TEST(CapiDryRun, MemoryAllocation) { ti_destroy_runtime(runtime); } - { - // CUDA Runtime - TiArch arch = TiArch::TI_ARCH_CUDA; - TiRuntime runtime = ti_create_runtime(arch); +#ifdef TI_WITH_CUDA + // CUDA Runtime + TiArch arch = TiArch::TI_ARCH_CUDA; + TiRuntime runtime = ti_create_runtime(arch); - TiMemory memory = ti_allocate_memory(runtime, &alloc_info); - ti_free_memory(runtime, memory); + TiMemory memory = ti_allocate_memory(runtime, &alloc_info); + ti_free_memory(runtime, memory); - ti_destroy_runtime(runtime); - } + ti_destroy_runtime(runtime); +#endif } TEST(CapiDryRun, CpuAotModule) { @@ -71,6 +69,7 @@ TEST(CapiDryRun, CpuAotModule) { } TEST(CapiDryRun, CudaAotModule) { +#ifdef TI_WITH_CUDA const auto folder_dir = getenv("TAICHI_AOT_FOLDER_PATH"); std::stringstream aot_mod_ss; @@ -86,4 +85,5 @@ TEST(CapiDryRun, CudaAotModule) { ti_destroy_runtime(runtime); } +#endif } diff --git a/tests/cpp/aot/llvm/field_aot_test.cpp b/tests/cpp/aot/llvm/field_aot_test.cpp index 29124a68dddc7..ed2135f3b6765 100644 --- a/tests/cpp/aot/llvm/field_aot_test.cpp +++ b/tests/cpp/aot/llvm/field_aot_test.cpp @@ -4,10 +4,13 @@ #include "taichi/runtime/llvm/llvm_runtime_executor.h" #include "taichi/system/memory_pool.h" #include "taichi/runtime/cpu/aot_module_loader_impl.h" -#include "taichi/runtime/cuda/aot_module_loader_impl.h" #include "taichi/runtime/llvm/llvm_aot_module_loader.h" + +#ifdef TI_WITH_CUDA +#include "taichi/runtime/cuda/aot_module_loader_impl.h" #include "taichi/rhi/cuda/cuda_driver.h" #include "taichi/platform/cuda/detect_cuda.h" +#endif #define TI_RUNTIME_HOST #include "taichi/program/context.h" @@ -122,6 +125,7 @@ TEST(LlvmAotTest, CpuField) { } TEST(LlvmAotTest, CudaField) { +#ifdef TI_WITH_CUDA if (is_cuda_api_available()) { CompileConfig cfg; cfg.arch = Arch::cuda; @@ -144,6 +148,7 @@ TEST(LlvmAotTest, CudaField) { run_field_tests(mod.get(), &exec, result_buffer); } +#endif } } // namespace lang diff --git a/tests/cpp/aot/llvm/graph_aot_test.cpp b/tests/cpp/aot/llvm/graph_aot_test.cpp index 1fdf217a7434f..28e500c9f5315 100644 --- a/tests/cpp/aot/llvm/graph_aot_test.cpp +++ b/tests/cpp/aot/llvm/graph_aot_test.cpp @@ -4,10 +4,13 @@ #include "taichi/runtime/program_impls/llvm/llvm_program.h" #include "taichi/system/memory_pool.h" #include "taichi/runtime/cpu/aot_module_loader_impl.h" -#include "taichi/runtime/cuda/aot_module_loader_impl.h" #include "taichi/runtime/llvm/llvm_aot_module_loader.h" + +#ifdef TI_WITH_CUDA +#include "taichi/runtime/cuda/aot_module_loader_impl.h" #include "taichi/rhi/cuda/cuda_driver.h" #include "taichi/platform/cuda/detect_cuda.h" +#endif #define TI_RUNTIME_HOST #include "taichi/program/context.h" @@ -70,6 +73,7 @@ TEST(LlvmCGraph, RunGraphCpu) { } TEST(LlvmCGraph, RunGraphCuda) { +#ifdef TI_WITH_CUDA if (is_cuda_api_available()) { CompileConfig cfg; cfg.arch = Arch::cuda; @@ -126,4 +130,5 @@ TEST(LlvmCGraph, RunGraphCuda) { EXPECT_EQ(cpu_data[i], 3 * i + base0 + base1 + base2); } } +#endif } diff --git a/tests/cpp/aot/llvm/kernel_aot_test.cpp b/tests/cpp/aot/llvm/kernel_aot_test.cpp index 93458b7c84613..70c53b13e59e1 100644 --- a/tests/cpp/aot/llvm/kernel_aot_test.cpp +++ b/tests/cpp/aot/llvm/kernel_aot_test.cpp @@ -4,9 +4,12 @@ #include "taichi/runtime/llvm/llvm_runtime_executor.h" #include "taichi/system/memory_pool.h" #include "taichi/runtime/cpu/aot_module_loader_impl.h" + +#ifdef TI_WITH_CUDA #include "taichi/runtime/cuda/aot_module_loader_impl.h" #include "taichi/rhi/cuda/cuda_driver.h" #include "taichi/platform/cuda/detect_cuda.h" +#endif #define TI_RUNTIME_HOST #include "taichi/program/context.h" @@ -56,6 +59,7 @@ TEST(LlvmAotTest, CpuKernel) { } TEST(LlvmAotTest, CudaKernel) { +#ifdef TI_WITH_CUDA if (is_cuda_api_available()) { CompileConfig cfg; cfg.arch = Arch::cuda; @@ -98,6 +102,7 @@ TEST(LlvmAotTest, CudaKernel) { EXPECT_EQ(cpu_data[i], i); } } +#endif } } // namespace lang diff --git a/tests/cpp/aot/llvm/mpm88_graph_aot_test.cpp b/tests/cpp/aot/llvm/mpm88_graph_aot_test.cpp index c9042934ad9d3..afd631a1cf201 100644 --- a/tests/cpp/aot/llvm/mpm88_graph_aot_test.cpp +++ b/tests/cpp/aot/llvm/mpm88_graph_aot_test.cpp @@ -4,10 +4,13 @@ #include "taichi/runtime/llvm/llvm_runtime_executor.h" #include "taichi/system/memory_pool.h" #include "taichi/runtime/cpu/aot_module_loader_impl.h" -#include "taichi/runtime/cuda/aot_module_loader_impl.h" #include "taichi/runtime/llvm/llvm_aot_module_loader.h" + +#ifdef TI_WITH_CUDA #include "taichi/rhi/cuda/cuda_driver.h" #include "taichi/platform/cuda/detect_cuda.h" +#include "taichi/runtime/cuda/aot_module_loader_impl.h" +#endif #define TI_RUNTIME_HOST #include "taichi/program/context.h" @@ -104,6 +107,7 @@ TEST(LlvmCGraph, Mpm88Cpu) { } TEST(LlvmCGraph, Mpm88Cuda) { +#ifdef TI_WITH_CUDA if (is_cuda_api_available()) { CompileConfig cfg; cfg.arch = Arch::cuda; @@ -185,4 +189,5 @@ TEST(LlvmCGraph, Mpm88Cuda) { g_update->run(args); exec.synchronize(); } +#endif } From 4b13d88207e3f9f1dd6476a88d5b3f3efe7668d0 Mon Sep 17 00:00:00 2001 From: jim19930609 Date: Fri, 15 Jul 2022 08:08:32 +0800 Subject: [PATCH 09/12] Added C-API tests for LLVM-CPU backend --- .github/workflows/scripts/unix_test.sh | 1 + c_api/src/taichi_llvm_impl.cpp | 9 +++-- c_api/src/taichi_llvm_impl.h | 2 ++ c_api/tests/c_api_interface_test.cpp | 38 --------------------- cmake/TaichiCAPI.cmake | 9 +++-- cmake/TaichiCAPITests.cmake | 10 +----- tests/cpp/aot/llvm/field_aot_test.cpp | 7 +--- tests/cpp/aot/llvm/graph_aot_test.cpp | 7 +--- tests/cpp/aot/llvm/kernel_aot_test.cpp | 5 --- tests/cpp/aot/llvm/mpm88_graph_aot_test.cpp | 7 +--- tests/test_utils.py | 2 -- 11 files changed, 17 insertions(+), 80 deletions(-) diff --git a/.github/workflows/scripts/unix_test.sh b/.github/workflows/scripts/unix_test.sh index d1f2ea59591af..f24e9d89b0ba9 100755 --- a/.github/workflows/scripts/unix_test.sh +++ b/.github/workflows/scripts/unix_test.sh @@ -13,6 +13,7 @@ check_in_docker() { export TI_SKIP_VERSION_CHECK=ON export TI_CI=1 export TI_IN_DOCKER=$(check_in_docker) +export LD_LIBRARY_PATH=$PWD/build/:$LD_LIBRARY_PATH if [[ "$TI_IN_DOCKER" == "true" ]]; then source $HOME/miniconda/etc/profile.d/conda.sh diff --git a/c_api/src/taichi_llvm_impl.cpp b/c_api/src/taichi_llvm_impl.cpp index fc6e71c2cd5f5..567b0165f2b80 100644 --- a/c_api/src/taichi_llvm_impl.cpp +++ b/c_api/src/taichi_llvm_impl.cpp @@ -3,6 +3,7 @@ #ifdef TI_WITH_LLVM +#include "taichi/program/compile_config.h" #include "taichi/runtime/llvm/llvm_runtime_executor.h" #include "taichi/runtime/cpu/aot_module_loader_impl.h" @@ -13,9 +14,11 @@ namespace capi { LlvmRuntime::LlvmRuntime(taichi::Arch arch) : Runtime(arch) { - taichi::lang::CompileConfig cfg; - cfg.arch = arch; - executor_ = std::make_unique(cfg, nullptr); + cfg_ = std::make_unique(); + cfg_->arch = arch; + + executor_ = + std::make_unique(*cfg_.get(), nullptr); taichi::lang::Device *compute_device = executor_->get_compute_device(); memory_pool_ = diff --git a/c_api/src/taichi_llvm_impl.h b/c_api/src/taichi_llvm_impl.h index fbb77cc9c60e3..ed9c684356cc7 100644 --- a/c_api/src/taichi_llvm_impl.h +++ b/c_api/src/taichi_llvm_impl.h @@ -7,6 +7,7 @@ namespace taichi { namespace lang { class LlvmRuntimeExecutor; class MemoryPool; +struct CompileConfig; } // namespace lang } // namespace taichi @@ -37,6 +38,7 @@ class LlvmRuntime : public Runtime { taichi::uint64 *result_buffer{nullptr}; std::unique_ptr executor_{nullptr}; std::unique_ptr memory_pool_{nullptr}; + std::unique_ptr cfg_{nullptr}; }; } // namespace capi diff --git a/c_api/tests/c_api_interface_test.cpp b/c_api/tests/c_api_interface_test.cpp index e2ac5ad383d21..d43d661a091c8 100644 --- a/c_api/tests/c_api_interface_test.cpp +++ b/c_api/tests/c_api_interface_test.cpp @@ -8,13 +8,6 @@ TEST(CapiDryRun, Runtime) { TiRuntime runtime = ti_create_runtime(arch); ti_destroy_runtime(runtime); } - -#ifdef TI_WITH_CUDA - // CUDA Runtime - TiArch arch = TiArch::TI_ARCH_CUDA; - TiRuntime runtime = ti_create_runtime(arch); - ti_destroy_runtime(runtime); -#endif } TEST(CapiDryRun, MemoryAllocation) { @@ -37,17 +30,6 @@ TEST(CapiDryRun, MemoryAllocation) { ti_destroy_runtime(runtime); } - -#ifdef TI_WITH_CUDA - // CUDA Runtime - TiArch arch = TiArch::TI_ARCH_CUDA; - TiRuntime runtime = ti_create_runtime(arch); - - TiMemory memory = ti_allocate_memory(runtime, &alloc_info); - ti_free_memory(runtime, memory); - - ti_destroy_runtime(runtime); -#endif } TEST(CapiDryRun, CpuAotModule) { @@ -67,23 +49,3 @@ TEST(CapiDryRun, CpuAotModule) { ti_destroy_runtime(runtime); } } - -TEST(CapiDryRun, CudaAotModule) { -#ifdef TI_WITH_CUDA - const auto folder_dir = getenv("TAICHI_AOT_FOLDER_PATH"); - - std::stringstream aot_mod_ss; - aot_mod_ss << folder_dir; - - { - // CUDA Runtime - TiArch arch = TiArch::TI_ARCH_CUDA; - TiRuntime runtime = ti_create_runtime(arch); - - TiAotModule aot_mod = ti_load_aot_module(runtime, aot_mod_ss.str().c_str()); - ti_destroy_aot_module(aot_mod); - - ti_destroy_runtime(runtime); - } -#endif -} diff --git a/cmake/TaichiCAPI.cmake b/cmake/TaichiCAPI.cmake index a07d283c8f65d..8a882e2e82629 100644 --- a/cmake/TaichiCAPI.cmake +++ b/cmake/TaichiCAPI.cmake @@ -1,16 +1,15 @@ cmake_minimum_required(VERSION 3.0) - - - set(TAICHI_C_API_NAME taichi_c_api) file(GLOB_RECURSE C_API_SOURCE "c_api/src/*.cpp") add_library(${TAICHI_C_API_NAME} SHARED ${C_API_SOURCE}) target_link_libraries(${TAICHI_C_API_NAME} PRIVATE taichi_isolated_core) + +set(C_API_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build") set_target_properties(${TAICHI_C_API_NAME} PROPERTIES - LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build" - ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build") + LIBRARY_OUTPUT_DIRECTORY ${C_API_OUTPUT_DIRECTORY} + ARCHIVE_OUTPUT_DIRECTORY ${C_API_OUTPUT_DIRECTORY}) target_include_directories(${TAICHI_C_API_NAME} PUBLIC diff --git a/cmake/TaichiCAPITests.cmake b/cmake/TaichiCAPITests.cmake index 7c94deeef10fd..49d7e43f3ba62 100644 --- a/cmake/TaichiCAPITests.cmake +++ b/cmake/TaichiCAPITests.cmake @@ -14,15 +14,7 @@ file(GLOB_RECURSE TAICHI_C_API_TESTS_SOURCE "c_api/tests/*.cpp") add_executable(${C_API_TESTS_NAME} ${TAICHI_C_API_TESTS_SOURCE}) -if (WIN32) - # Output the executable to bin/ instead of build/Debug/... - set(TESTS_OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/bin") - set_target_properties(${C_API_TESTS_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${TESTS_OUTPUT_DIR}) - set_target_properties(${C_API_TESTS_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${TESTS_OUTPUT_DIR}) - set_target_properties(${C_API_TESTS_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${TESTS_OUTPUT_DIR}) - set_target_properties(${C_API_TESTS_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${TESTS_OUTPUT_DIR}) - set_target_properties(${C_API_TESTS_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${TESTS_OUTPUT_DIR}) -endif() + target_link_libraries(${C_API_TESTS_NAME} PRIVATE taichi_c_api) target_link_libraries(${C_API_TESTS_NAME} PRIVATE gtest_main) diff --git a/tests/cpp/aot/llvm/field_aot_test.cpp b/tests/cpp/aot/llvm/field_aot_test.cpp index ed2135f3b6765..29124a68dddc7 100644 --- a/tests/cpp/aot/llvm/field_aot_test.cpp +++ b/tests/cpp/aot/llvm/field_aot_test.cpp @@ -4,13 +4,10 @@ #include "taichi/runtime/llvm/llvm_runtime_executor.h" #include "taichi/system/memory_pool.h" #include "taichi/runtime/cpu/aot_module_loader_impl.h" -#include "taichi/runtime/llvm/llvm_aot_module_loader.h" - -#ifdef TI_WITH_CUDA #include "taichi/runtime/cuda/aot_module_loader_impl.h" +#include "taichi/runtime/llvm/llvm_aot_module_loader.h" #include "taichi/rhi/cuda/cuda_driver.h" #include "taichi/platform/cuda/detect_cuda.h" -#endif #define TI_RUNTIME_HOST #include "taichi/program/context.h" @@ -125,7 +122,6 @@ TEST(LlvmAotTest, CpuField) { } TEST(LlvmAotTest, CudaField) { -#ifdef TI_WITH_CUDA if (is_cuda_api_available()) { CompileConfig cfg; cfg.arch = Arch::cuda; @@ -148,7 +144,6 @@ TEST(LlvmAotTest, CudaField) { run_field_tests(mod.get(), &exec, result_buffer); } -#endif } } // namespace lang diff --git a/tests/cpp/aot/llvm/graph_aot_test.cpp b/tests/cpp/aot/llvm/graph_aot_test.cpp index 28e500c9f5315..1fdf217a7434f 100644 --- a/tests/cpp/aot/llvm/graph_aot_test.cpp +++ b/tests/cpp/aot/llvm/graph_aot_test.cpp @@ -4,13 +4,10 @@ #include "taichi/runtime/program_impls/llvm/llvm_program.h" #include "taichi/system/memory_pool.h" #include "taichi/runtime/cpu/aot_module_loader_impl.h" -#include "taichi/runtime/llvm/llvm_aot_module_loader.h" - -#ifdef TI_WITH_CUDA #include "taichi/runtime/cuda/aot_module_loader_impl.h" +#include "taichi/runtime/llvm/llvm_aot_module_loader.h" #include "taichi/rhi/cuda/cuda_driver.h" #include "taichi/platform/cuda/detect_cuda.h" -#endif #define TI_RUNTIME_HOST #include "taichi/program/context.h" @@ -73,7 +70,6 @@ TEST(LlvmCGraph, RunGraphCpu) { } TEST(LlvmCGraph, RunGraphCuda) { -#ifdef TI_WITH_CUDA if (is_cuda_api_available()) { CompileConfig cfg; cfg.arch = Arch::cuda; @@ -130,5 +126,4 @@ TEST(LlvmCGraph, RunGraphCuda) { EXPECT_EQ(cpu_data[i], 3 * i + base0 + base1 + base2); } } -#endif } diff --git a/tests/cpp/aot/llvm/kernel_aot_test.cpp b/tests/cpp/aot/llvm/kernel_aot_test.cpp index 70c53b13e59e1..93458b7c84613 100644 --- a/tests/cpp/aot/llvm/kernel_aot_test.cpp +++ b/tests/cpp/aot/llvm/kernel_aot_test.cpp @@ -4,12 +4,9 @@ #include "taichi/runtime/llvm/llvm_runtime_executor.h" #include "taichi/system/memory_pool.h" #include "taichi/runtime/cpu/aot_module_loader_impl.h" - -#ifdef TI_WITH_CUDA #include "taichi/runtime/cuda/aot_module_loader_impl.h" #include "taichi/rhi/cuda/cuda_driver.h" #include "taichi/platform/cuda/detect_cuda.h" -#endif #define TI_RUNTIME_HOST #include "taichi/program/context.h" @@ -59,7 +56,6 @@ TEST(LlvmAotTest, CpuKernel) { } TEST(LlvmAotTest, CudaKernel) { -#ifdef TI_WITH_CUDA if (is_cuda_api_available()) { CompileConfig cfg; cfg.arch = Arch::cuda; @@ -102,7 +98,6 @@ TEST(LlvmAotTest, CudaKernel) { EXPECT_EQ(cpu_data[i], i); } } -#endif } } // namespace lang diff --git a/tests/cpp/aot/llvm/mpm88_graph_aot_test.cpp b/tests/cpp/aot/llvm/mpm88_graph_aot_test.cpp index afd631a1cf201..c9042934ad9d3 100644 --- a/tests/cpp/aot/llvm/mpm88_graph_aot_test.cpp +++ b/tests/cpp/aot/llvm/mpm88_graph_aot_test.cpp @@ -4,13 +4,10 @@ #include "taichi/runtime/llvm/llvm_runtime_executor.h" #include "taichi/system/memory_pool.h" #include "taichi/runtime/cpu/aot_module_loader_impl.h" +#include "taichi/runtime/cuda/aot_module_loader_impl.h" #include "taichi/runtime/llvm/llvm_aot_module_loader.h" - -#ifdef TI_WITH_CUDA #include "taichi/rhi/cuda/cuda_driver.h" #include "taichi/platform/cuda/detect_cuda.h" -#include "taichi/runtime/cuda/aot_module_loader_impl.h" -#endif #define TI_RUNTIME_HOST #include "taichi/program/context.h" @@ -107,7 +104,6 @@ TEST(LlvmCGraph, Mpm88Cpu) { } TEST(LlvmCGraph, Mpm88Cuda) { -#ifdef TI_WITH_CUDA if (is_cuda_api_available()) { CompileConfig cfg; cfg.arch = Arch::cuda; @@ -189,5 +185,4 @@ TEST(LlvmCGraph, Mpm88Cuda) { g_update->run(args); exec.synchronize(); } -#endif } diff --git a/tests/test_utils.py b/tests/test_utils.py index e79b6748349da..8522288ece0e6 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -36,8 +36,6 @@ __capi_aot_test_cases = { "CapiDryRun.CpuAotModule": [os.path.join('cpp', 'aot', 'llvm', 'kernel_aot_test.py'), "--arch=cpu"], - "CapiDryRun.CudaAotModule": - [os.path.join('cpp', 'aot', 'llvm', 'kernel_aot_test.py'), "--arch=cuda"], } From d36b62d532b5cc8deda28b22c6b187afb29f7a0d Mon Sep 17 00:00:00 2001 From: jim19930609 Date: Mon, 18 Jul 2022 11:04:10 +0800 Subject: [PATCH 10/12] [llvm] [aot] Add LLVM to CAPI part 5: Added C-API tests for Vulkan backend --- c_api/src/c_api_test_utils.cpp | 19 +++++++++++++++++++ c_api/src/c_api_test_utils.h | 10 ++++++++++ c_api/tests/c_api_interface_test.cpp | 8 ++++++++ cmake/TaichiCAPI.cmake | 4 ++++ cmake/TaichiCAPITests.cmake | 1 + 5 files changed, 42 insertions(+) create mode 100644 c_api/src/c_api_test_utils.cpp create mode 100644 c_api/src/c_api_test_utils.h diff --git a/c_api/src/c_api_test_utils.cpp b/c_api/src/c_api_test_utils.cpp new file mode 100644 index 0000000000000..0215dcf3e736a --- /dev/null +++ b/c_api/src/c_api_test_utils.cpp @@ -0,0 +1,19 @@ +#include "c_api_test_utils.h" + +#ifdef TI_WITH_VULKAN +#include "taichi/rhi/vulkan/vulkan_loader.h" +#endif + +namespace capi { +namespace utils { + +bool is_vulkan_available() { +#ifdef TI_WITH_VULKAN + return taichi::lang::vulkan::is_vulkan_api_available(); +#else + return false; +#endif +} + +} // namespace utils +} // namespace capi diff --git a/c_api/src/c_api_test_utils.h b/c_api/src/c_api_test_utils.h new file mode 100644 index 0000000000000..95e002106ce63 --- /dev/null +++ b/c_api/src/c_api_test_utils.h @@ -0,0 +1,10 @@ +#pragma once +#include + +namespace capi { +namespace utils { + +TI_DLL_EXPORT bool TI_API_CALL is_vulkan_available(); + +} // namespace utils +} // namespace capi diff --git a/c_api/tests/c_api_interface_test.cpp b/c_api/tests/c_api_interface_test.cpp index d43d661a091c8..6937bbf877cd5 100644 --- a/c_api/tests/c_api_interface_test.cpp +++ b/c_api/tests/c_api_interface_test.cpp @@ -1,4 +1,5 @@ #include "gtest/gtest.h" +#include "c_api_test_utils.h" #include "taichi/taichi_core.h" TEST(CapiDryRun, Runtime) { @@ -8,6 +9,13 @@ TEST(CapiDryRun, Runtime) { TiRuntime runtime = ti_create_runtime(arch); ti_destroy_runtime(runtime); } + + if (capi::utils::is_vulkan_available()) { + // Vulkan Runtime + TiArch arch = TiArch::TI_ARCH_VULKAN; + TiRuntime runtime = ti_create_runtime(arch); + ti_destroy_runtime(runtime); + } } TEST(CapiDryRun, MemoryAllocation) { diff --git a/cmake/TaichiCAPI.cmake b/cmake/TaichiCAPI.cmake index 8a882e2e82629..94857ed2d6af0 100644 --- a/cmake/TaichiCAPI.cmake +++ b/cmake/TaichiCAPI.cmake @@ -3,6 +3,10 @@ cmake_minimum_required(VERSION 3.0) set(TAICHI_C_API_NAME taichi_c_api) file(GLOB_RECURSE C_API_SOURCE "c_api/src/*.cpp") +if(NOT TI_BUILD_TESTS) + list(REMOVE_ITEM C_API_SOURCE "c_api/src/c_api_interface_test.cpp") +endif() + add_library(${TAICHI_C_API_NAME} SHARED ${C_API_SOURCE}) target_link_libraries(${TAICHI_C_API_NAME} PRIVATE taichi_isolated_core) diff --git a/cmake/TaichiCAPITests.cmake b/cmake/TaichiCAPITests.cmake index 49d7e43f3ba62..832c55c4d1ab1 100644 --- a/cmake/TaichiCAPITests.cmake +++ b/cmake/TaichiCAPITests.cmake @@ -22,6 +22,7 @@ target_include_directories(${C_API_TESTS_NAME} PRIVATE ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/c_api/include + ${PROJECT_SOURCE_DIR}/c_api/src ) add_test(NAME ${C_API_TESTS_NAME} COMMAND ${C_API_TESTS_NAME}) From d3dfe01a1b315c7d9f43f57528948f4a39e756ae Mon Sep 17 00:00:00 2001 From: jim19930609 Date: Mon, 18 Jul 2022 11:07:40 +0800 Subject: [PATCH 11/12] Minor bug fix --- cmake/TaichiCAPI.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/TaichiCAPI.cmake b/cmake/TaichiCAPI.cmake index 94857ed2d6af0..127b7be4da10b 100644 --- a/cmake/TaichiCAPI.cmake +++ b/cmake/TaichiCAPI.cmake @@ -4,7 +4,7 @@ set(TAICHI_C_API_NAME taichi_c_api) file(GLOB_RECURSE C_API_SOURCE "c_api/src/*.cpp") if(NOT TI_BUILD_TESTS) - list(REMOVE_ITEM C_API_SOURCE "c_api/src/c_api_interface_test.cpp") + list(REMOVE_ITEM C_API_SOURCE "c_api/src/c_api_test_utils.cpp") endif() add_library(${TAICHI_C_API_NAME} SHARED ${C_API_SOURCE}) From d2622f65bb6aa846694865826e9c7c18f37b451c Mon Sep 17 00:00:00 2001 From: jim19930609 Date: Mon, 18 Jul 2022 13:29:58 +0800 Subject: [PATCH 12/12] Added more test cases --- c_api/src/c_api_test_utils.cpp | 5 ++ c_api/src/c_api_test_utils.h | 1 + c_api/tests/c_api_interface_test.cpp | 71 +++++++++++++++++++++++++++ tests/cpp/aot/llvm/kernel_aot_test.py | 2 + tests/test_utils.py | 6 +++ 5 files changed, 85 insertions(+) diff --git a/c_api/src/c_api_test_utils.cpp b/c_api/src/c_api_test_utils.cpp index 0215dcf3e736a..2e30a16ea40ba 100644 --- a/c_api/src/c_api_test_utils.cpp +++ b/c_api/src/c_api_test_utils.cpp @@ -1,4 +1,5 @@ #include "c_api_test_utils.h" +#include "taichi/platform/cuda/detect_cuda.h" #ifdef TI_WITH_VULKAN #include "taichi/rhi/vulkan/vulkan_loader.h" @@ -15,5 +16,9 @@ bool is_vulkan_available() { #endif } +bool is_cuda_available() { + return taichi::is_cuda_api_available(); +} + } // namespace utils } // namespace capi diff --git a/c_api/src/c_api_test_utils.h b/c_api/src/c_api_test_utils.h index 95e002106ce63..13f91666580fe 100644 --- a/c_api/src/c_api_test_utils.h +++ b/c_api/src/c_api_test_utils.h @@ -5,6 +5,7 @@ namespace capi { namespace utils { TI_DLL_EXPORT bool TI_API_CALL is_vulkan_available(); +TI_DLL_EXPORT bool TI_API_CALL is_cuda_available(); } // namespace utils } // namespace capi diff --git a/c_api/tests/c_api_interface_test.cpp b/c_api/tests/c_api_interface_test.cpp index 6937bbf877cd5..683b6569bfb2b 100644 --- a/c_api/tests/c_api_interface_test.cpp +++ b/c_api/tests/c_api_interface_test.cpp @@ -16,6 +16,13 @@ TEST(CapiDryRun, Runtime) { TiRuntime runtime = ti_create_runtime(arch); ti_destroy_runtime(runtime); } + + if (capi::utils::is_cuda_available()) { + // Vulkan Runtime + TiArch arch = TiArch::TI_ARCH_CUDA; + TiRuntime runtime = ti_create_runtime(arch); + ti_destroy_runtime(runtime); + } } TEST(CapiDryRun, MemoryAllocation) { @@ -38,6 +45,28 @@ TEST(CapiDryRun, MemoryAllocation) { ti_destroy_runtime(runtime); } + + if (capi::utils::is_vulkan_available()) { + // Vulkan Runtime + TiArch arch = TiArch::TI_ARCH_VULKAN; + TiRuntime runtime = ti_create_runtime(arch); + + TiMemory memory = ti_allocate_memory(runtime, &alloc_info); + ti_free_memory(runtime, memory); + + ti_destroy_runtime(runtime); + } + + if (capi::utils::is_cuda_available()) { + // Cuda Runtime + TiArch arch = TiArch::TI_ARCH_CUDA; + TiRuntime runtime = ti_create_runtime(arch); + + TiMemory memory = ti_allocate_memory(runtime, &alloc_info); + ti_free_memory(runtime, memory); + + ti_destroy_runtime(runtime); + } } TEST(CapiDryRun, CpuAotModule) { @@ -57,3 +86,45 @@ TEST(CapiDryRun, CpuAotModule) { ti_destroy_runtime(runtime); } } + +TEST(CapiDryRun, VulkanAotModule) { + if (capi::utils::is_vulkan_available()) { + const auto folder_dir = getenv("TAICHI_AOT_FOLDER_PATH"); + + std::stringstream aot_mod_ss; + aot_mod_ss << folder_dir; + + { + // Vulkan Runtime + TiArch arch = TiArch::TI_ARCH_VULKAN; + TiRuntime runtime = ti_create_runtime(arch); + + TiAotModule aot_mod = + ti_load_aot_module(runtime, aot_mod_ss.str().c_str()); + ti_destroy_aot_module(aot_mod); + + ti_destroy_runtime(runtime); + } + } +} + +TEST(CapiDryRun, CudaAotModule) { + if (capi::utils::is_cuda_available()) { + const auto folder_dir = getenv("TAICHI_AOT_FOLDER_PATH"); + + std::stringstream aot_mod_ss; + aot_mod_ss << folder_dir; + + { + // Vulkan Runtime + TiArch arch = TiArch::TI_ARCH_CUDA; + TiRuntime runtime = ti_create_runtime(arch); + + TiAotModule aot_mod = + ti_load_aot_module(runtime, aot_mod_ss.str().c_str()); + ti_destroy_aot_module(aot_mod); + + ti_destroy_runtime(runtime); + } + } +} diff --git a/tests/cpp/aot/llvm/kernel_aot_test.py b/tests/cpp/aot/llvm/kernel_aot_test.py index dda556d662678..d4bb27a9ea333 100644 --- a/tests/cpp/aot/llvm/kernel_aot_test.py +++ b/tests/cpp/aot/llvm/kernel_aot_test.py @@ -13,5 +13,7 @@ compile_kernel_aot(arch=ti.cpu) elif args.arch == "cuda": compile_kernel_aot(arch=ti.cuda) + elif args.arch == "vulkan": + compile_kernel_aot(arch=ti.vulkan) else: assert False diff --git a/tests/test_utils.py b/tests/test_utils.py index 8522288ece0e6..c6fd5432da96c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -36,6 +36,12 @@ __capi_aot_test_cases = { "CapiDryRun.CpuAotModule": [os.path.join('cpp', 'aot', 'llvm', 'kernel_aot_test.py'), "--arch=cpu"], + "CapiDryRun.VulkanAotModule": [ + os.path.join('cpp', 'aot', 'llvm', 'kernel_aot_test.py'), + "--arch=vulkan" + ], + "CapiDryRun.CudaAotModule": + [os.path.join('cpp', 'aot', 'llvm', 'kernel_aot_test.py'), "--arch=cuda"], }