tvm
io.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
24 #ifndef TVM_SUPPORT_IO_H_
25 #define TVM_SUPPORT_IO_H_
26 
27 #include <cstddef>
28 #include <cstdint>
29 
30 namespace tvm {
31 namespace support {
32 
41 template <typename T, typename = void>
42 struct Serializer {
43  static constexpr bool enabled = false;
44 };
45 
57 class Stream {
58  public:
65  virtual size_t Read(void* ptr, size_t size) = 0;
66 
73  virtual size_t Write(const void* ptr, size_t size) = 0;
74 
76  virtual ~Stream() = default;
77 
83  template <typename T>
84  void Write(const T& data) {
85  static_assert(Serializer<T>::enabled, "No Serializer<T> specialization for this type");
86  Serializer<T>::Write(this, data);
87  }
88 
95  template <typename T>
96  bool Read(T* out_data) {
97  static_assert(Serializer<T>::enabled, "No Serializer<T> specialization for this type");
98  return Serializer<T>::Read(this, out_data);
99  }
100 
106  template <typename T>
107  void WriteArray(const T* data, size_t num_elems) {
108  for (size_t i = 0; i < num_elems; ++i) Write<T>(data[i]);
109  }
110 
117  template <typename T>
118  bool ReadArray(T* data, size_t num_elems) {
119  for (size_t i = 0; i < num_elems; ++i) {
120  if (!Read<T>(data + i)) return false;
121  }
122  return true;
123  }
124 };
125 
126 } // namespace support
127 } // namespace tvm
128 
129 #endif // TVM_SUPPORT_IO_H_
Abstract binary stream for serialization.
Definition: io.h:57
void WriteArray(const T *data, size_t num_elems)
Write an array of typed values element by element.
Definition: io.h:107
virtual size_t Read(void *ptr, size_t size)=0
Read raw bytes from the stream.
virtual ~Stream()=default
Virtual destructor.
bool ReadArray(T *data, size_t num_elems)
Read an array of typed values element by element.
Definition: io.h:118
virtual size_t Write(const void *ptr, size_t size)=0
Write raw bytes to the stream.
void Write(const T &data)
Write a typed value using Serializer<T>.
Definition: io.h:84
bool Read(T *out_data)
Read a typed value using Serializer<T>.
Definition: io.h:96
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
Primary Serializer template. Specialize for each serializable type.
Definition: io.h:42
static constexpr bool enabled
Definition: io.h:43