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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. namespace mindspore {
  28. namespace dataset {
  29. // Forward declares
  30. class StorageClient;
  31. // The DataBuffer class is a base class that will represent the data for n values based
  32. // on a unique row id for each row of data.
  33. // There can be different types of DataBuffers to abstract over how the data is stored
  34. // in memory and acquired from storage.
  35. // Each buffer holds a range of consecutive row id's.
  36. class DataBuffer {
  37. public:
  38. // Buffer flags
  39. enum BufferFlags : uint32_t {
  40. kDeBFlagNone = 0,
  41. kDeBFlagEOF = 1, // The buffer is an eof end-of-data msg
  42. kDeBFlagEOE = 1u << 1 // The buffer is an eoe end-of-epoch msg
  43. };
  44. // Name: Constructor #1
  45. // Description: This is the main constructor that is used for making a buffer
  46. DataBuffer(int32_t id, BufferFlags flags);
  47. // Destructor
  48. virtual ~DataBuffer();
  49. // Name: CreateDataBuffer()
  50. // Description: A factory method to create the appropriate type of derived class
  51. // buffer. Returns the base class reference for DataBuffer.
  52. static Status CreateDataBuffer(
  53. int32_t id, // In: The id for the new buffer
  54. std::shared_ptr<StorageClient>, // In: The StorageClient is used to choose the buffer type to create
  55. std::unique_ptr<DataBuffer> *);
  56. // Name: print()
  57. // Description: A function that prints info about the DataBuffer (base class version)
  58. virtual void Print(std::ostream &out, // In: The output stream to print to
  59. bool show_all) const; // In: T/F if it should show everything
  60. // Provide stream operator for displaying it
  61. friend std::ostream &operator<<(std::ostream &out, const DataBuffer &cb) {
  62. cb.Print(out, false);
  63. return out;
  64. }
  65. // Name: load()
  66. // Description: populates the DataBuffer with data based on it's id
  67. virtual Status Load();
  68. // Convenience getter functions for flag checking
  69. bool eof() const { return (static_cast<uint32_t>(buffer_flags_) & static_cast<uint32_t>(kDeBFlagEOF)); }
  70. bool eoe() const { return (static_cast<uint32_t>(buffer_flags_) & static_cast<uint32_t>(kDeBFlagEOE)); }
  71. // Simple getter funcs
  72. int32_t id() const { return buffer_id_; }
  73. void set_id(int32_t id) { buffer_id_ = id; }
  74. int32_t NumRows() const { return ((tensor_table_) ? tensor_table_->size() : 0); }
  75. int32_t NumCols() const {
  76. return (tensor_table_ == nullptr || tensor_table_->empty()) ? 0 : tensor_table_->at(0).size();
  77. }
  78. BufferFlags buffer_flags() const { return buffer_flags_; }
  79. // Remove me!! Callers should fetch rows via pop
  80. Status GetTensor(std::shared_ptr<Tensor> *, int32_t row_id, int32_t col_id) const;
  81. // Remove me!! Callers should drain rows via pop.
  82. Status GetRow(int32_t row_id, TensorRow *) const;
  83. // Get a row from the TensorTable
  84. Status PopRow(TensorRow *);
  85. Status SliceOff(int64_t number_of_rows);
  86. // Replacing mTensorTable, the unique_ptr assignment will release the old TensorTable.
  87. void set_tensor_table(std::unique_ptr<TensorQTable> new_table) { tensor_table_ = std::move(new_table); }
  88. void set_flag(BufferFlags in_flag) {
  89. buffer_flags_ = static_cast<BufferFlags>(static_cast<uint32_t>(buffer_flags_) | static_cast<uint32_t>(in_flag));
  90. }
  91. void Shuffle() {} // does nothing right now. possibly remove later
  92. protected:
  93. int32_t buffer_id_; // An id for the buffer.
  94. std::unique_ptr<TensorQTable> tensor_table_; // A table (row major) of Tensors
  95. BufferFlags buffer_flags_; // bit mask for various buffer properties
  96. };
  97. } // namespace dataset
  98. } // namespace mindspore
  99. #endif // DATASET_ENGINE_DATA_BUFFER_H_