From 8a6f147593856f4709cb6bb30ce6419df8f31e04 Mon Sep 17 00:00:00 2001 From: luohang Date: Tue, 15 Sep 2020 10:29:36 +0000 Subject: [PATCH] [xpu] add XPU_L3_LOCK_REQUIRED to support multi-process run test=develop test=xpu --- lite/core/program.cc | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/lite/core/program.cc b/lite/core/program.cc index 14855e778d..5e684afe5b 100644 --- a/lite/core/program.cc +++ b/lite/core/program.cc @@ -23,6 +23,9 @@ #ifdef LITE_WITH_PRECISION_PROFILE #include "lite/core/profile/precision_profiler.h" #endif +#ifdef LITE_WITH_XPU +#include +#endif namespace paddle { namespace lite { @@ -439,7 +442,44 @@ void Instruction::Run() { CHECK(op_->CheckShape()); } - if (op_->run_once() && has_run_) { +#ifdef LITE_WITH_XPU + static int reentrant = 0; + int xpu_l3_lock_fd; + auto need_lock_l3 = std::getenv("XPU_L3_LOCK_REQUIRED"); + struct flock f_lock; + f_lock.l_whence = 0; + f_lock.l_len = 0; + + if (reentrant == 0) { + if (need_lock_l3) { + int pd = 0; // TODO(luohang): need get from lite api + std::string buf = "/opt/xpu_lock" + std::to_string(pd); + xpu_l3_lock_fd = open(buf.c_str(), O_RDWR); + CHECK(xpu_l3_lock_fd > 0) << "open " << buf + << " failed: " << xpu_l3_lock_fd; + + // lock + f_lock.l_type = F_WRLCK; + fcntl(xpu_l3_lock_fd, F_SETLKW, &f_lock); + } else { + xpu_l3_lock_fd = -1; + } + } + reentrant++; +#endif + + bool run_res = op_->run_once() && has_run_; + +#ifdef LITE_WITH_XPU + if (need_lock_l3 && reentrant == 1) { + f_lock.l_type = F_UNLCK; + fcntl(xpu_l3_lock_fd, F_SETLKW, &f_lock); + close(xpu_l3_lock_fd); + } + reentrant--; +#endif + + if (run_res) { return; } -- GitLab