diff --git a/cpart.cpp b/cpart.cpp index ee02053..a3d16c8 100644 --- a/cpart.cpp +++ b/cpart.cpp @@ -16,9 +16,11 @@ @param name 计算模块的名字 @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->name = name; + + this->src_name = src_name; // 去掉源文件的后缀 unsigned long qp = src_name.find(".",0); if(qp == string::npos){ @@ -26,12 +28,12 @@ CPart::CPart(string src_path,string src_name,string name,bool ffresh){ } // 生成lib文件的文件名 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->src_name = src_name; +// 如果lib文件存在且不要求每次建立该结构都重新编译一次源文件的话就不执行编译 + if(!~access(("Libs/"+t_libname).data(), F_OK) || ffresh){ + BuildSo(); + GetSo(); + } } @@ -104,6 +106,7 @@ void CPart::setArgsType(vector fargs_in, vector fargs_out){ @return 如果执行成功则返回SUCCESS */ int CPart::Run(void){ + if(func == nullptr) throw "func is nullptr"; // 对计算模块传入参数 unsigned long count = fargs_in.size()-1; for(auto k = args_in.rbegin(); k != args_in.rend();k++,count--){ diff --git a/cpart.h b/cpart.h index ef936d8..19f8f95 100644 --- a/cpart.h +++ b/cpart.h @@ -104,7 +104,6 @@ public: static void addArg(vector *args,T value){ T *p_value = new T(value); if(p_value == nullptr) throw "fail to malloc"; - *p_value = value; args->push_back(p_value); } // 一般由lib文件中的计算模块调用的从vector中获得参数并释放其占用的内存空间而后返回相关值 diff --git a/cthread.cpp b/cthread.cpp index 320bf4e..c722457 100644 --- a/cthread.cpp +++ b/cthread.cpp @@ -9,32 +9,95 @@ #include "cthread.h" CThread::CThread(CMap *tp_map):p_map(tp_map){ +// 构造空的传入与传出参数列表 for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){ vector args,args_out; rargs.insert(pair>((*k).first,args)); rargs_out.insert(pair>((*k).first,args_out)); } +// 构造任务进度列表 for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){ ifsolved.insert(pair((*k).first,false)); } } +CThread::~CThread(){ + for(auto item = rargs.begin(); item != rargs.end(); item++){ + int count = 0; + vector 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 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){ for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){ auto cpart_depends = (*k).second->depends; +// 如果该计算模块含有依赖模块 if(cpart_depends.size()){ bool if_ok = true; for(auto ditem = cpart_depends.begin(); ditem != cpart_depends.end(); ditem++){ string name = ditem->t_cpart->name; +// 如果依赖模块还没有被调用过 if(!(ifsolved.find(name)->second)){ if_ok = false; + break; } } - if(if_ok) line.push_back((*k).second); + if(if_ok){ + int count = 0; + vector s_fargs_in = k->second->fargs_in; + for(auto ditem = cpart_depends.begin(); ditem != cpart_depends.end(); ditem++){ + vector args = ditem->args; +// 输入参数列表 + vector &args_in = rargs.find(k->second->name)->second; +// 输出形式参数列表 + vector f_fargs_out = ditem->t_cpart->fargs_out; +// 输出参数列表 + vector 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(&args_in, *((int *)(args_out[*itm]))); + } + else if(f_fargs_out[*itm] == DOUBLE){ + CPart::addArg(&args_in, *((double *)(args_out[*itm]))); + } + } + + } + line.push_back((*k).second); + } } +// 如果该计算模块没有依赖模块 else{ string name = (*k).second->name; if(rargs.find(k->second->name)->second.size() == k->second->fargs_in.size()){ +// 如果该模块还没有被调用 if(ifsolved.find(name)->second == false){ line.push_back(k->second); } @@ -53,8 +116,10 @@ void CThread::DoLine(void){ vector fargs = (*pcp)->fargs_in; vector fargs_out = (*pcp)->fargs_out; vector &argso = (*pcp)->args_out; +// 清空调用数据 (*pcp)->Clear(); int cout = 0; +// 传入输入参数 for(auto arg = args.begin(); arg != args.end(); arg++,cout++){ if(fargs[cout] == INT){ (*pcp)->addArgsIn(*((int *)(*arg))); @@ -63,9 +128,11 @@ void CThread::DoLine(void){ (*pcp)->addArgsIn(*((double *)(*arg))); } } +// 调用计算模块 if(!(*pcp)->Run()){ ifsolved.find(name)->second = true; int cout = 0; +// 处理输出 for(auto argo = argso.begin(); argo != argso.end(); argo++,cout++){ if(fargs_out[cout] == INT){ int *p_value = new int(*((int *)(*argo))); @@ -78,4 +145,5 @@ void CThread::DoLine(void){ } } } + line.clear(); } diff --git a/cthread.h b/cthread.h index af4444b..a108584 100644 --- a/cthread.h +++ b/cthread.h @@ -31,6 +31,7 @@ public: map ifsolved; // 使用图结构管理结构来构造计算进程管理结构 CThread(CMap *tp_map); + ~CThread(); template // 添加相关计算模块的传入参数 void AddArgs(string name, T value){ diff --git a/net.cpp b/net.cpp index 3a238aa..8923786 100644 --- a/net.cpp +++ b/net.cpp @@ -8,7 +8,8 @@ #include "net.h" #include "cpart.h" - +#include "cmap.h" +#include "cthread.h" @@ -16,10 +17,16 @@ int main(void){ CMap map("./PCS"); CThread thread(&map); thread.AddArgs("B", 4); - thread.AddArgs("B", 8); + thread.AddArgs("B", 9.0); + thread.AddArgs("C", 1.0); + thread.AddArgs("C", 3.0); thread.Analyse(); thread.DoLine(); - cout<second.size()<second)[0])<