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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.";
  27. row->clear();
  28. }
  29. }
  30. // Shut down the data pipeline.
  31. void Iterator::Stop() {
  32. // Releasing the iterator_ unique_ptre. This should trigger the destructor of iterator_.
  33. iterator_.reset();
  34. // Release ownership of tree_ shared pointer. This will decrement the ref count.
  35. tree_.reset();
  36. }
  37. // Function to build and launch the execution tree.
  38. Status Iterator::BuildAndLaunchTree(std::shared_ptr<Dataset> ds) {
  39. // One time init
  40. Status rc;
  41. rc = GlobalInit();
  42. RETURN_IF_NOT_OK(rc);
  43. // Instantiate the execution tree
  44. tree_ = std::make_shared<ExecutionTree>();
  45. // Iterative BFS converting Dataset tree into runtime Execution tree.
  46. std::queue<std::pair<std::shared_ptr<Dataset>, std::shared_ptr<DatasetOp>>> q;
  47. if (ds == nullptr) {
  48. RETURN_STATUS_UNEXPECTED("Input is null pointer");
  49. } else {
  50. // Convert the current root node.
  51. auto root_op = ds->Build()->front();
  52. RETURN_UNEXPECTED_IF_NULL(root_op);
  53. RETURN_IF_NOT_OK(tree_->AssociateNode(root_op));
  54. q.push(std::make_pair(ds, root_op));
  55. // Traverse down to the children and convert them to the corresponding DatasetOps (i.e. execution tree nodes)
  56. while (!q.empty()) {
  57. auto node_pair = q.front();
  58. q.pop();
  59. // Iterate through all the direct children of the first element in our BFS queue
  60. for (auto child : node_pair.first->children) {
  61. auto child_ops = child->Build();
  62. RETURN_UNEXPECTED_IF_NULL(child_ops);
  63. auto node_op = node_pair.second;
  64. // Iterate through all the DatasetOps returned by calling Build on the last Dataset object, associate them
  65. // with the execution tree and add the child and parent relationship between the nodes
  66. // Note that some Dataset objects might return more than one DatasetOps
  67. // e.g. MapDataset will return MapOp and ProjectOp if project_columns is set for MapDataset
  68. for (auto child_op : *child_ops) {
  69. RETURN_IF_NOT_OK(tree_->AssociateNode(child_op));
  70. RETURN_IF_NOT_OK(node_op->AddChild(child_op));
  71. node_op = child_op;
  72. }
  73. // Add the child and the last element of the returned DatasetOps (which is now the leaf node in our current
  74. // execution tree) to the BFS queue
  75. q.push(std::make_pair(child, child_ops->back()));
  76. }
  77. }
  78. RETURN_IF_NOT_OK(tree_->AssignRoot(root_op));
  79. }
  80. // Launch the execution tree.
  81. RETURN_IF_NOT_OK(tree_->Prepare());
  82. RETURN_IF_NOT_OK(tree_->Launch());
  83. iterator_ = std::make_unique<DatasetIterator>(tree_);
  84. RETURN_UNEXPECTED_IF_NULL(iterator_);
  85. return rc;
  86. }
  87. } // namespace api
  88. } // namespace dataset
  89. } // namespace mindspore