Parallel code slower than serial one

General OpenMP discussion

Parallel code slower than serial one

Postby rbanos » Tue Apr 24, 2012 11:55 am

Dear all,
I write this thread because I am parallelizing a serial code with OpenMP but, I do not know why the parallel code is slower than the serial one (i.e.using omp_set_num_threads(1) is faster than omp_set_num_threads(1) in a multi-core processor. I attach a part of the code. Is there anything wrong?

Thanks in advance,
Raúl.

..........


omp_set_num_threads(2);

void funcion_objetivo(int poblacion, int indiv)
{
i_factible=1;
if(poblacion==1)
{
P1[indiv].f1_distancia_total=0;
#pragma omp parallel shared(P1)
{
#pragma omp for private(i_nodo_actual,i_r)
for(i_v=0;i_v<I_max_num_vehiculos;i_v++)
{
if(i_factible==1)
{
P1[indiv].f1_distancia_total+=P1[indiv].V[i_v].longitud_ruta;
for(i_r=1;i_r<=P1[indiv].V[i_v].total_nodos_visitados;i_r++)
{
i_nodo_actual=P1[indiv].V[i_v].R[i_r].nodo_visitado;
if((P1[indiv].V[i_v].R[i_r].hora_llegada>NODOS[i_nodo_actual].t_closed) || (P1[indiv].V[i_v].carga_ruta>I_max_capacidad_vehiculos))
{
i_factible=0;
P1[indiv].f1_distancia_total=INFINITO; //solucion infactible
P1[indiv].f2_desbalanceo_distancia=INFINITO;
P1[indiv].f3_desbalanceo_carga=INFINITO;
}
}
}
}
}
}
}
rbanos
 
Posts: 3
Joined: Mon Apr 23, 2012 5:36 am

Re: Parallel code slower than serial one

Postby ftinetti » Wed Apr 25, 2012 3:56 am

Hi,

I would like to be able to play a little bit with this code, I would like you to send a complete version, so that I can compile and run in my computers.

Thanks in advance,

Fernando.
ftinetti
 
Posts: 567
Joined: Wed Feb 10, 2010 2:44 pm

Re: Parallel code slower than serial one

Postby MarkB » Wed Apr 25, 2012 9:10 am

Hi Raul,

It looks like your code has synchronisation bugs (race conditions): there is the possibility of multiple threads all updating shared variables (i_factible, P1[indiv].f1_distancia_total, P1[indiv].f2_desbalanceo_distancia, P1[indiv].f3_desbalanceo_carga) at the same time. This may also be resulting in cache thrashing, which contributes to the poor performance. Another possibility is that there is not enough work to offset the overheads of parallelisation: how long does a typical call to funcion_objetivo take?
MarkB
 
Posts: 422
Joined: Thu Jan 08, 2009 10:12 am

Re: Parallel code slower than serial one

Postby rbanos » Wed Apr 25, 2012 11:41 am

Hello again,
the thing is that "function_objective" is called several hundred thousand times, and perhaps, these multiple fork-join calls could produce large overheas that would not compensate the parallel work.

I have a very large source code, and, while I have successfully parallelized it using MPI, I have this problem with OpenMP.

Do you recommend me to use omp parallel sections to parallelize a complex code, such that each section could include the same "sequential code" (the aim of this parallelization is to improve the quality of the solutions without increasing the runtime.

Many thanks in advance,
Raúl


Hi Raul,

It looks like your code has synchronisation bugs (race conditions): there is the possibility of multiple threads all updating shared variables (i_factible, P1[indiv].f1_distancia_total, P1[indiv].f2_desbalanceo_distancia, P1[indiv].f3_desbalanceo_carga) at the same time. This may also be resulting in cache thrashing, which contributes to the poor performance. Another possibility is that there is not enough work to offset the overheads of parallelisation: how long does a typical call to funcion_objetivo take?
rbanos
 
Posts: 3
Joined: Mon Apr 23, 2012 5:36 am

Re: Parallel code slower than serial one

Postby MarkB » Fri Apr 27, 2012 9:22 am

rbanos wrote:the thing is that "function_objective" is called several hundred thousand times, and perhaps, these multiple fork-join calls could produce large overheas that would not compensate the parallel work.

I have a very large source code, and, while I have successfully parallelized it using MPI, I have this problem with OpenMP.


Yes, it is possible there is not enough work to compensate for the parallel overhead. If each MPI process is calling function_objective lots of times, it may be better to call different instances of the function on each thread, instead of trying to parallelise within the function.

Is there some problem with your MPI code that you are trying to solve by adding OpenMP?

rbanos wrote:Do you recommend me to use omp parallel sections to parallelize a complex code, such that each section could include the same "sequential code" (the aim of this parallelization is to improve the quality of the solutions without increasing the runtime.


I'm sorry, I don't really understand what you are suggesting here!
MarkB
 
Posts: 422
Joined: Thu Jan 08, 2009 10:12 am


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot] and 10 guests

cron