提交 a9bc92c3 编写于 作者: L Leo Chen 提交者: Zeng Jinle

add c++ unique_name_generator, test=develop (#20871)

上级 d4289125
...@@ -187,6 +187,16 @@ TEST(test_tracer, test_trace_op_with_multi_device_inputs) { ...@@ -187,6 +187,16 @@ TEST(test_tracer, test_trace_op_with_multi_device_inputs) {
} }
} }
#endif #endif
TEST(test_tracer, test_unique_name_generator) {
// generate two unique names
imperative::Tracer tracer;
auto fc_1 = tracer.GenerateUniqueName("fc");
auto fc_2 = tracer.GenerateUniqueName("fc");
ASSERT_STREQ("fc_1", fc_1.c_str());
ASSERT_STREQ("fc_2", fc_2.c_str());
}
} // namespace imperative } // namespace imperative
} // namespace paddle } // namespace paddle
......
...@@ -29,13 +29,26 @@ ...@@ -29,13 +29,26 @@
namespace paddle { namespace paddle {
namespace imperative { namespace imperative {
class UniqueNameGenerator {
public:
explicit UniqueNameGenerator(std::string prefix = "") : prefix_(prefix) {}
std::string Generate(std::string key = "tmp") {
return prefix_ + key + "_" + std::to_string(++id_);
}
private:
std::atomic<int> id_{0};
std::string prefix_;
};
class Tracer { class Tracer {
DISABLE_COPY_AND_ASSIGN(Tracer); DISABLE_COPY_AND_ASSIGN(Tracer);
public: public:
Tracer() Tracer()
: engine_(new BasicEngine()), : engine_(new BasicEngine()),
program_desc_tracer_(new jit::ProgramDescTracer()) {} program_desc_tracer_(new jit::ProgramDescTracer()),
generator_(new UniqueNameGenerator()) {}
~Tracer() = default; ~Tracer() = default;
...@@ -64,6 +77,10 @@ class Tracer { ...@@ -64,6 +77,10 @@ class Tracer {
return program_desc_tracer_.get(); return program_desc_tracer_.get();
} }
std::string GenerateUniqueName(std::string key = "tmp") {
return generator_->Generate(key);
}
private: private:
static size_t GenerateUniqueId() { static size_t GenerateUniqueId() {
static std::atomic<size_t> id{0}; static std::atomic<size_t> id{0};
...@@ -74,6 +91,7 @@ class Tracer { ...@@ -74,6 +91,7 @@ class Tracer {
std::unique_ptr<Engine> engine_; std::unique_ptr<Engine> engine_;
std::unique_ptr<jit::ProgramDescTracer> program_desc_tracer_; std::unique_ptr<jit::ProgramDescTracer> program_desc_tracer_;
bool enable_program_desc_tracing_{false}; bool enable_program_desc_tracing_{false};
std::unique_ptr<UniqueNameGenerator> generator_;
}; };
} // namespace imperative } // namespace imperative
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册