TXMem.cpp 6.03 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 35
/* NiuTrans.Tensor - an open-source tensor library
 * Copyright (C) 2018, 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 (xiaotong@mail.neu.edu.cn) 2018-6-24
 */

#include "../XGlobal.h"
#include "../XUtility.h"
#include "TXMem.h"

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

/* case 1: test memory pool class */
bool TestXMemCase1()
{
    bool ok = true;
    int caseNum = 1000;
    int blcokSize = 16;
    int testNum = caseNum * 10;

xiaotong committed
36 37 38
    int devIDs[2];
    int devNum = 1;
    devIDs[0] = -1;
39

xiaotong committed
40 41 42 43 44 45
    /*if (GDevs.nGPU > 0) {
        devIDs[1] = 0;
        devNum = 2;
        devIDs[0] = 0;
        devNum = 1;
    }*/
46

47
    int * buf = new int[blcokSize * 10];
48

xiaotong committed
49 50
    for (int id = 0; id < devNum; id++) {
        int devID = devIDs[id];
51
        for (int iter = 0, scalar = 1; iter < 3; iter++) {
xiaotong committed
52 53 54
            XMem mem;
            mem.Initialize(devID, FREE_ON_THE_FLY, blcokSize * sizeof(int) * scalar * scalar, 1000, 0);
            mem.SetIndex(10000, blcokSize * sizeof(int) / 2);
55

xiaotong committed
56
            srand(907);
57

xiaotong committed
58 59 60 61 62 63
            int ** p = new int*[caseNum];
            int * size = new int[caseNum];

            for (int i = 0; i < caseNum; i++) {
                p[i] = NULL;
                size[i] = rand() % (2 * blcokSize);
64 65
            }

xiaotong committed
66
            for (int i = 0; i < testNum * scalar; i++) {
67
                testxmemid++;
xiaotong committed
68 69
                int j = rand() % caseNum;

70
                //fprintf(stderr, "%d %d %d\n", testxmemid, j, ok);
71
                //fprintf(stderr, "iter %d %d %d\n", iter, i, j);
72

xiaotong committed
73 74 75
                if (p[j] == NULL) {
                    p[j] = (int*)mem.AllocStandard(mem.devID, size[j] * sizeof(int));
                    for (int k = 0; k < size[j]; k++)
76 77
                        buf[k] = j;
                    XMemCopy(p[j], devID, buf, -1, sizeof(int) * size[j]);
xiaotong committed
78 79
                }
                else {
80
                    mem.ReleaseStandard(mem.devID, p[j], size[j] * sizeof(int));
xiaotong committed
81
                    for (int k = 0; k < size[j]; k++)
82 83
                        buf[k] = -1;
                    XMemCopy(p[j], devID, buf, -1, sizeof(int) * size[j]);
xiaotong committed
84 85 86 87 88
                    p[j] = NULL;
                }

                for (int k = 0; k < caseNum; k++) {
                    if (p[k] != NULL) {
89
                        XMemCopy(buf, -1, p[k], devID, sizeof(int) * size[k]);
xiaotong committed
90
                        for (int o = 0; o < size[k]; o++) {
91
                            if (buf[o] != k) {
xiaotong committed
92 93
                                ok = false;
                            }
94 95 96
                        }
                    }
                }
97 98 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
                
                /*MPieceNode * entry = NULL;
                MPieceNode * node = NULL;
                
                entry = mem.memIndex + mem.indexEntryNum + mem.FindIndexEntry(112);
                
                int cc = 0;
                node = entry->next;
                while(node != NULL){
                    fprintf(stderr, "%d ", cc++);
                    if(node->size == 0){
                        MPieceNode * next = node->next;
                        node = next;
                    }
                    else{
                        CheckNTErrors(node->pReal != NULL, "Illegal pointer!");
                        node = node->next;
                    }
                }
                fprintf(stderr, "\n");*/
                
                /*int ccc = 0;
                bool hhh = recordp != NULL ? false : true;
                for(int i = 0; i < mem.indexEntryNum; i++){
                    MPieceNode * entry = mem.memIndex + mem.indexEntryNum + i;
                    
                    MPieceNode * last = entry;
                    MPieceNode * node = entry->next;
                    
                    ccc = 0;
                    while(node != NULL){
                        CheckNTErrors(node->pre == last, "XSomething is wrong!");
                        CheckNTErrors(last->next == node, "XSomething is wrong!");
                        
                        last = node;
                        
                        ccc++;
                        if(node->pReal == recordp){
                            hhh = true;
                        }
                        
                        if(node->size == 0){
                            MPieceNode * next = node->next;
                            node = next;
                        }
                        else{
                            CheckNTErrors(node->pReal != NULL, "Illegal pointer!");
                            node = node->next;
                        }
                    }
                }
                
                if(!hhh){
                    int nnn = 0;
                }*/
152 153
            }

xiaotong committed
154 155 156 157
            delete[] p;
            delete[] size;
            scalar *= 2;
        }
158 159
    }

160 161
    delete[] buf;

162 163 164 165 166 167 168 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
    return ok;
}

/* test for memory pool class */
bool TestXMem()
{
    XPRINT(0, stdout, "[Test] Memory pool ... Began\n");
    bool returnFlag = true;
    bool caseFlag = true;

    double startT = GetClock();

    /* case 1 test */
    caseFlag = TestXMemCase1();
    if (!caseFlag) {
        returnFlag = false;
        XPRINT(0, stdout, ">> case 1 failed!\n");
    }
    else
        XPRINT(0, stdout, ">> case 1 passed!\n");

    if (returnFlag) {
        XPRINT(0, stdout, ">> All Passed!\n");
    }
    else
        XPRINT(0, stdout, ">> Failed!\n");

    double endT = GetClock();

    XPRINT1(0, stdout, "[Test] Finished (took %.3lfms)\n\n", endT - startT);

    return returnFlag;
}

196
} // namespace nts(NiuTrans.Tensor)