FlushToMem.cu 4.93 KB
Newer Older
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 29 30 31 32 33 34
/* 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: LI Yinqiao (li.yin.qiao.2012@hotmail.com) 2018-06-14
*/

#include "FlushToMem.cuh"
#include "../../XUtility.h"

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

#ifdef USE_CUDA

/*
flush a list of XTensor to GPU memory
>> mList - list of the tensors
>> devID - target GPU id
>> GPUMem - memory pool for the GPU
*/
35
void CudaCPUToGPUFlush(TensorList * mList, int devID, XMem * GPUMem)
36 37 38 39 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 69 70 71 72 73 74 75 76 77 78 79 80
{
    if (mList == NULL || mList->count == 0)
        return;

#ifdef USE_CUDA
    int size = 0, p = 0;
    int reqiredSize = 0;

    /* compute the requried memory size */
    for (int i = 0; i < mList->count; i++) {
        XTensor * m = (XTensor*)mList->GetItem(i);

        CheckNTErrors((m->devID < 0), "Cannot do gpu-flush on matrices that are already on GPUs.");

        if (m->isSparse)
            reqiredSize = sizeof(int) + (sizeof(int) + m->unitSize) * m->unitNumNonZero;
        else
            reqiredSize = m->unitSize * m->unitNum;

        size += reqiredSize;
    }

    char * data = new char[size];
    char * GPUData = GPUMem != NULL ? (char*)GPUMem->Alloc(GPUMem->devID, size):
                                      (char*)XMemAlloc(devID, size);
    int pSize = 0;

    /* place the data in a memory block */
    for (int i = 0; i < mList->count; i++) {
        XTensor * m = (XTensor*)mList->GetItem(i);

        if (m->isSparse)
            pSize = sizeof(int) + (sizeof(int) + m->unitSize) * m->unitNumNonZero;
        else
            pSize = m->unitSize * m->unitNum;

        reqiredSize = pSize;

        memcpy(data + p, m->data, pSize);

        if (m->dataHost != NULL)
            delete[](char*)m->dataHost;

        if(m->mem == NULL)
            delete[] (char*)m->data;
xuchen committed
81 82
        else
            m->mem->Release(m->data, m->GetDataSizeInChar(), m->signature);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

        m->dataHost = NULL;
        m->data = GPUData + p;
        m->devID = GPUMem != NULL ? GPUMem->devID : devID;
        m->mem = GPUMem;

        p += reqiredSize;
    }

    /* copy from CPU memory to GPU memory */
    cudaMemcpy(GPUData, data, size, cudaMemcpyHostToDevice);

    delete[] data;
#endif
}

xuchen committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/* copy the data from GPU memory to CPU memory (memory pool) */
void CudaGPUToCPUFlush(XTensor * tensor, int devID, XMem * CPUMem)
{
#ifdef USE_CUDA
    CheckNTErrors((tensor->devID >= 0), "Cannot do cpu-flush on matrices that are already on CPU.");

    /* compute the requried memory size */
    int size = 0;
    if (tensor->isSparse)
        size = sizeof(int) + (sizeof(int) + tensor->unitSize) * tensor->unitNumNonZero;
    else
        size = tensor->unitSize * tensor->unitNum;

    char * CPUData = CPUMem != NULL ? (char*)CPUMem->Alloc(CPUMem->devID, size):
                                      (char*)XMemAlloc(devID, size);

    /* copy from CPU memory to GPU memory */
    cudaMemcpy(CPUData, tensor->data, size, cudaMemcpyDeviceToHost);

    if (tensor->dataHost != NULL)
        delete[](char*)tensor->dataHost;
    tensor->dataHost = NULL;
    tensor->mem->Release(tensor->data, tensor->GetDataSizeInChar(), tensor->signature);
    tensor->data = CPUData;
    tensor->devID = CPUMem != NULL ? CPUMem->devID : devID;
    tensor->mem = CPUMem;
#endif
}

/* copy the data from GPU memory to CPU memory ((dataHost)) and do not delete the data */
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
void CudaGPUToCPUFlush(XTensor * tensor)
{
    CheckNTErrors((sizeof(DTYPE) == tensor->unitSize), "Unsupported data type.");

    if (tensor->dataHost != NULL)
        delete[](char*)tensor->dataHost;

    if (tensor->isSparse) {
        int num = int(tensor->unitNum * tensor->denseRatio + 1);
        cudaMemcpy(&num, (DTYPE*)tensor->data, sizeof(int), cudaMemcpyDeviceToHost);

        int tupleSize = sizeof(int) + sizeof(DTYPE);
        int size = sizeof(int) + tupleSize*(num);

        CheckNTErrors((size >= 0), "Illegal data size in the sparse matrix!");

        tensor->dataHost = new char[size];
        cudaMemcpy(tensor->dataHost, tensor->data, size, cudaMemcpyDeviceToHost);
    }
    else {
        tensor->dataHost = new char[tensor->unitNum * tensor->unitSize];
        if (tensor->data != NULL)
xiaotong committed
151
            XMemCopy(tensor->dataHost, -1, tensor->data, tensor->devID, tensor->unitNum * tensor->unitSize);
152 153 154 155 156 157 158
        else
            memset(tensor->dataHost, 0, tensor->unitNum * tensor->unitSize);
    }
}
#endif // USE_CUDA

} // namespace nts(NiuTrans.Tensor)