4 ms·
Shouldn't the CPU just switch to a different thread while it is waiting for a memory request to be fulfilled? So you could just run binary searches in different
by amelius 7y ago
Shouldn't the CPU just switch to a different thread while it is waiting for a memory request to be fulfilled? So you could just run binary searches in different threads.
- phoe-krk 7y agoI am not sure if context switches between different threads aren't going to dominate the cost here. It does sound like a little bit of assembly code that performs a binary search has a much smaller cost than all the stack manipulations associated with a thread preemption.
- foota 7y agoContext switches are extremely expensive, no way would it make sense to do it every memory access. Edit: https://eli.thegreenplace.net/2018/measuring-context-switching-and-memory-overheads-for-linux-threads/ https://eli.thegreenplace.net/2018/measuring-context-switchi... cites a couple microseconds, whereas memory latency is measured in tens of nanoseconds. So several dozen times more expensive to context switch.
- magicalhippo 7y agoThis is what Hyper-threading[1] is designed to do. The point is that it's expensive for the OS to switch threads, so the CPU instead tells the OS it can run two threads at the same time, and does so by sharing execution resources. Gives a nice boost for programs that do a fair bit of calculations while also having a fair share of cache misses. If you have tight loops with little memory access you won't gain much. [1]: https://en.wikipedia.org/wiki/Hyper-threading https://en.wikipedia.org/wiki/Hyper-threading
- nkurz 7y agoIt's a good question, but I think that the problem is that the overhead of switching between threads is much greater than the number of cycles wasted by waiting for the memory request. I think the fastest context switches on Linux are around 1 µs (1/1000000 of a second, or about 4,000 cycles)[1], whereas a request from RAM is around 25 ns (1/40 of a µs, or about 100 cycles)[2]. It's possible my numbers are off by a bit, but I think this means that on a standard CPU, you really need to parallelize the search more explicitly rather than letting the OS handle it for you. GPU's on the other hand do take a similar "brute force" approach to context switching, and clearly have good results on some similar problems. [1] https://eli.thegreenplace.net/2018/measuring-context-switching-and-memory-overheads-for-linux-threads/ https://eli.thegreenplace.net/2018/measuring-context-switchi... [2] https://www.anandtech.com/show/9483/intel-skylake-review-6700k-6600k-ddr4-ddr3-ipc-6th-generation/7 https://www.anandtech.com/show/9483/intel-skylake-review-670...
- asdfasgasdgasdg 7y agoAs others have stated, thread switching overhead is much higher than even the cost of a main memory fetch, so we can't afford to switch a thread on every pipeline stall. Numbers Every Programmer Should Know: https://people.eecs.berkeley.edu/~rcs/research/interactive_latency.html https://people.eecs.berkeley.edu/~rcs/research/interactive_l... (Although I see that this does not include thread switching latency, possibly because it depends on the OS and thread type. I'll leave the link because it's generally useful.)
- gridspy 7y agoThis is actually exactly what is going on in this example. The CPU is able to execute instructions from the various searches in whatever order the data arrives. You can learn more about the process here : https://en.wikipedia.org/wiki/Out-of-order_execution https://en.wikipedia.org/wiki/Out-of-order_execution Because the process does not create full "threads," the CPU doesn't need to spend time doing things like saving registers or figuring out what to execute next.