Skip to content

Commit

Permalink
Merge pull request #31 from lipracer/dev
Browse files Browse the repository at this point in the history
refine support any linux
  • Loading branch information
lipracer authored Jan 3, 2024
2 parents 9fb1765 + 6295d7a commit b4c47b5
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 60 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "third_party/nanobind"]
path = third_party/nanobind
url = https://github.com/wjakob/nanobind
[submodule "third_party/fmt"]
path = third_party/fmt
url = https://github.com/fmtlib/fmt.git
Expand Down
25 changes: 0 additions & 25 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,6 @@ add_compile_options(-Wall -Wextra -Wno-unused-parameter
-Werror=return-type -Werror)


if (NOT SKBUILD)
message(WARNING "\
This CMake file is meant to be executed using 'scikit-build-core'.
Running it directly will almost certainly not produce the desired
result. If you are a user trying to install this package, use the
command below, which will install all necessary build dependencies,
compile the package in an isolated environment, and then install it.
=====================================================================
$ pip install .
=====================================================================
If you are a software developer, and this is your own package, then
it is usually much more efficient to install the build dependencies
in your environment once and use the following command that avoids
a costly creation of a new virtual environment at every compilation:
=====================================================================
$ pip install nanobind scikit-build-core[pyproject]
$ pip install --no-build-isolation -ve .
=====================================================================
You may optionally add -Ceditable.rebuild=true to auto-rebuild when
the package is imported. Otherwise, you need to rerun the above
after editing C++ files.")
endif()

find_package(Python 3.8 COMPONENTS Interpreter Development.Module REQUIRED)

Expand All @@ -51,14 +29,11 @@ if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
endif()

# Detect the installed nanobind package and import it into CMake
list(APPEND CMAKE_PREFIX_PATH "third_party/nanobind")
find_package(nanobind CONFIG REQUIRED)

# list(APPEND CMAKE_PREFIX_PATH "third_party/fmt")
# find_package(fmt)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third_party/fmt)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third_party/nanobind)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/lib)

Expand Down
16 changes: 10 additions & 6 deletions include/hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,19 @@ class HookRuntimeContext {
}
map_type& map() { return map_; }

map_type::const_iterator& current_iter() {
thread_local static map_type::const_iterator iter;
return iter;
}

map_type::const_iterator setCurrentState(size_t UniqueId) {
cur_iter_ = map_.begin();
std::advance(cur_iter_, UniqueId);
return cur_iter_;
current_iter() = map_.begin();
std::advance(current_iter(), UniqueId);
return current_iter();
}

const std::string& curLibName() { return cur_iter_->first.lib_name; }
const std::string& curSymName() { return cur_iter_->first.sym_name; }
const std::string& curLibName() { return current_iter()->first.lib_name; }
const std::string& curSymName() { return current_iter()->first.sym_name; }

void dump() {
LOG(WARN) << "dum context map:";
Expand All @@ -119,7 +124,6 @@ class HookRuntimeContext {

private:
map_type map_;
map_type::const_iterator cur_iter_;
};

struct StringLiteral {
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
[build-system]
requires = ["scikit-build-core >=0.4.3", "nanobind >=1.3.2"]
build-backend = "scikit_build_core.build"

[project]
name = "cuda-mock"
version = "0.0.2"
Expand Down Expand Up @@ -39,3 +35,7 @@ test-requires = "pytest"
# Needed for full C++17 support
[tool.cibuildwheel.macos.environment]
MACOSX_DEPLOYMENT_TARGET = "10.14"

[build-system]
requires = ["setuptools>=42", "wheel", "scikit-build", "cmake>=3.17", "ninja"]
build-backend = "setuptools.build_meta"
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from setuptools import setup
import os

os.system("cmake -S . -B build && cmake --build build")
os.system("cd build/lib && ls | grep -v '\<cuda_mock\>' | xargs -I {} rm -rf {}")
setup()
9 changes: 7 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

nanobind_add_module(cuda_mock_impl STABLE_ABI cuda_mock_impl.cpp)
add_library(cuda_mock_impl SHARED cuda_mock_impl.cpp)

target_link_libraries(cuda_mock_impl PRIVATE cuda_mock)

include_directories(PRIVATE ${CMAKE_SOURCE_DIR}/include)
include_directories(PRIVATE ${CMAKE_SOURCE_DIR}/include)

set_target_properties(cuda_mock_impl
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/cuda_mock
)
19 changes: 19 additions & 0 deletions src/cuda_mock/cuda_mock_impl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os
import ctypes

script_dir = os.path.dirname(os.path.abspath(__file__))
cuda_mock_impl = ctypes.CDLL(f'{script_dir}/libcuda_mock_impl.so')

def add(lhs, rhs):
return lhs + rhs

def initialize():
return cuda_mock_impl.initialize()

def internal_install_hook(*args):
new_args = [ctypes.c_char_p(arg.encode('utf-8')) for arg in args]
return cuda_mock_impl.internal_install_hook(*new_args)

def xpu_initialize():
return cuda_mock_impl.xpu_initialize()

57 changes: 38 additions & 19 deletions src/cuda_mock_impl.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
#include <nanobind/nanobind.h>

#include "cuda_mock.h"
#include "xpu_mock.h"

namespace nb = nanobind;
using namespace nb::literals;

NB_MODULE(cuda_mock_impl, m) {
m.def(
"add", [](int a, int b) { return a + b; }, "a"_a, "b"_a);
m.def("initialize", []() { dh_initialize(); });
m.def("internal_install_hook", [](const char* srcLib, const char* targetLib,
const char* symbolName) {
dh_internal_install_hook(srcLib, targetLib, symbolName);
});
m.def("internal_install_hook",
[](const char* srcLib, const char* targetLib, const char* symbolName,
const char* hookerLibPath, const char* hookerSymbolName) {
dh_internal_install_hook(srcLib, targetLib, symbolName,
hookerLibPath, hookerSymbolName);
});
m.def("xpu_initialize", []() { xpu_dh_initialize(); });
// namespace nb = nanobind;
// using namespace nb::literals;

// NB_MODULE(cuda_mock_impl, m) {
// m.def(
// "add", [](int a, int b) { return a + b; }, "a"_a, "b"_a);
// m.def("initialize", []() { dh_initialize(); });
// m.def("internal_install_hook", [](const char* srcLib, const char*
// targetLib,
// const char* symbolName) {
// dh_internal_install_hook(srcLib, targetLib, symbolName);
// });
// m.def("internal_install_hook",
// [](const char* srcLib, const char* targetLib, const char*
// symbolName,
// const char* hookerLibPath, const char* hookerSymbolName) {
// dh_internal_install_hook(srcLib, targetLib, symbolName,
// hookerLibPath, hookerSymbolName);
// });
// m.def("xpu_initialize", []() { xpu_dh_initialize(); });
// }

typedef const char* PHStr;

extern "C" {

HOOK_API int add(int lhs, int rhs) { return lhs + rhs; }

HOOK_API void initialize() { dh_initialize(); }

HOOK_API void internal_install_hook(PHStr srcLib, PHStr targetLib,
PHStr symbolName, PHStr hookerLibPath,
PHStr hookerSymbolName) {
dh_internal_install_hook(srcLib, targetLib, symbolName, hookerLibPath,
hookerSymbolName);
}

HOOK_API void xpu_initialize() { xpu_dh_initialize(); }
}
1 change: 0 additions & 1 deletion third_party/nanobind
Submodule nanobind deleted from 1c462d

0 comments on commit b4c47b5

Please sign in to comment.