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
+82 -87
View File
@@ -1,5 +1,5 @@
//Util.cpp
/**
* @file utils.cpp
*
@@ -10,8 +10,8 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "utils.h"
#include <sys/time.h>
#include "acl/acl.h"
#include <sys/time.h>
using namespace std;
extern bool g_isDevice;
extern bool f_isTXT;
@@ -19,14 +19,14 @@ extern int32_t device;
extern bool is_profi;
extern bool is_dump;
void* Utils::ReadBinFile(std::string fileName, uint32_t &fileSize)
void* Utils::ReadBinFile(std::string fileName, uint32_t& fileSize)
{
std::ifstream binFile(fileName, std::ifstream::binary);
if (binFile.is_open() == false) {
ERROR_LOG("open file %s failed", fileName.c_str());
return nullptr;
}
binFile.seekg(0, binFile.end);
uint32_t binFileBufferLen = binFile.tellg();
if (binFileBufferLen == 0) {
@@ -34,9 +34,9 @@ void* Utils::ReadBinFile(std::string fileName, uint32_t &fileSize)
binFile.close();
return nullptr;
}
binFile.seekg(0, binFile.beg);
void* binFileBufferData = nullptr;
aclError ret = ACL_ERROR_NONE;
if (!g_isDevice) {
@@ -54,14 +54,14 @@ void* Utils::ReadBinFile(std::string fileName, uint32_t &fileSize)
return nullptr;
}
}
binFile.read(static_cast<char *>(binFileBufferData), binFileBufferLen);
binFile.read(static_cast<char*>(binFileBufferData), binFileBufferLen);
binFile.close();
fileSize = binFileBufferLen;
return binFileBufferData;
}
void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t &fileSize)
void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t& fileSize)
{
uint32_t inputHostBuffSize = 0;
void* inputHostBuff = Utils::ReadBinFile(fileName, inputHostBuffSize);
@@ -69,7 +69,7 @@ void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t &fileSize)
return nullptr;
}
if (!g_isDevice) {
void *inBufferDev = nullptr;
void* inBufferDev = nullptr;
uint32_t inBufferSize = inputHostBuffSize;
aclError ret = aclrtMalloc(&inBufferDev, inBufferSize, ACL_MEM_MALLOC_NORMAL_ONLY);
if (ret != ACL_ERROR_NONE) {
@@ -77,7 +77,7 @@ void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t &fileSize)
aclrtFreeHost(inputHostBuff);
return nullptr;
}
ret = aclrtMemcpy(inBufferDev, inBufferSize, inputHostBuff, inputHostBuffSize, ACL_MEMCPY_HOST_TO_DEVICE);
if (ret != ACL_ERROR_NONE) {
ERROR_LOG("memcpy failed. device buffer size is %u, input host buffer size is %u",
@@ -94,15 +94,14 @@ void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t &fileSize)
return inputHostBuff;
}
}
void Utils::SplitString(std::string& s, std::vector<std::string>& v, char c)
{
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while(std::string::npos != pos2)
{
std::string s1 = s.substr(pos1, pos2-pos1);
while (std::string::npos != pos2) {
std::string s1 = s.substr(pos1, pos2 - pos1);
size_t n = s1.find_last_not_of(" \r\n\t");
if (n != string::npos) {
s1.erase(n + 1, s.size() - n);
@@ -115,8 +114,7 @@ void Utils::SplitString(std::string& s, std::vector<std::string>& v, char c)
pos1 = pos2 + 1;
pos2 = s.find(c, pos1);
}
if(pos1 != s.length())
{
if (pos1 != s.length()) {
std::string s1 = s.substr(pos1);
size_t n = s1.find_last_not_of(" \r\n\t");
if (n != string::npos) {
@@ -129,18 +127,16 @@ void Utils::SplitString(std::string& s, std::vector<std::string>& v, char c)
v.push_back(s1);
}
}
int Utils::str2num(char *str)
int Utils::str2num(char* str)
{
int n = 0;
int flag = 0;
while(*str >= '0' && *str <= '9')
{
n = n*10 + (*str - '0');
while (*str >= '0' && *str <= '9') {
n = n * 10 + (*str - '0');
str++;
}
if(flag == 1)
{
if (flag == 1) {
n = -n;
}
return n;
@@ -148,71 +144,70 @@ int Utils::str2num(char *str)
std::string Utils::modelName(string& s)
{
string::size_type position1,position2;
position1 = s.find_last_of("/");
if (position1 == s.npos)
{
position1 = 0;
}
position2 = s.find_last_of(".");
std::string modelName = s.substr(position1,position2-position1);
return modelName;
string::size_type position1, position2;
position1 = s.find_last_of("/");
if (position1 == s.npos) {
position1 = 0;
}
position2 = s.find_last_of(".");
std::string modelName = s.substr(position1, position2 - position1);
return modelName;
}
std::string Utils::TimeLine()
std::string Utils::TimeLine()
{
time_t currentTime = time(NULL);
char chCurrentTime[64];
strftime(chCurrentTime, sizeof(chCurrentTime), "%Y%m%d_%H%M%S", localtime(&currentTime));
std::string stCurrentTime = chCurrentTime;
return stCurrentTime;
time_t currentTime = time(NULL);
char chCurrentTime[64];
strftime(chCurrentTime, sizeof(chCurrentTime), "%Y%m%d_%H%M%S", localtime(&currentTime));
std::string stCurrentTime = chCurrentTime;
return stCurrentTime;
}
void Utils::printCurrentTime()
{
char szBuf[256] = {0};
struct timeval tv;
struct timezone tz;
struct tm *p;
char szBuf[256] = { 0 };
struct timeval tv;
struct timezone tz;
struct tm* p = nullptr;
gettimeofday(&tv, &tz);
p = localtime(&tv.tv_sec);
printf("%02d-%02d-%02d %02d:%02d:%02d.%06ld\n", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec);
}
void Utils::printHelpLetter()
{
cout<< endl;
cout<< "Usage:" << endl;
cout<< "generate offline model inference output file example:" << endl;
cout<< "./msame --model /home/HwHiAiUser/ljj/colorization.om --input /home/HwHiAiUser/ljj/colorization_input.bin --output /home/HwHiAiUser/ljj/AMEXEC/out/output1 --outfmt TXT --loop 2" << endl << endl;
cout << endl;
cout << "Usage:" << endl;
cout << "generate offline model inference output file example:" << endl;
cout << "./msame --model /home/HwHiAiUser/ljj/colorization.om --input /home/HwHiAiUser/ljj/colorization_input.bin --output /home/HwHiAiUser/ljj/AMEXEC/out/output1 --outfmt TXT --loop 2" << endl
<< endl;
cout<< "arguments explain:" << endl;
cout<< " --model Model file path" << endl;
cout<< " --input Input data path(only accept binary data file) If there are several file, please seprate by ','" << endl;
cout<< " --output Output path(User needs to have permission to create directories)" << endl;
cout<< " --outfmt Output file format (TXT or BIN)" << endl;
cout<< " --loop loop time(must in 1 to 100)" << endl;
cout<< " --dump Enable dump (true or false)(Do not support now)" << endl;
cout<< " --profiler Enable profiler (true or false)" << endl;
cout<< " --device Designated the device ID(must in 0 to 255)" << endl;
cout<< " --debug Debug switch,print model information (true or false)" << endl;
cout<< " --dymBatch dynamic batch (Do not support now)" << endl << endl << endl;
// cout<< "NOTECE: " << endl;
// cout<< " The order of parameter must follow in --model --input --output --outfmt --loop " << endl;
cout << "arguments explain:" << endl;
cout << " --model Model file path" << endl;
cout << " --input Input data path(only accept binary data file) If there are several file, please seprate by ','" << endl;
cout << " --output Output path(User needs to have permission to create directories)" << endl;
cout << " --outfmt Output file format (TXT or BIN)" << endl;
cout << " --loop loop time(must in 1 to 100)" << endl;
cout << " --dump Enable dump (true or false)(Do not support now)" << endl;
cout << " --profiler Enable profiler (true or false)" << endl;
cout << " --device Designated the device ID(must in 0 to 255)" << endl;
cout << " --debug Debug switch,print model information (true or false)" << endl;
cout << " --dymBatch dynamic batch (Do not support now)" << endl
<< endl
<< endl;
// cout<< "NOTECE: " << endl;
// cout<< " The order of parameter must follow in --model --input --output --outfmt --loop " << endl;
}
double Utils::printDiffTime(time_t begin, time_t end)
{
double diffT = difftime(begin, end);
printf("The inference time is: %f millisecond\n", 1000*diffT);
printf("The inference time is: %f millisecond\n", 1000 * diffT);
return diffT * 1000;
}
double Utils::InferenceTimeAverage(double *x, int len)
double Utils::InferenceTimeAverage(double* x, int len)
{
double sum = 0;
for (int i = 0; i < len; i++)
@@ -220,20 +215,20 @@ double Utils::InferenceTimeAverage(double *x, int len)
return sum / len;
}
double Utils::InferenceTimeAverageWithoutFirst(double *x, int len)
double Utils::InferenceTimeAverageWithoutFirst(double* x, int len)
{
double sum = 0;
for (int i = 0; i < len; i++)
if (i !=0){
sum += x[i];
}
if (i != 0) {
sum += x[i];
}
return sum / (len - 1);
}
void Utils::ProfilerJson(bool isprof, map<char,string>& params)
void Utils::ProfilerJson(bool isprof, map<char, string>& params)
{
if (isprof){
if (isprof) {
std::string out_path = params['o'].c_str();
std::string out_profiler_path = out_path + "/profiler";
ofstream outstr("acl.json", ios::out);
@@ -243,15 +238,15 @@ void Utils::ProfilerJson(bool isprof, map<char,string>& params)
//outstr << "\"ai_core_metrics\": \"aicorePipelineStall\"}\n}";
//outstr <<"}]n}";
outstr.close();
//mkdir profiler output dir
const char* temp_s = out_path.c_str();
if (NULL == opendir(temp_s)){
mkdir(temp_s,0775);
if (NULL == opendir(temp_s)) {
mkdir(temp_s, 0775);
}
const char* temp_s1 = out_profiler_path.c_str();
if (NULL == opendir(temp_s1)){
mkdir(temp_s1,0775);
if (NULL == opendir(temp_s1)) {
mkdir(temp_s1, 0775);
}
/*{
"profiler": {
@@ -265,9 +260,9 @@ void Utils::ProfilerJson(bool isprof, map<char,string>& params)
}
}
void Utils::DumpJson(bool isdump, map<char,string>& params)
void Utils::DumpJson(bool isdump, map<char, string>& params)
{
if (is_dump){
if (is_dump) {
std::string modelPath = params['m'].c_str();
std::string modelName = Utils::modelName(modelPath);
std::string out_path = params['o'].c_str();
@@ -279,20 +274,20 @@ void Utils::DumpJson(bool isdump, map<char,string>& params)
outstr << " \"model_name\": \"" << modelName << "\",\n }]\n";
outstr << " }\n}";
outstr.close();
//mkdir dump output dir
const char* temp_s = out_path.c_str();
if (NULL == opendir(temp_s)){
mkdir(temp_s,0775);
if (NULL == opendir(temp_s)) {
mkdir(temp_s, 0775);
}
const char* temp_s1 = out_dump_path.c_str();
if (NULL == opendir(temp_s1)){
mkdir(temp_s1,0775);
if (NULL == opendir(temp_s1)) {
mkdir(temp_s1, 0775);
}
// {
// "dump": {
// "dump_path": "output_path",
// "dump_mode": "output",
// "dump_mode": "output",
// "dump_list": [{
// "model_name": "model_name",
// }]