Search code examples
multithreadingalgorithmboost-thread

Dividing work among threads


I know there are similar questions, but none of them answered my question.

I'm trying to divide a big loop(2^60) into several smaller loops distributed among threads. The range of loop can vary from (2^4) to (2^60). My question is how do I decide the optimal number(max=8, min=1) of threads and how to divide the work among them. If someone can show with an example that would be great.

For example.. I was trying to divide 2^32 among 8 threads. So every thread will do 2^29 amount of work, right? but how do I divide the range? (0... 2^29),(2^29....2^30)... ? Sorry if that's poor mathematics but my minds not working properly anymore.


Solution

  • Easy thing to do is just divide the number with number of threads and use it. Let m=floor(n/k). 0..(m-1), m..(2m-1).... are the starts and ends of loops.

    Not sure why you are obsessing 2^as. Is there any specific reason? Otherwise do as you would do for any other range. Divide equally.