forked from secretflow/spu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.cc
92 lines (76 loc) · 2.72 KB
/
context.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2021 Ant Group Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "libspu/core/context.h"
#include "yacl/link/algorithm/allgather.h"
#include "yacl/utils/parallel.h"
#include "libspu/core/trace.h"
namespace spu {
namespace {
std::string genRootObjectId(const std::shared_ptr<yacl::link::Context>& lctx) {
// In single-process simulation mode, multi-context need to use different id,
// or tracing will not work.
if (lctx) {
return fmt::format("root-{}", lctx->Rank());
}
return "root";
}
} // namespace
SPUContext::SPUContext(const RuntimeConfig& config,
const std::shared_ptr<yacl::link::Context>& lctx)
: config_(config),
prot_(std::make_unique<Object>(genRootObjectId(lctx))),
lctx_(lctx),
max_cluster_level_concurrency_(yacl::get_num_threads()) {
// Limit number of threads
if (config.max_concurrency() > 0) {
yacl::set_num_threads(config.max_concurrency());
max_cluster_level_concurrency_ = std::min<int32_t>(
max_cluster_level_concurrency_, config.max_concurrency());
}
if (lctx_) {
auto other_max = yacl::link::AllGather(
lctx, {&max_cluster_level_concurrency_, sizeof(int32_t)}, "num_cores");
// Comupte min
for (const auto& o : other_max) {
max_cluster_level_concurrency_ = std::min<int32_t>(
max_cluster_level_concurrency_, o.data<int32_t>()[0]);
}
}
}
std::unique_ptr<SPUContext> SPUContext::fork() const {
std::shared_ptr<yacl::link::Context> new_lctx =
lctx_ ? lctx_->Spawn() : nullptr;
auto new_sctx = std::make_unique<SPUContext>(config_, new_lctx);
new_sctx->prot_ = prot_->fork();
return new_sctx;
}
void setupTrace(spu::SPUContext* sctx, const spu::RuntimeConfig& rt_config) {
int64_t tr_flag = 0;
// TODO: Support tracing for parallel op execution
if (rt_config.enable_action_trace() &&
!rt_config.experimental_enable_intra_op_par()) {
tr_flag |= TR_LOG;
}
if (rt_config.enable_pphlo_profile()) {
tr_flag |= TR_HLO;
tr_flag |= TR_REC;
}
if (rt_config.enable_hal_profile()) {
tr_flag |= TR_HAL | TR_MPC;
tr_flag |= TR_REC;
}
initTrace(sctx->id(), tr_flag);
GET_TRACER(sctx)->getProfState()->clearRecords();
}
} // namespace spu