XBaseTemplate.cpp 2.35 KB
Newer Older
1 2
/*
* NiuTrans.Tensor - an open-source tensor library
3
* Copyright (C) 2016-2021
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
* 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
*/

29
#include "XBaseTemplate.h"
30 31 32 33 34 35 36 37

namespace nts { // namespace nts(NiuTrans.Tensor)

/******************************* 
 * data loader template 
 *******************************/

/* constructor */
38
DataDistributeBase::DataDistributeBase()
39 40 41 42 43
{
    MUTEX_INIT(loadMutex);
}

/* de-constructor */
44
DataDistributeBase::~DataDistributeBase()
45 46 47 48
{
    MUTEX_DELE(loadMutex);
}

49
/* * start the job (e.g., open the file) */
xiaotong committed
50
bool DataDistributeBase::Start()
51
{
52
    ShowNTErrors("DataDistributeBase::Start must be overloaded!");
53 54 55
    return true;
}

56
/* end the job (e.g., close the file) */
xiaotong committed
57
bool DataDistributeBase::End()
58
{
59
    ShowNTErrors("DataDistributeBase::End must be overloaded!");
60 61 62
    return true;
}

xiaotong committed
63 64 65 66 67 68 69 70 71 72
/* 
get a batch of samples 
>> inputs - inputs of the model
>> golds - gold standards
*/
bool DataDistributeBase::GetBatchSimple(XList * inputs, XList * golds)
{
    return false;
}

73 74
/* get a batch of samples */
bool DataDistributeBase::GetBatch(XList * args)
75
{
xiaotong committed
76 77 78 79 80 81 82 83 84 85
    CheckNTErrors(args->count >= 2, "More input arguments are required!");

    XList * input = (XList*)args->GetItem(0);
    XList * gold = (XList*)args->GetItem(1);

    if (GetBatchSimple(input, gold))
        return true;

    ShowNTErrors("You must be overload one of these: DataDistributeBase::GetBatchSimple ... !");
    return false;
86 87
}

88 89
/* get a batch of samples (for multi-threading) */
bool DataDistributeBase::GetBatchSafe(XList * args)
90 91 92 93
{
    bool r;

    MUTEX_LOCK(loadMutex);
94
    r = GetBatch(args);
95 96 97 98 99
    MUTEX_UNLOCK(loadMutex);

    return r;
}

100
}