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

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