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.

connector_test.cc 12 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 <fcntl.h>
  17. #include <iostream>
  18. #include <memory>
  19. #include <vector>
  20. #include <chrono>
  21. #include <thread>
  22. #include "common/common.h"
  23. #include "minddata/dataset/engine/connector.h"
  24. #include "minddata/dataset/util/task_manager.h"
  25. #include "utils/log_adapter.h"
  26. using namespace mindspore::dataset;
  27. using mindspore::MsLogLevel::INFO;
  28. using mindspore::ExceptionType::NoExceptionType;
  29. using mindspore::LogStream;
  30. class MindDataTestConnector : public UT::Common {
  31. public:
  32. MindDataTestConnector();
  33. // Test scenario: single producer, single consumer.
  34. // This means there is only one queue in the connector.
  35. Status Run_test_0();
  36. // Test scenario: multiple producers, multiple cosumers
  37. // A chain of three layer of thread groups connected by two Connectors between
  38. // two layer. You can set different num of threads on layer 1 and 2, and layer 3
  39. // that does the serialization to _ouput vector needs to be single thread.
  40. // A random sleep/delay can be introduced for each thread. See run().
  41. Status Run_test_1();
  42. void SetSleepMilliSec(uint32_t ms) { sleep_ms_ = ms; }
  43. private:
  44. std::unique_ptr<TaskGroup> tg_;
  45. uint32_t last_input_;
  46. uint32_t sleep_ms_ = 0;
  47. std::vector<uint32_t> input_;
  48. WaitPost wp;
  49. // This worker loop is to be called by a single thread. It will pop my_conn Connector
  50. // and populate output vector
  51. Status SerialWorkerPull(
  52. int tid,
  53. std::shared_ptr<Connector<uint32_t>> my_conn,
  54. std::vector<uint32_t> *output
  55. );
  56. // This worker loop read from input_ vector that have complete list of tasks/elements.
  57. // The assignment from the elements in input_ to each worker is ensured in RoundRobin,
  58. // i.e., tid-0 will pick input_[0], tid-1 will pick input_[1], so-on circularly.
  59. Status FirstWorkerPush(
  60. int tid,
  61. std::shared_ptr<Connector<uint32_t> > my_conn,
  62. int start_in,
  63. int offset);
  64. // This worker loop read from a Connector and put the result into another Connector.
  65. Status MidWorkerJob(
  66. int tid,
  67. std::shared_ptr<Connector<uint32_t> > from_conn,
  68. std::shared_ptr<Connector<uint32_t> > to_conn);
  69. Status ValidateOutput(const std::vector<uint32_t> &output);
  70. uint32_t GenRand(int max);
  71. // Put the current thread to sleep mode for MaxDue milliseconds.
  72. // (Imitating nondeterministic processing time)
  73. void GoToSleep(int max_dur);
  74. };
  75. // Test0 : single producer, single consumer which means there is only one queue in the connector
  76. TEST_F(MindDataTestConnector, Test0) {
  77. MS_LOG(INFO) << "MindDataTestConnector Test0: single producer, single consumer.";
  78. Status rc = this->Run_test_0();
  79. ASSERT_TRUE(rc.IsOk());
  80. rc = TaskManager::GetMasterThreadRc();
  81. ASSERT_TRUE(rc.IsOk());
  82. }
  83. // Test1: multiple producers, multiple consumers without random delay
  84. // A chain of three layer of thread groups connected by two Connectors between
  85. // two layer.
  86. TEST_F(MindDataTestConnector, Test1) {
  87. MS_LOG(INFO) << "MindDataTestConnector Test1.";
  88. Status rc = this->Run_test_1();
  89. ASSERT_TRUE(rc.IsOk());
  90. rc = TaskManager::GetMasterThreadRc();
  91. ASSERT_TRUE(rc.IsOk());
  92. }
  93. // Test1: multiple producers, multiple consumers with random delay after push/pop
  94. // A chain of three layer of thread groups connected by two Connectors between
  95. // two layer.
  96. TEST_F(MindDataTestConnector, Test2) {
  97. MS_LOG(INFO) << "MindDataTestConnector Test2.";
  98. this->SetSleepMilliSec(30);
  99. Status rc = this->Run_test_1();
  100. ASSERT_TRUE(rc.IsOk());
  101. rc = TaskManager::GetMasterThreadRc();
  102. ASSERT_TRUE(rc.IsOk());
  103. }
  104. // Implementation of MindDataTestConnector class and the helper functions.
  105. MindDataTestConnector::MindDataTestConnector() : tg_(new TaskGroup()) {
  106. last_input_ = 150;
  107. for (int i = 1; i <= last_input_; i++) {
  108. input_.push_back(i);
  109. }
  110. wp.Register(tg_.get());
  111. }
  112. Status MindDataTestConnector::Run_test_0() {
  113. Status rc;
  114. std::vector<uint32_t> output;
  115. wp.Clear();
  116. auto my_conn = std::make_shared<Connector<uint32_t>>(1, // num of producers
  117. 1, // num of consumers
  118. 10); // capacity of each queue
  119. MS_ASSERT(my_conn != nullptr);
  120. rc = my_conn->Register(tg_.get());
  121. RETURN_IF_NOT_OK(rc);
  122. // Spawn a thread to read input_ vector and put it in my_conn
  123. rc = tg_->CreateAsyncTask("Worker Push",
  124. std::bind(&MindDataTestConnector::FirstWorkerPush,
  125. this, // passing this instance
  126. 0, // id = 0 for this simple one to one case
  127. my_conn, // the connector
  128. 0, // start index to read from the input_ list
  129. 1)); // the offset to read the next index
  130. RETURN_IF_NOT_OK(rc);
  131. // Spawn another thread to read from my_conn and write to _output vector.
  132. rc = tg_->CreateAsyncTask("Worker Pull",
  133. std::bind(&MindDataTestConnector::SerialWorkerPull,
  134. this,
  135. 0,
  136. my_conn,
  137. &output));
  138. RETURN_IF_NOT_OK(rc);
  139. // Wait for the threads to finish.
  140. rc = wp.Wait();
  141. EXPECT_TRUE(rc.IsOk());
  142. tg_->interrupt_all();
  143. tg_->join_all(Task::WaitFlag::kNonBlocking);
  144. my_conn.reset();
  145. return ValidateOutput(output);
  146. }
  147. Status MindDataTestConnector::Run_test_1() {
  148. std::vector<uint32_t> output;
  149. Status rc;
  150. wp.Clear();
  151. // number of threads in each layer
  152. int l1_threads = 15;
  153. int l2_threads = 20;
  154. int l3_threads = 1;
  155. // Capacity for the first and second connectors
  156. int conn1_qcap = 5;
  157. int conn2_qcap = 10;
  158. auto conn1 = std::make_shared<Connector<uint32_t>>(l1_threads, // num of producers
  159. l2_threads, // num of consumers
  160. conn1_qcap); // the cap of each queue
  161. auto conn2 = std::make_shared<Connector<uint32_t>>(l2_threads,
  162. l3_threads,
  163. conn2_qcap);
  164. rc = conn1->Register(tg_.get());
  165. RETURN_IF_NOT_OK(rc);
  166. rc = conn2->Register(tg_.get());
  167. RETURN_IF_NOT_OK(rc);
  168. // Instantiating the threads in the first layer
  169. for (int i = 0; i < l1_threads; i++) {
  170. rc = tg_->CreateAsyncTask("First Worker Push",
  171. std::bind(&MindDataTestConnector::FirstWorkerPush,
  172. this, // passing this instance
  173. i, // thread id in this group of thread
  174. conn1, // the connector
  175. i, // start index to read from the input_ list
  176. l1_threads)); // the offset to read the next index
  177. RETURN_IF_NOT_OK(rc);
  178. }
  179. // Instantiating the threads in the 2nd layer
  180. for (int i = 0; i < l2_threads; i++) {
  181. rc = tg_->CreateAsyncTask("Mid Worker Job",
  182. std::bind(&MindDataTestConnector::MidWorkerJob,
  183. this, // passing this instance
  184. i, // thread id in this group of thread
  185. conn1, // the 1st connector
  186. conn2)); // the 2nd connector
  187. RETURN_IF_NOT_OK(rc);
  188. }
  189. // Last layer doing serialization to one queue to check if the order is preserved
  190. rc = tg_->CreateAsyncTask("Worker Pull",
  191. std::bind(&MindDataTestConnector::SerialWorkerPull,
  192. this,
  193. 0, // thread id = 0, since it's the only one
  194. conn2, // poping the data from conn2
  195. &output));
  196. RETURN_IF_NOT_OK(rc);
  197. // Wait for the threads to finish.
  198. rc = wp.Wait();
  199. EXPECT_TRUE(rc.IsOk());
  200. tg_->interrupt_all();
  201. tg_->join_all(Task::WaitFlag::kNonBlocking);
  202. conn1.reset();
  203. conn2.reset();
  204. return ValidateOutput(output);
  205. }
  206. Status MindDataTestConnector::SerialWorkerPull(
  207. int tid,
  208. std::shared_ptr<Connector<uint32_t>> my_conn,
  209. std::vector<uint32_t> *output
  210. ) {
  211. Status rc;
  212. TaskManager::FindMe()->Post();
  213. while (1) {
  214. uint32_t res;
  215. rc = my_conn->Pop(tid, &res);
  216. RETURN_IF_NOT_OK(rc);
  217. output->push_back(res);
  218. // Emulate different processing time for each thread
  219. if (sleep_ms_ != 0) {
  220. GoToSleep(sleep_ms_);
  221. }
  222. // Signal master thread after it processed the last_input_.
  223. // This will trigger the MidWorkerJob threads to quit their worker loop.
  224. if (res == last_input_) {
  225. MS_LOG(INFO) << "All data is collected.";
  226. wp.Set();
  227. break;
  228. }
  229. }
  230. return Status::OK();
  231. }
  232. Status MindDataTestConnector::FirstWorkerPush(
  233. int tid,
  234. std::shared_ptr<Connector<uint32_t> > my_conn,
  235. int start_in,
  236. int offset) {
  237. TaskManager::FindMe()->Post();
  238. MS_ASSERT(my_conn != nullptr);
  239. Status rc;
  240. for (int i = start_in; i < input_.size(); i += offset) {
  241. rc = my_conn->Push(tid, input_[i]);
  242. // Emulate different processing time for each thread
  243. if (sleep_ms_ != 0)
  244. GoToSleep(sleep_ms_);
  245. }
  246. return Status::OK();
  247. }
  248. // This worker loop read from a Connector and put the result into another Connector.
  249. Status MindDataTestConnector::MidWorkerJob(
  250. int tid,
  251. std::shared_ptr<Connector<uint32_t> > from_conn,
  252. std::shared_ptr<Connector<uint32_t> > to_conn) {
  253. MS_ASSERT((from_conn != nullptr) && (to_conn != nullptr));
  254. Status rc;
  255. TaskManager::FindMe()->Post();
  256. while (1) {
  257. uint32_t el;
  258. rc = from_conn->Pop(tid, &el);
  259. RETURN_IF_NOT_OK(rc);
  260. // Emulate different processing time for each thread
  261. if (sleep_ms_ != 0) {
  262. GoToSleep(sleep_ms_);
  263. }
  264. rc = to_conn->Push(tid, el);
  265. RETURN_IF_NOT_OK(rc);
  266. }
  267. return Status::OK();
  268. }
  269. Status MindDataTestConnector::ValidateOutput(const std::vector<uint32_t> &output) {
  270. int prev = 0;
  271. for (auto el : output) {
  272. if (prev >= el) {
  273. return Status(StatusCode::kUnexpectedError, "Output vector are not in-order.");
  274. }
  275. prev = el;
  276. }
  277. return Status::OK();
  278. }
  279. uint32_t MindDataTestConnector::GenRand(int max) {
  280. uint32_t r_int = 0;
  281. if (max == 0) {
  282. return r_int;
  283. }
  284. // open urandom not random
  285. int fd = open("/dev/urandom", O_RDONLY);
  286. if (fd > 0) {
  287. if (read(fd, &r_int, sizeof(uint32_t)) != sizeof(uint32_t)) {
  288. r_int = max / 2;
  289. }
  290. }
  291. (void)close(fd); // close it!
  292. return r_int % max;
  293. }
  294. // Put the current thread to sleep mode for MaxDue milliseconds.
  295. // (Imitating nondeterministic processing time)
  296. void MindDataTestConnector::GoToSleep(int max_dur) {
  297. uint32_t duration = GenRand(max_dur);
  298. std::this_thread::sleep_for(std::chrono::milliseconds(duration));
  299. }