clean code

This commit is contained in:
liuchengju
2020-09-01 11:51:28 +08:00
parent bea100c789
commit 7d654d22cb
44 changed files with 4486 additions and 724 deletions
Executable → Regular
+34 -81
View File
@@ -1,5 +1,5 @@
//Sample_process.cpp
/**
* @file sample_process.cpp
*
@@ -10,8 +10,8 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "sample_process.h"
#include "model_process.h"
#include "acl/acl.h"
#include "model_process.h"
#include "utils.h"
using namespace std;
extern bool g_isDevice;
@@ -22,42 +22,34 @@ extern bool is_debug;
extern bool is_profi;
extern bool is_dump;
SampleProcess::SampleProcess() :deviceId_(0), context_(nullptr), stream_(nullptr)
SampleProcess::SampleProcess()
: deviceId_(0)
, context_(nullptr)
, stream_(nullptr)
{
}
SampleProcess::~SampleProcess()
{
DestroyResource();
}
Result SampleProcess::InitResource()
{
// ACL init
aclError ret;
const char *aclConfigPath = "acl.json";
if (is_profi == true){
const char* aclConfigPath = "acl.json";
if (is_profi == true) {
ret = aclInit(aclConfigPath);
} else
{
} else {
ret = aclInit(nullptr);
}
if (ret != ACL_ERROR_NONE) {
/* if (is_debug || is_profi){
if (remove("acl.json")==0){
INFO_LOG("delete acl.json success");
}
else{
ERROR_LOG("delete acl.json failed");
}
}
*/
ERROR_LOG("acl init failed");
return FAILED;
}
}
INFO_LOG("acl init success");
// open device
deviceId_ = device;
ret = aclrtSetDevice(deviceId_);
@@ -66,7 +58,7 @@ Result SampleProcess::InitResource()
return FAILED;
}
INFO_LOG("open device %d success", deviceId_);
// create context (set current)
ret = aclrtCreateContext(&context_, deviceId_);
if (ret != ACL_ERROR_NONE) {
@@ -74,7 +66,7 @@ Result SampleProcess::InitResource()
return FAILED;
}
INFO_LOG("create context success");
// create stream
ret = aclrtCreateStream(&stream_);
if (ret != ACL_ERROR_NONE) {
@@ -82,7 +74,7 @@ Result SampleProcess::InitResource()
return FAILED;
}
INFO_LOG("create stream success");
// get run mode
aclrtRunMode runMode;
ret = aclrtGetRunMode(&runMode);
@@ -93,32 +85,22 @@ Result SampleProcess::InitResource()
g_isDevice = (runMode == ACL_DEVICE);
INFO_LOG("get run mode success");
/* // dump init
if (is_dump == true){
ret = aclmdlInitDump();
if (ret != ACL_ERROR_NONE) {
ERROR_LOG("dump init failed");
return FAILED;
}
INFO_LOG("dump init success");
}
*/
return SUCCESS;
}
Result SampleProcess::Process(map<char,string>& params, vector<string>& input_files)
Result SampleProcess::Process(map<char, string>& params, vector<string>& input_files)
{
// model init
ModelProcess processModel;
const char* omModelPath = params['m'].c_str();
const std::string& omModelPath = params['m'];
std::string output_path = params['o'].c_str();
const char* outfmt = params['f'].c_str();
const char* fmt_TXT = "TXT";
f_isTXT = (strcmp(outfmt,fmt_TXT)==0);
f_isTXT = (strcmp(outfmt, fmt_TXT) == 0);
std::string modelPath = params['m'].c_str();
std::string modelName = Utils::modelName(modelPath);
struct timeval begin;
struct timeval end;
double inference_time[loop];
@@ -127,24 +109,14 @@ Result SampleProcess::Process(map<char,string>& params, vector<string>& input_fi
ERROR_LOG("load model from file failed");
return FAILED;
}
ret = processModel.CreateDesc();
if (ret != SUCCESS) {
ERROR_LOG("create model description failed");
return FAILED;
}
/* // dump init
if (is_dump){
const char *aclConfigPath = "acl.json";
ret = aclmdlSetDump(aclConfigPath);
if (ret != SUCCESS) {
ERROR_LOG("dump init failed");
return FAILED;
}
INFO_LOG("dump init success");
}
*/
if (is_debug){
if (is_debug) {
ret = processModel.PrintDesc();
if (ret != SUCCESS) {
ERROR_LOG("print model descrtption failed");
@@ -156,16 +128,15 @@ Result SampleProcess::Process(map<char,string>& params, vector<string>& input_fi
ERROR_LOG("create model output failed");
return FAILED;
}
if (input_files.empty() == 1) {
ret = processModel.CreateZeroInput();
if (ret != SUCCESS) {
ERROR_LOG("model create input failed");
return FAILED;
}
}
else {
vector < void* > picDevBuffer(input_files.size(), nullptr);
} else {
vector<void*> picDevBuffer(input_files.size(), nullptr);
for (size_t index = 0; index < input_files.size(); ++index) {
INFO_LOG("start to process file:%s", input_files[index].c_str());
// model process
@@ -193,9 +164,6 @@ Result SampleProcess::Process(map<char,string>& params, vector<string>& input_fi
std::cout << "Inference time: " << inference_time[t] << "ms" << endl;
if (ret != SUCCESS) {
ERROR_LOG("model execute failed");
// for (size_t i = 0; i < input_files.size(); i++) {
// aclrtFree(picDevBuffer[i]);
// }
return FAILED;
}
processModel.OutputModelResult(output_path, modelName, t);
@@ -208,23 +176,17 @@ Result SampleProcess::Process(map<char,string>& params, vector<string>& input_fi
}
processModel.DestroyInput();
if (is_dump || is_profi){
if (remove("acl.json")==0){
if (is_dump || is_profi) {
if (remove("acl.json") == 0) {
INFO_LOG("delete acl.json success");
}
else{
} else {
ERROR_LOG("delete acl.json failed");
}
}
// release model input buffer
// for (size_t i = 0; i < input_files.size(); i++) {
// aclrtFree(picDevBuffer[i]);
// }
return SUCCESS;
}
void SampleProcess::DestroyResource()
{
aclError ret;
@@ -236,7 +198,7 @@ void SampleProcess::DestroyResource()
stream_ = nullptr;
}
INFO_LOG("end to destroy stream");
if (context_ != nullptr) {
ret = aclrtDestroyContext(context_);
if (ret != ACL_ERROR_NONE) {
@@ -245,25 +207,16 @@ void SampleProcess::DestroyResource()
context_ = nullptr;
}
INFO_LOG("end to destroy context");
ret = aclrtResetDevice(deviceId_);
if (ret != ACL_ERROR_NONE) {
ERROR_LOG("reset device failed");
}
INFO_LOG("end to reset device is %d", deviceId_);
/* if (is_dump){
ret = aclmdlFinalizeDump();
if (ret != ACL_ERROR_NONE) {
ERROR_LOG("finalize dump failed");
}
}
INFO_LOG("end to finalize dump");
*/
ret = aclFinalize();
if (ret != ACL_ERROR_NONE) {
ERROR_LOG("finalize acl failed");
}
INFO_LOG("end to finalize acl");
}