Link Copied
This sounds like your parallel sections are entered from within a parallel section.
Since you know you have 2seperate main processes, specify the parallel sections as using 2 threads.
For each process you will want to experiment with the number of threads to use in order to get best performance. If the two main processes take the same time to compute then selecting the number of threads to use as 1/2 the number of available cores might yield less context switching (assuming nothing else running on system). If other things are running on the system or if the two main processes take different amount of computation time then limit each thread to some number from 1/2 to number of cores.
Unless your code is performing I/O it makes little sense to use more threads than you have cores.
Jim Dempsey
Mambru37,
If you do not specify the number of threads for the parallel sections then the parallel sections begins with the default number of available threads. Each section within the parallel sections in turn will run on one of the threads, in your case one thread from the set of threads for the sections will run for each of two sections. Any additional threads will wait at the end parallel sections (assuming you do not use NOWAIT).
Pseudo code for you to consider
...
int NumberOfPackages = YourGetNumberOfPackages();
void* PackageMask[] = {NULL, NULL};
bool FirstTime[] = {TRUE, TRUE};
void DoWork0(void)
{
if(FirstTime[0])
{
#omp parallel
{
YourSetAffinity(PackageMask[0]);
}
FirstTime[0] = FALSE;
}
...
}
// DoWork1 similar to above
main(...)
{
...
PackageMask[0] = YourGetAffinityMaskForPackage(0);
if(NumberOfPackages > 1)
{
PackageMask[1] = YourGetAffinityMaskForPackage(1);
} else {
PackageMask[1] = PackageMask[0];
}
#pragma omp parallel num_threads(2)
{
int ThreadNum = omp_get_thread_num();
while(ProcessMainLoop)
{
if(FirstTime[ThreadNum])
{
SetAffinity(PackageMask[ThreadNum]);
}
DoMainIterationPreamble(ThreadNum);// e.g. read data
#pragma omp barrier
if(ThreadNum == 0)
{
DoWork0();
} else {
DoWork1();
}
#pragma omp barrier
}
}
}
Jim Dempsey
For more complete information about compiler optimizations, see our Optimization Notice.