简体   繁体   中英

C++ design of a multi-threaded sort? Futures vs Threadpool vs others?

I'm looking to parallelize bucket sort as an exercise. I am sorting integers lexiographically.

The motivation is, this is my first time doing any form of parallel programming. I'm trying to learn about the different ways of doing it, advantages/disadvantages.

Eg {1,6,2,6778,8,2,43,52,23} -> [1 2 2 23 43 52 6 6778 8]

There are 3 steps to the task:

Initialize 9 vectors, 1 for each final bucket.

1) sort a chunk into buckets. This step parallelized by giving each thread a portion of the data.

2) sort each bucket into lexicographic order

3) concat all buckets

Visualization of the sort:

Option 1: Threadpool I'm considering either dividing up all those tasks into jobs for 2 different functions, a bucketize function and a sort_bucket function then feeding them into a thread pool.

Option 2: Futures Alternatively create futures of the functions and wait at the end of each step. Wait for all futures to return at the end of step 1, then create futures of sort_bucket in step 2 and join them. Can anyone provide any opinions on these methods?

CPU utilization: I can be sure that in the threadpool version I am using the appropirate number of threads as regards to available processors. In futures, they would be scheduled appropriately by the OS?

Are there other ways I've missed out on? I'm trying to learn so would like to compare all the possible methods of doing this.

Thanks!

You could sort subsequences of the initial array (in parallel, so in different threads) then merge them.

BTW the overhead is not negligible. You probably need to get an initial array of many dozen of thousands to observe a gain in parallelisation, and you are likely to sometimes observe some loss (eg with a too small initial array).

And for a first multi-threaded project, I'll rather suggest having a (nearly) fixed small set of threads (at most a dozen of them, assuming your computer has 8 cores). So both thread pools and futures are IMHO too complex for that.

Threads are heavy and expensive. They need at least a call stack (of a megabyte) each, and actually much more.

Don't forget synchronization (eg with mutexes).

This is a Pthread tutorial that you could adapt to C++ threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM