Added and fixed.

This commit is contained in:
Saturneic 2019-01-18 11:09:08 +08:00
parent 929d5b9369
commit d94f2e54e6
12 changed files with 174 additions and 164 deletions

View File

@ -14,6 +14,7 @@
92A1F29821F0C19500340EFA /* socket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29721F0C19500340EFA /* socket.cpp */; };
92A1F29B21F0C5CC00340EFA /* clock.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29921F0C5CC00340EFA /* clock.cpp */; };
92A1F29E21F0C72C00340EFA /* addr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29D21F0C72C00340EFA /* addr.cpp */; };
92A1F2A121F1663300340EFA /* memory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29F21F1663300340EFA /* memory.cpp */; };
92D6CE6921EE4920005AEF3B /* server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92D6CE6721EE4920005AEF3B /* server.cpp */; };
/* End PBXBuildFile section */
@ -45,6 +46,8 @@
92A1F29A21F0C5CC00340EFA /* clock.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = clock.h; sourceTree = "<group>"; };
92A1F29C21F0C67600340EFA /* type.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = type.h; sourceTree = "<group>"; };
92A1F29D21F0C72C00340EFA /* addr.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = addr.cpp; sourceTree = "<group>"; };
92A1F29F21F1663300340EFA /* memory.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = memory.cpp; sourceTree = "<group>"; };
92A1F2A021F1663300340EFA /* memory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = memory.h; sourceTree = "<group>"; };
92D6CE6721EE4920005AEF3B /* server.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = server.cpp; sourceTree = "<group>"; };
92D6CE6821EE4920005AEF3B /* server.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = server.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -89,6 +92,7 @@
92D6CE6721EE4920005AEF3B /* server.cpp */,
92A1F29921F0C5CC00340EFA /* clock.cpp */,
92A1F29D21F0C72C00340EFA /* addr.cpp */,
92A1F29F21F1663300340EFA /* memory.cpp */,
);
name = Net;
sourceTree = "<group>";
@ -96,6 +100,7 @@
925A13AA21EC989500CBD427 /* include */ = {
isa = PBXGroup;
children = (
92A1F2A021F1663300340EFA /* memory.h */,
92A1F29A21F0C5CC00340EFA /* clock.h */,
925A13A821EC973000CBD427 /* cmap.h */,
9221DA1421EB62F6007310A7 /* cpart.h */,
@ -168,6 +173,7 @@
92A1F29B21F0C5CC00340EFA /* clock.cpp in Sources */,
925A13A921EC973000CBD427 /* cmap.cpp in Sources */,
92A1F29E21F0C72C00340EFA /* addr.cpp in Sources */,
92A1F2A121F1663300340EFA /* memory.cpp in Sources */,
92D6CE6921EE4920005AEF3B /* server.cpp in Sources */,
92A1F29821F0C19500340EFA /* socket.cpp in Sources */,
925A13AD21EC9DB900CBD427 /* cthread.cpp in Sources */,

View File

@ -6,6 +6,7 @@
// Copyright © 2019年 Bakantu. All rights reserved.
//
#include "memory.h"
#include "cmap.h"

View File

@ -6,6 +6,7 @@
// Copyright © 2019年 Bakantu. All rights reserved.
//
#include "memory.h"
#include "cpart.h"
/**
@ -74,15 +75,9 @@ int CPart::GetSo(void){
CPart::~CPart(){
// 释放储存接口输入参数所占用的内存
for(auto k = 0; k < args_in.size(); k++){
if(fargs_in[k] == 0) delete (int *)(args_in[k]);
else delete (double *)(args_in[k]);
}
for(auto arg : args_in) main_pool.b_free(arg);
// 释放储存接口输出参数所占用的内存
for(auto k = 0; k < args_out.size(); k++){
if(fargs_in[k] == 0) delete (int *)(args_out[k]);
else delete (double *)(args_out[k]);
}
for(auto arg : args_out) main_pool.b_free(arg);
// 停止对lib文件的操作
if(handle != nullptr)
dlclose(handle);
@ -108,27 +103,16 @@ void CPart::setArgsType(vector<int> fargs_in, vector<int> fargs_out){
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--){
if(fargs_in[count] == INT){
CPart::addArg(libargs_in, *((int *)(*k)));
}
else if(fargs_in[count] == DOUBLE){
CPart::addArg(libargs_in, *((double *)(*k)));
}
}
for(auto arg : args_in) libargs_in->push_back(arg);
// 执行计算模块
if(func() == SUCCESS){
int count = 0;
//储存计算结果
for(auto k = libargs_out->begin(); k != libargs_out->end();k++,count++){
if(fargs_out[count] == INT){
CPart::addArg(&args_out, *((int *)(*k)));
}
else if(fargs_out[count] == DOUBLE){
CPart::addArg(&args_out, *((double *)(*k)));
}
for(auto arg : *libargs_out){
// 获得内存块的访问量
main_pool.b_get(arg);
args_out.push_back(arg);
}
libargs_out->clear();
return SUCCESS;
}
else return -1;
@ -140,15 +124,9 @@ int CPart::Run(void){
*/
void CPart::Clear(void){
// 释放传入参数所占的空间
for(auto k = args_in.size() - 1; ~k; k--){
if(fargs_in[k] == INT) delete (int *)(args_in[k]);
else delete (double *)(args_in[k]);
args_in.pop_back();
}
for(auto arg : args_in) main_pool.b_free(arg);
args_in.clear();
// 释放传出参数所占用的内存空间
for(auto k = args_out.size() - 1; ~k; k--){
if(fargs_in[k] == INT) delete (int *)(args_out[k]);
else delete (double *)(args_out[k]);
args_out.pop_back();
}
for(auto arg : args_out) main_pool.b_free(arg);
args_out.clear();
}

11
cpart.h
View File

@ -10,6 +10,7 @@
#define cpart_h
#include "type.h"
#include "memory.h"
//声明计算模块的传入与传出参数列表
#define ARGS_DECLAER(name) vector<void *> __##name##_args_in, __##name##_args_out
@ -98,7 +99,8 @@ public:
// 一般由lib文件中的计算模块调用的向vector中添加参数并分配内存空间而后初始化
template<class T>
static void addArg(vector<void *> *args,T value){
T *p_value = new T(value);
T *p_value = (T *) main_pool.b_malloc(sizeof(T));
*p_value = value;
if(p_value == nullptr) throw "fail to malloc";
args->push_back(p_value);
}
@ -106,9 +108,16 @@ public:
template<class T>
static T popArg(vector<void *> *args){
if(args == nullptr) throw "the pointer to vector is null";
T *p_value = (T *)args->back();
p_value = main_pool.b_get(p_value);
if(p_value == nullptr) throw "infomation lost";
p_value = main_pool.b_free(p_value);
T value = *p_value;
args->pop_back();
return value;
}
};

View File

@ -6,53 +6,32 @@
// Copyright © 2019年 Bakantu. All rights reserved.
//
#include "memory.h"
#include "cthread.h"
list<CThread *> daemon_list = {};
CThread::CThread(CMap *tp_map,int thdnum):p_map(tp_map),idxtid(0),thdnum(thdnum){
lpcs.if_als = false;
// 构造空的传入与传出参数列表
for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){
for(auto cp : p_map->cparts){
// 构造空的传入与传出参数列表
vector<void *> args,args_out;
rargs.insert(pair<string,vector<void *>>((*k).first,args));
rargs_out.insert(pair<string,vector<void *>>((*k).first,args_out));
}
// 构造任务进度列表
for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){
ifsolved.insert(pair<string,bool>((*k).first,false));
if_rargs.insert(pair<string,bool>((*k).first,false));
rargs.insert(pair<string,vector<void *>>(cp.first,args));
rargs_out.insert(pair<string,vector<void *>>(cp.first,args_out));
// 构造任务进度列表
ifsolved.insert(pair<string,bool>(cp.first,false));
if_rargs.insert(pair<string,bool>(cp.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){
for(auto litem : item.second) main_pool.b_free(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);
}
}
}
for(auto item : rargs){
for(auto litem : item.second)
if(litem != nullptr) main_pool.b_free(litem);
}
}
@ -61,15 +40,15 @@ void CThread::Analyse(void){
if(lpcs.if_als == true){
//还没想好怎么做
}
for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){
auto cpart_depends = (*k).second->depends;
for(auto cp : p_map->cparts){
auto cpart_depends = cp.second->depends;
// 如果计算模块已经执行则跳过
if(ifsolved.find(k->second->name)->second) continue;
if(ifsolved.find(cp.second->name)->second) continue;
// 如果该计算模块含有依赖模块
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;
for(auto cpd : cpart_depends){
string name = cpd.t_cpart->name;
// 如果依赖模块还没有被调用过
if(!(ifsolved.find(name)->second)){
if_ok = false;
@ -78,41 +57,36 @@ void CThread::Analyse(void){
}
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<int> s_fargs_in = cp.second->fargs_in;
for(auto cpd : cpart_depends){
vector<int> args = cpd.args;
// 输入参数列表
vector<void *> &args_in = rargs.find(k->second->name)->second;
vector<void *> &args_in = rargs.find(cp.second->name)->second;
// 输出形式参数列表
vector<int> f_fargs_out = ditem->t_cpart->fargs_out;
vector<int> f_fargs_out = cpd.t_cpart->fargs_out;
// 输出参数列表
vector<void *> args_out = rargs_out.find(ditem->t_cpart->name)->second;
vector<void *> args_out = rargs_out.find(cpd.t_cpart->name)->second;
// 检查传入传出参数的类型是否匹配
for(auto itm = args.begin(); itm != args.end();itm++){
for(auto itm : args){
// 参数不匹配则报异常
if(s_fargs_in[count++] != f_fargs_out[*itm]) throw "type conflict";
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])));
}
args_in.push_back(args_out[itm]);
}
}
lpcs.line.push_back((*k).second);
lpcs.line.push_back(cp.second);
// 大于线程最高并行数则跳出
if(count == thdnum) break;
}
}
// 如果该计算模块没有依赖模块
else{
string name = (*k).second->name;
if(rargs.find(k->second->name)->second.size() == k->second->fargs_in.size()){
string name = cp.second->name;
if(rargs.find(cp.second->name)->second.size() == cp.second->fargs_in.size()){
// 如果该模块还没有被调用
if(ifsolved.find(name)->second == false){
lpcs.line.push_back(k->second);
lpcs.line.push_back(cp.second);
}
}
@ -121,22 +95,22 @@ void CThread::Analyse(void){
}
void CThread::DoLine(void){
for(auto pcp = lpcs.line.begin(); pcp != lpcs.line.end(); pcp++){
string name = (*pcp)->name;
for(auto pcp : lpcs.line){
string name = pcp->name;
vector<void *> args = rargs.find(name)->second;
vector<int> fargs = (*pcp)->fargs_in;
vector<int> fargs_out = (*pcp)->fargs_out;
vector<int> fargs = pcp->fargs_in;
vector<int> fargs_out = pcp->fargs_out;
unsigned long ntid = idxtid++;
pthread_t npdt = 0;
// 创建新线程
struct thread_args *pt_ta = new struct thread_args({ntid,this,(*pcp),-1});
struct thread_args *pt_ta = new struct thread_args({ntid,this,pcp,-1});
if(pthread_create(&npdt,NULL,&CThread::NewThread,(void *)(pt_ta))){
throw "fail to create thread";
}
lpcs.threads.insert({ntid,npdt});
lpcs.cpttid.insert({(*pcp),ntid});
lpcs.cpttid.insert({pcp,ntid});
}
}
@ -147,8 +121,8 @@ void CThread::SetDaemon(void){
void CThread::Daemon(void){
// 等待线程返回
for(auto i = lpcs.child_finished.begin(); i != lpcs.child_finished.end(); i++){
unsigned long tid = (*i)->tid;
for(auto cfh : lpcs.child_finished){
unsigned long tid = cfh->tid;
pthread_t cpdt = lpcs.threads.find(tid)->second;
struct thread_args *rpv = nullptr;
pthread_join(cpdt, (void **)&rpv);
@ -168,6 +142,7 @@ void CThread::Daemon(void){
lpcs.threads.erase(tid);
lpcs.cpttid.erase(rpv->pcp);
printf("TID: %lu Deleted.\n",tid);
// 删除线程传入参数
delete rpv;
}
lpcs.child_finished.clear();
@ -221,14 +196,9 @@ void CThread::PrepareArgsIn(CThread *pct,CPart *pcp){
// 清空历史数据
pcp->Clear();
// 传入输入参数
int cout = 0;
for(auto arg = args.begin(); arg != args.end(); arg++,cout++){
if(fargs[cout] == INT){
pcp->addArgsIn<int>(*((int *)(*arg)));
}
else if(fargs[cout] == DOUBLE){
pcp->addArgsIn<double>(*((double *)(*arg)));
}
for(auto arg : args){
if(main_pool.b_get(arg) == nullptr) throw "information lost";
pcp->args_in.push_back(arg);
}
}
@ -242,17 +212,7 @@ void CThread::GetArgsOut(CThread *pct,CPart *pcp){
vector<void *> &args_out = pct->rargs_out.find(pcp->name)->second;
// 处理输出
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)));
args_out.push_back((void *)p_value);
}
else if(fargs_out[cout] == DOUBLE){
double *p_value = new double(*((double *)(*argo)));
args_out.push_back((double *)p_value);
}
}
for(auto argo : argso) args_out.push_back(argo);
}
@ -268,29 +228,13 @@ int CThread::CancelChildPCS(unsigned long tid){
int CThread::GetCPUResult(struct compute_result *pcrt){
ifsolved.find(pcrt->name)->second = true;
// 处理输出参数
int count = 0;
CPart *pcp = p_map->cparts.find(pcrt->name)->second;
vector<int> farg_out = pcp->fargs_out;
for(auto argo : *pcrt->args_out) AddArgsOut(pcrt->name, argo);
for(auto i = pcrt->args_out->begin(); i != pcrt->args_out->end(); i++,count++){
if(farg_out[count] == INT){
AddArgsOut<int>(pcrt->name, *((int *)(*i)));
}
else if(farg_out[count] == DOUBLE){
AddArgsOut<double>(pcrt->name, *((double *)(*i)));
}
}
// 处理输入参数
vector<int> farg_in = pcp->fargs_in;
count = 0;
for(auto i = pcrt->args_in->begin(); i != pcrt->args_in->end(); i++,count++){
if(farg_in[count] == INT){
AddArgs<int>(pcrt->name, *((int *)(*i)));
}
else if(farg_in[count] == DOUBLE){
AddArgs<double>(pcrt->name, *((double *)(*i)));
}
}
for(auto argi : *pcrt->args_in) AddArgs(pcrt->name, argi);
ifsolved.find(pcrt->name)->second = true;
if_rargs.find(pcrt->name)->second = true;

View File

@ -70,22 +70,18 @@ public:
CThread(CMap *tp_map, int thdnum = 4);
~CThread();
template<class T>
// 添加相关计算模块的传入参数
void AddArgs(string name, T value){
auto k = rargs.find(name);
T *p_value = new T();
*p_value = value;
(*k).second.push_back((void *)p_value);
void AddArgs(string name, void *pvle){
if(main_pool.b_get(pvle) == nullptr) throw "information lost";
auto argil = rargs.find(name);
argil->second.push_back(pvle);
}
template<class T>
// 添加相关计算模块的传出参数
void AddArgsOut(string name, T value){
auto k = rargs_out.find(name);
T *p_value = new T();
*p_value = value;
(*k).second.push_back((void *)p_value);
void AddArgsOut(string name, void *pvle){
if(main_pool.b_get(pvle) == nullptr) throw "information lost";
auto argol = rargs_out.find(name);
argol->second.push_back(pvle);
}
// 设置守护进程

View File

@ -7,6 +7,7 @@
//
#include "type.h"
#include "memory.h"
#include "clock.h"
#include "net.h"
#include "cpart.h"
@ -42,10 +43,6 @@ int main(void){
void CPMT(void){
CMap map("./PCS");
CThread thread(&map);
thread.AddArgs<int>("B", 4);
thread.AddArgs<double>("B", 9.0);
thread.AddArgs<int>("C", 1.0);
thread.AddArgs<double>("C", 3.0);
thread.Analyse();
thread.DoLine();
thread.SetDaemon();

11
memory.cpp Normal file
View File

@ -0,0 +1,11 @@
//
// memory.cpp
// Net
//
// Created by 胡一兵 on 2019/1/18.
// Copyright © 2019年 Bakantu. All rights reserved.
//
#include "memory.h"
BlocksPool main_pool;

67
memory.h Normal file
View File

@ -0,0 +1,67 @@
//
// memory.h
// Net
//
// Created by 胡一兵 on 2019/1/18.
// Copyright © 2019年 Bakantu. All rights reserved.
//
#ifndef memory_h
#define memory_h
#include "type.h"
struct block_info{
uint32_t size = 0;
int lock = 0;
bool pted = false;
};
class BlocksPool{
map<void *, block_info> blocks_list;
public:
void *b_malloc(uint32_t size){
void *ptr = malloc(size);
if(ptr == nullptr) return nullptr;
blocks_list.insert({ptr,{size,1}});
return ptr;
}
void *b_get(void *ptr){
auto blk = blocks_list.find(ptr);
if(blk != blocks_list.end()){
blk->second.lock++;
return ptr;
}
else return nullptr;
}
void b_protect(void *ptr){
auto blk = blocks_list.find(ptr);
if(blk != blocks_list.end()){
blk->second.pted = true;
}
else throw "protect nil value";
}
void b_noprotect(void *ptr){
auto blk = blocks_list.find(ptr);
if(blk != blocks_list.end()){
blk->second.pted = false;
}
else throw "noprotect nil value";
}
void b_free(void *ptr){
auto blk = blocks_list.find(ptr);
if(blk != blocks_list.end() && blk->second.pted == false){
if(blk->second.lock - 1 == 0){
free(blk->first);
blocks_list.erase(blk);
}
else blk->second.lock--;
}
}
};
extern BlocksPool main_pool;
#endif /* memory_h */

View File

@ -6,11 +6,9 @@
// Copyright © 2019年 Bakantu. All rights reserved.
//
#include "memory.h"
#include "server.h"
list<server_clock> server_list;
list<server_clock> process_list;
extern list<clock_register> clocks_list;
void setServerClock(Server *psvr, int clicks){
@ -34,7 +32,7 @@ void Server::SetSendIP(string ip_addr){
}
// 将计算结果包转化为结构数据包
packet Server::CPURS2Packet(compute_result tcpur){
packet CNodeServer::CPURS2Packet(compute_result tcpur){
packet rawpkt;
rawpkt.type = 0;
int count = 0;
@ -122,7 +120,7 @@ packet Server::Rawdata2Packet(raw_data trdta){
return pkt;
}
compute_result Server::Packet2CPUR(packet *tpkt){
compute_result CNodeServer::Packet2CPUR(packet *tpkt){
compute_result tcpur;
tcpur.args_in = new vector<void *>();
tcpur.args_out = new vector<void *>();

View File

@ -47,17 +47,18 @@ public:
unsigned long msg_size = 0;
// 用简单字符串直接出适合
void setData(string str){
data = (char *)malloc(str.size()+1);
size = str.size()+1;
data = (char *)malloc(str.size());
size = str.size();
memcpy(data, str.data(),str.size());
data[str.size()+1] = '\0';
}
};
//通用服务器类
class Server{
protected:
// 缓存通用数据包
vector<packet> packets_in;
// 缓存带标签的二进制串管理结构
vector<raw_data> rawdata_in;
public:
// 服务器类的接收套接字对象与发送套接字对象
@ -88,7 +89,8 @@ public:
static bool CheckRawMsg(char *p_rdt, ssize_t size);
// 处理一个已贴上标签的原始二进制串,获得其包含的信息
static raw_data ProcessSignedRawMsg(char *p_rdt, ssize_t size);
// 服务器守护线程
friend void *serverDeamon(void *psvr);
};

View File

@ -6,6 +6,7 @@
// Copyright © 2019年 Bakantu. All rights reserved.
//
#include "memory.h"
#include "net.h"
void SocketClient::SetSendPort(int port){