Leonid wrote:
Please help me with calculation parallelization. I have 2
processors DELL PRECISION 530 computer and I'd like
parallelize cycles like
for(int i = 0; i < last; ++i)
{
a[i] = b[i] + c[i];
}
Just make two worker threads which would work on one half of your
data. One (and only) parameter of a worker thread should be pointer to
a struct embodying your input and output parameters, like:
class ThreadParams
{
public:
ThreadParams(int* o, int* i1, int* i2, size_t l) : output(o),
input1(i1), input2(i2), length(l) {}
int* output;
int* input1;
int* input2;
size_t length;
}
Worker thread function should look like this:
unsigned int __stdcall ThreadProc(void* pParam)
{
ThreadParams* pParams = reinterpret_cast<ThreadParams*>(pParam);
size_t i;
for (i = 0; i < pParams->length; ++i)
pParams->output[i] = pParams->input1[i] + pParams->input2[i];
return 0;
}
You create non-MFC threads with _beginthreadex and MFC threads with
AfxBeginThread. ThreadParams class should be created on heap like this:
ThreadParams* pTp = new ThreadParams(a+last/2, b+last/2, c+last/2,
last/2);
Also consider the situation where <last> is odd number.