From: hackbard Date: Tue, 4 Nov 2008 18:29:47 +0000 (+0100) Subject: PTHREADS printf, made neighbour index thread safe, check of mutex X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4416acfc0eb87fa9d6a5e3671e22181591836fe9;p=physik%2Fposic.git PTHREADS printf, made neighbour index thread safe, check of mutex (un)lock return values --- diff --git a/moldyn.c b/moldyn.c index bcc78c0..13d7e5d 100644 --- a/moldyn.c +++ b/moldyn.c @@ -1740,6 +1740,11 @@ int moldyn_integrate(t_moldyn *moldyn) { } #endif +#ifdef PTHREADS + printf("##################\n"); + printf("# USING PTHREADS #\n"); + printf("##################\n"); +#endif /* tell the world */ printf("[moldyn] integration start, go get a coffee ...\n"); diff --git a/potentials/albe_fast.c b/potentials/albe_fast.c index 9fc362e..79162b1 100644 --- a/potentials/albe_fast.c +++ b/potentials/albe_fast.c @@ -804,14 +804,13 @@ void *potential_force_thread(void *ptr) { if(!(itom[i].attr&ATOM_ATTR_3BP)) return 0; - link_cell_neighbour_index(moldyn, + // thread safe this way! + dnlc=link_cell_neighbour_index(moldyn, (itom[i].r.x+moldyn->dim.x/2)/lc->x, (itom[i].r.y+moldyn->dim.y/2)/lc->y, (itom[i].r.z+moldyn->dim.z/2)/lc->z, neighbour_i); - dnlc=lc->dnlc; - /* copy the neighbour lists */ #ifdef STATIC_LISTS #elif LOWMEM_LISTS @@ -839,7 +838,7 @@ void *potential_force_thread(void *ptr) { while(p!=-1) { jtom=&(itom[p]); - p=lc->subcell->list[p]; + p=lc->subcell->list[p]; // thread safe! #else this=&(neighbour_i[j]); list_reset_f(this); @@ -1098,21 +1097,27 @@ void *potential_force_thread(void *ptr) { scale=-0.5*(f_c*(df_r-b*df_a)+df_c*(f_r-b*f_a)); // - in albe formalism #endif v3_scale(&force,&(dist_ij),scale); - pthread_mutex_lock(&(amutex[ai->tag])); + if(pthread_mutex_lock(&(amutex[ai->tag]))) + perror("[albe fast] mutex lock (1)\n"); v3_add(&(ai->f),&(ai->f),&force); - pthread_mutex_unlock(&(amutex[ai->tag])); + if(pthread_mutex_unlock(&(amutex[ai->tag]))) + perror("[albe fast] mutex unlock (1)\n"); /* force contribution for atom j */ v3_scale(&force,&force,-1.0); // dri rij = - drj rij - pthread_mutex_lock(&(amutex[jtom->tag])); + if(pthread_mutex_lock(&(amutex[jtom->tag]))) + perror("[albe fast] mutex lock (2)\n"); v3_add(&(jtom->f),&(jtom->f),&force); - pthread_mutex_unlock(&(amutex[jtom->tag])); + if(pthread_mutex_unlock(&(amutex[jtom->tag]))) + perror("[albe fast] mutex unlock (2)\n"); /* virial */ - pthread_mutex_lock(&(amutex[ai->tag])); + if(pthread_mutex_lock(&(amutex[ai->tag]))) + perror("[albe fast] mutex lock (3)\n"); albe_v_calc(ai,&force,&(dist_ij)); //virial_calc(ai,&force,&(dist_ij)); - pthread_mutex_unlock(&(amutex[ai->tag])); + if(pthread_mutex_unlock(&(amutex[ai->tag]))) + perror("[albe fast] mutex unlock (3)\n"); #ifdef DEBUG if(moldyn->time>DSTART&&moldyn->timetime>DSTART&&moldyn->timeenergy+=energy; - pthread_mutex_unlock(&emutex); - pthread_mutex_lock(&(amutex[ai->tag])); + if(pthread_mutex_unlock(&emutex)) + perror("[albe fast] mutex unlock (energy)\n"); + if(pthread_mutex_lock(&(amutex[ai->tag]))) + perror("[albe fast] mutex lock (4)\n"); ai->e+=energy; - pthread_mutex_unlock(&(amutex[ai->tag])); + if(pthread_mutex_unlock(&(amutex[ai->tag]))) + perror("[albe fast] mutex unlock (4)\n"); /* reset k counter for second k loop */ kcount=0; @@ -1235,9 +1244,11 @@ if(moldyn->time>DSTART&&moldyn->timetag])); + if(pthread_mutex_lock(&(amutex[jtom->tag]))) + perror("[albe fast] mutex lock (5)\n"); v3_add(&(jtom->f),&(jtom->f),&force); - pthread_mutex_unlock(&(amutex[jtom->tag])); + if(pthread_mutex_unlock(&(amutex[jtom->tag]))) + perror("[albe fast] mutex unlock (5)\n"); #ifdef DEBUG if(moldyn->time>DSTART&&moldyn->timetime>DSTART&&moldyn->timetag])); + if(pthread_mutex_lock(&(amutex[ai->tag]))) + perror("[albe fast] mutex lock (6)\n"); albe_v_calc(ai,&force,&dist_ij); //virial_calc(ai,&force,&dist_ij); /* force contribution to atom i */ v3_scale(&force,&force,-1.0); v3_add(&(ai->f),&(ai->f),&force); - pthread_mutex_unlock(&(amutex[ai->tag])); + if(pthread_mutex_unlock(&(amutex[ai->tag]))) + perror("[albe fast] mutex unlock (6)\n"); /* derivative wrt k */ #ifdef MATTONI @@ -1273,9 +1286,11 @@ if(moldyn->time>DSTART&&moldyn->timetag])); + if(pthread_mutex_lock(&(amutex[ktom->tag]))) + perror("[albe fast] mutex lock (7)\n"); v3_add(&(ktom->f),&(ktom->f),&force); - pthread_mutex_unlock(&(amutex[ktom->tag])); + if(pthread_mutex_unlock(&(amutex[ktom->tag]))) + perror("[albe fast] mutex unlock (7)\n"); #ifdef DEBUG if(moldyn->time>DSTART&&moldyn->timetime>DSTART&&moldyn->timetag])); + if(pthread_mutex_lock(&(amutex[ai->tag]))) + perror("[albe fast] mutex lock (8)\n"); albe_v_calc(ai,&force,&dist_ik); //virial_calc(ai,&force,&dist_ik); /* force contribution to atom i */ v3_scale(&force,&force,-1.0); v3_add(&(ai->f),&(ai->f),&force); - pthread_mutex_unlock(&(amutex[ai->tag])); + if(pthread_mutex_unlock(&(amutex[ai->tag]))) + perror("[albe fast] mutex unlock (8)\n"); /* increase k counter */ kcount++;