NVIDIA Index example code nvidia_logo_transpbg.gif Up
session.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright 2023 NVIDIA Corporation. All rights reserved.
3 *****************************************************************************/
6
7#include <mi/dice.h>
8#include <sstream>
9#include <map>
10
11#include <nv/index/app/index_connect.h>
12#include <nv/index/app/string_dict.h>
13
14// Include code shared by examples.
15#include "utility/example_shared.h"
16
17#include <nv/index/iindex.h>
18#include <nv/index/isession.h>
19
20//----------------------------------------------------------------------
21// This example creates an session.
22int main(int argc, const char* argv[])
23{
24 const std::string com_name(argv[0]);
25
26 nv::index::app::String_dict sdict;
27 sdict.insert("dice::verbose", "3"); // log level
28 sdict.insert("unittest", "0"); // default mode
29 sdict.insert("is_dump_comparison_image_when_failed", "1"); // default: dump images when failed.
30 sdict.insert("is_call_from_test", "0"); // default: not call from make check.
31
32 if (nv::index::app::get_bool(sdict.get("unittest", "false")))
33 {
34 if (nv::index::app::get_bool(sdict.get("is_call_from_test", "false")))
35 {
36 sdict.insert("is_dump_comparison_image_when_failed", "0");
37 }
38 sdict.insert("dice::verbose", "2");
39 }
40 info_cout(std::string("running ") + com_name, sdict);
41
42 {
43 // This is a index_connect scope.
44
45 // Load IndeX library via Index_connect
46 nv::index::app::Index_connect index_connect;
47 index_connect.initialize(argc, argv, sdict);
48
49 // This scope is for handles
50 {
51 info_cout(std::string("NVIDIA IndeX version: ") + index_connect.get_index_interface()->get_version(), sdict);
52
53 // initialize_log_module(index_connect, opt_map);
54
55 // Define service mode of the cluster machine. This suppress the following message.
56 // info: No particular service role has been assigned to the cluster machine.
57 // The IndeX library assigns it to service the 'rendering and compositing' by default.
58 mi::base::Handle<nv::index::ICluster_configuration> rendering_properties_query(
59 index_connect.get_index_interface()->
60 get_api_component<nv::index::ICluster_configuration>());
61 check_success(rendering_properties_query.is_valid_interface());
62 rendering_properties_query->set_service_mode("rendering_and_compositing");
63 }
64
65 // session
66 {
67 if (!index_connect.is_initialized())
68 {
69 printf("error: Fatal error! Initialization of the IndeX library failed.\n");
70 return 1;
71 }
72
73 // Begin of internal application scope
74 // Note: index_connect has database, global_scope,
75 // index_session. Here we demonstrate to create
76 // own. When you want to use index_connect's database,
77 // global_scope, index_session, you need to derive a own
78 // index_connect application class. See other API examples.
79 mi::base::Handle<mi::neuraylib::IDatabase> database(
80 index_connect.get_index_interface()->get_api_component<mi::neuraylib::IDatabase>());
81 check_success(database.is_valid_interface());
82
83 mi::base::Handle<mi::neuraylib::IScope> global_scope(database->get_global_scope());
84 check_success(global_scope.is_valid_interface());
85
86 mi::base::Handle<mi::neuraylib::IDice_transaction> dice_transaction(index_connect.create_transaction());
87 check_success(dice_transaction.is_valid_interface());
88 {
89 // This is a dice_transaction scope.
90
91 // IIndex_session to create an session
92 mi::base::Handle<nv::index::IIndex_session> iindex_session(
93 index_connect.get_index_interface()->get_api_component<nv::index::IIndex_session>());
94 check_success(iindex_session.is_valid_interface());
95
96 // Setup session information
97 mi::neuraylib::Tag session_tag = iindex_session->create_session(dice_transaction.get());
98 check_success(session_tag.is_valid());
99
100 // Access the session
101 mi::base::Handle<nv::index::ISession const> session(
102 dice_transaction->access<nv::index::ISession const>(session_tag));
103 check_success(session);
104
105 std::stringstream sstr;
106
107 // How to check the reference count status (create_session
108 // makes one reference by the IndeX library. The handle
109 // session has one reference. Totally two object reference this object.)
110 session.get()->retain(); // manually reference count up (+1).
111 const mi::Uint32 ref_count = session.get()->release(); // count down (-1), returns ref count.
112 sstr << "Current reference count of session: " << ref_count;
113 info_cout(sstr.str(), sdict);
114 check_success(ref_count == 2);
115
116 sstr.str("");
117 sstr << "Created an session: tag = " << session_tag.id;
118 info_cout(sstr.str(), sdict);
119 }
120 dice_transaction->commit(); // Finish this transaction
121 }
122 // shutdown when index_connect out of scope
123 }
124
125 return EXIT_SUCCESS;
126}
int main(int argc, const char *argv[])
Definition: session.cpp:22
#define check_success(expr)