File size: 12,636 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
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
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
152
153
154
155
156
157
158
159
160
161
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*
  
  Author(s):  Anton Deguet
  Created on: 2007-08-02
  
  (C) Copyright 2007-2019 Johns Hopkins University (JHU), All Rights Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---
*/

#include <cisstVector/vctDynamicNArray.h>
#include <cisstVector/vctDynamicNArrayRef.h>
#include <cisstVector/vctDynamicVector.h>
#include <cisstVector/vctContainerTraits.h>
#include <cisstOSAbstraction/osaStopwatch.h>
#include <cisstCommon/cmnRandomSequence.h>
#include <cisstVector/vctRandom.h>
#include <cisstCommon/cmnPrintf.h>
#include <iostream>

using namespace std;

/* test "parameters" */
const unsigned int test_size = 60;
typedef double value_type;
const unsigned int numIterations = 10;

/* derived typedef */
typedef vctDynamicVector<value_type> VectorType;
typedef vctDynamicVectorRef<value_type> VectorRefType;
typedef vctDynamicMatrix<value_type> MatrixType;
typedef vctDynamicMatrixRef<value_type> MatrixRefType;
typedef vctDynamicNArray<value_type, 1> NArray1Type;
typedef vctDynamicNArray<value_type, 2> NArray2Type;
typedef vctDynamicNArray<value_type, 3> NArray3Type;
typedef vctDynamicNArray<value_type, 4> NArray4Type;
typedef vctDynamicNArrayRef<value_type, 1> NArray1RefType;
typedef vctDynamicNArrayRef<value_type, 2> NArray2RefType;
typedef vctDynamicNArrayRef<value_type, 3> NArray3RefType;
typedef vctDynamicNArrayRef<value_type, 4> NArray4RefType;


int main()
{
    /* allocate two large blocks used for all tests. */
    NArray4Type::nsize_type size4(test_size);
    NArray3Type::nsize_type size3(test_size);
    NArray2Type::nsize_type size2(test_size);
    
    /* create parent containers */
    NArray4Type sourceNArray;
    NArray4Type destinationNArray;
    sourceNArray.SetSize(size4);
    destinationNArray.SetSize(size4);

    /* create parent container overlay for fast random and tests */
    const size_t totalSize = sourceNArray.size(); /* aka size^4 */
    VectorRefType sourceVector(totalSize, sourceNArray.Pointer());
    VectorRefType destinationVector(totalSize, destinationNArray.Pointer());

    /* create parent container matrix */
    const unsigned int matrixSize = test_size * test_size;
    MatrixRefType sourceMatrix(matrixSize, matrixSize, sourceNArray.Pointer());
    MatrixRefType destinationMatrix(matrixSize, matrixSize, destinationNArray.Pointer());

    /* create timers */
    osaStopwatch timer1, timer2, timer3, timer4, timer5, timer6;
    timer1.Reset(); timer2.Reset();
    timer3.Reset(); timer4.Reset();
    timer5.Reset(); timer6.Reset();

    VectorRefType compactVectorSource, compactVectorDestination, nonCompactVectorSource, nonCompactVectorDestination;
    MatrixRefType compactMatrixSource, compactMatrixDestination, nonCompactMatrixSource, nonCompactMatrixDestination;

    /* get started */
    std::cout << "This program is used to show how the different cisstVector engines perform.  To test mostly the memory access performances, we test the XoXi engines using Assign().  Engines tested are VoVi, MoMi, NoNi and CoCi.\n\n";
    std::cout << "Quit all CPU intensive applications, type any character\nand press <ENTER> to begin benchmark:\n";
    char dummy[5];
    std::cin >> dummy;
    unsigned int i;


    /* ---- TEST ---- */
    std::cout << "Comparing optimized engines on compact containers\n" 
              << cmnPrintf("%5s%15s%15s%15s\n")
              << ""
              << "vector"
              << "matrix"
              << "nArray";

    for (i = 0; i <= numIterations; ++i) {
        /* vectors */
        vctRandom(sourceVector, value_type(-20), value_type(20));
        timer1.Start();
        destinationVector.Assign(sourceVector);
        timer1.Stop();

        /* matrices */
        vctRandom(sourceMatrix, value_type(-20), value_type(20));
        timer2.Start();
        destinationMatrix.Assign(sourceMatrix);
        timer2.Stop();

        /* nArrays */
        vctRandom(sourceNArray, value_type(-20), value_type(20));
        timer3.Start();
        destinationNArray.Assign(sourceNArray);
        timer3.Stop();

        /* skip first iteration to "prime" the pump */
        if (i == 0) {
            timer1.Reset(); timer2.Reset(); timer3.Reset();
        } else {
            /* show progress */
            std::cout << cmnPrintf("%5d%15.6f%15.6f%15.6f\n")
                      << i
                      << timer1.GetElapsedTime()
                      << timer2.GetElapsedTime()
                      << timer3.GetElapsedTime();
            std::cout << std::flush;
        }
    }
    
    std::cout << cmnPrintf("%5s%15.6f%15.6f%15.6f\n\n")
              << "TOTAL"
              << timer1.GetElapsedTime()
              << timer2.GetElapsedTime()
              << timer3.GetElapsedTime();


    /* ---- TEST ---- */
    std::cout << "Comparing engines on 5 different 1D containers:\ncompact vec/mat, non compact vec/mat and non compact array\n"
              << "As we are using the XoXi engines to assign, we also compare to FastCopyOf\n"
              << cmnPrintf("%5s%15s%15s%15s%15s%15s%15s\n")
              << ""
              << "comp vec"
              << "non comp vec"
              << "comp mat"
              << "non comp mat"
              << "non comp arr"
              << "fast copy";

    /* size and stride that would fit in source */
    const unsigned int vectorSize = test_size * test_size * test_size * test_size / 2;
    const int vectorStride = 2;
    /* create all required references */
    compactVectorSource.SetRef(vectorSize, sourceNArray.Pointer(), 1); 
    compactVectorDestination.SetRef(vectorSize, destinationNArray.Pointer(), 1); 

    nonCompactVectorSource.SetRef(vectorSize, sourceNArray.Pointer(), vectorStride); 
    nonCompactVectorDestination.SetRef(vectorSize, destinationNArray.Pointer(), vectorStride); 

    compactMatrixSource.SetRef(1, vectorSize, sourceNArray.Pointer(), VCT_ROW_MAJOR);
    compactMatrixDestination.SetRef(1, vectorSize, destinationNArray.Pointer(), VCT_ROW_MAJOR);

    nonCompactMatrixSource.SetRef(1, vectorSize, vectorSize * 2, 2, sourceNArray.Pointer());
    nonCompactMatrixDestination.SetRef(1, vectorSize, vectorSize * 2, 2, destinationNArray.Pointer());

    NArray1RefType nonCompactNArray1Source(sourceNArray.Pointer(), NArray1Type::nsize_type(vectorSize), NArray1Type::nstride_type(vectorStride));
    NArray1RefType nonCompactNArray1Destination(destinationNArray.Pointer(), NArray1Type::nsize_type(vectorSize), NArray1Type::nstride_type(vectorStride));

    for (i = 0; i <= numIterations; ++i) {
        /* compact vectors */
        vctRandom(compactVectorSource, value_type(-20), value_type(20));
        timer1.Start();
        compactVectorDestination.Assign(compactVectorSource);
        timer1.Stop();

        /* non compact vectors */
        vctRandom(nonCompactVectorSource, value_type(-20), value_type(20));
        timer2.Start();
        nonCompactVectorDestination.Assign(nonCompactVectorSource);
        timer2.Stop();

        /* compact matrices */
        vctRandom(compactMatrixSource, value_type(-20), value_type(20));
        timer3.Start();
        compactMatrixDestination.Assign(compactMatrixSource);
        timer3.Stop();

        /* non compact matrices */
        vctRandom(nonCompactMatrixSource, value_type(-20), value_type(20));
        timer4.Start();
        nonCompactMatrixDestination.Assign(nonCompactMatrixSource);
        timer4.Stop();

        /* non compact nArrays */
        vctRandom(nonCompactNArray1Source, value_type(-20), value_type(20));
        timer5.Start();
        nonCompactNArray1Destination.Assign(nonCompactNArray1Source);
        timer5.Stop();

        /* fast copy */
        vctRandom(compactVectorSource, value_type(-20), value_type(20));
        timer6.Start();
        compactVectorDestination.FastCopyOf(compactVectorSource);
        timer6.Stop();

        /* skip first iteration to "prime" the pump */
        if (i == 0) {
            timer1.Reset(); timer2.Reset();
            timer3.Reset(); timer4.Reset();
            timer5.Reset(); timer6.Reset();
        } else {
            /* show progress */
            std::cout << cmnPrintf("%5d%15.6f%15.6f%15.6f%15.6f%15.6f%15.6f\n")
                      << i
                      << timer1.GetElapsedTime()
                      << timer2.GetElapsedTime()
                      << timer3.GetElapsedTime()
                      << timer4.GetElapsedTime()
                      << timer5.GetElapsedTime()
                      << timer6.GetElapsedTime();
            std::cout << std::flush;
        }
    }
    
    std::cout << cmnPrintf("%5s%15.6f%15.6f%15.6f%15.6f%15.6f%15.6f\n\n")
              << "TOTAL"
              << timer1.GetElapsedTime()
              << timer2.GetElapsedTime()
              << timer3.GetElapsedTime()
              << timer4.GetElapsedTime()
              << timer5.GetElapsedTime()
              << timer6.GetElapsedTime();


    /* ---- TEST ---- */
    std::cout << "Comparing engines on 3 different 2D containers:\ncompact mat, non compact mat and non compact array\n"
              << "As we are using the XoXi engines to assign, we also compare to FastCopyOf\n"
              << cmnPrintf("%5s%15s%15s%15s%15s\n")
              << ""
              << "comp mat"
              << "non comp mat"
              << "non comp arr"
              << "fast copy";

    /* size and stride that would fit in source */
    const unsigned int rows = test_size * test_size;
    const unsigned int cols = test_size * test_size / 2;
    const int rowStride = cols * 2;
    const int colStride = 2;

    /* create all required references */
    compactMatrixSource.SetRef(rows, cols, sourceNArray.Pointer(), VCT_ROW_MAJOR);
    compactMatrixDestination.SetRef(rows, cols, destinationNArray.Pointer(), VCT_ROW_MAJOR);

    nonCompactMatrixSource.SetRef(rows, cols, rowStride, colStride, sourceNArray.Pointer());
    nonCompactMatrixDestination.SetRef(rows, cols, rowStride, colStride, destinationNArray.Pointer());

    NArray2RefType nonCompactNArray2Source(sourceNArray.Pointer(), NArray2Type::nsize_type(rows, cols), NArray2Type::nstride_type(rowStride, colStride));
    NArray2RefType nonCompactNArray2Destination(destinationNArray.Pointer(), NArray2Type::nsize_type(rows, cols), NArray2Type::nstride_type(rowStride, colStride));

    for (i = 0; i <= numIterations; ++i) {
        /* compact matrices */
        vctRandom(compactMatrixSource, value_type(-20), value_type(20));
        timer1.Start();
        compactMatrixDestination.Assign(compactMatrixSource);
        timer1.Stop();

        /* non compact matrices */
        vctRandom(nonCompactMatrixSource, value_type(-20), value_type(20));
        timer2.Start();
        nonCompactMatrixDestination.Assign(nonCompactMatrixSource);
        timer2.Stop();

        /* non compact nArrays */
        vctRandom(nonCompactNArray2Source, value_type(-20), value_type(20));
        timer3.Start();
        nonCompactNArray2Destination.Assign(nonCompactNArray2Source);
        timer3.Stop();

        /* fast copy */
        vctRandom(compactMatrixSource, value_type(-20), value_type(20));
        timer4.Start();
        compactMatrixDestination.FastCopyOf(compactMatrixSource);
        timer4.Stop();

        /* skip first iteration to "prime" the pump */
        if (i == 0) {
            timer1.Reset(); timer2.Reset();
            timer3.Reset(); timer4.Reset();
        } else {
            /* show progress */
            std::cout << cmnPrintf("%5d%15.6f%15.6f%15.6f%15.6f\n")
                      << i
                      << timer1.GetElapsedTime()
                      << timer2.GetElapsedTime()
                      << timer3.GetElapsedTime()
                      << timer4.GetElapsedTime();
            std::cout << std::flush;
        }
    }
    
    std::cout << cmnPrintf("%5s%15.6f%15.6f%15.6f%15.6f\n\n")
              << "TOTAL"
              << timer1.GetElapsedTime()
              << timer2.GetElapsedTime()
              << timer3.GetElapsedTime()
              << timer4.GetElapsedTime();


    /* ---- TEST ---- */
    /* TO DO test on 4D, compact array, non compact array, fast copy?  That would require to add fast copy. */
    /* instead of non compact, use storage order / permutation to disable optimized engines */

    return 0;
}