by dragontamer · 5y ago · 0 comments
Seriously: try fork() + wait(), or spawn in bash / cmd line. make -j offers parallelism (and Ninja is "make -j by default").
OpenMP's "#pragma omp parallel for" is extremely easy to use.
Things get harder with thread libraries, but the "parallelism" part of pthreads is pretty easy. pthread_create(&thread_id, NULL, function, args);, at least if you're fine with the default settings.
C++ Threads are even easier than pthreads.
-----------
The hard part is:
1. Communication / Synchronization -- But really, condition variables are the most complicated thing most people need. fork() + wait() push the difficult communication part to the wait() or to pipes. But if you stick with wait() / waitpid() as your main communication mechanism, its pretty easy to think about.
2. High performance programming -- This is just hard, especially if you begin to reach for the highest performance "memory barrier / lock-free programming" styles. This is where "false sharing" comes in (your code correctly executes under false sharing, but slowly due to how the L1 cache handles multicore architectures).
---------
However, if you stick with a simple fork-join communication model (ex: pthread_join, or wait()/waitpid() based synchronization)... its really not too hard at all. Just try to stay away from the high-performance techniques unless you really need them.
I'm pretty sure that anyone who's come into even vague contact with parallel programming understands that the hardest part is communication and synchronization.
Your post feels incredibly pedantic.
For many problems, that's literally good enough parallelism.
--------
Case in point: I once needed to render an animation in Blender, and I noticed that Blender's built-in parallelism was kind of crap (it was an older version of Blender at the time. My 32-thread / 16-core system barely reached 400% utilization... I know I could do better).
So I just spawned 16x Blenders in headless mode, each handling a different frame of the animation, and achieved far higher utilization than the innate Blender parallelism.
Yes, literally:
#!/bin/bash
for i in `seq 1 16`;
do
blender (params) & # Yeah, just run a bunch of blenders in headless + background mode
done
Bonus points: I tweaked the script to take advantage of NUMA-locality, by automatically pinning blender to one NUMA-node. All without ever touching a single line of C code.-------
Communication / synchronization is hard. But parallelism (even high-performance NUMA-locality with thread affinity pinned to particular CPU nodes) doesn't have to be hard. Just think a bit smarter.
--------
The equivalent to doing "./some_job &" is fork() and wait() in POSIX C / C++. That's good enough for a ton of problems (hell, its how us older programmers achieved parallelism back in the CGI-BIN days).
Don't do complicated high-performance techniques if you only need a bit of parallelism. Don't overthink things, stick to simple parallelism when simple solutions solve simple problem.
Don't premature optimize into complicated producer/consumer patterns unless you actually need the performance. Definitely don't premature optimize into high-performance lockless programming techniques unless you really need to.
We can quibble about the semantics of "parallel" programming all day, but I think that either too broad or too narrow a scope for the term misses the point: that data coherency is what makes parallel programming hard.
In the ultimate reductio ad absurdum, you can just say it falls into one of the two classes of hard computer science problems, that being "cache invalidation", where the "cache" is some form of shared data. In this sense, I agree with you: it's not the parallelism per se that's hard, but I think anyone talking about the difficulty of it quite clearly is referring to the shared data problems.
In my experience, I keep running into problems *already partitioned*.
Make -j. 3d-animation frames. SAXY arrays. (Seriously, do you know how many for loops I've come across that can become #pragma omp parallel for and just automatically benefit from parallelism??). Multiple connections to a server (ex: Fork/join Apache model / CGI-bin / PHP / etc. etc.). Multiple connections to other servers (SMTP, IRC, etc. etc.)
A ton of problems are "easy parallelism" in my experience. Probably most problems. Yeah, there's some hard problems out there, but beginners should learn how to parallelize the easy stuff first.
I think it's important to show and use the easy ways before getting into the complex things.
The rest of the book talks about doing data sharing efficiently (i.e. for not embarrassingly parallel problems), including some things I don't think I ever would have run across otherwise like RCU. All the while he explains the use cases for each of the techniques. If you are interested in making the most of your processors (and I suspect you are given this post), and are up for a technical read, I'd highly recommend this book.
EDIT: By which I mean to say that he covers all the stuff you posted, and explains exactly when it is worthwhile to use the high performance and communication techniques. If you want to know just the basic stuff, read the first 3 chapters.
* Who's there?
- GPUs.
* Closes door.
- Knock knock
- Knock knock
- Do you really want to ignore ~90% of the compute throughput and ~90% of the memory bandwidth of your machine?
- Knock knock
- ...
----
An AMD EPYC 7742 (Milan, Zen 3, announced last week, high-end server model) has ~100 GB/s DRAM bandwidth and 1-2 TFLOPS double precision.
A single PCI-e A100 has ~2Tb/s DRAM bandwidth, and 10-20 TFLOPs double precision. You can pack 4x of these per CPU socket... so you end up at 8 TB/s and 40-80 TFLOPs... That makes the GPUs like 99% of both the compute and memory BW of a socket.
---------
The issue is that there's two difference concepts here:
1. Parallelism -- When a problem you're working on has an undefined ordering to it explicitly. Protocols and communications for example have undefined orders. You could write a state-machine explicitly, but writing a thread is usually much simpler to understand. Ultimately: this is cofunctions, Python Yield / generators, async-code, etc. etc. Remember: Djikstra invented semaphores on a single-core processor. The concept of "single core parallelism" is hugely useful and can grossly simplify code if used correctly.
2. High performance -- This is a different concept entirely!! GPUs are within the realm of high-performance parallelism (and not just merely "conceptual parallelism" in #1).
GPUs are hard because anyone who uses a GPU expects high performance. Be it the video gamer who wants raytracing on his video game, or the machine learning enthusiast training a billion neurons.
Writing a simple "GPU Hello World" is easy as all heck actually. Its all the performance stuff that gets really hard.
The SIMD-model of A64Fx: the SVE instruction set of ARM, is coming to mainstream Neoverse V-series ARM chips within a year or two.
SVE is CPU-parallelism. Well, its SIMD, but its just 512-bit SIMD (equivalent to AVX512 in width). So that's smaller scale SIMD / CPU parallelism IMO.
Those A64Fx are going to be branch-predicting and executing those SIMD-instructions out-of-order. Its a CPU.
-------
I consider GPUs to be the simpler in-order but more parallel stuff (AMD, NVidia, Xilinx maybe). But you're right in that the line keeps getting blurred each year. (NVidia's dual-issue pipelines is a very CPU-like feature. AVX512 and SVE pushing 512-bits is getting closer-and-closer to GPU-like SIMD widths)
And if you can tolerate the overhead of spawning processes than none of this really matters, fork/join is fine.
Soundness within a pure Fork/join model is easier than you might think.
If any data needs to be shared between threads, just pthread_join() / wait(). Then execute the shared-portion of the data in a single thread. Then fork() back off after the sequential portion is complete.
There's no race condition because all the other threads are dead after the pthread_join() or wait() calls. As such, you're fully sound.
> And if you can tolerate the overhead of spawning processes than none of this really matters, fork/join is fine.
Exactly.
The main issue at hand here is that "parallel models" are useful mechanisms to organize code. A lot of parallel programming isn't about speed of execution, but instead about representing an innately parallel problem in their code.
If you have an "innately parallel problem" but do NOT care about performance, just pthread_create() / pthread_join() or fork()/wait() and be done with it.
It has nothing to do with code organization.
> If you have an "innately parallel problem" but do NOT care about performance, just pthread_create() / pthread_join() or fork()/wait() and be done with it.
No, just do it in serial. It's easier to read and debug. If you do care about performance and the tasks vastly dominate the time of spawning/joining, then fork/join. It's slightly harder to read and much more difficult to debug, but an order of magnitude faster for a large number of problems.
It should also be noted that promises/futures are close cousins of this model, and their implementation is significantly more complex than pthread_create/pthread_join (but APIs are much nicer, imo, since they do the imperative/cooperative transformation for you).
Don't hate it until you try it!
#pragma openmp parallel for
for(...) { ... } // Parallel portion
// No longer parallel. Implicit join!
foobar(); // execute sequentially
#pragma openmp parallel for
for(...) { ... } // Parallel portion again
It really covers a lot of parallel situations. Its often better than no parallelism at all (especially with 4-core/8-threads on even the cheapest CPUs today), and its really, really, really easy.---------
Also consider how CUDA / OpenCL kernels execute.
someCUDAKernel<<<ten-thousand CUDA-threads>>>(params);
cudaMemcpy(...); // transfers data from GPU back to CPU.
foobar(data); // Fully sequential portion when parallelism is non-obvious
cudaMemcpy(...); // transfer data from CPU back to GPU
someCUDAKernel2<<<ten-thousand CUDA-threads>>>(params); // Parallelism when its easy
Which is basically how most Tensorflow operations work today. (Bonus points: the "foobar" sequential portion is written in Python, something like 1000x slower than C/C++, but you don't care because 99.999% of the work is done inside of those CUDA-kernels)This "stupid" fork-join parallelism is really the core concept of modern heterogenous processors. A really easy way to increase utilization of both CPU-and-GPU resources.
1. Your problem is not big enough to justify it (CPU cores aren't idle because people are evil, there just isnt enough work in the first place)
2. Companies don't spend money on performance optimizations unless they have to (parallelism is just an optimization for your boss)
3. Not every problem can be divided easily, requiring contortions that are only understood by people who specialize in parallel programming