Previous | Next --- Slide 22 of 116
Back to Lecture Thumbnails
csmith23

how would a compiled language express that this for loop should be parallelized without specifying the number of threads to use?

gmudel

I think there would be several ways to do this in C++. This documentation says that C++17 allows us to set an "execution policy," and that's how I would do it.

LeGoat

@cmsmith23 we could also use a C/C++ library like OpenMP, which can make multithreading for loops over data really simple. See the code example here: https://en.wikipedia.org/wiki/OpenMP#Work-sharing_constructs

rubensl

As mentioned above, openMP is a popular framework that can be used to write parallel code including parallel loops similar to what is shown on the slides (different syntax of course but same idea). In the case where the user just invokes one of the multithreading primitives without specifying any parameters, OpenMP uses default values to generate the parallel threaded code for you I believe. I am assuming the Professor's example fictitious language would be using a similar mechanism. In openMP by default, I believe the number of threads would be the total number of threads available on the system, it also chooses a default chunk size if not specified for how to divide the loop iterations among the available team of threads (by default, this is loop_count/number_of_threads), and the scheduling mechanism to use to amortize the work. This link provides some useful background info to expand more on this: https://stackoverflow.com/a/66955211.

laimagineCS149

Is there some compiler that would perform analysis on legacy code and automatically convert eligible loops to use these parallelism (e.g. converting loops to foreach loops automatically for the syntax of this example)? The amount of legacy code out there in the industry pretty much means very little of them would be re-written manually, and without such compiler automation, most code wouldn't be able to leverage modern processors at all.

Please log in to leave a comment.