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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. // Spawn a thread to read input_ vector and put it in my_conn
  118. rc = tg_->CreateAsyncTask("Worker Push",
  119. std::bind(&MindDataTestConnector::FirstWorkerPush,
  120. this, // passing this instance
  121. 0, // id = 0 for this simple one to one case
  122. my_conn, // the connector
  123. 0, // start index to read from the input_ list
  124. 1)); // the offset to read the next index
  125. RETURN_IF_NOT_OK(rc);
  126. // Spawn another thread to read from my_conn and write to _output vector.
  127. rc = tg_->CreateAsyncTask("Worker Pull",
  128. std::bind(&MindDataTestConnector::SerialWorkerPull,
  129. this,
  130. 0,
  131. my_conn,
  132. &output));
  133. RETURN_IF_NOT_OK(rc);
  134. tg_->join_all();
  135. my_conn.reset();
  136. return ValidateOutput(output);
  137. }
  138. Status MindDataTestConnector::Run_test_1() {
  139. std::vector<uint32_t> output;
  140. Status rc;
  141. // number of threads in each layer
  142. int l1_threads = 15;
  143. int l2_threads = 20;
  144. int l3_threads = 1;
  145. // Capacity for the first and second connectors
  146. int conn1_qcap = 5;
  147. int conn2_qcap = 10;
  148. auto conn1 = std::make_shared<Connector<uint32_t>>(l1_threads, // num of producers
  149. l2_threads, // num of consumers
  150. conn1_qcap); // the cap of each queue
  151. auto conn2 = std::make_shared<Connector<uint32_t>>(l2_threads,
  152. l3_threads,
  153. conn2_qcap);
  154. // Instantiating the threads in the first layer
  155. for (int i = 0; i < l1_threads; i++) {
  156. rc = tg_->CreateAsyncTask("First Worker Push",
  157. std::bind(&MindDataTestConnector::FirstWorkerPush,
  158. this, // passing this instance
  159. i, // thread id in this group of thread
  160. conn1, // the connector
  161. i, // start index to read from the input_ list
  162. l1_threads)); // the offset to read the next index
  163. RETURN_IF_NOT_OK(rc);
  164. }
  165. // Instantiating the threads in the 2nd layer
  166. for (int i = 0; i < l2_threads; i++) {
  167. rc = tg_->CreateAsyncTask("Mid Worker Job",
  168. std::bind(&MindDataTestConnector::MidWorkerJob,
  169. this, // passing this instance
  170. i, // thread id in this group of thread
  171. conn1, // the 1st connector
  172. conn2)); // the 2nd connector
  173. RETURN_IF_NOT_OK(rc);
  174. }
  175. // Last layer doing serialization to one queue to check if the order is preserved
  176. rc = tg_->CreateAsyncTask("Worker Pull",
  177. std::bind(&MindDataTestConnector::SerialWorkerPull,
  178. this,
  179. 0, // thread id = 0, since it's the only one
  180. conn2, // poping the data from conn2
  181. &output));
  182. RETURN_IF_NOT_OK(rc);
  183. tg_->join_all();
  184. conn1.reset();
  185. conn2.reset();
  186. return ValidateOutput(output);
  187. }
  188. Status MindDataTestConnector::SerialWorkerPull(
  189. int tid,
  190. std::shared_ptr<Connector<uint32_t>> my_conn,
  191. std::vector<uint32_t> *output
  192. ) {
  193. Status rc;
  194. TaskManager::FindMe()->Post();
  195. while (1) {
  196. uint32_t res;
  197. rc = my_conn->Pop(tid, &res);
  198. RETURN_IF_NOT_OK(rc);
  199. output->push_back(res);
  200. // Emulate different processing time for each thread
  201. if (sleep_ms_ != 0) {
  202. GoToSleep(sleep_ms_);
  203. }
  204. // Raising interrupt after it processed the last_input_.
  205. // This will trigger the MidWorkerJob threads to quit their worker loop.
  206. if (res == last_input_) {
  207. MS_LOG(INFO) << "All data is collected.";
  208. tg_->interrupt_all();
  209. break;
  210. }
  211. }
  212. return Status::OK();
  213. }
  214. Status MindDataTestConnector::FirstWorkerPush(
  215. int tid,
  216. std::shared_ptr<Connector<uint32_t> > my_conn,
  217. int start_in,
  218. int offset) {
  219. TaskManager::FindMe()->Post();
  220. DS_ASSERT(my_conn != nullptr);
  221. Status rc;
  222. for (int i = start_in; i < input_.size(); i += offset) {
  223. rc = my_conn->Push(tid, input_[i]);
  224. // Emulate different processing time for each thread
  225. if (sleep_ms_ != 0)
  226. GoToSleep(sleep_ms_);
  227. }
  228. return Status::OK();
  229. }
  230. // This worker loop read from a Connector and put the result into another Connector.
  231. Status MindDataTestConnector::MidWorkerJob(
  232. int tid,
  233. std::shared_ptr<Connector<uint32_t> > from_conn,
  234. std::shared_ptr<Connector<uint32_t> > to_conn) {
  235. DS_ASSERT((from_conn != nullptr) && (to_conn != nullptr));
  236. Status rc;
  237. TaskManager::FindMe()->Post();
  238. while (1) {
  239. uint32_t el;
  240. rc = from_conn->Pop(tid, &el);
  241. RETURN_IF_NOT_OK(rc);
  242. // Emulate different processing time for each thread
  243. if (sleep_ms_ != 0) {
  244. GoToSleep(sleep_ms_);
  245. }
  246. rc = to_conn->Push(tid, el);
  247. RETURN_IF_NOT_OK(rc);
  248. }
  249. return Status::OK();
  250. }
  251. Status MindDataTestConnector::ValidateOutput(const std::vector<uint32_t> &output) {
  252. int prev = 0;
  253. for (auto el : output) {
  254. if (prev >= el) {
  255. return Status(StatusCode::kUnexpectedError, "Output vector are not in-order.");
  256. }
  257. prev = el;
  258. }
  259. return Status::OK();
  260. }
  261. uint32_t MindDataTestConnector::GenRand(int max) {
  262. uint32_t r_int = 0;
  263. if (max == 0) {
  264. return r_int;
  265. }
  266. // open urandom not random
  267. int fd = open("/dev/urandom", O_RDONLY);
  268. if (fd > 0) {
  269. if (read(fd, &r_int, sizeof(uint32_t)) != sizeof(uint32_t)) {
  270. r_int = max / 2;
  271. }
  272. }
  273. (void)close(fd); // close it!
  274. return r_int % max;
  275. }
  276. // Put the current thread to sleep mode for MaxDue milliseconds.
  277. // (Imitating nondeterministic processing time)
  278. void MindDataTestConnector::GoToSleep(int max_dur) {
  279. uint32_t duration = GenRand(max_dur);
  280. std::this_thread::sleep_for(std::chrono::milliseconds(duration));
  281. }