Unary.cpp 10.5 KB
Newer Older
linye committed
1 2 3 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
/* 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: Xu Chen (email: hello_master1954@163.com) 2018-07-31
 */

#include <math.h>
#include "../../XName.h"
#include "Unary.h"
#include "Unary.cuh"

namespace nts{
29 30 31 32 33 34 35 36
  
template<class T>
T UnaryNegate(T x) {
    return (T)-x;
}

template<class T>
T UnarySquare(T x)
linye committed
37
{
38
    return (T)(x * x);
linye committed
39 40
}

41 42
template<class T>
T UnaryRound(T r)
linye committed
43
{
44
	return (r > 0.0) ? (T)floor(r + 0.5) : (T)ceil(r - 0.5);
linye committed
45 46
}

47 48
template<class T>
T UnarySign(T r)
linye committed
49
{
50 51 52 53 54 55
    if (r > 0.0)
       return (T)1.0;
    else if (r == 0.0)
       return (T)0.0;
    else
       return (T)-1.0;
linye committed
56 57
}

58 59
template<class T>
T UnaryIsNonZero(T r)
linye committed
60
{
61
    return (r != 0.0) ? (T)1.0 : (T)0.0;
linye committed
62 63
}

64 65 66 67
template<class T>
T UnaryIsZero(T r)
{
    return (r == 0.0) ? (T)1.0 : (T)0.0;
linye committed
68 69
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/* define three marco separately, specify the respective function names */
#define _SIMPLE_UNARY_FUNCTION(_funcName, _cudaFuncName, origFunc)                   \
void _funcName(const XTensor * a, XTensor * b)                                       \
{                                                                                    \
    /* run it on GPUs */                                                             \
    if (a->devID >= 0) {                                                             \
        if (useCUDA) {                                                               \
            _cudaFuncName(a, b);                                                     \
            return;                                                                  \
        }                                                                            \
        else                                                                         \
            ShowNTErrors("No GPU devices support!")                                  \
    }                                                                                \
    CheckNTErrors((XTensor::IsSameShaped(a, b)),                                     \
                  "Input tensors should have the same type!");                       \
    if (a->dataType == X_INT) {                                                      \
        int * d = (int*)a->data;                                                     \
        int * db = (int*)b->data;                                                    \
        for (int i = 0; i < a->unitNum; i++)                                         \
            db[i] = (int)origFunc(d[i]);                                             \
    }                                                                                \
    else if (a->dataType == X_FLOAT) {                                               \
        float * d = (float*)a->data;                                                 \
        float * db = (float*)b->data;                                                \
        for (int i = 0; i < a->unitNum; i++)                                         \
            db[i] = (float)origFunc(d[i]);                                           \
    }                                                                                \
    else if (a->dataType == X_DOUBLE) {                                              \
        double * d = (double*)a->data;                                               \
        double * db = (double*)b->data;                                              \
        for (int i = 0; i < a->unitNum; i++)                                         \
            db[i] = (double)origFunc(d[i]);                                          \
    }                                                                                \
    else                                                                             \
        ShowNTErrors("TO DO!");                                                      \
}                                       

#define _SIMPLE_UNARY_FUNCTION_ME(_funcNameMe, _funcName)                            \
void _funcNameMe(XTensor * a)                                                        \
{                                                                                    \
    _funcName(a, a);                                                                 \
linye committed
111 112
}        

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
#define SIMPLE_UNARY_FUNCTION_ME(funcNameMe, _funcName)                              \
void funcNameMe(XTensor & a)                                                         \
{                                                                                    \
    _funcName(&a, &a);                                                               \
}                                                                                    
                                                                                     
#define SIMPLE_UNARY_FUNCTION(funcName, _funcName, operationId)                      \
XTensor funcName(const XTensor & a)                                                  \
{                                                                                    \
    XTensor b(&a);                                                                   \
    b.SetTMPFlag();                                                                  \
    _funcName(&a, &b);                                                               \
    XLink::MakeLink(&a, NULL, &b, operationId);                                      \
    return b;                                                                        \
}                                                                                    
                                                                                     
#define SIMPLE_UNARY_FUNCTION_VOID(funcName, _funcName, operationId)                 \
void funcName(const XTensor & a, XTensor & b)                                        \
{                                                                                    \
    if (!b.isInit || !XTensor::IsSameShaped(&a, &b)) {                               \
        InitTensor(&b, &a);                                                          \
    }                                                                                \
    _funcName(&a, &b);                                                               \
    if (b.enableGrad) {                                                              \
        XLink::MakeLink(&a, NULL, &b, operationId);                                  \
    }                                                                                \
linye committed
139 140 141 142 143 144
}

_SIMPLE_UNARY_FUNCTION(_Absolute, _CudaAbsolute, fabs)
_SIMPLE_UNARY_FUNCTION(_Ceil, _CudaCeil, ceil)
_SIMPLE_UNARY_FUNCTION(_Exp, _CudaExp, exp)
_SIMPLE_UNARY_FUNCTION(_Floor, _CudaFloor, floor)
145 146
_SIMPLE_UNARY_FUNCTION(_IsNonZero, _CudaIsNonZero, UnaryIsNonZero)
_SIMPLE_UNARY_FUNCTION(_IsZero, _CudaIsZero, UnaryIsZero)
linye committed
147
_SIMPLE_UNARY_FUNCTION(_Log, _CudaLog, log)
148
_SIMPLE_UNARY_FUNCTION(_Negate, _CudaNegate, UnaryNegate)
linye committed
149
_SIMPLE_UNARY_FUNCTION(_Round, _CudaRound, round)
150
_SIMPLE_UNARY_FUNCTION(_Sign, _CudaSign, UnarySign)
linye committed
151
_SIMPLE_UNARY_FUNCTION(_Sqrt, _CudaSqrt, sqrt)
152
_SIMPLE_UNARY_FUNCTION(_Square, _CudaSquare, UnarySquare)
linye committed
153 154 155 156 157
_SIMPLE_UNARY_FUNCTION(_Sin, _CudaSin, sin)
_SIMPLE_UNARY_FUNCTION(_Cos, _CudaCos, cos)
_SIMPLE_UNARY_FUNCTION(_Tan, _CudaTan, tan)

_SIMPLE_UNARY_FUNCTION_ME(_AbsoluteMe, _Absolute)
158
SIMPLE_UNARY_FUNCTION_ME(AbsoluteMe, _Absolute)
linye committed
159 160 161 162
SIMPLE_UNARY_FUNCTION(Absolute, _Absolute, MATH_ABSOLUTE)
SIMPLE_UNARY_FUNCTION_VOID(Absolute, _Absolute, MATH_ABSOLUTE)

_SIMPLE_UNARY_FUNCTION_ME(_CeilMe, _Ceil)
163
SIMPLE_UNARY_FUNCTION_ME(CeilMe, _Ceil)
linye committed
164 165 166 167
SIMPLE_UNARY_FUNCTION(Ceil, _Ceil, MATH_CEIL)
SIMPLE_UNARY_FUNCTION_VOID(Ceil, _Ceil, MATH_CEIL)

_SIMPLE_UNARY_FUNCTION_ME(_ExpMe, _Exp)
168
SIMPLE_UNARY_FUNCTION_ME(ExpMe, _Exp)
linye committed
169 170 171 172
SIMPLE_UNARY_FUNCTION(Exp, _Exp, MATH_EXP)
SIMPLE_UNARY_FUNCTION_VOID(Exp, _Exp, MATH_EXP)

_SIMPLE_UNARY_FUNCTION_ME(_FloorMe, _Floor)
173
SIMPLE_UNARY_FUNCTION_ME(FloorMe, _Floor)
linye committed
174 175 176 177
SIMPLE_UNARY_FUNCTION(Floor, _Floor, MATH_FLOOR)
SIMPLE_UNARY_FUNCTION_VOID(Floor, _Floor, MATH_FLOOR)

_SIMPLE_UNARY_FUNCTION_ME(_IsNonZeroMe, _IsNonZero)
178
SIMPLE_UNARY_FUNCTION_ME(IsNonZeroMe, _IsNonZero)
linye committed
179 180 181 182
SIMPLE_UNARY_FUNCTION(IsNonZero, _IsNonZero, MATH_ISNONZERO)
SIMPLE_UNARY_FUNCTION_VOID(IsNonZero, _IsNonZero, MATH_ISNONZERO)

_SIMPLE_UNARY_FUNCTION_ME(_IsZeroMe, _IsZero)
183
SIMPLE_UNARY_FUNCTION_ME(IsZeroMe, _IsZero)
linye committed
184 185 186 187
SIMPLE_UNARY_FUNCTION(IsZero, _IsZero, MATH_ISZERO)
SIMPLE_UNARY_FUNCTION_VOID(IsZero, _IsZero, MATH_ISZERO)

_SIMPLE_UNARY_FUNCTION_ME(_LogMe, _Log)
188
SIMPLE_UNARY_FUNCTION_ME(LogMe, _Log)
linye committed
189 190 191
SIMPLE_UNARY_FUNCTION(Log, _Log, MATH_LOG)
SIMPLE_UNARY_FUNCTION_VOID(Log, _Log, MATH_LOG)

192 193 194 195 196
_SIMPLE_UNARY_FUNCTION_ME(_NegateMe, _Negate)
SIMPLE_UNARY_FUNCTION_ME(NegateMe, _Negate)
SIMPLE_UNARY_FUNCTION(Negate, _Negate, MATH_NEGATE)
SIMPLE_UNARY_FUNCTION_VOID(Negate, _Negate, MATH_NEGATE)

linye committed
197
_SIMPLE_UNARY_FUNCTION_ME(_RoundMe, _Round)
198
SIMPLE_UNARY_FUNCTION_ME(RoundMe, _Round)
linye committed
199 200 201
SIMPLE_UNARY_FUNCTION(Round, _Round, MATH_ROUND)
SIMPLE_UNARY_FUNCTION_VOID(Round, _Round, MATH_ROUND)

202 203 204 205 206
_SIMPLE_UNARY_FUNCTION_ME(_SignMe, _Sign)
SIMPLE_UNARY_FUNCTION_ME(SignMe, _Sign)
SIMPLE_UNARY_FUNCTION(Sign, _Sign, MATH_SIGN)
SIMPLE_UNARY_FUNCTION_VOID(Sign, _Sign, MATH_SIGN)

linye committed
207
_SIMPLE_UNARY_FUNCTION_ME(_SqrtMe, _Sqrt)
208
SIMPLE_UNARY_FUNCTION_ME(SqrtMe, _Sqrt)
linye committed
209 210 211 212
SIMPLE_UNARY_FUNCTION(Sqrt, _Sqrt, MATH_SQRT)
SIMPLE_UNARY_FUNCTION_VOID(Sqrt, _Sqrt, MATH_SQRT)

_SIMPLE_UNARY_FUNCTION_ME(_SquareMe, _Square)
213
SIMPLE_UNARY_FUNCTION_ME(SquareMe, _Square)
linye committed
214 215 216 217
SIMPLE_UNARY_FUNCTION(Square, _Square, MATH_SQUARE)
SIMPLE_UNARY_FUNCTION_VOID(Square, _Square, MATH_SQUARE)

_SIMPLE_UNARY_FUNCTION_ME(_SinMe, _Sin)
218
SIMPLE_UNARY_FUNCTION_ME(SinMe, _Sin)
linye committed
219 220 221 222
SIMPLE_UNARY_FUNCTION(Sin, _Sin, MATH_SIN)
SIMPLE_UNARY_FUNCTION_VOID(Sin, _Sin, MATH_SIN)

_SIMPLE_UNARY_FUNCTION_ME(_CosMe, _Cos)
223
SIMPLE_UNARY_FUNCTION_ME(CosMe, _Cos)
linye committed
224 225 226 227
SIMPLE_UNARY_FUNCTION(Cos, _Cos, MATH_COS)
SIMPLE_UNARY_FUNCTION_VOID(Cos, _Cos, MATH_COS)

_SIMPLE_UNARY_FUNCTION_ME(_TanMe, _Tan)
228
SIMPLE_UNARY_FUNCTION_ME(TanMe, _Tan)
linye committed
229 230 231 232
SIMPLE_UNARY_FUNCTION(Tan, _Tan, MATH_TAN)
SIMPLE_UNARY_FUNCTION_VOID(Tan, _Tan, MATH_TAN)

} // namespace nts(NiuTrans.Tensor)