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