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.

execution_tree_test.cc 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <string>
  17. #include "minddata/dataset/util/circular_pool.h"
  18. #include "minddata/dataset/core/client.h"
  19. #include "minddata/dataset/engine/execution_tree.h"
  20. #include "minddata/dataset/engine/datasetops/shuffle_op.h"
  21. #include "minddata/dataset/engine/datasetops/source/tf_reader_op.h"
  22. #include "common/common.h"
  23. #include "gtest/gtest.h"
  24. #include "utils/log_adapter.h"
  25. using namespace mindspore::dataset;
  26. using mindspore::MsLogLevel::INFO;
  27. using mindspore::ExceptionType::NoExceptionType;
  28. using mindspore::LogStream;
  29. class MindDataTestExecutionTree : public UT::DatasetOpTesting {
  30. public:
  31. };
  32. // Construct some tree nodes and play with them
  33. TEST_F(MindDataTestExecutionTree, TestExecutionTree1) {
  34. MS_LOG(INFO) << "Doing MindDataTestExecutionTree1.";
  35. // Start with an empty execution tree
  36. auto my_tree = std::make_shared<ExecutionTree>();
  37. ASSERT_NE(my_tree, nullptr);
  38. uint32_t shuffle_size = 32;
  39. uint32_t connector_size = 8;
  40. std::shared_ptr<ShuffleOp> leaf_op1 = std::make_shared<ShuffleOp>(shuffle_size, 0, connector_size, false);
  41. ASSERT_NE(leaf_op1, nullptr);
  42. my_tree->AssociateNode(leaf_op1);
  43. shuffle_size = 16;
  44. std::shared_ptr<ShuffleOp> leaf_op2 = std::make_shared<ShuffleOp>(shuffle_size, 0, connector_size, false);
  45. ASSERT_NE(leaf_op2, nullptr);
  46. my_tree->AssociateNode(leaf_op2);
  47. shuffle_size = 8;
  48. std::shared_ptr<ShuffleOp> parent_op = std::make_shared<ShuffleOp>(shuffle_size, 0, connector_size, false);
  49. ASSERT_NE(parent_op, nullptr);
  50. my_tree->AssociateNode(parent_op);
  51. // It's up to you if you want to use move semantic or not here.
  52. // By using move, we transfer ownership of the child to the parent.
  53. // If you do not use move,
  54. // we get a reference count bump to the pointer and you have
  55. // your own pointer to it, plus the parent has a copy of the pointer.
  56. parent_op->AddChild(std::move(leaf_op1));
  57. parent_op->AddChild(std::move(leaf_op2));
  58. shuffle_size = 4;
  59. std::shared_ptr<DatasetOp> root_op = std::make_shared<ShuffleOp>(shuffle_size, 0, connector_size, false);
  60. my_tree->AssignRoot(root_op);
  61. root_op->AddChild(parent_op);
  62. ASSERT_NE(root_op, nullptr);
  63. // Testing Iterator
  64. MS_LOG(INFO) << "Testing Tree Iterator from root.";
  65. for (auto itr = my_tree->begin(); itr != my_tree->end(); ++itr) {
  66. itr->Print(std::cout, false);
  67. }
  68. MS_LOG(INFO) << "Finished testing Tree Iterator from root.";
  69. MS_LOG(INFO) << "Testing Tree Iterator from parentOp.";
  70. for (auto itr = my_tree->begin(parent_op); itr != my_tree->end(); ++itr) {
  71. itr->Print(std::cout, false);
  72. }
  73. MS_LOG(INFO) << "Finished testing Tree Iterator from parentOp.";
  74. // At this point, since move semantic was used,
  75. // I don't have any operator access myself now.
  76. // Ownership is fully transferred into the tree.
  77. // explicitly drive tree destruction rather than
  78. // wait for descoping (to examine in debugger
  79. // just to see it work)
  80. my_tree.reset();
  81. MS_LOG(INFO) << "Done.";
  82. }
  83. // Construct some tree nodes and play with them
  84. TEST_F(MindDataTestExecutionTree, TestExecutionTree2) {
  85. MS_LOG(INFO) << "Doing MindDataTestExecutionTree2.";
  86. Status rc;
  87. auto my_tree = std::make_shared<ExecutionTree>();
  88. std::string dataset_path = datasets_root_path_ + "/testDataset1/testDataset1.data";
  89. std::shared_ptr<TFReaderOp> my_tfreader_op;
  90. TFReaderOp::Builder()
  91. .SetDatasetFilesList({dataset_path})
  92. .SetWorkerConnectorSize(2)
  93. .SetNumWorkers(2)
  94. .Build(&my_tfreader_op);
  95. my_tree->AssociateNode(my_tfreader_op);
  96. my_tree->AssignRoot(my_tfreader_op);
  97. // prepare the tree
  98. my_tree->Prepare();
  99. // Launch the tree execution to kick off threads
  100. // and start running the pipeline
  101. MS_LOG(INFO) << "Launching my tree.";
  102. my_tree->Launch();
  103. // Simulate a parse of data from our pipeline.
  104. std::shared_ptr<DatasetOp> root_node = my_tree->root();
  105. // Start the loop of reading from our pipeline using iterator
  106. MS_LOG(INFO) << "Testing DatasetIterator in testTree2.";
  107. DatasetIterator di(my_tree);
  108. TensorRow buffer;
  109. rc = di.FetchNextTensorRow(&buffer);
  110. EXPECT_TRUE(rc.IsOk());
  111. while (!buffer.empty()) {
  112. rc = di.FetchNextTensorRow(&buffer);
  113. EXPECT_TRUE(rc.IsOk());
  114. }
  115. }
  116. // Construct some tree nodes and play with them
  117. TEST_F(MindDataTestExecutionTree, TestExecutionTree3) {
  118. MS_LOG(INFO) << "Doing MindDataTestExecutionTree3.";
  119. }