NVIDIA Index example code nvidia_logo_transpbg.gif Up
start_index.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright 2023 NVIDIA Corporation. All rights reserved.
3 *****************************************************************************/
6
7// Note: This is an example without Index_connect. Usual case, we
8// recommend to use the convenient class: Index_connect in the
9// application_layer.
10
11#include <mi/dice.h>
12#include <nv/index/iindex.h>
13
14#include <cstdio>
15#include <iostream>
16#include <map>
17#include <sstream>
18#include <string>
19
20#ifndef MI_PLATFORM_WINDOWS
21#include <dlfcn.h>
22#else
23#include <windows.h>
24#endif // MI_PLATFORM_WINDOWS
25
26//----------------------------------------------------------------------
27namespace { // anonymous namespace for the local functions
28//----------------------------------------------------------------------
29// Helper macro. Checks whether the expression is true and if not prints a message and exits.
30//
31// Note: gcc accepts ({}) expression, but intel compiler said
32// error: destructible entities are not allowed inside of a statement
33// expression
34// Therefore outmost () has been removed.
35#define check_success(expr) \
36 { if (!(expr)) { \
37 fprintf(stderr, "Error in file %s, line %d: \"%s\".\n", __FILE__, __LINE__, #expr); \
38 exit(EXIT_FAILURE); } }
39
40//----------------------------------------------------------------------
41// global variables to keep the library name and handles
42static std::string G_nvindexlib_fname;
43static void * G_p_handle = 0;
44
45//----------------------------------------------------------------------
46// print a info message to stdout if not unittest mode
47//
48// This function is a kind of logger, but used when the dice is not
49// started or shutdown-ed.
50//
51// \param[in] message message output
52// \param[in] opt_map option map that should contain dice::verbosity key value.
53void info_cout(
54 const std::string& message,
55 std::map< std::string, std::string >& opt_map)
56{
57 if(opt_map.find("dice::verbose") == opt_map.end())
58 {
59 // no verbose option found, just output all
60 std::cout << "info: " << message << std::endl;
61 return;
62 }
63 // when verbose option is less than there, return.
64 std::string lv = opt_map["dice::verbose"];
65 if((lv == "0") || (lv == "1") || (lv == "2")){
66 return;
67 }
68 std::cout << "info: " << message << std::endl;
69}
70
71//----------------------------------------------------------------------
72// Loads the nvindex library and calls the main factory function.
73//
74// \param nvindexlib_fname The file name of the IndeX library DSO.
75// \return A pointer to an instance of the main
76// nv::index::IIndex interface
77nv::index::IIndex* load_and_get_iindex(const std::string & nvindexlib_fname)
78{
79#ifdef MI_PLATFORM_WINDOWS
80 G_p_handle = LoadLibrary(TEXT(nvindexlib_fname.c_str()));
81 if (!G_p_handle)
82 {
83 printf("error: Could not retrieve a handle to the %s library\n", nvindexlib_fname.c_str());
84 return NULL;
85 }
86 void* symbol = GetProcAddress((HMODULE)G_p_handle, "nv_factory");
87 if (!symbol)
88 {
89 printf("error: Could not retrieve the entry point into the %s library\n", nvindexlib_fname.c_str());
90 return NULL;
91 }
92#else // MI_PLATFORM_WINDOWS
93 G_p_handle = dlopen(nvindexlib_fname.c_str(), RTLD_LAZY);
94 if (!G_p_handle) {
95 printf("error: iindex: handle: %s\n", dlerror());
96 return 0;
97 }
98
99 void* symbol = dlsym(G_p_handle, "nv_factory");
100 if (!symbol) {
101 printf("error: iindex: symbol: %s\n", dlerror());
102 return NULL;
103 }
104#endif // MI_PLATFORM_WINDOWS
105
106 G_nvindexlib_fname = nvindexlib_fname;
107
108 return nv::index::nv_factory<nv::index::IIndex>(symbol);
109}
110
111//----------------------------------------------------------------------
112// Unloads the nvindex library.
113// \param[in] opt_map option map that should contain dice::verbosity key value.
114// \return true when succeeded
115bool unload_iindex(std::map< std::string, std::string > & opt_map)
116{
117 check_success(G_p_handle != 0);
118#ifdef MI_PLATFORM_WINDOWS
119 if (FALSE == FreeLibrary((HMODULE)G_p_handle)) {
120 fprintf(stderr, "error: unload_iindex.\n");
121 return false;
122 }
123#else // MI_PLATFORM_WINDOWS
124 int result = dlclose(G_p_handle);
125 if (result != 0) {
126 fprintf(stderr, "error: unload_iindex: %s\n", dlerror());
127 return false;
128 }
129
130#endif // MI_PLATFORM_WINDOWS
131 return true;
132}
133
134//----------------------------------------------------------------------
135// Creating and accessing an instance of NVIDIA IndeX library.
136class Nvindex_access
137{
138public:
139 // Constructor
140 Nvindex_access()
141 {
142 // empty
143 }
144 // Destructor
145 virtual ~Nvindex_access()
146 {
147 // empty
148 }
149
150 // Load nvindex library
151 // \return true if loading was successful
152 bool load_library();
153
154 // Is the nvindex library initialized?
155 // \return true when it has been initialized
156 bool is_initialized() const
157 {
158 return m_nvindex_interface.is_valid_interface();
159 }
160
161 // Get to the nvindex interface
162 // \return reference to the nvindex interface handle
163 mi::base::Handle<nv::index::IIndex>& get_interface()
164 {
165 return m_nvindex_interface;
166 }
167
168 // Start IndeX service
169 // * start DiCE, IndeX
170 // \return success when 0
171 mi::Sint32 start_service()
172 {
173 check_success(m_nvindex_interface.is_valid_interface());
174 m_nvindex_interface->start();
175 // No application_layer support for this example.
176 return 0;
177 }
178
179 // Shut down the nvindex and dice library
180 // \return true when shutdown was successful
181 bool shutdown();
182
183 // clear the IIndex instance
184 void clear()
185 {
186 m_nvindex_interface = 0;
187 }
188
189
190private:
191 mi::base::Handle<nv::index::IIndex> m_nvindex_interface;
192};
193
194//----------------------------------------------------------------------
195bool Nvindex_access::load_library()
196{
197 // load shared libraries.
198 std::string lib_name = "libnvindex";
199
200#if _WIN32
201# ifdef DEBUG
202 lib_name += "d.dll";
203# else // DEBUG
204 lib_name += ".dll";
205# endif // DEBUG
206#else // _WIN32
207 lib_name += ".so";
208#endif // _WIN32
209
210 m_nvindex_interface = load_and_get_iindex(lib_name);
211
212 check_success(m_nvindex_interface != 0);
213
214 return true;
215}
216
217//----------------------------------------------------------------------
218bool Nvindex_access::shutdown()
219{
220 check_success(m_nvindex_interface.is_valid_interface());
221 const mi::Sint32 nvi_shutdown = m_nvindex_interface->shutdown();
222 if(nvi_shutdown != 0){
223 fprintf(stderr, "error: failed IndeX library shutdown (code: %d)\n", nvi_shutdown);
224 }
225
226 return nvi_shutdown == 0;
227}
228
229//----------------------------------------------------------------------
230} // namespace anonymous
231//----------------------------------------------------------------------
232// The main function initializes the NVIDIA IndeX library,
233// starts it, and shuts it down after waiting for user input.
234int main(int argc, char* argv[])
235{
236 const std::string com_name(argv[0]);
237
238 std::map< std::string, std::string > opt_map;
239 opt_map["dice::verbose"] = "3"; // log level
240 opt_map["unittest"] = "0"; // default mode
241
242 if(opt_map["unittest"] == "1")
243 {
244 opt_map["dice::verbose"] = "2";
245 }
246 info_cout("running " + com_name, opt_map);
247
248 // Load NVIDIA IndeX library.
249 Nvindex_access nvindex_accessor;
250 check_success(nvindex_accessor.load_library());
251
252 // Start IndeX service
253 nvindex_accessor.start_service();
254
255 // Access NVIDIA IndeX and DiCE API and library version once the NVIDIA IndeX has been started.
256 info_cout("NVIDIA IndeX version: " + std::string(nvindex_accessor.get_interface()->get_version()), opt_map);
257
258 const char* version = nvindex_accessor.get_interface()->get_dice_version();
259 check_success(version != 0);
260 info_cout("DiCE header API version = " + std::string(MI_NEURAYLIB_VERSION_QUALIFIED_STRING), opt_map);
261 info_cout("DiCE library build version string = \"" + std::string(version) + "\".", opt_map);
262
263 // Shut down NVIDIA IndeX library.
264 check_success(nvindex_accessor.shutdown());
265
266 // Clear the instance before unloading the library.
267 nvindex_accessor.clear();
268
269 // Unload the libraries.
270 check_success(unload_iindex(opt_map));
271
272 return EXIT_SUCCESS;
273}
int main(int argc, char *argv[])
#define check_success(expr)
Definition: start_index.cpp:35