pthredas working now (by partitioning atoms into count/#THREADS blocks)
[physik/posic.git] / potentials / albe_fast.c
index 952f1fe..603606e 100644 (file)
@@ -703,7 +703,7 @@ if(moldyn->time>DSTART&&moldyn->time<DEND) {
 
 typedef struct s_pft_data {
        t_moldyn *moldyn;
-       int i;
+       int start,end;
 } t_pft_data;
 
 void *potential_force_thread(void *ptr) {
@@ -785,14 +785,12 @@ void *potential_force_thread(void *ptr) {
        // optimized
        params=moldyn->pot_params;
 
+       /* get energy, force and virial for atoms */
 
-       /* get energy, force and virial of every atom */
-
-       /* first (and only) loop over atoms i */
-       for(i=0;i<count;i++) {
+       for(i=pft_data->start;i<pft_data->end;i++) {
 
                if(!(itom[i].attr&ATOM_ATTR_3BP))
-                       continue;
+                       return 0;
 
                link_cell_neighbour_index(moldyn,
                                          (itom[i].r.x+moldyn->dim.x/2)/lc->x,
@@ -1302,6 +1300,8 @@ if(moldyn->time>DSTART&&moldyn->time<DEND) {
 #endif
                
                }
+
+       } // i loop
                
 #ifdef DEBUG
        //printf("\n\n");
@@ -1310,8 +1310,6 @@ if(moldyn->time>DSTART&&moldyn->time<DEND) {
        printf("\n\n");
 #endif
 
-       }
-
 #ifdef DEBUG
        //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
        if(moldyn->time>DSTART&&moldyn->time<DEND) {
@@ -1365,49 +1363,37 @@ int albe_potential_force_calc(t_moldyn *moldyn) {
 
        }
 
-       i=0;
-       while(i<count) {
-
-               /* start threads */
-               for(j=0;j<MAX_THREADS;j++) {
-
-                       /* break if all atoms are processed */
-                       if(j+i==count)
-                               break;
-
-                       /* prepare thread data */
-                       pft_data[j].moldyn=moldyn;
-                       pft_data[j].i=j+i;
+       /* start threads */
+       for(j=0;j<MAX_THREADS;j++) {
 
-                       ret=pthread_create(&(pft_thread[j]),NULL,
-                                           potential_force_thread,
-                                           &(pft_data[j]));
-                       if(ret)  {
-                               perror("[albe fast] pf thread create");
-                               return ret;
-                       }
+               /* prepare thread data */
+               pft_data[j].moldyn=moldyn;
+               pft_data[j].start=j*count/MAX_THREADS;
+               if(j==MAX_THREADS-1) {
+                       pft_data[j].end=count;
                }
-
-               //printf("threads created! %d\n",j);
-
-               /* join threads */
-               for(j=0;j<MAX_THREADS;j++) {
-
-                       if(j+i==count)
-                               break;
-
-                       ret=pthread_join(pft_thread[j],NULL);
-                       if(ret) {
-                               perror("[albe fast] pf thread join");
-                               return ret;
-                       }
+               else {
+                       pft_data[j].end=pft_data[j].start;
+                       pft_data[j].end+=count/MAX_THREADS;
                }
 
-               /* increment counter */
-               i+=MAX_THREADS;
+               ret=pthread_create(&(pft_thread[j]),NULL,
+                                   potential_force_thread,
+                                   &(pft_data[j]));
+               if(ret)  {
+                       perror("[albe fast] pf thread create");
+                       return ret;
+               }
+       }
 
-               //printf("threads joined! -> %d\n",i);
+       /* join threads */
+       for(j=0;j<MAX_THREADS;j++) {
 
+               ret=pthread_join(pft_thread[j],NULL);
+               if(ret) {
+                       perror("[albe fast] pf thread join");
+                       return ret;
+               }
        }
 
        /* some postprocessing */
@@ -1427,8 +1413,6 @@ int albe_potential_force_calc(t_moldyn *moldyn) {
                               i);
        }
 
-       pthread_exit(NULL);
-
        return 0;
 }