# 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 simplified UML diagram of Figure 5 shows some developed Java classes for supporting Parallel (but also serial) K-Means using Theatre. Basic classes of the Theatre infrastructure include _Actor_, _Theatre_, _PConcurrent_ and _PCTimeServer_ (see [13] for more details). A programmer-defined actor class must inherit, directly or indirectly, from the _Actor_ abstract base class, which exposes the basic services of (unblocking) message _send_, and the _move_ operation. The _send_ operation rests on Java _reflection_ for associating a message name (string) to a message server (method), and for passing arguments at the message delivery time, regulated by the control-layer. Theatres of a system are identified by unique identifiers from 0 to \(P-1\). \[\textit{void send}\ (\textit{String message\_name},\textit{ Object\ldots args});\]  
\[\textit{void move}\ (\textit{int theatre\_id});\]  
A sent message gets (transparently) scheduled on the underlying control-layer. The _move_ operation transfers an actor to a given (target) theatre, so that its relevant messages are ultimately handled by the target theatre control-layer. _PConcurrent_ and _PCTimeServer_ represent respectively the concurrent control-layer and the time server used in a parallel untimed application. Timed versions of these classes are described in [13]. _PConcurrent_ ensures messages are delivered in the sending order. By convention, the time server is associated with the theatre 0 which often plays the role of the master theatre. Figure 5: Supporting classes for Parallel K-Means based on Theatre. The actual actor programming style can be checked in Figure 7. An actor admits a local hidden data status which also specifies the _acquaintances_, that is the Java references to known actors (including itself for proactivity) to which this actor can send messages. Local data can only be modified by processing messages. The message interface is specified by the exposed _message server_ methods, provided by the annotation _@Msgsrv_, whose names _are_ the message names. Message servers are scheduled by the _send_ operation and finally dispatched according to the discipline enforced by the control-layer. An actor can also introduce normal helper methods (without the annotation _@Msgsrv_), to facilitate its code structuring. As indicated in Figure 5, and also shown by the message exchanges abstracted in Figure 6, the Theatre based K-Means adopts a _master/worker_ organization (see later in this section for further details). The dataset is equally split in \(P\) regions (subblocks) (\(P\) is the number of used theatres/cores), each region being (almost) of size \(N/P\). The \(main()\) method of the _K__Means_ class (see Figure 7) instantiates \(P\) theatres, each one equipped with an instance of \(PConcurrent\) and one of \(PCTimeServer\). The \(run()\) method of the master theatre is redefined for completing the parallel configuration. \(P\) Manager actors are created, one of the type _Master_ and \(P-1\) of the _Worker_ type. Such actors are moved to separate theatres. For brevity, Figure 8 shows only the _Worker_ actor class. Each manager understands the basic \(step()\) message (see the \(step(\dots)\) message server in Figure 8), introduced by the base abstract _Manager_ class. As a rule, every programmer-defined actor class is initialized by an explicit \(init(\dots)\) message (see also Figure 6), whose parameter list is tuned to the specific task/role assigned to the actor. The \(init(\dots)\) of _Master_ receives the master ID, the assigned subblock of the dataset, the initial values of the centroids and the array of all the manager references (acquaintances). The \(init(\dots)\) of _Worker_, instead, receives (see Figure 8) its unique ID, the assigned subblock of the dataset, and the reference to the _Master_ manager. Configuration ends by the master theatre which activates all the theatres and finally launches, on itself, the execution of the control-layer by invoking the \(controller()\) method.