Tutorial 28 - Node with simple OGN computeVectorized

This tutorial demonstrates how to compose nodes that implements a very simple computeVectorized function. It shows how to access the data, using the different available methods.

OgnTutorialVectorizedPassthrough.ogn

The ogn file shows the implementation of a node named “omni.graph.tutorials.TutorialVectorizedPassThrough”, which takes input of a floating point value, and just copy it to its output.

 1{
 2    "TutorialVectorizedPassThrough": {
 3        "version": 1,
 4        "description": "Simple passthrough node that copy its input to its output in a vectorized way",
 5        "categories": "tutorials",
 6        "uiName": "Tutorial Node: Vectorized Passthrough",
 7        "inputs":{
 8            "value":{
 9                "type": "float",
10                "description": "input value"
11            }
12        },
13        "outputs": {
14            "value": {
15                "type": "float",
16                "description": "output value"
17            }
18        },
19        "tests" : [
20            { "inputs:value": 1, "outputs:value": 1 },
21            { "inputs:value": 2, "outputs:value": 2 },
22            { "inputs:value": 3, "outputs:value": 3 },
23            { "inputs:value": 4, "outputs:value": 4 }
24        ]
25    }
26}

OgnTutorialVectorizedPassthrough.cpp

The cpp file contains the implementation of the node. It takes a floating point input and just copy it to its output, demonstrating how to handle a vectorized compute. It shows what would be the implementation for a regular compute function, and the different way it could implement a computeVectorized function. - method #1: by switching the entire database to the next instance, while performing the computation in a loop - method #2: by directly indexing attributes for the right instance in a loop - method #3: by retrieving the raw data, and working directly with it

 1// SPDX-FileCopyrightText: Copyright (c) 2023-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 2// SPDX-License-Identifier: LicenseRef-NvidiaProprietary
 3//
 4// NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
 5// property and proprietary rights in and to this material, related
 6// documentation and any modifications thereto. Any use, reproduction,
 7// disclosure or distribution of this material and related documentation
 8// without an express license agreement from NVIDIA CORPORATION or
 9// its affiliates is strictly prohibited.
10#include <OgnTutorialVectorizedPassthroughDatabase.h>
11
12// The method used by the computation to perform the passthrough
13// 0 = simple copy, no vectorization
14#define TUTO_PASSTHROUGH_METHOD_SIMPLE 0
15
16// 1 = vectorized copy by moving the entire database to the next instance
17#define TUTO_PASSTHROUGH_METHOD_DB 1
18
19// 2 = vectorized copy by indexing the instance directly per attribute
20#define TUTO_PASSTHROUGH_METHOD_ATTR 2
21
22// 3 = vectorized copy using raw data
23#define TUTO_PASSTHROUGH_METHOD_RAW 3
24
25// By default, use the most efficient method
26#define TUTO_PASSTHROUGH_METHOD TUTO_PASSTHROUGH_METHOD_RAW
27
28// This node perform a copy of its input to its output
29class OgnTutorialVectorizedPassthrough
30{
31public:
32#if TUTO_PASSTHROUGH_METHOD == TUTO_PASSTHROUGH_METHOD_SIMPLE
33    // begin-regular
34    static bool compute(OgnTutorialVectorizedPassthroughDatabase& db)
35    {
36        db.outputs.value() = db.inputs.value();
37        return true;
38    }
39    // end-regular
40#elif TUTO_PASSTHROUGH_METHOD == TUTO_PASSTHROUGH_METHOD_DB
41    // begin-db
42    static size_t computeVectorized(OgnTutorialVectorizedPassthroughDatabase& db, size_t count)
43    {
44        for (size_t idx = 0; idx < count; ++idx)
45        {
46            db.outputs.value() = db.inputs.value();
47            db.moveToNextInstance();
48        }
49        return count;
50    }
51    // end-db
52#elif TUTO_PASSTHROUGH_METHOD == TUTO_PASSTHROUGH_METHOD_ATTR
53    // begin-attr
54    static size_t computeVectorized(OgnTutorialVectorizedPassthroughDatabase& db, size_t count)
55    {
56        for (size_t idx = 0; idx < count; ++idx)
57            db.outputs.value(idx) = db.inputs.value(idx);
58        return count;
59    }
60    // end-attr
61#elif TUTO_PASSTHROUGH_METHOD == TUTO_PASSTHROUGH_METHOD_RAW
62    // begin-raw
63    static size_t computeVectorized(OgnTutorialVectorizedPassthroughDatabase& db, size_t count)
64    {
65        auto spanIn = db.inputs.value.vectorized(count);
66        auto spanOut = db.outputs.value.vectorized(count);
67
68        memcpy(spanOut.data(), spanIn.data(), std::min(spanIn.size_bytes(), spanOut.size_bytes()));
69
70        return count;
71    }
72    // end-raw
73#endif
74};
75
76REGISTER_OGN_NODE()