BSPPeer<Text, VectorWritable, Text, VectorWritable, MapWritable> peer,
DoubleMatrix featureMatrix, IntWritable msgFeatureMatrix, boolean broadcast)
throws IOException, SyncException, InterruptedException {
// send to master peer
MapWritable msg = new MapWritable();
MatrixWritable mtx = new MatrixWritable(featureMatrix);
msg.put(msgFeatureMatrix, mtx);
String master = peer.getPeerName(peer.getNumPeers()/2);
peer.send(master, msg);
peer.sync();
// normalize
DoubleMatrix res = null;
if (peer.getPeerName().equals(master)) {
res = new DenseDoubleMatrix(featureMatrix.getRowCount(),
featureMatrix.getColumnCount(), 0);
int incomingMsgCount = 0;
while ( (msg = peer.getCurrentMessage()) != null) {
MatrixWritable tmp = (MatrixWritable) msg.get(msgFeatureMatrix);
res.add(tmp.getMatrix());
incomingMsgCount++;
}
res.divide(incomingMsgCount);
}
if (broadcast) {
if (peer.getPeerName().equals(master)) {
// broadcast to all
msg = new MapWritable();
msg.put(msgFeatureMatrix, new MatrixWritable(res));
// send to all
for (String peerName : peer.getAllPeerNames()) {
peer.send(peerName, msg);
}
}