This commit is contained in:
Saturneic 2019-01-14 21:25:56 +08:00
parent 5f0d9e17ef
commit df81887b43
5 changed files with 89 additions and 11 deletions

View File

@ -16,9 +16,11 @@
@param name @param name
@param ffresh @param ffresh
*/ */
CPart::CPart(string src_path,string src_name,string name,bool ffresh){ CPart::CPart(string src_path,string src_name,string name,bool ffresh):func(nullptr),handle(nullptr),libargs_in(nullptr),libargs_out(nullptr){
this->src_path = src_path; this->src_path = src_path;
this->name = name; this->name = name;
this->src_name = src_name;
// 去掉源文件的后缀 // 去掉源文件的后缀
unsigned long qp = src_name.find(".",0); unsigned long qp = src_name.find(".",0);
if(qp == string::npos){ if(qp == string::npos){
@ -26,12 +28,12 @@ CPart::CPart(string src_path,string src_name,string name,bool ffresh){
} }
// 生成lib文件的文件名 // 生成lib文件的文件名
string t_libname = "lib"+src_name.substr(0,qp)+".so"; string t_libname = "lib"+src_name.substr(0,qp)+".so";
// 如果lib文件存在且不要求每次建立该结构都重新编译一次源文件的话就不执行编译
if(!~access(("Libs/"+t_libname).data(), F_OK) || ffresh)
BuildSo();
// 记录必要信息
this->libname = t_libname; this->libname = t_libname;
this->src_name = src_name; // 如果lib文件存在且不要求每次建立该结构都重新编译一次源文件的话就不执行编译
if(!~access(("Libs/"+t_libname).data(), F_OK) || ffresh){
BuildSo();
GetSo();
}
} }
@ -104,6 +106,7 @@ void CPart::setArgsType(vector<int> fargs_in, vector<int> fargs_out){
@return SUCCESS @return SUCCESS
*/ */
int CPart::Run(void){ int CPart::Run(void){
if(func == nullptr) throw "func is nullptr";
// 对计算模块传入参数 // 对计算模块传入参数
unsigned long count = fargs_in.size()-1; unsigned long count = fargs_in.size()-1;
for(auto k = args_in.rbegin(); k != args_in.rend();k++,count--){ for(auto k = args_in.rbegin(); k != args_in.rend();k++,count--){

View File

@ -104,7 +104,6 @@ public:
static void addArg(vector<void *> *args,T value){ static void addArg(vector<void *> *args,T value){
T *p_value = new T(value); T *p_value = new T(value);
if(p_value == nullptr) throw "fail to malloc"; if(p_value == nullptr) throw "fail to malloc";
*p_value = value;
args->push_back(p_value); args->push_back(p_value);
} }
// 一般由lib文件中的计算模块调用的从vector中获得参数并释放其占用的内存空间而后返回相关值 // 一般由lib文件中的计算模块调用的从vector中获得参数并释放其占用的内存空间而后返回相关值

View File

@ -9,32 +9,95 @@
#include "cthread.h" #include "cthread.h"
CThread::CThread(CMap *tp_map):p_map(tp_map){ CThread::CThread(CMap *tp_map):p_map(tp_map){
// 构造空的传入与传出参数列表
for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){ for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){
vector<void *> args,args_out; vector<void *> args,args_out;
rargs.insert(pair<string,vector<void *>>((*k).first,args)); rargs.insert(pair<string,vector<void *>>((*k).first,args));
rargs_out.insert(pair<string,vector<void *>>((*k).first,args_out)); rargs_out.insert(pair<string,vector<void *>>((*k).first,args_out));
} }
// 构造任务进度列表
for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){ for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){
ifsolved.insert(pair<string,bool>((*k).first,false)); ifsolved.insert(pair<string,bool>((*k).first,false));
} }
} }
CThread::~CThread(){
for(auto item = rargs.begin(); item != rargs.end(); item++){
int count = 0;
vector<int> fargs = p_map->cparts.find(item->first)->second->fargs_in;
for(auto litem = item->second.begin(); litem != item->second.end(); litem++,count++){
if(fargs[count] == INT){
delete (int *)(*litem);
}
else if (fargs[count] == DOUBLE){
delete (double *)(*litem);
}
}
}
for(auto item = rargs_out.begin(); item != rargs_out.end(); item++){
int count = 0;
vector<int> fargs = p_map->cparts.find(item->first)->second->fargs_out;
for(auto litem = item->second.begin(); litem != item->second.end(); litem++,count++){
if((*litem) != nullptr){
if(fargs[count] == INT){
delete (int *)(*litem);
}
else if (fargs[count] == DOUBLE){
delete (double *)(*litem);
}
}
}
}
}
void CThread::Analyse(void){ void CThread::Analyse(void){
for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){ for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){
auto cpart_depends = (*k).second->depends; auto cpart_depends = (*k).second->depends;
// 如果该计算模块含有依赖模块
if(cpart_depends.size()){ if(cpart_depends.size()){
bool if_ok = true; bool if_ok = true;
for(auto ditem = cpart_depends.begin(); ditem != cpart_depends.end(); ditem++){ for(auto ditem = cpart_depends.begin(); ditem != cpart_depends.end(); ditem++){
string name = ditem->t_cpart->name; string name = ditem->t_cpart->name;
// 如果依赖模块还没有被调用过
if(!(ifsolved.find(name)->second)){ if(!(ifsolved.find(name)->second)){
if_ok = false; if_ok = false;
break;
} }
} }
if(if_ok) line.push_back((*k).second); if(if_ok){
int count = 0;
vector<int> s_fargs_in = k->second->fargs_in;
for(auto ditem = cpart_depends.begin(); ditem != cpart_depends.end(); ditem++){
vector<int> args = ditem->args;
// 输入参数列表
vector<void *> &args_in = rargs.find(k->second->name)->second;
// 输出形式参数列表
vector<int> f_fargs_out = ditem->t_cpart->fargs_out;
// 输出参数列表
vector<void *> args_out = rargs_out.find(ditem->t_cpart->name)->second;
// 检查传入传出参数的类型是否匹配
for(auto itm = args.begin(); itm != args.end();itm++){
if(s_fargs_in[count++] != f_fargs_out[*itm]) throw "type conflict";
// 共用内存
if(f_fargs_out[*itm] == INT){
CPart::addArg<int>(&args_in, *((int *)(args_out[*itm])));
}
else if(f_fargs_out[*itm] == DOUBLE){
CPart::addArg<double>(&args_in, *((double *)(args_out[*itm])));
}
}
}
line.push_back((*k).second);
}
} }
// 如果该计算模块没有依赖模块
else{ else{
string name = (*k).second->name; string name = (*k).second->name;
if(rargs.find(k->second->name)->second.size() == k->second->fargs_in.size()){ if(rargs.find(k->second->name)->second.size() == k->second->fargs_in.size()){
// 如果该模块还没有被调用
if(ifsolved.find(name)->second == false){ if(ifsolved.find(name)->second == false){
line.push_back(k->second); line.push_back(k->second);
} }
@ -53,8 +116,10 @@ void CThread::DoLine(void){
vector<int> fargs = (*pcp)->fargs_in; vector<int> fargs = (*pcp)->fargs_in;
vector<int> fargs_out = (*pcp)->fargs_out; vector<int> fargs_out = (*pcp)->fargs_out;
vector<void *> &argso = (*pcp)->args_out; vector<void *> &argso = (*pcp)->args_out;
// 清空调用数据
(*pcp)->Clear(); (*pcp)->Clear();
int cout = 0; int cout = 0;
// 传入输入参数
for(auto arg = args.begin(); arg != args.end(); arg++,cout++){ for(auto arg = args.begin(); arg != args.end(); arg++,cout++){
if(fargs[cout] == INT){ if(fargs[cout] == INT){
(*pcp)->addArgsIn<int>(*((int *)(*arg))); (*pcp)->addArgsIn<int>(*((int *)(*arg)));
@ -63,9 +128,11 @@ void CThread::DoLine(void){
(*pcp)->addArgsIn<double>(*((double *)(*arg))); (*pcp)->addArgsIn<double>(*((double *)(*arg)));
} }
} }
// 调用计算模块
if(!(*pcp)->Run()){ if(!(*pcp)->Run()){
ifsolved.find(name)->second = true; ifsolved.find(name)->second = true;
int cout = 0; int cout = 0;
// 处理输出
for(auto argo = argso.begin(); argo != argso.end(); argo++,cout++){ for(auto argo = argso.begin(); argo != argso.end(); argo++,cout++){
if(fargs_out[cout] == INT){ if(fargs_out[cout] == INT){
int *p_value = new int(*((int *)(*argo))); int *p_value = new int(*((int *)(*argo)));
@ -78,4 +145,5 @@ void CThread::DoLine(void){
} }
} }
} }
line.clear();
} }

View File

@ -31,6 +31,7 @@ public:
map<string,bool> ifsolved; map<string,bool> ifsolved;
// 使用图结构管理结构来构造计算进程管理结构 // 使用图结构管理结构来构造计算进程管理结构
CThread(CMap *tp_map); CThread(CMap *tp_map);
~CThread();
template<class T> template<class T>
// 添加相关计算模块的传入参数 // 添加相关计算模块的传入参数
void AddArgs(string name, T value){ void AddArgs(string name, T value){

13
net.cpp
View File

@ -8,7 +8,8 @@
#include "net.h" #include "net.h"
#include "cpart.h" #include "cpart.h"
#include "cmap.h"
#include "cthread.h"
@ -16,10 +17,16 @@ int main(void){
CMap map("./PCS"); CMap map("./PCS");
CThread thread(&map); CThread thread(&map);
thread.AddArgs<int>("B", 4); thread.AddArgs<int>("B", 4);
thread.AddArgs<double>("B", 8); thread.AddArgs<double>("B", 9.0);
thread.AddArgs<int>("C", 1.0);
thread.AddArgs<double>("C", 3.0);
thread.Analyse(); thread.Analyse();
thread.DoLine(); thread.DoLine();
cout<<thread.rargs_out.find("B")->second.size()<<endl; thread.Analyse();
thread.DoLine();
thread.Analyse();
thread.DoLine();
cout<<*((int *)(thread.rargs_out.find("A")->second)[0])<<endl;
return 0; return 0;
} }