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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "minddata/dataset/engine/data_buffer.h"
  17. #include "minddata/dataset/util/allocator.h"
  18. #include "minddata/dataset/core/global_context.h"
  19. #include "minddata/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. // A method for debug printing of the buffer
  26. void DataBuffer::Print(std::ostream &out, bool show_all) const {
  27. out << "bufferId: " << buffer_id_ << "\nflags: " << std::hex << buffer_flags_ << std::dec << "\n";
  28. // If the column counts are set then it means that data has been set into
  29. // the tensor table. Display the tensor table here.
  30. if (this->NumCols() > 0) {
  31. out << "Tensor table:\n";
  32. for (int32_t row = 0; row < DataBuffer::NumRows(); ++row) {
  33. out << "Row # : " << row << "\n";
  34. TensorRow currRow = (*tensor_table_)[row];
  35. for (int32_t col = 0; col < this->NumCols(); ++col) {
  36. out << "Column #: " << col << "\n"; // Should add the column name here as well?
  37. // Call the tensor display
  38. out << *(currRow[col]) << "\n";
  39. }
  40. }
  41. }
  42. }
  43. // Remove me!! Callers should fetch rows via pop
  44. Status DataBuffer::GetTensor(std::shared_ptr<Tensor> *ptr, int32_t row_id, int32_t col_id) const {
  45. if (row_id < tensor_table_->size() && col_id < tensor_table_->at(row_id).size()) {
  46. *ptr = (tensor_table_->at(row_id)).at(col_id);
  47. } else {
  48. std::string err_msg =
  49. "indices for mTensorTable out of range: (" + std::to_string(row_id) + "," + std::to_string(col_id) + ").";
  50. RETURN_STATUS_UNEXPECTED(err_msg);
  51. }
  52. return Status::OK();
  53. }
  54. // Remove me!! Callers should fetch rows via pop
  55. Status DataBuffer::GetRow(int32_t row_id, TensorRow *ptr) const {
  56. if (tensor_table_ && !tensor_table_->empty() && row_id < tensor_table_->size()) {
  57. *ptr = tensor_table_->at(row_id);
  58. } else {
  59. std::string err_msg = "rowId for mTensorTable out of range: " + std::to_string(row_id);
  60. RETURN_STATUS_UNEXPECTED(err_msg);
  61. }
  62. return Status::OK();
  63. }
  64. Status DataBuffer::PopRow(TensorRow *ptr) {
  65. if (tensor_table_ && !tensor_table_->empty()) {
  66. *ptr = std::move(tensor_table_->front());
  67. tensor_table_->pop_front();
  68. }
  69. return Status::OK();
  70. }
  71. Status DataBuffer::SliceOff(int64_t number_of_rows) {
  72. while (number_of_rows > 0) {
  73. tensor_table_->pop_back();
  74. number_of_rows--;
  75. }
  76. return Status::OK();
  77. }
  78. } // namespace dataset
  79. } // namespace mindspore