VisionWorks Toolkit Reference

April 8, 2016 | 1.4 Release

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Utility.hpp
Go to the documentation of this file.
1 /*
2 # Copyright (c) 2014-2016, NVIDIA CORPORATION. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 # * Neither the name of NVIDIA CORPORATION nor the names of its
13 # contributors may be used to endorse or promote products derived
14 # from this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
17 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef NVXIO_UTILITY_HPP
30 #define NVXIO_UTILITY_HPP
31 
32 #include <iostream>
33 #include <memory>
34 #include <sstream>
35 #include <stdexcept>
36 #include <utility>
37 #include <vector>
38 #include <string>
39 
40 #ifdef __ANDROID__
41 #include <android/log.h>
42 #endif
43 
44 #include <NVX/nvx.h>
45 #include <NVXIO/Export.hpp>
46 
52 namespace nvxio
53 {
54 
62 //
63 // NVXIO_LOG* macros
64 //
65 
66 #ifdef __ANDROID__
67 
73 #define NVXIO_LOGV(tag, ...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, tag, __VA_ARGS__))
74 
80 #define NVXIO_LOGD(tag, ...) ((void)__android_log_print(ANDROID_LOG_DEBUG, tag, __VA_ARGS__))
81 
87 #define NVXIO_LOGI(tag, ...) ((void)__android_log_print(ANDROID_LOG_INFO, tag, __VA_ARGS__))
88 
94 #define NVXIO_LOGW(tag, ...) ((void)__android_log_print(ANDROID_LOG_WARN, tag, __VA_ARGS__))
95 
101 #define NVXIO_LOGE(tag, ...) ((void)__android_log_print(ANDROID_LOG_ERROR, tag, __VA_ARGS__))
102 
103 #endif
104 
105 //
106 // Auxiliary macros
107 //
108 
115 #define NVXIO_THROW_EXCEPTION(msg) \
116  do { \
117  std::ostringstream ostr_; \
118  ostr_ << msg; \
119  throw std::runtime_error(ostr_.str()); \
120  } while(0)
121 
129 #define NVXIO_SAFE_CALL(vxOp) \
130  do \
131  { \
132  vx_status status = (vxOp); \
133  if (status != VX_SUCCESS) \
134  { \
135  NVXIO_THROW_EXCEPTION(# vxOp << " failure [status = " << status << "]" << " in file " << __FILE__ << " line " << __LINE__); \
136  } \
137  } while (0)
138 
145 #define NVXIO_ASSERT(cond) \
146  do \
147  { \
148  if (!(cond)) \
149  { \
150  NVXIO_THROW_EXCEPTION(# cond << " failure in file " << __FILE__ << " line " << __LINE__); \
151  } \
152  } while (0)
153 
160 #define NVXIO_CHECK_REFERENCE(ref) \
161  NVXIO_ASSERT(ref != 0 && vxGetStatus((vx_reference)ref) == VX_SUCCESS)
162 
170 #define NVXIO_CUDA_SAFE_CALL(cudaOp) \
171  do \
172  { \
173  cudaError_t err = (cudaOp); \
174  if (err != cudaSuccess) \
175  { \
176  std::ostringstream ostr; \
177  ostr << "CUDA Error in " << # cudaOp << __FILE__ << " file " << __LINE__ << " line : " << cudaGetErrorString(err); \
178  throw std::runtime_error(ostr.str()); \
179  } \
180  } while (0)
181 
187 template <typename T, vx_size N>
188 vx_size dimOf(T (&)[N]) { return N; }
189 
190 //
191 // Common constants
192 //
193 
199 const vx_float64 PI = 3.1415926535897932;
205 const vx_float32 PI_F = 3.14159265f;
206 
207 //
208 // Auxiliary functions
209 //
210 
211 #ifdef __ANDROID__
212 
221 NVXIO_EXPORT void VX_CALLBACK androidLogCallback(vx_context context, vx_reference ref, vx_status status, const vx_char string[]);
222 #else
223 
232 NVXIO_EXPORT void VX_CALLBACK stdoutLogCallback(vx_context context, vx_reference ref, vx_status status, const vx_char string[]);
233 #endif
234 
241 NVXIO_EXPORT void printPerf(vx_graph graph, const char* label);
242 
249 NVXIO_EXPORT void printPerf(vx_node node, const char* label);
250 
257 NVXIO_EXPORT void checkIfContextIsValid(vx_context context);
258 
265 struct NVXIO_EXPORT ContextGuard
266 {
267  ContextGuard() : context(vxCreateContext()) {
268  checkIfContextIsValid(context);
269  }
270  ContextGuard(const ContextGuard &) = delete;
271  ContextGuard &operator = (const ContextGuard &) = delete;
273  vxReleaseContext(&context);
274  }
275  operator vx_context() { return context; }
276 
277 private:
278  vx_context context;
279 };
280 
286 template <typename T, typename... Args>
287 std::unique_ptr<T> makeUP(Args &&... args)
288 {
289  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
290 }
291 
292 #ifndef __ANDROID__
293 
339 NVXIO_EXPORT std::vector<std::string> getSupportedFeatures();
340 
341 #endif
342 
343 } // namespace nvxio
344 
345 #endif // NVXIO_UTILITY_HPP
#define VX_CALLBACK
Definition: vx_types.h:78
size_t vx_size
A wrapper of size_t to keep the naming convention uniform.
Definition: vx_types.h:166
NVXIO_EXPORT void checkIfContextIsValid(vx_context context)
Checks whether the context is valid and throws an exception in case of failure.
NVIDIA VisionWorks Framework and Primitives API.
ContextGuard is a wrapper for vx_context.
Definition: Utility.hpp:265
char vx_char
An 8 bit ASCII character.
Definition: vx_types.h:85
float vx_float32
A 32-bit float value.
Definition: vx_types.h:138
NVXIO_EXPORT void VX_CALLBACK stdoutLogCallback(vx_context context, vx_reference ref, vx_status status, const vx_char string[])
The callback for OpenVX error logs, which prints messages to standard output.
const vx_float64 PI
Double-precision PI.
Definition: Utility.hpp:199
vx_status VX_API_CALL vxReleaseContext(vx_context *context)
Releases the OpenVX object context.
vx_enum vx_status
A formal status type with known fixed size.
Definition: vx_types.h:421
struct _vx_context * vx_context
An opaque reference to the implementation context.
Definition: vx_types.h:222
NVXIO_EXPORT void printPerf(vx_graph graph, const char *label)
Prints performance information for the provided graph object.
struct _vx_reference * vx_reference
A generic opaque reference to any object within OpenVX.
Definition: vx_types.h:154
std::unique_ptr< T > makeUP(Args &&...args)
make_unique function.
Definition: Utility.hpp:287
double vx_float64
A 64-bit float value (aka double).
Definition: vx_types.h:143
const vx_float32 PI_F
Float-precision PI.
Definition: Utility.hpp:205
vx_context VX_API_CALL vxCreateContext()
Creates a vx_context.
struct _vx_graph * vx_graph
An opaque reference to a graph.
Definition: vx_types.h:215
vx_size dimOf(T(&)[N])
Returns the size of an array (the N template argument).
Definition: Utility.hpp:188
struct _vx_node * vx_node
An opaque reference to a kernel node.
Definition: vx_types.h:208
NVXIO_EXPORT std::vector< std::string > getSupportedFeatures()
Returns a vector of NVXIO supported features.