Debian Codegraph @

src/dr/inference/parallel/MPILikelihoodRunner.java

raw symbols SCIP
1 /*
2 * MPILikelihoodRunner.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 dr.inference.loggers.Logger;
29 import dr.inference.model.AbstractModel;
30 import dr.inference.model.Likelihood;
31 import dr.util.Identifiable;
32 import dr.xml.*;
33 import mpi.MPI;
34
35 import java.util.ArrayList;
36
37 /**
38 * @author Marc A. Suchard
39 */
40
41 public class MPILikelihoodRunner implements Runnable, Identifiable {
42
43 public static final String PARALLEL_CALCULATOR = "parallelCalculator";
44
45
46 public MPILikelihoodRunner(String id, Likelihood likelihood) {
47 this.id = id;
48 this.likelihood = likelihood;
49 }
50
51 boolean terminate = false;
52
53 public void run() {
54
55 while (!terminate) {
56 // Receive msg
57 ServiceRequest request = MPIServices.getRequest(0);
58 // System.err.println("Process "+mpiRank+" received request "+request.getId());
59 switch (request) {
60 case calculateLikeliood:
61 calculateLikelihood();
62 break;
63 case terminateProcess:
64 terminateMe();
65 default:
66 break;
67 }
68 }
69
70 // finalize();
71 }
72
73
74 private void calculateLikelihood() {
75 // Receive current state
76 // System.err.println("did i get here?");
77 // MPI.Finalize();
78 // System.exit(-1);
79 AbstractModel model = (AbstractModel) likelihood.getModel();
80 // System.err.println("trying to receive model state");
81 model.receiveState(0);
82 likelihood.makeDirty();
83 model.fireModelChanged();
84 // System.err.println("state received");
85
86 // perform calculation
87 double logLikelihood = likelihood.getLogLikelihood();
88 // double logLikelihood = likelihood.
89 // System.err.println("likelihood = "+logLikelihood+" on process "+mpiRank);
90 MPIServices.sendDouble(logLikelihood, 0);
91 }
92
93 private void terminateMe() {
94 terminate = true;
95 //finalize();
96 }
97
98 public String getId() {
99 return id;
100 }
101
102 public void setId(String id) {
103 this.id = id;
104
105 }
106
107 /**
108 * Initialize connection to MPI world
109 */
110 public void init() {
111 // MPI.Init(null);
112 mpiRank = MPI.COMM_WORLD.Rank();
113 mpiSize = MPI.COMM_WORLD.Size();
114 }
115
116 public void finalize() {
117 MPI.Finalize();
118 }
119
120 /**
121 * XML Parser
122 */
123
124 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
125
126 public String getParserName() {
127 return PARALLEL_CALCULATOR;
128 }
129
130 /**
131 * @return a tree object based on the XML element it was passed.
132 */
133 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
134
135 Likelihood likelihood = (Likelihood) xo.getChild(Likelihood.class);
136
137 MPILikelihoodRunner runner = new MPILikelihoodRunner("slave", likelihood);
138 runner.init();
139 // MCMCOptions options = new MCMCOptions();
140 // OperatorSchedule opsched = (OperatorSchedule)xo.getChild(OperatorSchedule.class);
141 ArrayList<Object> loggers = new ArrayList<Object>();
142
143 // options.setChainLength(xo.getIntegerAttribute(CHAIN_LENGTH));
144
145
146 for (int i = 0; i < xo.getChildCount(); i++) {
147 Object child = xo.getChild(i);
148 if (child instanceof Logger) {
149 loggers.add(child);
150 }
151 }
152
153 Logger[] loggerArray = new Logger[loggers.size()];
154 loggers.toArray(loggerArray);
155
156 java.util.logging.Logger.getLogger("dr.inference").info("Creating the parallelCalculator chain:");
157 /*+
158 "\n chainLength=" + options.getChainLength() +
159 "\n autoOptimize=" + options.useAdaptation());*/
160
161 runner.init();
162
163 // MarkovChain mc = mcmc.getMarkovChain();
164 // double initialScore = mc.getCurrentScore();
165
166 // if (initialScore == Double.NEGATIVE_INFINITY) {
167 // throw new IllegalArgumentException("The initial model is invalid because it has zero likelihood!");
168 // }
169
170
171 return runner;
172 }
173
174 //************************************************************************
175 // AbstractXMLObjectParser implementation
176 //************************************************************************
177
178 public String getParserDescription() {
179 return "This element returns an MPI-based likelihood calculator.";
180 }
181
182 public Class getReturnType() {
183 return MPILikelihoodRunner.class;
184 }
185
186 public XMLSyntaxRule[] getSyntaxRules() {
187 return rules;
188 }
189
190 private final XMLSyntaxRule[] rules = {
191 // AttributeRule.newIntegerRule(CHAIN_LENGTH),
192 // AttributeRule.newBooleanRule(COERCION, true),
193 // AttributeRule.newIntegerRule(PRE_BURNIN, true),
194 // new ElementRule(OperatorSchedule.class ),
195 new ElementRule(Likelihood.class),
196 // new ElementRule(Logger.class, 1, Integer.MAX_VALUE )
197 };
198
199 };
200
201 // Private variables
202
203 private String id;
204 private int mpiRank;
205 private int mpiSize;
206 private final Likelihood likelihood;
207
208 }