HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ortmemoryinfo.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 #pragma once
5 
6 #include <string_view>
7 
9 
10 struct OrtMemoryInfo {
11  OrtMemoryInfo() = default; // to allow default construction of Tensor
12 
13  // use string for name, so we could have customized allocator in execution provider.
14  const char* name = nullptr;
15  int id = -1;
19 
20  constexpr OrtMemoryInfo(const char* name_, OrtAllocatorType type_, OrtDevice device_ = OrtDevice(), int id_ = 0,
21  OrtMemType mem_type_ = OrtMemTypeDefault)
22 #if ((defined(__GNUC__) && __GNUC__ > 4) || defined(__clang__))
23  // this causes a spurious error in CentOS gcc 4.8 build so disable if GCC version < 5
24  __attribute__((nonnull))
25 #endif
26  : name(name_),
27  id(id_),
28  mem_type(mem_type_),
29  alloc_type(type_),
30  device(device_) {
31  }
32 
33  // To make OrtMemoryInfo become a valid key in std map
34  bool operator<(const OrtMemoryInfo& other) const {
35  if (alloc_type != other.alloc_type)
36  return alloc_type < other.alloc_type;
37  if (mem_type != other.mem_type)
38  return mem_type < other.mem_type;
39  if (id != other.id)
40  return id < other.id;
41 
42  return strcmp(name, other.name) < 0;
43  }
44 
45  // This is to make OrtMemoryInfo a valid key in hash tables
46  // we ignore device id
47  size_t Hash() const {
48  auto h = std::hash<int>()(alloc_type);
51  onnxruntime::HashCombine<std::string_view>(name, h);
52  return h;
53  }
54 
56  std::ostringstream ostr;
57  ostr << "OrtMemoryInfo:["
58  << "name:" << name
59  << " id:" << id
60  << " OrtMemType:" << mem_type
61  << " OrtAllocatorType:" << alloc_type
62  << " " << device.ToString()
63  << "]";
64  return ostr.str();
65  }
66 };
67 
68 // Required by hash tables
69 inline bool operator==(const OrtMemoryInfo& left, const OrtMemoryInfo& other) {
70  return left.mem_type == other.mem_type &&
71  left.alloc_type == other.alloc_type &&
72  left.id == other.id &&
73  strcmp(left.name, other.name) == 0;
74 }
75 
76 inline bool operator!=(const OrtMemoryInfo& lhs, const OrtMemoryInfo& rhs) { return !(lhs == rhs); }
77 
78 std::ostream& operator<<(std::ostream& out, const OrtMemoryInfo& info);
79 
80 namespace std {
81 template<>
82 struct hash<OrtMemoryInfo> {
83  size_t operator()(const OrtMemoryInfo& i) const {
84  return i.Hash();
85  }
86 };
87 }
std::ostream & operator<<(std::ostream &out, const OrtMemoryInfo &info)
const char * name
Definition: ortmemoryinfo.h:14
GLint left
Definition: glcorearb.h:2005
OrtAllocatorType alloc_type
Definition: ortmemoryinfo.h:17
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
OrtAllocatorType
__attribute__((visibility("default")))
void HashCombine(const T &value, size_t &seed)
Definition: hash_combine.h:17
The default allocator for execution provider.
OrtMemType mem_type
Definition: ortmemoryinfo.h:16
OrtMemoryInfo()=default
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:556
OrtDevice device
Definition: ortmemoryinfo.h:18
GLuint const GLchar * name
Definition: glcorearb.h:786
bool operator<(const OrtMemoryInfo &other) const
Definition: ortmemoryinfo.h:34
std::string ToString() const
Definition: ortmemoryinfo.h:55
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
size_t Hash() const
Definition: ortmemoryinfo.h:47
constexpr OrtMemoryInfo(const char *name_, OrtAllocatorType type_, OrtDevice device_=OrtDevice(), int id_=0, OrtMemType mem_type_=OrtMemTypeDefault)
Definition: ortmemoryinfo.h:20
size_t operator()(const OrtMemoryInfo &i) const
Definition: ortmemoryinfo.h:83
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:542
std::string ToString() const
Definition: ortdevice.h:47
OrtMemType
Memory types for allocated memory, execution provider specific types should be extended in each provi...