-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathcontext.cc
70 lines (57 loc) · 2.02 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
// 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 "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) {}
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