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 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. namespace mindspore {
  21. namespace dataset {
  22. // Name: Constructor #1
  23. // Description: This is the main constructor that is used for making a buffer
  24. DataBuffer::DataBuffer(int32_t id, BufferFlags flags) : buffer_id_(id), tensor_table_(nullptr), buffer_flags_(flags) {}
  25. // Name: print()
  26. // Description: A function that prints info about the DataBuffer (base class version)
  27. void DataBuffer::Print(std::ostream &out, // In: The output stream to print to
  28. bool show_all) const { // In: T/F if it should show everything
  29. out << "bufferId: " << buffer_id_ << "\nflags: " << std::hex << buffer_flags_ << std::dec << "\n";
  30. // If the column counts are set then it means that data has been set into
  31. // the tensor table. Display the tensor table here.
  32. if (this->NumCols() > 0) {
  33. out << "Tensor table:\n";
  34. for (int32_t row = 0; row < DataBuffer::NumRows(); ++row) {
  35. out << "Row # : " << row << "\n";
  36. TensorRow currRow = (*tensor_table_)[row];
  37. for (int32_t col = 0; col < this->NumCols(); ++col) {
  38. out << "Column #: " << col << "\n"; // Should add the column name here as well?
  39. // Call the tensor display
  40. out << *(currRow[col]) << "\n";
  41. }
  42. }
  43. }
  44. }
  45. Status DataBuffer::Load() {
  46. std::string err_msg = "Base class load called, but it does not have an implementation!";
  47. RETURN_STATUS_UNEXPECTED(err_msg);
  48. }
  49. // Remove me!! Callers should fetch rows via pop
  50. Status DataBuffer::GetTensor(std::shared_ptr<Tensor> *ptr, int32_t row_id, int32_t col_id) const {
  51. if (row_id < tensor_table_->size() && col_id < tensor_table_->at(row_id).size()) {
  52. *ptr = (tensor_table_->at(row_id)).at(col_id);
  53. } else {
  54. std::string err_msg =
  55. "indices for mTensorTable out of range: (" + std::to_string(row_id) + "," + std::to_string(col_id) + ").";
  56. RETURN_STATUS_UNEXPECTED(err_msg);
  57. }
  58. return Status::OK();
  59. }
  60. // Remove me!! Callers should fetch rows via pop
  61. Status DataBuffer::GetRow(int32_t row_id, TensorRow *ptr) const {
  62. if (tensor_table_ && !tensor_table_->empty() && row_id < tensor_table_->size()) {
  63. *ptr = tensor_table_->at(row_id);
  64. } else {
  65. std::string err_msg = "rowId for mTensorTable out of range: " + std::to_string(row_id);
  66. RETURN_STATUS_UNEXPECTED(err_msg);
  67. }
  68. return Status::OK();
  69. }
  70. Status DataBuffer::PopRow(TensorRow *ptr) {
  71. if (tensor_table_ && !tensor_table_->empty()) {
  72. *ptr = std::move(tensor_table_->front());
  73. tensor_table_->pop_front();
  74. }
  75. return Status::OK();
  76. }
  77. Status DataBuffer::SliceOff(int64_t number_of_rows) {
  78. while (number_of_rows > 0) {
  79. tensor_table_->pop_back();
  80. number_of_rows--;
  81. }
  82. return Status::OK();
  83. }
  84. // Destructor
  85. DataBuffer::~DataBuffer() {}
  86. } // namespace dataset
  87. } // namespace mindspore