Skip to content

Commit 3a226bd

Browse files
wanglingfwanglingfeng
andauthored
[Feature](gtest): Add mluOpGetGenCaseDirectory/mluOpSetGenCaseDirectory funcs. (#1216)
Co-authored-by: wanglingfeng <[email protected]>
1 parent ae147d8 commit 3a226bd

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

core/gen_case.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,41 @@ void genCaseModeSet(int mode) {
168168
LOG(INFO) << "[gen_case] Set GEN_CASE mode to " << mode << ".";
169169
}
170170

171+
std::string genCaseConfig::replacePlaceholder(std::string pathStr) {
172+
size_t pos = pathStr.find("{pid}");
173+
if (pos != std::string::npos) {
174+
pathStr.replace(pos, 5, "pid" + std::to_string(syscall(SYS_getpid)));
175+
}
176+
pos = pathStr.find("{deviceid}");
177+
if (pos != std::string::npos) {
178+
int dev_index = -1;
179+
cnrtRet_t ret = cnrtGetDevice(&dev_index);
180+
if (ret != cnrtSuccess || dev_index < 0) {
181+
LOG(ERROR) << "get device ID failed.";
182+
}
183+
pathStr.replace(pos, 10, "deviceid" + std::to_string(dev_index));
184+
}
185+
return pathStr;
186+
}
187+
188+
void genCaseConfig::setDirectory(const char *path) {
189+
std::string pathStr = std::string(path);
190+
gen_case_dir_ = pathStr;
191+
}
192+
193+
std::string genCaseConfig::getDirectory() {
194+
if (gen_case_dir_ != "") {
195+
std::string pathStr = genCaseConfig::replacePlaceholder(gen_case_dir_);
196+
return pathStr;
197+
} else {
198+
char current_dir[PATH_MAX];
199+
if (getcwd(current_dir, sizeof(current_dir)) == NULL) {
200+
return "";
201+
}
202+
return std::string(current_dir);
203+
}
204+
}
205+
171206
// TO DO: can use regex and global variable for efficiency
172207
inline int getOpNameMask(const std::string op_name_,
173208
const std::string op_name) {
@@ -454,6 +489,7 @@ std::string PbNode::getFolderName() {
454489
folder_name = current_dir;
455490
}
456491

492+
folder_name = genCaseConfig::replacePlaceholder(folder_name);
457493
folder_name = folder_name + "/gen_case/" + op_name;
458494
return folder_name;
459495
}
@@ -787,3 +823,51 @@ std::string descToString(mluOpTensorDescriptor_t desc, char delimiter) {
787823
void MLUOP_WIN_API mluOpSetGenCaseMode(int mode) {
788824
mluop::gen_case::genCaseModeSet(mode);
789825
}
826+
827+
mluOpStatus_t MLUOP_WIN_API mluOpSetGenCaseDirectory(const char *path) {
828+
PARAM_CHECK("[mluOpSetGenCaseDirectory]", path != NULL);
829+
mluop::gen_case::genCaseConfig::setDirectory(path);
830+
return MLUOP_STATUS_SUCCESS;
831+
}
832+
833+
const char MLUOP_WIN_API *mluOpGetGenCaseDirectory(char *buffer,
834+
size_t bufferSize,
835+
size_t *pathLen,
836+
mluOpStatus_t *status) {
837+
std::string ret = mluop::gen_case::genCaseConfig::getDirectory();
838+
if (ret == "") {
839+
LOG(ERROR) << "[mluOpGetGenCaseDirectory] getting directory failed!";
840+
mluop::gen_case::genCaseConfig::setValue(pathLen, (size_t)0);
841+
mluop::gen_case::genCaseConfig::setValue(status,
842+
MLUOP_STATUS_INTERNAL_ERROR);
843+
return nullptr;
844+
}
845+
if (ret.size() >= bufferSize && buffer != nullptr) {
846+
LOG(ERROR) << "[mluOpGetGenCaseDirectory] buffer size is too small to "
847+
"store the gen case directory.";
848+
mluop::gen_case::genCaseConfig::setValue(pathLen, (size_t)0);
849+
mluop::gen_case::genCaseConfig::setValue(status, MLUOP_STATUS_BAD_PARAM);
850+
return buffer;
851+
}
852+
if (buffer == nullptr) {
853+
char *newBuffer = (char *)malloc(ret.size() + 1);
854+
if (newBuffer == nullptr) {
855+
LOG(ERROR) << "[mluOpGetGenCaseDirectory] malloc failed.";
856+
mluop::gen_case::genCaseConfig::setValue(pathLen, (size_t)0);
857+
mluop::gen_case::genCaseConfig::setValue(status,
858+
MLUOP_STATUS_ALLOC_FAILED);
859+
return nullptr;
860+
}
861+
strncpy(newBuffer, ret.c_str(), ret.size());
862+
newBuffer[ret.size()] = '\0';
863+
mluop::gen_case::genCaseConfig::setValue(pathLen, ret.size());
864+
mluop::gen_case::genCaseConfig::setValue(status, MLUOP_STATUS_SUCCESS);
865+
return newBuffer;
866+
} else {
867+
strncpy(buffer, ret.c_str(), ret.size());
868+
buffer[ret.size()] = '\0';
869+
mluop::gen_case::genCaseConfig::setValue(pathLen, ret.size());
870+
mluop::gen_case::genCaseConfig::setValue(status, MLUOP_STATUS_SUCCESS);
871+
return buffer;
872+
}
873+
}

core/gen_case.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,19 @@ class PbNode {
521521
void debugTensorAddress();
522522
};
523523

524+
class genCaseConfig {
525+
public:
526+
static void setDirectory(const char *name);
527+
static std::string getDirectory();
528+
static std::string replacePlaceholder(std::string pathStr);
529+
template <typename T>
530+
static inline void setValue(T *pointer, T value) {
531+
if (pointer != nullptr) {
532+
*pointer = value;
533+
}
534+
}
535+
};
536+
524537
template <>
525538
inline void PbNode::appendOpParam<std::string>(std::string param_name,
526539
std::string param_value,

docs/user_guide/11_env_var/index.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ MLUOP_GEN_CASE
5454

5555
更详细请参见 `MLU-OPS™ GEN_CASE 使用指南 <https://github.com/Cambricon/mlu-ops/blob/master/docs/Gencase-User-Guide-zh.md>`_ 。
5656

57+
.. _MLUOP_GEN_CASE_DIR:
58+
59+
MLUOP_GEN_CASE_DIR
60+
######################
61+
62+
**功能描述**
63+
64+
用于指定 :ref:`MLUOP_GEN_CASE` 功能生成的目录所在的根目录。默认在当前目录下生成。
65+
66+
**使用方法**
67+
68+
- export MLUOP_GEN_CASE_DIR={path}: 通过环境变量设置,只在初始化阶段生效。
69+
70+
- 在程序运行过程中也可以调用 ``mluOpSetGenCaseDirectory()`` 和 ``mluOpGetGenCaseDirectory()`` 来动态设置和获取此环境变量的值。更多内容,参见《Cambricon MLU-OPS Developer Guide》。
71+
5772
.. _MLUOP_MIN_VLOG_LEVEL:
5873

5974
MLUOP_MIN_VLOG_LEVEL

mlu_op.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13221,6 +13221,67 @@ mluOpSyncBatchNormBackwardElemtV2(mluOpHandle_t handle,
1322113221
void MLUOP_WIN_API
1322213222
mluOpSetGenCaseMode(int mode);
1322313223

13224+
// Group:Common Interface
13225+
// Subgroup:Debugging
13226+
/*!
13227+
* @brief Sets the gen case directory name through the \p path parameter. For more
13228+
* information, see "Cambricon MLU-OPS User Guide".
13229+
*
13230+
* @param[in] path
13231+
* Input. A NULL-terminated character string used to specify the directory name.
13232+
*
13233+
* @par Return
13234+
* - ::MLUOP_STATUS_SUCCESS, ::MLUOP_STATUS_BAD_PARAM
13235+
*
13236+
* @note
13237+
* - The settings of this API are valid for all threads.
13238+
*
13239+
* @par Requirements
13240+
* - None.
13241+
*
13242+
* @par Example
13243+
* - None.
13244+
*/
13245+
mluOpStatus_t MLUOP_WIN_API
13246+
mluOpSetGenCaseDirectory(const char *path);
13247+
13248+
// Group:Common Interface
13249+
// Subgroup:Debugging
13250+
/*!
13251+
* @brief Gets the gen case directory name. For more information, see "Cambricon MLU-OPS User Guide".
13252+
*
13253+
* @param[in] buffer
13254+
* Input. This is a buffer used to store the directory name. If \p buffer is NULL,
13255+
* the directory name is placed in a region of memory allocated with malloc.
13256+
*
13257+
* @param[in] bufferSize
13258+
* Input. The size of the buffer.
13259+
*
13260+
* @param[out] pathLen
13261+
* Output. If \p pathLen is not NULL, the length of directory name is placed in *pathLen.
13262+
*
13263+
* @param[out] status
13264+
* Output. If \p status is not NULL, the execution status of this API is placed in *status.
13265+
* It includes ::MLUOP_STATUS_SUCCESS, ::MLUOP_STATUS_BAD_PARAM, ::MLUOP_STATUS_ALLOC_FAILED,
13266+
* ::MLUOP_STATUS_INTERNAL_ERROR.
13267+
*
13268+
* @par Return
13269+
* - A pointer to the start of the NULL-terminated directory name, or NULL if get
13270+
* directory name failed. The API caller is responsible for deallocating this memory by using
13271+
* the free() function.
13272+
*
13273+
* @note
13274+
* - None.
13275+
*
13276+
* @par Requirements
13277+
* - None.
13278+
*
13279+
* @par Example
13280+
* - None.
13281+
*/
13282+
const char MLUOP_WIN_API *
13283+
mluOpGetGenCaseDirectory(char *buffer, size_t bufferSize, size_t *pathLen, mluOpStatus_t *status);
13284+
1322413285
// Group: DeformConv
1322513286
/*!
1322613287
* @brief Creates a descriptor pointed by \p dcn_desc for a deformable convolution forward

0 commit comments

Comments
 (0)