Skip to content

Commit 502692b

Browse files
authored
cherry-pick #1855, #1806, #1798
Sync master to dev-1.x
2 parents 3c110b8 + fabdacc commit 502692b

File tree

18 files changed

+117
-20
lines changed

18 files changed

+117
-20
lines changed

CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ include(cmake/MMDeploy.cmake)
9999
add_subdirectory(csrc/mmdeploy)
100100

101101
if (MMDEPLOY_BUILD_SDK)
102-
install(TARGETS MMDeployStaticModules
103-
MMDeployDynamicModules
104-
MMDeployLibs
105-
EXPORT MMDeployTargets)
102+
if (NOT MMDEPLOY_BUILD_SDK_MONOLITHIC)
103+
install(TARGETS MMDeployStaticModules
104+
MMDeployDynamicModules
105+
MMDeployLibs
106+
EXPORT MMDeployTargets)
107+
endif ()
106108

107109
if (MMDEPLOY_BUILD_TEST)
108110
add_subdirectory(tests/test_csrc)

cmake/MMDeploy.cmake

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright (c) OpenMMLab. All rights reserved.
22

3-
function (mmdeploy_export NAME)
3+
function (mmdeploy_export_impl NAME)
44
set(_LIB_DIR lib)
55
if (MSVC)
66
set(_LIB_DIR bin)
@@ -15,13 +15,22 @@ endfunction ()
1515
macro(mmdeploy_add_net NAME)
1616
if (MMDEPLOY_DYNAMIC_BACKEND)
1717
mmdeploy_add_library(${NAME} SHARED ${ARGN})
18+
# DYNAMIC_BACKEND implies BUILD_SDK_MONOLITHIC
19+
mmdeploy_export_impl(${NAME})
1820
target_link_libraries(${PROJECT_NAME} PRIVATE mmdeploy)
1921
set(BACKEND_LIB_NAMES ${BACKEND_LIB_NAMES} ${PROJECT_NAME} PARENT_SCOPE)
2022
else ()
2123
mmdeploy_add_module(${NAME} ${ARGN})
2224
endif ()
2325
endmacro()
2426

27+
function (mmdeploy_export NAME)
28+
if (NOT MMDEPLOY_BUILD_SDK_MONOLITHIC)
29+
mmdeploy_export_impl(${NAME})
30+
endif ()
31+
endfunction ()
32+
33+
2534
function (mmdeploy_add_library NAME)
2635
# EXCLUDE: exclude from registering & exporting
2736
cmake_parse_arguments(_MMDEPLOY "EXCLUDE" "" "" ${ARGN})

csrc/mmdeploy/apis/c/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,5 @@ if (MMDEPLOY_BUILD_SDK_CSHARP_API OR MMDEPLOY_BUILD_SDK_MONOLITHIC)
8989
INSTALL_RPATH "\$ORIGIN"
9090
BUILD_RPATH "\$ORIGIN")
9191
endif ()
92-
mmdeploy_export(mmdeploy)
92+
mmdeploy_export_impl(mmdeploy)
9393
endif ()

csrc/mmdeploy/apis/cxx/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ if (TARGET mmdeploy)
3434
else ()
3535
target_link_libraries(${PROJECT_NAME} INTERFACE mmdeploy::core)
3636
endif ()
37-
mmdeploy_export(${PROJECT_NAME})
37+
mmdeploy_export_impl(${PROJECT_NAME})
3838
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/mmdeploy/common.hpp
3939
DESTINATION include/mmdeploy)
4040
install(DIRECTORY ${CMAKE_SOURCE_DIR}/demo/csrc/ DESTINATION example/cpp

csrc/mmdeploy/core/device.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
#include <functional>
88
#include <memory>
99
#include <optional>
10+
#include <ostream>
1011
#include <string>
1112
#include <vector>
1213

1314
#include "mmdeploy/core/macro.h"
1415
#include "mmdeploy/core/mpl/type_traits.h"
1516
#include "mmdeploy/core/status_code.h"
17+
#include "mmdeploy/core/utils/formatter.h"
1618

1719
namespace mmdeploy {
1820

@@ -97,6 +99,11 @@ class Device {
9799
return PlatformId(platform_id_);
98100
}
99101

102+
friend std::ostream& operator<<(std::ostream& os, const Device& device) {
103+
os << "(" << device.platform_id_ << ", " << device.device_id_ << ")";
104+
return os;
105+
}
106+
100107
private:
101108
int platform_id_{0};
102109
int device_id_{0};
@@ -112,6 +119,9 @@ class MMDEPLOY_API Platform {
112119
// throws if not found
113120
explicit Platform(int platform_id);
114121

122+
// bind device with the current thread
123+
Result<void> Bind(Device device, Device* prev);
124+
115125
// -1 if invalid
116126
int GetPlatformId() const;
117127

@@ -135,6 +145,27 @@ class MMDEPLOY_API Platform {
135145

136146
MMDEPLOY_API const char* GetPlatformName(PlatformId id);
137147

148+
class DeviceGuard {
149+
public:
150+
explicit DeviceGuard(Device device) : platform_(device.platform_id()) {
151+
auto r = platform_.Bind(device, &prev_);
152+
if (!r) {
153+
MMDEPLOY_ERROR("failed to bind device {}: {}", device, r.error().message().c_str());
154+
}
155+
}
156+
157+
~DeviceGuard() {
158+
auto r = platform_.Bind(prev_, nullptr);
159+
if (!r) {
160+
MMDEPLOY_ERROR("failed to unbind device {}: {}", prev_, r.error().message().c_str());
161+
}
162+
}
163+
164+
private:
165+
Platform platform_;
166+
Device prev_;
167+
};
168+
138169
class MMDEPLOY_API Stream {
139170
public:
140171
Stream() = default;

csrc/mmdeploy/core/device_impl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Platform::Platform(int platform_id) {
5454
}
5555
}
5656

57+
Result<void> Platform::Bind(Device device, Device* prev) { return impl_->BindDevice(device, prev); }
58+
5759
const char* GetPlatformName(PlatformId id) {
5860
if (auto impl = gPlatformRegistry().GetPlatformImpl(id); impl) {
5961
return impl->GetPlatformName();

csrc/mmdeploy/core/device_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class PlatformImpl {
2727

2828
virtual void SetPlatformId(int id) { platform_id_ = id; }
2929

30-
virtual Result<void> SetDevice(Device device) { return success(); };
30+
virtual Result<void> BindDevice(Device device, Device* prev) = 0;
3131

3232
virtual shared_ptr<BufferImpl> CreateBuffer(Device device) = 0;
3333

csrc/mmdeploy/core/registry.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ class Registry : public Registry<void> {
129129
using Signature = GetSignature<Tag>;
130130
using CreatorType = Creator<Signature>;
131131

132+
// workaround for gcc-10.2 (https://github.com/open-mmlab/mmdeploy/issues/1796)
133+
Registry() : Registry<void>{} {}
134+
132135
bool Add(CreatorType& creator) & { return AddCreator(creator); }
133136

134137
CreatorType* Get(const string_view& name, int version) & {

csrc/mmdeploy/device/cpu/cpu_device.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ class CpuHostMemory : public NonCopyable {
7070
////////////////////////////////////////////////////////////////////////////////
7171
/// CpuPlatformImpl
7272

73+
Result<void> CpuPlatformImpl::BindDevice(Device device, Device* prev) {
74+
// do nothing
75+
if (prev) {
76+
*prev = device;
77+
}
78+
return success();
79+
}
80+
7381
shared_ptr<BufferImpl> CpuPlatformImpl::CreateBuffer(Device device) {
7482
return std::make_shared<CpuBufferImpl>(device);
7583
}

csrc/mmdeploy/device/cpu/cpu_device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class CpuPlatformImpl : public PlatformImpl {
1717

1818
const char* GetPlatformName() const noexcept override;
1919

20+
Result<void> BindDevice(Device device, Device* prev) override;
21+
2022
shared_ptr<BufferImpl> CreateBuffer(Device device) override;
2123

2224
shared_ptr<StreamImpl> CreateStream(Device device) override;

0 commit comments

Comments
 (0)