testCTestHardwareAllocator.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <vector>
  5. #include "cmCTestHardwareAllocator.h"
  6. #include "cmCTestHardwareSpec.h"
  7. static const cmCTestHardwareSpec spec{ { {
  8. /* clang-format off */
  9. { "gpus", { { "0", 4 }, { "1", 8 }, { "2", 0 }, { "3", 8 } } },
  10. /* clang-format on */
  11. } } };
  12. bool testInitializeFromHardwareSpec()
  13. {
  14. bool retval = true;
  15. cmCTestHardwareAllocator allocator;
  16. allocator.InitializeFromHardwareSpec(spec);
  17. static const std::map<
  18. std::string, std::map<std::string, cmCTestHardwareAllocator::Resource>>
  19. expected{
  20. /* clang-format off */
  21. { "gpus", {
  22. { "0", { 4, 0 } },
  23. { "1", { 8, 0 } },
  24. { "2", { 0, 0 } },
  25. { "3", { 8, 0 } },
  26. } },
  27. /* clang-format on */
  28. };
  29. if (allocator.GetResources() != expected) {
  30. std::cout << "GetResources() did not return expected value\n";
  31. retval = false;
  32. }
  33. return retval;
  34. }
  35. bool testAllocateResource()
  36. {
  37. bool retval = true;
  38. cmCTestHardwareAllocator allocator;
  39. allocator.InitializeFromHardwareSpec(spec);
  40. static const std::map<
  41. std::string, std::map<std::string, cmCTestHardwareAllocator::Resource>>
  42. expected1{
  43. /* clang-format off */
  44. { "gpus", {
  45. { "0", { 4, 2 } },
  46. { "1", { 8, 0 } },
  47. { "2", { 0, 0 } },
  48. { "3", { 8, 0 } },
  49. } },
  50. /* clang-format on */
  51. };
  52. if (!allocator.AllocateResource("gpus", "0", 2)) {
  53. std::cout
  54. << "AllocateResource(\"gpus\", \"0\", 2) returned false, should be "
  55. "true\n";
  56. retval = false;
  57. }
  58. if (allocator.GetResources() != expected1) {
  59. std::cout << "GetResources() did not return expected value\n";
  60. retval = false;
  61. }
  62. static const std::map<
  63. std::string, std::map<std::string, cmCTestHardwareAllocator::Resource>>
  64. expected2{
  65. /* clang-format off */
  66. { "gpus", {
  67. { "0", { 4, 4 } },
  68. { "1", { 8, 0 } },
  69. { "2", { 0, 0 } },
  70. { "3", { 8, 0 } },
  71. } },
  72. /* clang-format on */
  73. };
  74. if (!allocator.AllocateResource("gpus", "0", 2)) {
  75. std::cout
  76. << "AllocateResource(\"gpus\", \"0\", 2) returned false, should be "
  77. "true\n";
  78. retval = false;
  79. }
  80. if (allocator.GetResources() != expected2) {
  81. std::cout << "GetResources() did not return expected value\n";
  82. retval = false;
  83. }
  84. static const std::map<
  85. std::string, std::map<std::string, cmCTestHardwareAllocator::Resource>>
  86. expected3{
  87. /* clang-format off */
  88. { "gpus", {
  89. { "0", { 4, 4 } },
  90. { "1", { 8, 0 } },
  91. { "2", { 0, 0 } },
  92. { "3", { 8, 0 } },
  93. } },
  94. /* clang-format on */
  95. };
  96. if (allocator.AllocateResource("gpus", "0", 1)) {
  97. std::cout
  98. << "AllocateResource(\"gpus\", \"0\", 1) returned true, should be "
  99. "false\n";
  100. retval = false;
  101. }
  102. if (allocator.GetResources() != expected3) {
  103. std::cout << "GetResources() did not return expected value\n";
  104. retval = false;
  105. }
  106. static const std::map<
  107. std::string, std::map<std::string, cmCTestHardwareAllocator::Resource>>
  108. expected4{
  109. /* clang-format off */
  110. { "gpus", {
  111. { "0", { 4, 4 } },
  112. { "1", { 8, 7 } },
  113. { "2", { 0, 0 } },
  114. { "3", { 8, 0 } },
  115. } },
  116. /* clang-format on */
  117. };
  118. if (!allocator.AllocateResource("gpus", "1", 7)) {
  119. std::cout
  120. << "AllocateResource(\"gpus\", \"1\", 7) returned false, should be "
  121. "true\n";
  122. retval = false;
  123. }
  124. if (allocator.AllocateResource("gpus", "1", 2)) {
  125. std::cout
  126. << "AllocateResource(\"gpus\", \"1\", 2) returned true, should be "
  127. "false\n";
  128. retval = false;
  129. }
  130. if (allocator.GetResources() != expected4) {
  131. std::cout << "GetResources() did not return expected value\n";
  132. retval = false;
  133. }
  134. static const std::map<
  135. std::string, std::map<std::string, cmCTestHardwareAllocator::Resource>>
  136. expected5{
  137. /* clang-format off */
  138. { "gpus", {
  139. { "0", { 4, 4 } },
  140. { "1", { 8, 7 } },
  141. { "2", { 0, 0 } },
  142. { "3", { 8, 0 } },
  143. } },
  144. /* clang-format on */
  145. };
  146. if (allocator.AllocateResource("gpus", "2", 1)) {
  147. std::cout
  148. << "AllocateResource(\"gpus\", \"2\", 1) returned true, should be "
  149. "false\n";
  150. retval = false;
  151. }
  152. if (allocator.GetResources() != expected5) {
  153. std::cout << "GetResources() did not return expected value\n";
  154. retval = false;
  155. }
  156. static const std::map<
  157. std::string, std::map<std::string, cmCTestHardwareAllocator::Resource>>
  158. expected6{
  159. /* clang-format off */
  160. { "gpus", {
  161. { "0", { 4, 4 } },
  162. { "1", { 8, 7 } },
  163. { "2", { 0, 0 } },
  164. { "3", { 8, 0 } },
  165. } },
  166. /* clang-format on */
  167. };
  168. if (allocator.AllocateResource("gpus", "4", 1)) {
  169. std::cout
  170. << "AllocateResource(\"gpus\", \"4\", 1) returned true, should be "
  171. "false\n";
  172. retval = false;
  173. }
  174. if (allocator.GetResources() != expected6) {
  175. std::cout << "GetResources() did not return expected value\n";
  176. retval = false;
  177. }
  178. static const std::map<
  179. std::string, std::map<std::string, cmCTestHardwareAllocator::Resource>>
  180. expected7{
  181. /* clang-format off */
  182. { "gpus", {
  183. { "0", { 4, 4 } },
  184. { "1", { 8, 7 } },
  185. { "2", { 0, 0 } },
  186. { "3", { 8, 0 } },
  187. } },
  188. /* clang-format on */
  189. };
  190. if (allocator.AllocateResource("threads", "0", 1)) {
  191. std::cout
  192. << "AllocateResource(\"threads\", \"0\", 1) returned true, should be"
  193. " false\n";
  194. retval = false;
  195. }
  196. if (allocator.GetResources() != expected7) {
  197. std::cout << "GetResources() did not return expected value\n";
  198. retval = false;
  199. }
  200. return retval;
  201. }
  202. bool testDeallocateResource()
  203. {
  204. bool retval = true;
  205. cmCTestHardwareAllocator allocator;
  206. allocator.InitializeFromHardwareSpec(spec);
  207. static const std::map<
  208. std::string, std::map<std::string, cmCTestHardwareAllocator::Resource>>
  209. expected1{
  210. /* clang-format off */
  211. { "gpus", {
  212. { "0", { 4, 1 } },
  213. { "1", { 8, 0 } },
  214. { "2", { 0, 0 } },
  215. { "3", { 8, 0 } },
  216. } },
  217. /* clang-format on */
  218. };
  219. if (!allocator.AllocateResource("gpus", "0", 2)) {
  220. std::cout
  221. << "AllocateResource(\"gpus\", \"0\", 2) returned false, should be "
  222. "true\n";
  223. retval = false;
  224. }
  225. if (!allocator.DeallocateResource("gpus", "0", 1)) {
  226. std::cout
  227. << "DeallocateResource(\"gpus\", \"0\", 1) returned false, should be"
  228. " true\n";
  229. retval = false;
  230. }
  231. if (allocator.GetResources() != expected1) {
  232. std::cout << "GetResources() did not return expected value\n";
  233. retval = false;
  234. }
  235. static const std::map<
  236. std::string, std::map<std::string, cmCTestHardwareAllocator::Resource>>
  237. expected2{
  238. /* clang-format off */
  239. { "gpus", {
  240. { "0", { 4, 1 } },
  241. { "1", { 8, 0 } },
  242. { "2", { 0, 0 } },
  243. { "3", { 8, 0 } },
  244. } },
  245. /* clang-format on */
  246. };
  247. if (allocator.DeallocateResource("gpus", "0", 2)) {
  248. std::cout
  249. << "DeallocateResource(\"gpus\", \"0\", 2) returned true, should be"
  250. " false\n";
  251. retval = false;
  252. }
  253. if (allocator.GetResources() != expected2) {
  254. std::cout << "GetResources() did not return expected value\n";
  255. retval = false;
  256. }
  257. static const std::map<
  258. std::string, std::map<std::string, cmCTestHardwareAllocator::Resource>>
  259. expected3{
  260. /* clang-format off */
  261. { "gpus", {
  262. { "0", { 4, 0 } },
  263. { "1", { 8, 0 } },
  264. { "2", { 0, 0 } },
  265. { "3", { 8, 0 } },
  266. } },
  267. /* clang-format on */
  268. };
  269. if (!allocator.DeallocateResource("gpus", "0", 1)) {
  270. std::cout
  271. << "DeallocateResource(\"gpus\", \"0\", 1) returned false, should be"
  272. " true\n";
  273. retval = false;
  274. }
  275. if (allocator.GetResources() != expected3) {
  276. std::cout << "GetResources() did not return expected value\n";
  277. retval = false;
  278. }
  279. static const std::map<
  280. std::string, std::map<std::string, cmCTestHardwareAllocator::Resource>>
  281. expected4{
  282. /* clang-format off */
  283. { "gpus", {
  284. { "0", { 4, 0 } },
  285. { "1", { 8, 0 } },
  286. { "2", { 0, 0 } },
  287. { "3", { 8, 0 } },
  288. } },
  289. /* clang-format on */
  290. };
  291. if (allocator.DeallocateResource("gpus", "0", 1)) {
  292. std::cout
  293. << "DeallocateResource(\"gpus\", \"0\", 1) returned true, should be"
  294. " false\n";
  295. retval = false;
  296. }
  297. if (allocator.GetResources() != expected4) {
  298. std::cout << "GetResources() did not return expected value\n";
  299. retval = false;
  300. }
  301. static const std::map<
  302. std::string, std::map<std::string, cmCTestHardwareAllocator::Resource>>
  303. expected5{
  304. /* clang-format off */
  305. { "gpus", {
  306. { "0", { 4, 0 } },
  307. { "1", { 8, 0 } },
  308. { "2", { 0, 0 } },
  309. { "3", { 8, 0 } },
  310. } },
  311. /* clang-format on */
  312. };
  313. if (allocator.DeallocateResource("gpus", "4", 1)) {
  314. std::cout
  315. << "DeallocateResource(\"gpus\", \"4\", 1) returned true, should be"
  316. " false\n";
  317. retval = false;
  318. }
  319. if (allocator.GetResources() != expected5) {
  320. std::cout << "GetResources() did not return expected value\n";
  321. retval = false;
  322. }
  323. static const std::map<
  324. std::string, std::map<std::string, cmCTestHardwareAllocator::Resource>>
  325. expected6{
  326. /* clang-format off */
  327. { "gpus", {
  328. { "0", { 4, 0 } },
  329. { "1", { 8, 0 } },
  330. { "2", { 0, 0 } },
  331. { "3", { 8, 0 } },
  332. } },
  333. /* clang-format on */
  334. };
  335. if (allocator.DeallocateResource("threads", "0", 1)) {
  336. std::cout
  337. << "DeallocateResource(\"threads\", \"0\", 1) returned true, should be"
  338. " false\n";
  339. retval = false;
  340. }
  341. if (allocator.GetResources() != expected6) {
  342. std::cout << "GetResources() did not return expected value\n";
  343. retval = false;
  344. }
  345. return retval;
  346. }
  347. bool testResourceFree()
  348. {
  349. bool retval = true;
  350. const cmCTestHardwareAllocator::Resource r1{ 5, 0 };
  351. if (r1.Free() != 5) {
  352. std::cout << "cmCTestHardwareAllocator::Resource::Free() did not return "
  353. "expected value for { 5, 0 }\n";
  354. retval = false;
  355. }
  356. const cmCTestHardwareAllocator::Resource r2{ 3, 2 };
  357. if (r2.Free() != 1) {
  358. std::cout << "cmCTestHardwareAllocator::Resource::Free() did not return "
  359. "expected value for { 3, 2 }\n";
  360. retval = false;
  361. }
  362. const cmCTestHardwareAllocator::Resource r3{ 4, 4 };
  363. if (r3.Free() != 0) {
  364. std::cout << "cmCTestHardwareAllocator::Resource::Free() did not return "
  365. "expected value for { 4, 4 }\n";
  366. retval = false;
  367. }
  368. return retval;
  369. }
  370. int testCTestHardwareAllocator(int, char** const)
  371. {
  372. int retval = 0;
  373. if (!testInitializeFromHardwareSpec()) {
  374. std::cout << "in testInitializeFromHardwareSpec()\n";
  375. retval = -1;
  376. }
  377. if (!testAllocateResource()) {
  378. std::cout << "in testAllocateResource()\n";
  379. retval = -1;
  380. }
  381. if (!testDeallocateResource()) {
  382. std::cout << "in testDeallocateResource()\n";
  383. retval = -1;
  384. }
  385. if (!testResourceFree()) {
  386. std::cout << "in testResourceFree()\n";
  387. retval = -1;
  388. }
  389. return retval;
  390. }