5 ms·
Could you be more precise about what do you mean by fork-join model? Is this similar to how in Go starting a thread is easy and waiting for it to finish and col
by amelius 6y ago
Could you be more precise about what do you mean by fork-join model? Is this similar to how in Go starting a thread is easy and waiting for it to finish and collecting results is easy as well?
- dragontamer 6y agoI don't program in Go, so I can't speak to that. The fork-join model generally starts off with one-thread, often called "The Single" or "The Master". Its where main() begins. But somewhere along the line, you discover a portion of code that needs to be multithreaded. So you fork into many threads: maybe dozens of threads, or thousands in the case of SIMD or GPUs. This multithreaded portion is then "joined", that is, the "master" thread refuses to continue until all children are done executing. Lets say there's a raytracer you are writing. main() { // In the "Single" thread Foo(); Bar(); fork(doRayCastingInParallel(), 5-million); // Spawns 5,000,000-threads, one for each ray // Implicitly wait for all 5-million threads to finish doSomethingElse(); } Now, raycasting has a bunch of things to do, but you can break it up into the same steps for all 5-million threads. doRayCastingInParallel(int myIdx){ doTrigonometry(ray[myIdx]); findCollision(ray[myIdx]); outputColor[myIdx] = calculateColors(ray[myIdx]); } So from both the "single" thread, as well as your "child" thread, it feels like you're writing single-threaded code. But in actuality, your "child" thread is a 5-million spawned copy, executing in gross parallelism. The "Fork-Join" model assigns a different "myIdx" number to each thread. So Ray#500 knows to do things differently than Ray#68121. But otherwise, they run through the same code. --------------- The important bit here is how general this mindset is. Its commonly used for GPU-programming (CPU issues a GPU command to spawn millions of threads often one-per-pixel, or even thousands per pixel). Then the CPU waits for the GPU threads to finish, and then just carries on. But ISPC proved that you can use this fork-join model on SIMD units like AVX512 or SSE. Julia and Python are showing off how you can use it in higher level languages. Etc. etc. OpenMP further proves that fork-join works on normal CPU threads. Maybe there's some other CPU-threading languages (but OpenMP is the one I'm personally familiar with).
- pkulak 6y agoIsn't that how nearly all multi-threaded code is written? Wouldn't the only other option be fire-and-forget? I've been writing code like this is nearly every language I've ever used for decades. Are you talking about the actual fork() function, instead of, say, spawning inside a loop explicitly?
- dragontamer 6y ago> Isn't that how nearly all multi-threaded code is written Producer-consumer queues say otherwise. As does Async. There's also message-passing. There's also thread-pools. Task-based programming doesn't wait for the tasks to finish. So without the "join", you don't have an easy synchronization point to rely upon, and have to use explicit locks for synchronization. ------ Producer - consumer can't work with fork/join, because there's no "join" at all! The producer keeps producing, and the consumers just keep consuming. Async is... a tangled mess. I'm pretty sure its just the modern "unstructured goto" style of spaghetti that has been rediscovered. Yeah, its super-efficient, but no, its not really easy to program. Task-based parallelism is probably my next favorite style. Fork-join is super easy, but the join causes a significant waiting period and underutilization of the processor. Task-based provides more flexibility (at the cost of a little bit more complexity). ------ There's a ton of other models of parallelism. But fork-join seems to be the methodology that more and more work is being focused on.
- leephillips 6y agoAs I mention in the article, Julia has task-based parellelism now, with a @spawn macro.
- dragontamer 6y agoThat's wonderful to hear! I'm looking into it: and it looks like "Stupid Fibonacci" proves that its working in Julia (https://julialang.org/blog/2019/07/multithreading/ https://julialang.org/blog/2019/07/multithreading/). Task-based parallelism is fraught with more overhead than other patterns. But it really is a powerful construct: converting recursive ideas into multithreaded concepts rather easily.