Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Création du dépôt AIL-PA
[ail-pa.git] / src / and / AIL / Convergence.java
1 package and.AIL ;
2
3 import java.io.Serializable;
4 import org.objectweb.proactive.api.PAGroup;
5 import org.objectweb.proactive.core.group.Group;
6
7 /**
8  * Class which manages the convergence of a problem.
9  * @author Sébastien Miquée
10  * @version 3.0
11  */
12 public class Convergence implements Serializable
13 {
14         /** Local variables **/
15         private static final long serialVersionUID = 1L ;
16         private boolean total[] ;
17         private short nbconv[] ;
18         private short taille ;
19         private boolean conv ;
20         private Group<AILObject> as ;
21         private boolean fin ;
22         private short attente ;
23         private short maxIterBeforeConv ;
24         private short __tag = 999 ;
25         
26         /**
27          * Empty constructor for the stub generation.
28          */
29         public Convergence()
30         {
31         }
32         
33         /**
34          * Constructor.
35          * @param nb : number of worker.
36          */
37         public Convergence( short nb )
38         {
39                 taille = nb ;
40                 total = new boolean[ taille ] ;
41                 nbconv = new short[ taille ] ;          
42                 conv = false ;
43                 fin = false ;
44                 maxIterBeforeConv = 0 ;
45                 attente = taille ;
46                 as = null ;
47                 
48                 for( short i = 0 ; i < taille ; i++ )
49                 {
50                         nbconv[ i ] = 0 ;
51                 }
52         }
53         
54         /**
55          * Set the ALPA group, in order to communicate with workers.
56          * @param group : AALPA group.
57          */
58         public void majGroup( AILObject group )
59         {
60                 as = PAGroup.getGroup( group ) ;
61         }
62         
63         /**
64          * Method to detect convergence in asynchronous mode.
65          * @param id : rank of the worker.
66          * @param b : convergence detected by the worker.
67          */
68         public synchronized void convergeAsync( short id, boolean b )
69         {       
70                 if( ! conv )
71                 {
72                         if( b )
73                         {
74                                 if( maxIterBeforeConv != 0 )
75                                         nbconv[ id ]++ ;
76                         } else {
77                                 nbconv[ id ] = 0 ;
78                         }
79                         
80                         if( nbconv[ id ] == maxIterBeforeConv )
81                         {
82                                 total[ id ] = b ;
83                                 nbconv[ id ] = 0 ;
84                         }
85
86                         boolean tmp = true ;
87
88                         for( short i = 0 ; i < taille ; i++ )
89                         {
90                                 tmp = tmp && total[i] ;
91                         }
92
93                         conv = tmp ;            
94                 }
95                 
96                 if( as != null )
97                 {
98                         as.get( id ).recvObject( taille, conv, __tag ) ;
99                 }
100
101         } 
102         
103         /**
104          Method to detect convergence in synchronous mode.
105          * @param id : rank of the worker.
106          * @param b : convergence detected by the worker.
107          */
108         public synchronized void convergeSync( short id, boolean b )
109         {       
110                 if( ! conv )
111                 {
112                         total[ id ] = b ;
113                         
114                         attente-- ;
115                         
116                         if( attente == 0 )
117                         {
118                                 boolean tmp = true ;
119                                 
120                                 for( short i = 0 ; i < taille ; i++ )
121                                 {
122                                         tmp = tmp && total[i] ;
123                                 }
124                         
125                                 conv = tmp ;
126                                 
127                                 for( short i = 0 ; i < taille ; i++ )
128                                 {
129                                         as.get( i ).recvObject( taille, conv, __tag ) ;
130                                 }
131                                 
132                                 attente = taille ;
133                         }
134                 
135                 } else {
136                         as.get( id ).recvObject( taille, conv, __tag ) ;
137                 }
138         } 
139         
140         /**
141          * Initialization of the convergence.
142          */
143         public void init()
144         {
145                 total = new boolean[ taille ] ;
146                 nbconv = new short[ taille ] ;
147                 conv = false ;
148                 attente = taille ;
149                 
150                 for( short i = 0 ; i < taille ; i++ )
151                 {
152                         nbconv[ i ] = 0 ;
153                 }
154         }
155         
156         /**
157          * Sets the number of time the convergence should be detected by a worker.
158          * (it is in the way to avoid the yo-yo effect)
159          * It is used only in asynchronous mode. 
160          * @param _nb
161          */
162         public void setIterBeforeConv( short _nb )
163         {
164                 if( _nb >= 0 )
165                 {
166                         maxIterBeforeConv = _nb ;
167                 } else {
168                         maxIterBeforeConv = 0 ;
169                 }
170         }
171         
172         /**
173          * Indicates that all workers have stopped.
174          */
175         public void termine()
176         {
177                 fin = true ;
178         }       
179         
180         /**
181          * Return the status of all workers, to the Main program.
182          * @return : the status of all workers.
183          */
184         public boolean fin()
185         {
186                 return fin ;
187         }       
188 }
189
190 /** La programmation est un art, respectons ceux qui la pratiquent !! **/