You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

data_buffer.h 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef DATASET_ENGINE_DATA_BUFFER_H_
  17. #define DATASET_ENGINE_DATA_BUFFER_H_
  18. #include <iostream>
  19. #include <memory>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #include "dataset/util/allocator.h"
  24. #include "dataset/util/status.h"
  25. #include "dataset/core/constants.h"
  26. #include "dataset/core/tensor.h"
  27. #include "dataset/core/tensor_row.h"
  28. namespace mindspore {
  29. namespace dataset {
  30. // The DataBuffer class is a base class that will represent the data for n values based
  31. // on a unique row id for each row of data.
  32. // There can be different types of DataBuffers to abstract over how the data is stored
  33. // in memory and acquired from storage.
  34. // Each buffer holds a range of consecutive row id's.
  35. class DataBuffer {
  36. public:
  37. // Buffer flags
  38. enum BufferFlags : uint32_t {
  39. kDeBFlagNone = 0,
  40. kDeBFlagEOF = 1, // The buffer is an eof end-of-data msg
  41. kDeBFlagEOE = 1u << 1 // The buffer is an eoe end-of-epoch msg
  42. };
  43. // Name: Constructor #1
  44. // Description: This is the main constructor that is used for making a buffer
  45. DataBuffer(int32_t id, BufferFlags flags);
  46. // Destructor
  47. virtual ~DataBuffer();
  48. // Name: print()
  49. // Description: A function that prints info about the DataBuffer (base class version)
  50. virtual void Print(std::ostream &out, // In: The output stream to print to
  51. bool show_all) const; // In: T/F if it should show everything
  52. // Provide stream operator for displaying it
  53. friend std::ostream &operator<<(std::ostream &out, const DataBuffer &cb) {
  54. cb.Print(out, false);
  55. return out;
  56. }
  57. // Name: load()
  58. // Description: populates the DataBuffer with data based on it's id
  59. virtual Status Load();
  60. // Convenience getter functions for flag checking
  61. bool eof() const { return (static_cast<uint32_t>(buffer_flags_) & static_cast<uint32_t>(kDeBFlagEOF)); }
  62. bool eoe() const { return (static_cast<uint32_t>(buffer_flags_) & static_cast<uint32_t>(kDeBFlagEOE)); }
  63. // Simple getter funcs
  64. int32_t id() const { return buffer_id_; }
  65. void set_id(int32_t id) { buffer_id_ = id; }
  66. int32_t NumRows() const { return ((tensor_table_) ? tensor_table_->size() : 0); }
  67. int32_t NumCols() const {
  68. return (tensor_table_ == nullptr || tensor_table_->empty()) ? 0 : tensor_table_->at(0).size();
  69. }
  70. BufferFlags buffer_flags() const { return buffer_flags_; }
  71. // Remove me!! Callers should fetch rows via pop
  72. Status GetTensor(std::shared_ptr<Tensor> *, int32_t row_id, int32_t col_id) const;
  73. // Remove me!! Callers should drain rows via pop.
  74. Status GetRow(int32_t row_id, TensorRow *) const;
  75. // Get a row from the TensorTable
  76. Status PopRow(TensorRow *);
  77. Status SliceOff(int64_t number_of_rows);
  78. // Replacing mTensorTable, the unique_ptr assignment will release the old TensorTable.
  79. void set_tensor_table(std::unique_ptr<TensorQTable> new_table) { tensor_table_ = std::move(new_table); }
  80. void set_flag(BufferFlags in_flag) {
  81. buffer_flags_ = static_cast<BufferFlags>(static_cast<uint32_t>(buffer_flags_) | static_cast<uint32_t>(in_flag));
  82. }
  83. void Shuffle() {} // does nothing right now. possibly remove later
  84. protected:
  85. int32_t buffer_id_; // An id for the buffer.
  86. std::unique_ptr<TensorQTable> tensor_table_; // A table (row major) of Tensors
  87. BufferFlags buffer_flags_; // bit mask for various buffer properties
  88. };
  89. } // namespace dataset
  90. } // namespace mindspore
  91. #endif // DATASET_ENGINE_DATA_BUFFER_H_