I have a situation in which I need to do some heavy computation. I found out that subdividing my data and then merging it back together is the fastest (as the size increases, time increases faster, so splitting is logical).
It should be able to give a data size to the application, let's say for example one million double values.
What I have in place now, is sending created data based on this size off to some function, returning it after computation, and then looping over the return to unload this data into the main vector.
I want to send parts of 200, with one "last" part. For example, giving size = 1000005 will perform this function 5000 times initially, and then the last one with data of size 5.
int size = 1000000;
int times = size / 200; // 5000
int leftover = size % 200; // 0, this not performed
QVector<double> x(size);
QVector<double> y(size);
x = createData(size);
x = createData(size);
for (int i = 0; i < times; i++)
{
holder = createData(200);
QVector<double> tempx = x.mid(i*200, 200);
QVector<double> tempy = y.mid(i*200, 200);
holder = myfunction(tempx, tempy, 200); // let it now just return `tempy`
for (int j = 0; j < 200; j++)
{
y[i*200 + j] = holder[j];
}
}
// leftover function here, really similar to this part before.
// plotting function here
At the end, x
will remain as initialized, y
will have had the computation upon.
Since these code parts can run apart from each other and speed is crucial, I would like to make use of several cores.
The following further characterizes the situation:
- These function calls are independent of each other, only in the end when the vectors are complete do I want to plot the result.
- The time of completion for each call will be varying a lot.
- The amount of
times
should be variable.
I read something about that maximum threads are advised to be the amount of cores (at least as a starting point), since using too many threads could slow the process down. Considering the situation a queueing system / threadpool would seem to make sense as not to lose time while one thread has some easy jobs and the others are slowing everything down by harder jobs.
While it seems easy to print some messages using some (usually 2) threads in some dozen of tutorials, could anyone provide more detailed help on how to return vectors and unload these thread safely into a main function, and how to create a threadpool so time would not be wasted?
Using Ubuntu 13.04, Qt and C++11x, though it should not matter.