Negate.cpp 2.12 KB
Newer Older
xiaotong committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* NiuTrans.Tensor - an open-source tensor library
* Copyright (C) 2017, Natural Language Processing Lab, Northestern University.
* 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.
*/

/*
* $Created by: XIAO Tong (email: xiaotong@mail.neu.edu.cn) 2018-04-24
*/

22
#include "../../XTensor.h"
23
#include "../../XName.h"
xiaotong committed
24 25 26 27 28 29
#include "Negate.h"
#include "Negate.cuh"

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

/*
liyinqiao committed
30
set every entry to its minus value
31 32
>> a - input tensor we are processing
>> b - output tensor we are processing
xiaotong committed
33
*/
34
void _Negate(const XTensor * a, XTensor * b)
xiaotong committed
35 36 37 38
{
#ifdef USE_CUDA
    /* run it on GPUs */
    if (a->devID >= 0) {
39
        _CudaNegate(a, b);
xiaotong committed
40
    return;
41
    }
xiaotong committed
42 43
#endif

44
    CheckNTErrors((XTensor::IsSameShaped(a, b)), "Input tensors should have the same type!");
xiaotong committed
45 46
    CheckNTErrors((a->dataType == DEFAULT_DTYPE), "TODO!");
    DTYPE * d = (DTYPE*)a->data;
47
    DTYPE * db = (DTYPE*)b->data;
xiaotong committed
48
    for (int i = 0; i < a->unitNum; i++)
49 50 51 52 53 54 55 56 57 58 59
        db[i] = -d[i];
}

/*
set every entry to its minus value (do it on site)
keep the result in the input tensor a and return nothing
>> a - the tensor we are processing
*/
void _NegateMe(XTensor * a)
{
    _Negate(a, a);
xiaotong committed
60
}
61 62

/*
xiaotong committed
63
set every entry to its minus value (return an XTensor structure)
64 65 66 67 68 69 70
make a new tensor to keep the result and return it
>> a - input tensor we are processing
<< return - the minus value of input tensor
*/
XTensor Negate(const XTensor & a)
{
    XTensor b(&a);
xiaotong committed
71
    b.SetTMPFlag();
72 73 74 75 76 77 78 79 80 81
    
    /* call _Negate function */
    _Negate(&a, &b);
    
    /* tensor connections */
    XLink::MakeLink(&a, NULL, &b, MATH_NEGATE);
    
    return b;
}

xiaotong committed
82
} // namespace nts(NiuTrans.Tensor)