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.cc 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include "dataset/engine/data_buffer.h"
  17. #include "dataset/util/allocator.h"
  18. #include "dataset/core/global_context.h"
  19. #include "dataset/core/tensor.h"
  20. #include "dataset/engine/datasetops/source/storage_client.h"
  21. #include "dataset/engine/datasetops/source/tf_buffer.h"
  22. namespace mindspore {
  23. namespace dataset {
  24. // Name: Constructor #1
  25. // Description: This is the main constructor that is used for making a buffer
  26. DataBuffer::DataBuffer(int32_t id, BufferFlags flags) : buffer_id_(id), tensor_table_(nullptr), buffer_flags_(flags) {}
  27. // Name: CreateDataBuffer()
  28. // Description: A static factory method to create the appropriate type of derived class
  29. // buffer. Returns the base class reference for DataBuffer.
  30. Status DataBuffer::CreateDataBuffer(
  31. int32_t id, // In: The id for the new buffer
  32. std::shared_ptr<StorageClient> storage_client, // In: The storage client that is related to this buffer type
  33. std::unique_ptr<DataBuffer> *ptr) {
  34. std::unique_ptr<DataBuffer> new_data_buffer;
  35. try {
  36. DatasetType ds_type = storage_client->schema()->dataset_type();
  37. switch (ds_type) {
  38. case DatasetType::kTf: {
  39. // This type of buffer is for TF record data.
  40. // Allocate derived class version for a TF buffers
  41. new_data_buffer = std::make_unique<TFBuffer>(id, kDeBFlagNone, storage_client);
  42. break;
  43. }
  44. default: {
  45. std::string errMsg("Invalid buffer type");
  46. RETURN_STATUS_UNEXPECTED(errMsg);
  47. }
  48. }
  49. } catch (std::bad_alloc &e) {
  50. return Status(StatusCode::kOutOfMemory, __LINE__, __FILE__, e.what());
  51. } catch (std::exception &e) {
  52. RETURN_STATUS_UNEXPECTED(e.what());
  53. }
  54. *ptr = std::move(new_data_buffer);
  55. return Status::OK();
  56. }
  57. // Name: print()
  58. // Description: A function that prints info about the DataBuffer (base class version)
  59. void DataBuffer::Print(std::ostream &out, // In: The output stream to print to
  60. bool show_all) const { // In: T/F if it should show everything
  61. out << "bufferId: " << buffer_id_ << "\nflags: " << std::hex << buffer_flags_ << std::dec << "\n";
  62. // If the column counts are set then it means that data has been set into
  63. // the tensor table. Display the tensor table here.
  64. if (this->NumCols() > 0) {
  65. out << "Tensor table:\n";
  66. for (int32_t row = 0; row < DataBuffer::NumRows(); ++row) {
  67. out << "Row # : " << row << "\n";
  68. TensorRow currRow = (*tensor_table_)[row];
  69. for (int32_t col = 0; col < this->NumCols(); ++col) {
  70. out << "Column #: " << col << "\n"; // Should add the column name here as well?
  71. // Call the tensor display
  72. out << *(currRow[col]) << "\n";
  73. }
  74. }
  75. }
  76. }
  77. Status DataBuffer::Load() {
  78. std::string err_msg = "Base class load called, but it does not have an implementation!";
  79. RETURN_STATUS_UNEXPECTED(err_msg);
  80. }
  81. // Remove me!! Callers should fetch rows via pop
  82. Status DataBuffer::GetTensor(std::shared_ptr<Tensor> *ptr, int32_t row_id, int32_t col_id) const {
  83. if (row_id < tensor_table_->size() && col_id < tensor_table_->at(row_id).size()) {
  84. *ptr = (tensor_table_->at(row_id)).at(col_id);
  85. } else {
  86. std::string err_msg =
  87. "indices for mTensorTable out of range: (" + std::to_string(row_id) + "," + std::to_string(col_id) + ").";
  88. RETURN_STATUS_UNEXPECTED(err_msg);
  89. }
  90. return Status::OK();
  91. }
  92. // Remove me!! Callers should fetch rows via pop
  93. Status DataBuffer::GetRow(int32_t row_id, TensorRow *ptr) const {
  94. if (row_id < tensor_table_->size()) {
  95. *ptr = tensor_table_->at(row_id);
  96. } else {
  97. std::string err_msg = "rowId for mTensorTable out of range: " + std::to_string(row_id);
  98. RETURN_STATUS_UNEXPECTED(err_msg);
  99. }
  100. return Status::OK();
  101. }
  102. Status DataBuffer::PopRow(TensorRow *ptr) {
  103. if (tensor_table_ && !tensor_table_->empty()) {
  104. *ptr = std::move(tensor_table_->front());
  105. tensor_table_->pop_front();
  106. }
  107. return Status::OK();
  108. }
  109. Status DataBuffer::SliceOff(int64_t number_of_rows) {
  110. while (number_of_rows > 0) {
  111. tensor_table_->pop_back();
  112. number_of_rows--;
  113. }
  114. return Status::OK();
  115. }
  116. // Destructor
  117. DataBuffer::~DataBuffer() {}
  118. } // namespace dataset
  119. } // namespace mindspore