Debian Codegraph @

src/dr/inference/parallel/MPIServices.java

raw symbols SCIP
1 /*
2 * MPIServices.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.inference.parallel;
27
28 import mpi.MPI;
29 import mpi.Status;
30
31 /**
32 * @author Marc A. Suchard
33 */
34
35 public class MPIServices {
36
37 public static void requestTermination(int slave) {
38 int[] msg = new int[]{ServiceRequest.terminateProcess.getId()};
39 MPI.COMM_WORLD.Send(msg,
40 0, 1, MPI.INT, slave, ServiceRequest.MSG_REQUEST_TYPE);
41 }
42
43 public static void requestLikelihood(int slave) {
44 int[] msg = new int[]{ServiceRequest.calculateLikeliood.getId()};
45 MPI.COMM_WORLD.Send(msg, 0, 1, MPI.INT, slave, ServiceRequest.MSG_REQUEST_TYPE);
46 }
47
48 public static ServiceRequest getRequest(int master) {
49 int[] msg = new int[1];
50 Status status = MPI.COMM_WORLD.Recv(msg, 0, 1, MPI.INT,
51 master, ServiceRequest.MSG_REQUEST_TYPE);
52 // todo check status and throw exception if error
53 return ServiceRequest.getByID(msg[0]);
54 }
55
56 public static void sendDouble(double value, int dest) {
57 double[] msg = new double[]{value};
58 // System.err.println("Sending double "+value+" to process "+dest);
59 MPI.COMM_WORLD.Send(msg, 0, 1, MPI.DOUBLE, dest, ServiceRequest.MSG_REQUEST_TYPE);
60 }
61
62 public static void sendDoubleArray(double[] values, int dest) {
63 MPI.COMM_WORLD.Send(values, 0, values.length, MPI.DOUBLE,
64 dest, ServiceRequest.MSG_REQUEST_TYPE);
65
66 }
67
68 public static void sendDoubleArray(Double[] value, int dest) {
69 double[] v = new double[value.length];
70 for (int i = 0; i < v.length; i++) {
71 v[i] = value[i];
72 }
73 sendDoubleArray(v, dest);
74 }
75
76 public static void sendIntArray(int[] values, int dest) {
77 MPI.COMM_WORLD.Send(values, 0, values.length, MPI.INT,
78 dest, ServiceRequest.MSG_REQUEST_TYPE);
79
80 }
81
82 public static void sendInt(int value, int dest) {
83 int[] msg = new int[]{value};
84 MPI.COMM_WORLD.Send(msg, 0, 1, MPI.INT, dest, ServiceRequest.MSG_REQUEST_TYPE);
85 }
86
87 public static int receiveInt(int source) {
88 int[] msg = new int[1];
89 Status status = MPI.COMM_WORLD.Recv(msg, 0, 1, MPI.INT,
90 source, ServiceRequest.MSG_REQUEST_TYPE);
91 return msg[0];
92 }
93
94 public static double receiveDouble(int source) {
95 double[] msg = new double[1];
96 Status status = MPI.COMM_WORLD.Recv(msg, 0, 1, MPI.DOUBLE,
97 source, ServiceRequest.MSG_REQUEST_TYPE);
98 return msg[0];
99 }
100
101 public static double[] receiveDoubleArray(int source, int length) {
102 double[] msg = new double[length];
103 Status status = MPI.COMM_WORLD.Recv(msg, 0, length, MPI.DOUBLE,
104 source, ServiceRequest.MSG_REQUEST_TYPE);
105 return msg;
106 }
107
108 public static int[] receiveIntArray(int source, int length) {
109 int[] msg = new int[length];
110 Status status = MPI.COMM_WORLD.Recv(msg, 0, length, MPI.INT,
111 source, ServiceRequest.MSG_REQUEST_TYPE);
112 return msg;
113 }
114
115 /*public static double receiveSumOfDoubleReduction(int master) {
116
117 }*/
118
119 }