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.

iterator.cc 4.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * Copyright 2020 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/include/iterator.h"
  17. #include "minddata/dataset/core/client.h"
  18. #include "minddata/dataset/include/datasets.h"
  19. namespace mindspore {
  20. namespace dataset {
  21. namespace api {
  22. // Get the next row from the data pipeline.
  23. void Iterator::GetNextRow(TensorMap *row) {
  24. Status rc = iterator_->GetNextAsMap(row);
  25. if (rc.IsError()) {
  26. MS_LOG(ERROR) << "GetNextRow: Failed to get next row. Error status: " << rc;
  27. row->clear();
  28. }
  29. }
  30. // Get the next row from the data pipeline.
  31. void Iterator::GetNextRow(TensorVec *row) {
  32. TensorRow tensor_row;
  33. Status rc = iterator_->FetchNextTensorRow(&tensor_row);
  34. if (rc.IsError()) {
  35. MS_LOG(ERROR) << "GetNextRow: Failed to get next row. Error status: " << rc;
  36. row->clear();
  37. }
  38. // Generate a vector as return
  39. row->clear();
  40. std::copy(tensor_row.begin(), tensor_row.end(), std::back_inserter(*row));
  41. }
  42. // Shut down the data pipeline.
  43. void Iterator::Stop() {
  44. // Releasing the iterator_ unique_ptre. This should trigger the destructor of iterator_.
  45. iterator_.reset();
  46. // Release ownership of tree_ shared pointer. This will decrement the ref count.
  47. tree_.reset();
  48. }
  49. // Function to build and launch the execution tree.
  50. Status Iterator::BuildAndLaunchTree(std::shared_ptr<Dataset> ds) {
  51. // One time init
  52. Status rc;
  53. rc = GlobalInit();
  54. RETURN_IF_NOT_OK(rc);
  55. // Instantiate the execution tree
  56. tree_ = std::make_shared<ExecutionTree>();
  57. // Iterative BFS converting Dataset tree into runtime Execution tree.
  58. std::queue<std::pair<std::shared_ptr<Dataset>, std::shared_ptr<DatasetOp>>> q;
  59. if (ds == nullptr) {
  60. RETURN_STATUS_UNEXPECTED("Input is null pointer");
  61. } else {
  62. // Convert the current root node.
  63. auto root_ops = ds->Build();
  64. if (root_ops.empty()) {
  65. RETURN_STATUS_UNEXPECTED("Node operation returned nothing");
  66. }
  67. // Iterate through all the DatasetOps returned by Dataset's Build(), associate them
  68. // with the execution tree and add the child and parent relationship between the nodes
  69. // Note that some Dataset objects might return more than one DatasetOps
  70. // e.g. MapDataset will return [ProjectOp, MapOp] if project_columns is set for MapDataset
  71. std::shared_ptr<DatasetOp> prev_op = nullptr;
  72. for (auto op : root_ops) {
  73. RETURN_IF_NOT_OK(tree_->AssociateNode(op));
  74. if (prev_op != nullptr) {
  75. RETURN_IF_NOT_OK(prev_op->AddChild(op));
  76. }
  77. prev_op = op;
  78. }
  79. // Add the last DatasetOp to the queue to be BFS.
  80. q.push(std::make_pair(ds, root_ops.back()));
  81. // Traverse down to the children and convert them to the corresponding DatasetOps (i.e. execution tree nodes)
  82. while (!q.empty()) {
  83. auto node_pair = q.front();
  84. q.pop();
  85. // Iterate through all the direct children of the first element in our BFS queue
  86. for (auto child : node_pair.first->children) {
  87. auto child_ops = child->Build();
  88. if (child_ops.empty()) {
  89. RETURN_STATUS_UNEXPECTED("Node operation returned nothing");
  90. }
  91. auto node_op = node_pair.second;
  92. // Iterate through all the DatasetOps returned by calling Build on the last Dataset object, associate them
  93. // with the execution tree and add the child and parent relationship between the nodes
  94. // Note that some Dataset objects might return more than one DatasetOps
  95. // e.g. MapDataset will return MapOp and ProjectOp if project_columns is set for MapDataset
  96. for (auto child_op : child_ops) {
  97. RETURN_IF_NOT_OK(tree_->AssociateNode(child_op));
  98. RETURN_IF_NOT_OK(node_op->AddChild(child_op));
  99. node_op = child_op;
  100. }
  101. // Add the child and the last element of the returned DatasetOps (which is now the leaf node in our current
  102. // execution tree) to the BFS queue
  103. q.push(std::make_pair(child, child_ops.back()));
  104. }
  105. }
  106. RETURN_IF_NOT_OK(tree_->AssignRoot(root_ops.front()));
  107. }
  108. // Launch the execution tree.
  109. RETURN_IF_NOT_OK(tree_->Prepare());
  110. RETURN_IF_NOT_OK(tree_->Launch());
  111. iterator_ = std::make_unique<DatasetIterator>(tree_);
  112. RETURN_UNEXPECTED_IF_NULL(iterator_);
  113. return rc;
  114. }
  115. } // namespace api
  116. } // namespace dataset
  117. } // namespace mindspore