# Performance of Parallel K-Means Algorithms in Java
## 3 Parallel K-Means in Java
### Actor-Based K-Means Using Theatre
#### 3.2.2 Mapping Parallel K-Means on Theatre
The master actor receives a \(done()\) message from a worker when the latter finishes the operations (Assign and Update sub-phases) of the current step (see Figure 7). The \(done()\) message carries as an argument the _partial_ new centroids calculated according to the _viewpoint_ of the worker. When all the \(done()\) messages of the current step are received, the master combines the partial centroids provided by the workers, and determines the resultant \(new\) centroids. A helper method of the _Master_ actor checks the termination condition which occurs (convergence) when, for each centroid, the Euclidean distance between the new centroid and the current one is found to be less than or equal to the threshold \(THR\) (see Table 1) or the maximum number of iterations \(T\) was reached. It is important to note that each partial centroid point returned by a worker contains the cumulative \(D\) features (coordinates) of the local points handled by the worker, together  
Figure 6: Master/worker organization of Theatre-based K-means. with the number of points belonging to the same cluster. This way, the master actor can correctly assemble all the received partial centroids. The master actor increments the iteration counter \(it\) at the beginning of each iteration. Each iteration is started by the master which broadcasts a \(step()\) message to each worker and to itself. The \(step()\) message carries as a parameter, the current values of the centroids. At the end of K-Means, the master actor copies the emerged centroids upon the \(centroids\) variable of the \(G\) class so that it can be eventually output by the main program. Each centroid point carries the number of dataset points which compose the corresponding cluster. In addition, each dataset point holds the final cluster id (\(CID\)) of the belonging cluster. Figure 7: Configuration program for parallel K-Means based on Theatre. ```
publicclassWorkerextendsManager{ privateDataPoint[]region,partial; privateintID; privateMasterm//acquaintance @Msgsrv publicvoidinit(IntegerID,DataPoint[]region,Masterm){ this.ID=ID; this.region=region;//asub-blockofthewholedataset this.m=m; partial=newDataPoint[K];//usedtobuildpartialnewcentroids for(inti=0,i<K;++i) partial[i]=newDataPoint(); ]//init @Msgsrv publicvoidstep(Integerid,DataPoint[]centroids){ //Mapstage:allocatelocaldatapointstoclusters for(inti=0,i<region.length;++i){ doubleminD=Double.MAX_VALUE,d=0; for(intj=0,j<K;++j){ d=region[i].distance(centroids[j]); if(d<minD){minD=d;region[i].setCID(j); } } } //Reducestage:updatespartialcentroids for(inti=0,i<K;++i)partial[i].reset(); for(inti=0,i<region.length;++i){ intcid=region[i].getCID(); partial[cid].add(region[i]); } m.send(\"done\",ID,partial); }//step //Worker
```  
**Figure 8**: The Worker actor class. The actor-based parallel K-Means can be easily adapted to work with one single theatre/core, thus achieving the _standalone_ K-Means program, useful for performance comparisons. Except for the synchronization messages exchanged at each step of the algorithm (broadcast of \(step()\) messages by the master to workers, followed by the reply \(done()\) messages sent by workers to the master), the parallel and the standalone K-Means programs execute _exactly_ the same number of operations.