Spreading CPU intensive function over multiple cores
-
I have a CPU intensive function that I have no control over. When it runs, it runs only on one core. I do have it on a separate thread from the main thread. But since I have no control over the function, I can't spread it over more threads to force it to use more cores. Any suggestions on how to spread it across more cores?
-
you can slice it by input parameters probably (eq. you can sort one part of array in one thread, second in another and than just merge results). You can introduce dynamic programming to make it more cache friendly.
but you have to tell more details about this function, what parameter do it take and return. What algorithm does it execute etc.
-
By no control I assume you mean that it is a black box which you can only control the inputs and record the outputs. If this is so and if you 1) have the inputs in advance and 2) the values calculated by the function are independent of each other, you could create a N (equal to the number of threads) arrays of the input data, of approximately the same size and run these in separate threads. The data would be written to N different output arrays. If you use a single input and output array you would have to lock and unlock them using mutexes.