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

refactor(dipu): add static_value_array and simplify code #920

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

wiryls
Copy link
Collaborator

@wiryls wiryls commented Jul 26, 2024

  1. 添加了一个小工具用于生成数组,用它简化了一些代码,不再需要从 1 遍历到 16 去查找对应的 static 变量。
  2. 修复了清空 event pool 时,只清空当前 device 的问题。
    • 不过这个修复也不太改好,它会导致全部 event pool 被初始化,还好初始化过程基本没有副作用

@wiryls wiryls added DIPU DIPU related refactor just better coding labels Jul 26, 2024
}
};

EventPool<deviceEvent_t>* getEventPool() {
const int index = devproxy::current_device();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我是觉得 event pool 不应该调用 devproxy 命名空间的方法,所以把它移到外面了

}

void releaseAllEvent() { getEventPool()->release(); }
void event_pool_clear() {
for (auto& pool : EventPoolHolderArray::value) {
Copy link
Collaborator Author

@wiryls wiryls Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个改法欠佳(导致全部 pool 都被初始化),但没想到更好同时又不麻烦的方法了

EventPool(const EventPool&) = delete;
EventPool(EventPool&&) = delete;
EventPool& operator=(const EventPool&) = delete;
EventPool& operator=(EventPool&&) = delete;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有锁作为成员,EventPool 自动变得无法拷贝和移动,所以我都删掉了

wiryls added 2 commits July 29, 2024 12:52
@wiryls wiryls changed the title refactor(dipu): add make_static_function_array and simplify code refactor(dipu): add static_value_array and simplify code Jul 31, 2024
@@ -6,10 +6,8 @@

namespace dipu {

void getEventFromPool(deviceEvent_t& event);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实原来的驼峰也没哈问题,真没必要都改成下划线,纯个人习惯问题。。。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

确实,这里是习惯了

dipu::devapis::createEvent(&event);
}

void release(dipu::deviceEvent_t& event) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

新的名字确实比原来的规范

void restoreEventToPool(deviceEvent_t& event) {
getEventPool()->restore(event);
void event_pool_release(int index, deviceEvent_t& event) {
event_pool(index).release(event);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参数加了 index, 上下层的职责划分也比原来明确些。

@@ -35,3 +35,7 @@ const auto DIPU_BACKEND_SPARSE_TYPE =
const auto DICL_BACKEND_NAME = "dicl";

} // namespace dipu

namespace dipu {
auto inline constexpr kMaxDeviceNumber = 16;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥要新建个 ns, 这和上面的不是一样的吗?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我当时是看到两边命名风格不一样,所以隔开了一下

@@ -0,0 +1,53 @@
#pragma once

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是否应该放到 utils/ 目录下

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我也在犹豫放哪里,有没有啥规范或者惯例,还有就是文件名字有没有更好的

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

附议

struct make_static_value_array<T, std::index_sequence<I...>> {
using value_type = std::decay_t<decltype(T::template value<0>)>;
using type = std::array<value_type, sizeof...(I)>;
type static inline constexpr value{T::template value<I>...};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static inline constexpr 为啥要放到 type 后面? 这是啥规范, 我咋觉得 放到前面更容易懂?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是个人习惯,C++ 的修饰词太多了,auto、static、inline、constexpr、const、thread_local……通常来说我就习惯写成“类型+其它类型标识”的风格,例如“char const *” 这样。

有个好处是“从右往左”看时顺序非常清晰,比如这种情况:char const * const。能很清楚知道最右侧 const 是修饰左边的 char const *,而左侧的 const 只修饰 char

struct make_static_value_array<T, std::index_sequence<I...>> {
using value_type = std::decay_t<decltype(T::template value<0>)>;
using type = std::array<value_type, sizeof...(I)>;
type static inline constexpr value{T::template value<I>...};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

类内定义方法和非特化模板函数都是隐式 inline,不需要加,下面也有几个 case 不重复提了

Copy link
Collaborator Author

@wiryls wiryls Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里如果不写 inline,编译器就会要求这里只声明,需要在类外定义静态成员变量,否则违反 odr

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦哦 static 不是方法,下面几个应该是

@@ -0,0 +1,53 @@
#pragma once

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

附议

namespace dipu {

template <typename T, typename S>
struct make_static_value_array {};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要是写新项目,我肯定不让你用下划线类名

static AsyncMemPoolImpl async_mem_pool;
static AllocatorImpl cache_allocator;
static int n = [&]() {
cache_allocator.set_raw_allocator(raw_allocator);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 precondition 好强啊,没有任何警示吗

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块没想到怎么改合适……索性直接复制之前的实现

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DIPU DIPU related refactor just better coding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants