ConcatenateSolely.cpp 3.45 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 24
#include "../../XTensor.h"
#include "../../XUtility.h"
#include "../../XName.h"
xiaotong committed
25 26 27 28 29 30 31
#include "ConcatenateSolely.h"
#include "MergeBlockLists.h"

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

/*
concatenate a list of tensors along a given dimension
32

xiaotong committed
33 34 35 36
>> smalls - a list of tensors for concatenation
>> big - the resulting tensor
>> dim - which dimension we perform the concatenation
*/
37
void _ConcatenateSolely(const TensorList * smalls, XTensor * big, int dim)
xiaotong committed
38
{
39
    CheckNTErrors(big->order > dim && dim >= 0, "Illegal dimension to concatenate!");
xiaotong committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

    int catDimSize = 0;
    int dimRDI = big->order - dim - 1;

    for (int i = 0; i < smalls->count; i++) {
        XTensor * tensor = (XTensor*)smalls->GetItem(i);
        CheckNTErrors((big->order == tensor->order), "Unmatched tensor orders!");
        for (int j = 0; j < big->order; j++) {
            if (j != dimRDI) {
                CheckNTErrors((big->dimSizeRDI[j] == tensor->dimSizeRDI[j]), "Unmatched tensor sizes!");
            }
            else {
                catDimSize += tensor->dimSizeRDI[j];
            }
        }
    }

    CheckNTErrors((catDimSize == big->dimSizeRDI[dimRDI]), "Unmatched tensor sizes!");

    int stride = 1;
    for (int i = 0; i < dimRDI; i++)
        stride *= big->dimSizeRDI[i];

    int blockNum = 1;
    for (int i = dimRDI + 1; i < big->order; i++)
        blockNum *= big->dimSizeRDI[i];

    int offset = 0;

liyinqiao committed
69 70
    /* 
    two strategies are used - we can either resort to memcpy2d for the case of
xiaotong committed
71
    concatenation of a few items, or use MergeBlockLists to merge a large number
liyinqiao committed
72 73
    of data blocks 
    */
xiaotong committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87
    if (smalls->count <= MIN_TENSOR_CAT_NUM) {
        for (int i = 0; i < smalls->count; i++) {
            XTensor * tensor = (XTensor*)smalls->GetItem(i);
            int sPitch = stride * tensor->dimSizeRDI[dimRDI] * tensor->unitSize;
            int tPitch = stride * big->dimSizeRDI[dimRDI] * big->unitSize;
            int mSize = sPitch;
            int n = blockNum;
            XMemCopy2D((char*)big->data + offset, tPitch, big->devID,
                (char*)tensor->data, sPitch, tensor->devID,
                mSize, n);
            offset += sPitch;
        }
    }
    else {
88
		StrList* sourceArrays = new StrList(smalls->count);
xiaotong committed
89 90 91 92
        int * blockSizes = new int[smalls->count];
        for (int i = 0; i < smalls->count; i++) {
            XTensor * tensor = (XTensor*)smalls->GetItem(i);
            blockSizes[i] = stride * tensor->dimSizeRDI[dimRDI] * tensor->unitSize;
93
            sourceArrays->Add((char*)tensor->data);
xiaotong committed
94 95
        }

96
        _MergeBlockLists(sourceArrays, blockSizes, blockNum, big->data, big->mem);
xiaotong committed
97 98 99 100 101

        delete[] blockSizes;
        delete sourceArrays;
    }
}
xiaotong committed
102
} // namespace nts(NiuTrans.Tensor)