File: far_perf.cpp

package info (click to toggle)
opensubdiv 3.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 75,132 kB
  • sloc: cpp: 137,820; python: 1,069; objc: 412; javascript: 361; lisp: 216; ansic: 170; makefile: 24
file content (404 lines) | stat: -rw-r--r-- 13,361 bytes parent folder | download | duplicates (2)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//
//   Copyright 2015 Pixar
//
//   Licensed under the Apache License, Version 2.0 (the "Apache License")
//   with the following modification; you may not use this file except in
//   compliance with the Apache License and the following modification to it:
//   Section 6. Trademarks. is deleted and replaced with:
//
//   6. Trademarks. This License does not grant permission to use the trade
//      names, trademarks, service marks, or product names of the Licensor
//      and its affiliates, except as required to comply with Section 4(c) of
//      the License and to reproduce the content of the NOTICE file.
//
//   You may obtain a copy of the Apache License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the Apache License with the above modification is
//   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//   KIND, either express or implied. See the Apache License for the specific
//   language governing permissions and limitations under the Apache License.
//

#include <cassert>
#include <cstdio>
#include <fstream>
#include <sstream>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <opensubdiv/far/primvarRefiner.h>
#include <opensubdiv/far/stencilTableFactory.h>
#include <opensubdiv/far/patchTableFactory.h>

#include "../../regression/common/far_utils.h"
#include "../../examples/common/stopwatch.h"

#include "init_shapes.h"

//------------------------------------------------------------------------------

using namespace OpenSubdiv;

struct TestOptions {
    TestOptions() :
        refineLevel(2),
        refineAdaptive(true),
        createPatches(true),
        createStencils(true),
        endCapType(Far::PatchTableFactory::Options::ENDCAP_GREGORY_BASIS) { }

    int  refineLevel;
    bool refineAdaptive;
    bool createPatches;
    bool createStencils;

    Far::PatchTableFactory::Options::EndCapType endCapType;
};

struct TestResult {
    TestResult() :
        level(-1),
        timeTotal(0),
        timeRefine(0),
        timePatchFactory(0),
        timeStencilFactory(0),
        timeAppendStencil(0) { }

    std::string name;
    int level;
    double timeTotal;
    double timeRefine;
    double timePatchFactory;
    double timeStencilFactory;
    double timeAppendStencil;
};

template <typename REAL>
static TestResult
RunPerfTest(Shape const & shape, TestOptions const & options) {

    typedef Far::StencilTableReal<REAL>        FarStencilTable;
    typedef Far::StencilTableFactoryReal<REAL> FarStencilTableFactory;

    Sdc::SchemeType sdcType = GetSdcType(shape);
    Sdc::Options sdcOptions = GetSdcOptions(shape);

    TestResult result;
    result.level = options.refineLevel;

    Stopwatch s; 

    // ----------------------------------------------------------------------
    // Configure the patch table factory options
    Far::PatchTableFactory::Options poptions(options.refineLevel);
    poptions.SetEndCapType(options.endCapType);
    poptions.SetPatchPrecision<REAL>();

    // ----------------------------------------------------------------------
    // Instantiate a FarTopologyRefiner from the descriptor and refine
    Far::TopologyRefiner * refiner = Far::TopologyRefinerFactory<Shape>::Create(
        shape, Far::TopologyRefinerFactory<Shape>::Options(sdcType, sdcOptions));
    assert(refiner);

    s.Start();
    if (options.refineAdaptive) {
        Far::TopologyRefiner::AdaptiveOptions rOptions =
            poptions.GetRefineAdaptiveOptions();
        refiner->RefineAdaptive(rOptions);
    } else {
        Far::TopologyRefiner::UniformOptions rOptions(options.refineLevel);
        refiner->RefineUniform(rOptions);
    }
    s.Stop();
    result.timeRefine = s.GetElapsed();

    // ----------------------------------------------------------------------
    // Create patch table
    Far::PatchTable const * patchTable = NULL;
    if (options.createPatches) {
        s.Start();
        patchTable = Far::PatchTableFactory::Create(*refiner, poptions);
        s.Stop();
        result.timePatchFactory = s.GetElapsed();
    } else {
        result.timePatchFactory = 0;
    }

    // ----------------------------------------------------------------------
    // Create stencil table
    FarStencilTable const * vertexStencils = NULL;
    if (options.createStencils) {
        s.Start();
        vertexStencils = FarStencilTableFactory::Create(*refiner);
        s.Stop();
        result.timeStencilFactory = s.GetElapsed();
    } else {
        result.timeStencilFactory = 0;
    }

    // ----------------------------------------------------------------------
    // append local points to stencils
    if (options.createPatches && options.createStencils) {
        s.Start();

        if (FarStencilTable const *vertexStencilsWithLocalPoints =
            FarStencilTableFactory::AppendLocalPointStencilTable(
                *refiner, vertexStencils,
                patchTable->GetLocalPointStencilTable<REAL>())) {
            delete vertexStencils;
            vertexStencils = vertexStencilsWithLocalPoints;
        }

        s.Stop();
        result.timeAppendStencil = s.GetElapsed();
    } else {
        result.timeAppendStencil = 0;
    }

    // ---------------------------------------------------------------------
    result.timeTotal = s.GetTotalElapsed();

    delete vertexStencils;
    delete patchTable;
    delete refiner;

    return result;
}

//------------------------------------------------------------------------------

struct PrintOptions {
    PrintOptions() :
        csvFormat(false),
        refineTime(true),
        patchTime(true),
        stencilTime(true),
        appendTime(true),
        totalTime(true) { }

    bool csvFormat;
    bool refineTime;
    bool patchTime;
    bool stencilTime;
    bool appendTime;
    bool totalTime;
};

static void
PrintShape(ShapeDesc const & shapeDesc, PrintOptions const & ) {

    static char const * g_schemeNames[3] = { "bilinear", "catmark", "loop" };

    char const * shapeName   = shapeDesc.name.c_str();
    Scheme       shapeScheme = shapeDesc.scheme;

    printf("%s (%s):\n", shapeName, g_schemeNames[shapeScheme]);
}

static void
PrintResult(TestResult const & result, PrintOptions const & options) {

    //  If only printing the total, combine on same line as level:
    if (!options.refineTime  && !options.patchTime &&
        !options.stencilTime && !options.appendTime) {
        printf("  level %d:  %f\n", result.level, result.timeTotal);
        return;
    }

    printf("  level %d:\n", result.level);

    if (options.refineTime) {
        printf("    TopologyRefiner::Refine     %f %5.2f%%\n",
               result.timeRefine,
               result.timeRefine/result.timeTotal*100);
    }
    if (options.patchTime) {
        printf("    PatchTableFactory::Create   %f %5.2f%%\n",
               result.timePatchFactory,
               result.timePatchFactory/result.timeTotal*100);
    }
    if (options.stencilTime) {
        printf("    StencilTableFactory::Create %f %5.2f%%\n",
               result.timeStencilFactory,
               result.timeStencilFactory/result.timeTotal*100);
    }
    if (options.appendTime) {
        printf("    StencilTableFactory::Append %f %5.2f%%\n",
               result.timeAppendStencil,
               result.timeAppendStencil/result.timeTotal*100);
    }
    if (options.totalTime) {
        printf("    Total                       %f\n",
               result.timeTotal);
    }
}

static void
PrintHeaderCSV(PrintOptions const & options) {

    // spreadsheet header row
    printf("shape");
    printf(",level");
    if (options.refineTime)  printf(",refine");
    if (options.patchTime)   printf(",patch");
    if (options.stencilTime) printf(",stencilFactory");
    if (options.appendTime)  printf(",stencilAppend");
    if (options.totalTime)   printf(",total");
    printf("\n");
}
static void
PrintResultCSV(TestResult const & result, PrintOptions const & options) {

    // spreadsheet data row
    printf("%s",  result.name.c_str());
    printf(",%d", result.level);
    if (options.refineTime)  printf(",%f", result.timeRefine);
    if (options.patchTime)   printf(",%f", result.timePatchFactory);
    if (options.stencilTime) printf(",%f", result.timeStencilFactory);
    if (options.appendTime)  printf(",%f", result.timeAppendStencil);
    if (options.totalTime)   printf(",%f", result.timeTotal);
    printf("\n");
}

//------------------------------------------------------------------------------

static int
parseIntArg(char const * argString, int dfltValue = 0) {
    char *argEndptr;
    int argValue = (int) strtol(argString, &argEndptr, 10);
    if (*argEndptr != 0) {
        fprintf(stderr,
                "Warning: non-integer option parameter '%s' ignored\n",
                argString);
        argValue = dfltValue;
    }
    return argValue;
}

int main(int argc, char **argv)
{
    TestOptions testOptions;
    PrintOptions printOptions;
    std::vector<std::string> objFiles;
    Scheme defaultScheme = kCatmark;
    int minLevel = 1;
    int maxLevel = 2;
    bool runDouble = false;

    for (int i = 1; i < argc; ++i) {
        if (strstr(argv[i], ".obj")) {
            objFiles.push_back(std::string(argv[i]));
        } else if (!strcmp(argv[i], "-a")) {
            testOptions.refineAdaptive = true;
        } else if (!strcmp(argv[i], "-u")) {
            testOptions.refineAdaptive = false;
        } else if (!strcmp(argv[i], "-l")) {
            if (++i < argc) maxLevel = parseIntArg(argv[i], maxLevel);
        } else if (!strcmp(argv[i], "-bilinear")) {
            defaultScheme = kBilinear;
        } else if (!strcmp(argv[i], "-catmark")) {
            defaultScheme = kCatmark;
        } else if (!strcmp(argv[i], "-loop")) {
            defaultScheme = kLoop;
        } else if (!strcmp(argv[i], "-e")) {
            char const * type = argv[++i];
            if (!strcmp(type, "linear")) {
                testOptions.endCapType =
                        Far::PatchTableFactory::Options::ENDCAP_BILINEAR_BASIS;
            } else if (!strcmp(type, "regular")) {
                testOptions.endCapType =
                        Far::PatchTableFactory::Options::ENDCAP_BSPLINE_BASIS;
            } else if (!strcmp(type, "gregory")) {
                testOptions.endCapType =
                        Far::PatchTableFactory::Options::ENDCAP_GREGORY_BASIS;
            } else {
                fprintf(stderr, "Error: Unknown endcap type %s\n", type);
                return 1;
            }
        } else if (!strcmp(argv[i], "-double")) {
            runDouble = true;
        } else if (!strcmp(argv[i], "-nopatches")) {
            testOptions.createPatches = false;

            printOptions.patchTime  = false;
            printOptions.appendTime = false;
        } else if (!strcmp(argv[i], "-nostencils")) {
            testOptions.createStencils = false;

            printOptions.stencilTime = false;
            printOptions.appendTime  = false;
        } else if (!strcmp(argv[i], "-total")) {
            printOptions.refineTime  = false;
            printOptions.patchTime   = false;
            printOptions.stencilTime = false;
            printOptions.appendTime  = false;
        } else if (!strcmp(argv[i], "-csv")) {
            printOptions.csvFormat = true;
        } else {
            fprintf(stderr,
                "Warning: unrecognized argument '%s' ignored\n", argv[i]);
        }
    }
    assert(minLevel <= maxLevel);

    if (!objFiles.empty()) {
        for (size_t i = 0; i < objFiles.size(); ++i) {
            char const * objFile = objFiles[i].c_str();
            std::ifstream ifs(objFile);
            if (ifs) {
                std::stringstream ss;
                ss << ifs.rdbuf();
                ifs.close();
                g_shapes.push_back(ShapeDesc(objFile, ss.str(), defaultScheme));
            } else {
                fprintf(stderr,
                    "Warning: cannot open shape file '%s'\n", objFile);
            }
        }
    }

    if (g_shapes.empty()) {
        initShapes();
    }

    //  For each shape, run tests for all specified levels -- printing the
    //  results in the specified format:
    //
    if (printOptions.csvFormat) {
        PrintHeaderCSV(printOptions);
    }
    for (size_t i = 0; i < g_shapes.size(); ++i) {
        ShapeDesc const & shapeDesc = g_shapes[i];
        Shape const * shape = Shape::parseObj(shapeDesc);

        if (!printOptions.csvFormat) {
            PrintShape(shapeDesc, printOptions);
        }

        for (int levelIndex = minLevel; levelIndex <= maxLevel; ++levelIndex) {
            testOptions.refineLevel = levelIndex;

            TestResult result;
            if (runDouble) {
                result = RunPerfTest<double>(*shape, testOptions);
            } else {
                result = RunPerfTest<float>(*shape, testOptions);
            }
            result.name = shapeDesc.name;

            if (printOptions.csvFormat) {
                PrintResultCSV(result, printOptions);
            } else {
                PrintResult(result, printOptions);
            }
        }
        delete shape;
    }
}

//------------------------------------------------------------------------------