{"comment":{"url":"https://api.github.com/repos/jhellis3/Stockfish/comments/13834009","html_url":"https://github.com/jhellis3/Stockfish/commit/9bb7880b0f9a47a97a91abe188c4b19a386f28e3#commitcomment-13834009","id":13834009,"user":{"login":"lucasart","id":2278294,"avatar_url":"https://avatars.githubusercontent.com/u/2278294?v=3","gravatar_id":"","url":"https://api.github.com/users/lucasart","html_url":"https://github.com/lucasart","followers_url":"https://api.github.com/users/lucasart/followers","following_url":"https://api.github.com/users/lucasart/following{/other_user}","gists_url":"https://api.github.com/users/lucasart/gists{/gist_id}","starred_url":"https://api.github.com/users/lucasart/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lucasart/subscriptions","organizations_url":"https://api.github.com/users/lucasart/orgs","repos_url":"https://api.github.com/users/lucasart/repos","events_url":"https://api.github.com/users/lucasart/events{/privacy}","received_events_url":"https://api.github.com/users/lucasart/received_events","type":"User","site_admin":false},"position":null,"line":null,"path":"","commit_id":"9bb7880b0f9a47a97a91abe188c4b19a386f28e3","created_at":"2015-10-18T02:05:52Z","updated_at":"2015-10-18T02:05:52Z","body":"Very nice simplification!\r\n\r\nBut you can get rid of the \"Thread Overhead\". The time it takes to stop threads is not relevant to time management, because it is well below the timer resolution.\r\n\r\nI've instrumented the code to measure it:\r\n```\r\nauto startTimer = std::chrono::high_resolution_clock::now();\r\n\r\n// Wait until all threads have finished\r\nfor (Thread* th : Threads)\r\n\tif (th != this)\r\n\t\tth->wait_while(th->searching);\r\n\r\nauto stopTimer = std::chrono::high_resolution_clock::now();\r\nauto microSec = std::chrono::duration_cast<std::chrono::microseconds>(stopTimer - startTimer).count();\r\nsync_cout << \"Time taken to stop threads: \" << microSec << \" micro-seconds\" << sync_endl;\r\n```\r\nTime to stop 4 threads, on my 4 core machine:\r\n```\r\n$ ./stockfish bench 16 4 2>&1 | grep \"Time taken\"\r\nTime taken to stop threads: 10 micro-seconds\r\nTime taken to stop threads: 17 micro-seconds\r\nTime taken to stop threads: 17 micro-seconds\r\nTime taken to stop threads: 11 micro-seconds\r\nTime taken to stop threads: 0 micro-seconds\r\nTime taken to stop threads: 20 micro-seconds\r\nTime taken to stop threads: 11 micro-seconds\r\nTime taken to stop threads: 12 micro-seconds\r\nTime taken to stop threads: 12 micro-seconds\r\nTime taken to stop threads: 10 micro-seconds\r\nTime taken to stop threads: 11 micro-seconds\r\nTime taken to stop threads: 11 micro-seconds\r\nTime taken to stop threads: 20 micro-seconds\r\nTime taken to stop threads: 12 micro-seconds\r\nTime taken to stop threads: 16 micro-seconds\r\nTime taken to stop threads: 23 micro-seconds\r\nTime taken to stop threads: 18 micro-seconds\r\nTime taken to stop threads: 22 micro-seconds\r\nTime taken to stop threads: 16 micro-seconds\r\nTime taken to stop threads: 11 micro-seconds\r\nTime taken to stop threads: 8 micro-seconds\r\nTime taken to stop threads: 7 micro-seconds\r\nTime taken to stop threads: 24 micro-seconds\r\nTime taken to stop threads: 18 micro-seconds\r\nTime taken to stop threads: 7 micro-seconds\r\nTime taken to stop threads: 18 micro-seconds\r\nTime taken to stop threads: 20 micro-seconds\r\nTime taken to stop threads: 16 micro-seconds\r\nTime taken to stop threads: 22 micro-seconds\r\nTime taken to stop threads: 62 micro-seconds\r\nTime taken to stop threads: 24 micro-seconds\r\nTime taken to stop threads: 16 micro-seconds\r\nTime taken to stop threads: 18 micro-seconds\r\nTime taken to stop threads: 18 micro-seconds\r\nTime taken to stop threads: 0 micro-seconds\r\nTime taken to stop threads: 15 micro-seconds\r\nTime taken to stop threads: 17 micro-seconds\r\n```\r\nWell below the 5000 micro-seconds timer resolution.\r\n\r\nSame experiment with 8 threads, still on my 4 core machines, so using hyper-threading:\r\n```\r\n$ ./stockfish bench 16 8 2>&1 | grep \"Time taken\"\r\nTime taken to stop threads: 18 micro-seconds\r\nTime taken to stop threads: 21 micro-seconds\r\nTime taken to stop threads: 21 micro-seconds\r\nTime taken to stop threads: 26 micro-seconds\r\nTime taken to stop threads: 11 micro-seconds\r\nTime taken to stop threads: 33 micro-seconds\r\nTime taken to stop threads: 32 micro-seconds\r\nTime taken to stop threads: 28 micro-seconds\r\nTime taken to stop threads: 22 micro-seconds\r\nTime taken to stop threads: 27 micro-seconds\r\nTime taken to stop threads: 26 micro-seconds\r\nTime taken to stop threads: 35 micro-seconds\r\nTime taken to stop threads: 23 micro-seconds\r\nTime taken to stop threads: 30 micro-seconds\r\nTime taken to stop threads: 12 micro-seconds\r\nTime taken to stop threads: 18 micro-seconds\r\nTime taken to stop threads: 16 micro-seconds\r\nTime taken to stop threads: 23 micro-seconds\r\nTime taken to stop threads: 13 micro-seconds\r\nTime taken to stop threads: 27 micro-seconds\r\nTime taken to stop threads: 10 micro-seconds\r\nTime taken to stop threads: 10 micro-seconds\r\nTime taken to stop threads: 16 micro-seconds\r\nTime taken to stop threads: 13 micro-seconds\r\nTime taken to stop threads: 16 micro-seconds\r\nTime taken to stop threads: 17 micro-seconds\r\nTime taken to stop threads: 21 micro-seconds\r\nTime taken to stop threads: 25 micro-seconds\r\nTime taken to stop threads: 21 micro-seconds\r\nTime taken to stop threads: 16 micro-seconds\r\nTime taken to stop threads: 16 micro-seconds\r\nTime taken to stop threads: 14 micro-seconds\r\nTime taken to stop threads: 9 micro-seconds\r\nTime taken to stop threads: 17 micro-seconds\r\nTime taken to stop threads: 18 micro-seconds\r\nTime taken to stop threads: 22 micro-seconds\r\nTime taken to stop threads: 17 micro-seconds\r\n```\r\nStill well below the 5000 micro-seconds timer resolution.\r\n\r\nNow, let's try 64 cores! Keep in mind that I have only 4 cores on my machine, so I am simulating unrealistic extreme conditions:\r\n```\r\n$ ./stockfish bench 16 64 2>&1 | grep \"Time taken\"\r\nTime taken to stop threads: 235 micro-seconds\r\nTime taken to stop threads: 309 micro-seconds\r\nTime taken to stop threads: 225 micro-seconds\r\nTime taken to stop threads: 264 micro-seconds\r\nTime taken to stop threads: 270 micro-seconds\r\nTime taken to stop threads: 232 micro-seconds\r\nTime taken to stop threads: 265 micro-seconds\r\nTime taken to stop threads: 281 micro-seconds\r\nTime taken to stop threads: 255 micro-seconds\r\nTime taken to stop threads: 229 micro-seconds\r\nTime taken to stop threads: 327 micro-seconds\r\nTime taken to stop threads: 304 micro-seconds\r\nTime taken to stop threads: 266 micro-seconds\r\nTime taken to stop threads: 212 micro-seconds\r\nTime taken to stop threads: 250 micro-seconds\r\nTime taken to stop threads: 308 micro-seconds\r\nTime taken to stop threads: 258 micro-seconds\r\nTime taken to stop threads: 96 micro-seconds\r\nTime taken to stop threads: 248 micro-seconds\r\nTime taken to stop threads: 335 micro-seconds\r\nTime taken to stop threads: 3099 micro-seconds\r\nTime taken to stop threads: 172 micro-seconds\r\nTime taken to stop threads: 276 micro-seconds\r\nTime taken to stop threads: 265 micro-seconds\r\nTime taken to stop threads: 219 micro-seconds\r\nTime taken to stop threads: 293 micro-seconds\r\nTime taken to stop threads: 299 micro-seconds\r\nTime taken to stop threads: 329 micro-seconds\r\nTime taken to stop threads: 278 micro-seconds\r\nTime taken to stop threads: 272 micro-seconds\r\nTime taken to stop threads: 278 micro-seconds\r\nTime taken to stop threads: 210 micro-seconds\r\nTime taken to stop threads: 257 micro-seconds\r\nTime taken to stop threads: 206 micro-seconds\r\nTime taken to stop threads: 227 micro-seconds\r\nTime taken to stop threads: 112 micro-seconds\r\nTime taken to stop threads: 221 micro-seconds\r\n```\r\nStill below the 5000 micro-seconds timer resolution. Note that one run took longer than all the others (3099 micro), but that's expected with so much noise (64 search threads on 4 real cores, already busy the with OS and the browser open).\r\n\r\nI believe @mcostalba did the same experiment, and came to the same conclusion.\r\n\r\nThis \"time it takes to stop the threads\" is a red herring."}}