Hello World
在我们的第一个示例中,我们将了解如何使用 Holoscan SDK 创建 Hello World 示例。
在本示例中,我们将介绍
如何定义您的应用程序类。
如何定义单算子工作流。
如何使用
CountCondition
来限制算子的执行次数。
示例源代码和运行说明可以在 GitHub 上的 examples 目录中找到,或者在 NGC 容器和 Debian 软件包中的 /opt/nvidia/holoscan/examples
下找到,以及它们的可执行文件。
有关更多详细信息,请参阅 定义应用程序类 部分。
我们定义了从 holoscan 的 Application
基类继承的 HelloWorldApp
类。应用程序的实例在 main
中创建。run()
方法将启动应用程序。
class HelloWorldApp : public holoscan::Application {
public:
void compose() override {
using namespace holoscan;
// Define the operators, allowing the hello operator to execute once
auto hello = make_operator<ops::HelloWorldOp>("hello", make_condition<CountCondition>(1));
// Define the workflow by adding operator into the graph
add_operator(hello);
}
};
int main(int argc, char** argv) {
auto app = holoscan::make_application<HelloWorldApp>();
app->run();
return 0;
}
class HelloWorldApp(Application):
def compose(self):
# Define the operators
hello = HelloWorldOp(self, CountCondition(self, 1), name="hello")
# Define the one-operator workflow
self.add_operator(hello)
def main():
app = HelloWorldApp()
app.run()
if __name__ == "__main__":
main()
有关更多详细信息,请参阅 应用程序工作流 部分。
在定义您的应用程序类时,主要任务是定义应用程序中使用的算子,以及它们之间的互连性以定义应用程序工作流。HelloWorldApp
使用最简单的工作流形式,它由单个算子组成:HelloWorldOp
。
为了第一个示例,我们将忽略定义自定义算子的细节,而专注于以下突出显示的信息:当此算子运行时 (compute
),它将向标准输出打印 Hello World!
class HelloWorldOp : public Operator {
public:
HOLOSCAN_OPERATOR_FORWARD_ARGS(HelloWorldOp)
HelloWorldOp() = default;
void setup(OperatorSpec& spec) override {
}
void compute(InputContext& op_input, OutputContext& op_output,
ExecutionContext& context) override {
std::cout << std::endl;
std::cout << "Hello World!" << std::endl;
std::cout << std::endl;
}
};
class HelloWorldOp(Operator):
"""Simple hello world operator.
This operator has no ports.
On each tick, this operator prints out hello world.
"""
def setup(self, spec: OperatorSpec):
pass
def compute(self, op_input, op_output, context):
print("")
print("Hello World!")
print("")
应用程序工作流的定义发生在应用程序的 compose()
方法中。在那里,我们首先创建上面定义的 HelloWorldOp
算子的实例,然后使用 add_operator()
将其添加到我们的简单工作流中。
class HelloWorldApp : public holoscan::Application {
public:
void compose() override {
using namespace holoscan;
// Define the operators, allowing the hello operator to execute once
auto hello = make_operator<ops::HelloWorldOp>("hello", make_condition<CountCondition>(1));
// Define the workflow by adding operator into the graph
add_operator(hello);
}
};
class HelloWorldApp(Application):
def compose(self):
# Define the operators
hello = HelloWorldOp(self, CountCondition(self, 1), name="hello")
# Define the one-operator workflow
self.add_operator(hello)
Holoscan 应用程序处理流数据,因此算子的 compute()
方法将持续调用,直到出现某种情况导致算子停止。对于我们的 Hello World 示例,我们希望仅执行算子一次。我们可以通过传递 CountCondition
对象作为参数给算子的构造函数来施加这样的条件。
有关更多详细信息,请参阅 配置算子条件 部分。
运行应用程序应该在您的终端中给出以下输出
Hello World!
恭喜!您已成功运行您的第一个 Holoscan SDK 应用程序!