Commit 13380048 by xiaotong

new classes: XLeader, DataLoaderBase and NetBase

parent b2dcbed0
/*
* NiuTrans.Tensor - an open-source tensor library
* Copyright (C) 2021
* Natural Language Processing Lab, Northeastern University
* and
* NiuTrans Research
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* A "leader" manages a number of "workers". The leader recieves jobs from
* the central server (can be remote), or acts as an independent server itself.
* For workers, the leader is the one who issues orders and organizes them.
* Note that the leader and workers must be on the same machine. In case of
* multi-machine training, one can deploy different leaders on different
* machines. BUT, at this time, we need an additional way of distributing
* data across machines.
*
* $Created by: XIAO Tong (xiaotong@mail.neu.edu.cn) 2021-02-25
*/
#include "XLeader.h"
/* the nts (NiuTrans.Tensor) namespace */
namespace nts {
/* constructor */
XLeader::XLeader()
{
}
/* de-constructor */
XLeader::~XLeader()
{
}
/* set id */
void XLeader::SetID(int myID)
{
id = myID;
}
/* get id */
int XLeader::GetID()
{
return id;
}
} /* end of the nts (NiuTrans.Tensor) namespace */
\ No newline at end of file
/*
* NiuTrans.Tensor - an open-source tensor library
* Copyright (C) 2021
* Natural Language Processing Lab, Northeastern University
* and
* NiuTrans Research
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* A "leader" manages a number of "workers". The leader recieves jobs from
* the central server (can be remote), or acts as an independent server itself.
* For workers, the leader is the one who issues orders and organizes them.
* Note that the leader and workers must be on the same machine. In case of
* multi-machine training, one can deploy different leaders on different
* machines. BUT, at this time, we need an additional way of distributing
* data across machines.
*
* $Created by: XIAO Tong (xiaotong@mail.neu.edu.cn) 2021-02-25
* We will go for a business trip. The first trip after the Spring Festival.
*/
#ifndef __XLEADER_H__
#define __XLEADER_H__
namespace nts { // namespace nts(NiuTrans.Tensor)
/* a leader who manages workers */
class XLeader
{
protected:
/* id of the leader */
int id;
public:
/* constructor */
XLeader();
/* de-constructor */
~XLeader();
/* set id */
void SetID(int myID);
/* get id */
int GetID();
/* set the data loader */
void SetDataLoader();
};
}
#endif // __XLEADER_H__
\ No newline at end of file
/*
* NiuTrans.Tensor - an open-source tensor library
* Copyright (C) 2021
* Natural Language Processing Lab, Northeastern University
* and
* NiuTrans Research
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* We define various template classes here. They will be overloaded and used
* in applications.
*
* $Created by: XIAO Tong (xiaotong@mail.neu.edu.cn) 2021-02-25
*/
#include "XNetTemplate.h"
namespace nts { // namespace nts(NiuTrans.Tensor)
/*******************************
* data loader template
*******************************/
/* constructor */
DataLoaderBase::DataLoaderBase()
{
MUTEX_INIT(loadMutex);
}
/* de-constructor */
DataLoaderBase::~DataLoaderBase()
{
MUTEX_DELE(loadMutex);
}
/* open data file */
bool DataLoaderBase::Open(XList * args)
{
ShowNTErrors("DataLoaderBase::Open must be overloaded!");
return true;
}
/* close data file */
bool DataLoaderBase::Close(XList * args)
{
ShowNTErrors("DataLoaderBase::Close must be overloaded!");
return true;
}
/* load a batch of samples */
bool DataLoaderBase::LoadBatch(XList * args)
{
ShowNTErrors("DataLoaderBase::LoadBatch must be overloaded!");
return true;
}
/* load a batch of samples (for multi-threading) */
bool DataLoaderBase::LoadBatchSafe(XList * args)
{
bool r;
MUTEX_LOCK(loadMutex);
r = LoadBatch(args);
MUTEX_UNLOCK(loadMutex);
return r;
}
/*******************************
* neural network template
*******************************/
/* constructor */
NetBase::NetBase()
{
MUTEX_INIT(netMutex);
}
/* de-constructor */
NetBase::~NetBase()
{
MUTEX_DELE(netMutex);
}
/* run the net */
bool NetBase::Run(XList * args)
{
ShowNTErrors("NetBase::Run must be overloaded!");
return true;
}
/* run the net (for multi-threading */
bool NetBase::RunSafe(XList * args)
{
bool r;
MUTEX_LOCK(netMutex);
r = Run(args);
MUTEX_UNLOCK(netMutex);
return r;
}
}
\ No newline at end of file
/*
* NiuTrans.Tensor - an open-source tensor library
* Copyright (C) 2021
* Natural Language Processing Lab, Northeastern University
* and
* NiuTrans Research
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* We define various template classes here. They will be overloaded and used
* in applications.
*
* $Created by: XIAO Tong (xiaotong@mail.neu.edu.cn) 2021-02-25
* The meeting at 3:00pm today was canceled. More time for coding.
*/
#ifndef __XNETTEMPLATE_H__
#define __XNETTEMPLATE_H__
#include "../tensor/XTensor.h"
#include "../tensor/XThread.h"
namespace nts { // namespace nts(NiuTrans.Tensor)
/* data loader template */
class DataLoaderBase
{
protected:
/* mutex of batch loading */
MUTEX_HANDLE loadMutex;
public:
/* constructor */
DataLoaderBase();
/* de-constructor */
~DataLoaderBase();
/* open data file */
virtual
bool Open(XList * args);
/* close data file */
virtual
bool Close(XList * args);
/* load a batch of samples */
virtual
bool LoadBatch(XList * args);
protected:
/* load a batch of samples (for multi-threading) */
bool LoadBatchSafe(XList * args);
};
/* neural network template */
class NetBase
{
protected:
/* mutex of the net */
MUTEX_HANDLE netMutex;
public:
/* constructor */
NetBase();
/* de-constructor */
~NetBase();
/* run the net */
virtual
bool Run(XList * args);
protected:
/* run the net (for multi-threading */
bool RunSafe(XList * args);
};
}
#endif // __XNETTEMPLATE_H__
......@@ -37,7 +37,7 @@ namespace nts { // namespace nts(NiuTrans.Tensor)
/* a model template for training */
class XWorkerJob : public XWorker
{
private:
protected:
public:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论