MatrixMul2D.cu 7.94 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 32 33 34 35 36 37 38 39
#include "MatrixMul2D.h"
#include "MatrixMul2D.cuh"
#include "XTensorBLAS.h"

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

#ifdef USE_CUDA
/*
mutilication of a dense matrix with a sparse matrix
c = a * b * \alpha

>> a - a dense matrix
>> transposedA - indicates whether a is transposed
>> aColSize - column size of matrix a
>> aRowSize - row size of matrix a
>> b - a sparse matrix
liyinqiao committed
40
>> transposedB - indicates whether b is transposed
xiaotong committed
41 42 43 44
>> bNonZeroNum - number of non-zero items in b
>> bColSize - column size of matrix b
>> bRowSize - row size of matrix b
>> c - the resulting (dense) matrix
liyinqiao committed
45 46
>> cColSize - column size of matrix c
>> cRowSize - row size of matrix c
xiaotong committed
47 48
>> alpha - the scaling factor
*/
49
__global__
xiaotong committed
50
void KernelMatrixMulDenseMSparseMV2(DTYPE * a, MATRIX_TRANS_TYPE transposedA, int aColSize, int aRowSize,
51 52
                                    void * b, MATRIX_TRANS_TYPE transposedB, int bNonZeroNum, int bColSize, int bRowSize,
                                    DTYPE * c, int cColSize, int cRowSize, DTYPE alpha)
xiaotong committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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
{
    int i = blockDim.x * blockIdx.x + threadIdx.x;

    char * bData = (char*)b;
    int tupleSize = sizeof(int) + sizeof(DTYPE);

    for (int k = 0; k < bNonZeroNum; k += blockDim.x) {
        __shared__ int   bEntryRow[MAX_CUDA_THREAD_NUM_PER_BLOCK];
        __shared__ int   bEntryCol[MAX_CUDA_THREAD_NUM_PER_BLOCK];
        __shared__ DTYPE bValue[MAX_CUDA_THREAD_NUM_PER_BLOCK];

        if (k + threadIdx.x < bNonZeroNum) {
            /* load the sub-block of the sparse matrix b */
            int key = *(int*)(bData + tupleSize * (k + threadIdx.x));

            bEntryRow[threadIdx.x] = key / bRowSize;
            bEntryCol[threadIdx.x] = key % bRowSize;
            bValue[threadIdx.x] = *(DTYPE*)(bData + tupleSize * (k + threadIdx.x) + sizeof(int));
        }

        /* synchronize to make sure the sub-block of the sparse matrix b is loaded */
        __syncthreads();

        if (i < cColSize) {
            if (transposedA == X_NOTRANS && transposedB == X_NOTRANS) {
                for (int m = 0; m < blockDim.x && k + m < bNonZeroNum; m++) {
                    DTYPE * aRow = a + aRowSize * i;
                    c[i * cRowSize + bEntryCol[m]] += aRow[bEntryRow[m]] * bValue[m] * alpha;
                }
            }
            else if (transposedA == X_TRANS && transposedB == X_NOTRANS) {
                for (int m = 0; m < blockDim.x && k + m < bNonZeroNum; m++) {
                    DTYPE * aCol = a + i;
                    c[i * cRowSize + bEntryCol[m]] += aCol[bEntryRow[m] * aRowSize] * bValue[m] * alpha;
                }
            }
            else if (transposedA == X_NOTRANS && transposedB == X_TRANS) {
                for (int m = 0; m < blockDim.x && k + m < bNonZeroNum; m++) {
                    DTYPE * aRow = a + aRowSize * i;
                    c[i * cRowSize + bEntryRow[m]] += aRow[bEntryCol[m]] * bValue[m] * alpha;
                }
            }
            else if (transposedA == X_TRANS && transposedB == X_TRANS) {
                for (int m = 0; m < blockDim.x && k + m < bNonZeroNum; m++) {
                    DTYPE * aCol = a + i;
                    c[i * cRowSize + bEntryRow[m]] += aCol[bEntryCol[m] * aRowSize] * bValue[m] * alpha;
                }
            }
        }

        /* synchronize to the preceding computation is done before loading new sub-blocks */
        __syncthreads();
    }
}


/*
matrix multiplication (for 2d tensors) (cuda version)
111

xiaotong committed
112 113
c = trans(a) * trans(b) * alpha + c * beta
where trans() return the transposed matrix if the flag is fired
114

xiaotong committed
115 116 117 118 119 120 121 122 123
>> a - tensor a
>> transposedA - indicates whether the matrices in a are transposed
>> b - tensor b
>> transposedB - indicates whether teh matrices in b are transposed
>> c - where we put a*b
>> alpha - a coefficient
>> beta - another coefficient
>> stream - the string for creating the job pipeline
*/
124
void _CudaMatrixMul2D(const XTensor * a, MATRIX_TRANS_TYPE transposedA,
125 126
                      const XTensor * b, MATRIX_TRANS_TYPE transposedB,
                      XTensor * c, DTYPE alpha, DTYPE beta, XStream * stream)
xiaotong committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
{
    int an = transposedA == X_TRANS ? a->dimSize[1] : a->dimSize[0];
    int am = transposedA == X_TRANS ? a->dimSize[0] : a->dimSize[1];
    int bn = transposedB == X_TRANS ? b->dimSize[1] : b->dimSize[0];
    int bm = transposedB == X_TRANS ? b->dimSize[0] : b->dimSize[1];
    int cn = c->dimSize[0];
    int cm = c->dimSize[1];

    CheckNTErrors((a && b && c),
        "Empty matrices in multiplication!");

    CheckNTErrors((am == bn && an == cn && bm == cm),
        "Unmatched matrices in multiplication!");

    CheckNTErrors((a->devID >= 0), "Cuda version matrix mutiplication must be run on GPUs.");

    CheckNTErrors(a->devID == b->devID && a->devID == c->devID,
        "Matrices used in multiplication are not on the same GPU.");

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

    /* a dense matrix multiply a dense matrix */
    if (!a->isSparse && !b->isSparse) {
        CheckNTErrors((!c->isSparse), "Illegal use of sparse matrix in multiplication!");

        cublasHandle_t * handle = a->mem == NULL ? GDevs.GetCudaHandle(a->devID) : a->mem->GetCublasHandle();

        /* !!!! might have problems */
        if (stream != NULL)
            cublasSetStream(*handle, stream->stream);

xuchen committed
159 160 161
        if (beta == 0)
            c->SetZeroAll();

xiaotong committed
162
        if (a->dataType == X_FLOAT && b->dataType == X_FLOAT && c->dataType == X_FLOAT) {
163 164 165 166 167 168
            _CudaBLASMatrixMUL(handle, a->data, transposedA, a->dataType, 
                               b->data, transposedB, a->dataType, c->data, c->dataType,
                               a->dimSize[0], a->dimSize[1], 
                               b->dimSize[0], b->dimSize[1], 
                               c->dimSize[0], c->dimSize[1],
                               alpha, beta);
xiaotong committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        }
        else {
            // TODO!!
            ShowNTErrors("TODO!");
        }
    }
    /* a dense matrix multiply a sparse matrix */
    else if (!a->isSparse && b->isSparse) {

        CheckNTErrors(!c->isSparse, "Illegal use of sparse matrix in multiplication!");
        CheckNTErrors((beta == 0 || beta == 1.0), "beta must be 0 or 1.");

        if (a->dataType == DEFAULT_DTYPE && b->dataType == DEFAULT_DTYPE && c->dataType == DEFAULT_DTYPE) {
            int gridSize[3], blockSize[3];

            GDevs.GetCudaThread(c->devID, a->dimSize[0], gridSize, blockSize);

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

            void * bData = (void*)((char*)b->data + sizeof(int));

            if (beta == 0)
                c->SetZeroAll();
            else if (beta != 1.0F) {
                ShowNTErrors("TODO!");
            }

            KernelMatrixMulDenseMSparseMV2 << <blocks, threads >> >((DTYPE*)a->data, transposedA, a->dimSize[0], a->dimSize[1],
                bData, transposedB, b->unitNumNonZero, b->dimSize[0], b->dimSize[1],
                (DTYPE*)c->data, c->dimSize[0], c->dimSize[1], alpha);
        }
        else {
            // TODO!!
            ShowNTErrors("TODO!");
        }

    }
    else {
        // TODO!!
        ShowNTErrors("TODO!");
    }

    BacktoCudaDev(a->devID, devIDBackup);
}
#endif // USE_CUDA

} // namespace nts(NiuTrans.Tensor)