Negate.cu 2.69 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 23
#include "../../XDevice.h"
#include "../../XTensor.h"
xiaotong committed
24 25 26 27 28 29 30 31
#include "Negate.h"
#include "Negate.cuh"

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

#ifdef USE_CUDA
/*
set each entry to its negtive value (CUDA Kernel)
32 33
>> a - pointer to the input data array
>> b - pointer to the output data array
xiaotong committed
34 35 36
>> size - size of the data array
*/
__global__
37
void KernelNegate(DTYPE * a, DTYPE * b, int size)
xiaotong committed
38 39 40 41
{
    int i = blockDim.x * blockIdx.x + threadIdx.x;

    if (i < size)
42
        b[i] = -a[i];
xiaotong committed
43 44 45
}

/*
liyinqiao committed
46 47
set each entry to its negtive value (CUDA Kernel)
This is for float16 computation
48 49
>> a - pointer to the input data array
>> b - pointer to the output data array
liyinqiao committed
50
>> size - size of the data array
xiaotong committed
51 52
*/
__global__
53
void KernelNegate(__half * a, __half * b, int size)
xiaotong committed
54 55 56 57 58
{
    int i = blockDim.x * blockIdx.x + threadIdx.x;

#if __CUDA_ARCH__ >= 530 || !defined(__CUDA_ARCH__)
        if (i < size)
59
            b[i] = __hsub(__float2half(0), a[i]);
xiaotong committed
60 61
#else
        if (i < size)
62
            b[i] = __float2half(-__half2float(a[i]));
xiaotong committed
63 64 65 66 67
#endif
}

/*
set each entry to its negtive value
68 69
>> a - input tensor
>> b - output tensor
xiaotong committed
70
*/
71
void _CudaNegate(const XTensor * a, XTensor * b)
xiaotong committed
72
{
73
    CheckNTErrors((XTensor::IsSameShaped(a, b)), "Input tensors should have the same type!");
xiaotong committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87
    CheckNTErrors((a->isSparse == false), "TODO!");

    int gridSize[3];
    int blockSize[3];

    GDevs.GetCudaThread(a->devID, a->unitNum, gridSize, blockSize);

    dim3 blocks(gridSize[0]);
    dim3 threads(blockSize[0]);

    int devIDBackup;
    ProtectCudaDev(a->devID, devIDBackup);

    if (a->dataType == DEFAULT_DTYPE) {
88
        KernelNegate << <blocks, threads >> >((DTYPE*)a->data, (DTYPE*)b->data, a->unitNum);
xiaotong committed
89 90
    }
    else if (a->dataType == X_FLOAT16) {
91
        KernelNegate << <blocks, threads >> >((__half*)a->data, (__half*)b->data, a->unitNum);
xiaotong committed
92 93 94 95 96 97 98 99 100 101
    }
    else {
        ShowNTErrors("TODO!");
    }

    BacktoCudaDev(a->devID, devIDBackup);
}

#endif // USE_CUDA
} // namespace nts(NiuTrans.Tensor)