Debian Codegraph @

src/dr/inference/model/ParallelCompoundLikelihood.java

raw symbols SCIP
1 /*
2 * ParallelCompoundLikelihood.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.model;
27
28 import dr.inference.parallel.MPIServices;
29 import dr.xml.*;
30
31 import java.util.*;
32
33 /**
34 * @author Marc A. Suchard
35 */
36
37 public class ParallelCompoundLikelihood extends CompoundLikelihood {
38
39 public static final String PARALLEL_COMPOUND_LIKELIHOOD = "parallelCompoundLikelihood";
40 public static final String LOCAL_CHECK = "doLocalCheck";
41 public static final String RUN_PARALLEL = "doInParallel";
42
43 public ParallelCompoundLikelihood(Collection<Likelihood> likelihoods, boolean doParallel, boolean checkLocal) {
44 super(1, likelihoods);
45 this.doParallel = doParallel;
46 this.checkLocal = checkLocal;
47 }
48
49 private boolean doParallel = true;
50 private boolean checkLocal = false;
51
52 public double getLogLikelihood() {
53
54 double logLikelihood = 0;
55 if (doParallel) {
56
57 logLikelihood = getLogLikelihoodRemote();
58 if (checkLocal) {
59 super.makeDirty();
60 double logLikelihoodLocal = super.getLogLikelihood();
61 System.err.printf("Local: %5.4f Remote: %5.4f\n", logLikelihoodLocal, logLikelihood);
62 }
63
64 } else
65 logLikelihood = super.getLogLikelihood();
66
67 return logLikelihood;
68 }
69
70 private double getLogLikelihoodRemote() {
71 double logLikelihood = 0.0;
72
73 final int N = getLikelihoodCount();
74
75 List<ParallelLikelihood> likelihoodsDistributed = new ArrayList<ParallelLikelihood>();
76 List<Integer> processorList = new ArrayList<Integer>();
77
78 for (int i = 0; i < N; i++) {
79 ParallelLikelihood likelihood = (ParallelLikelihood) getLikelihood(i);
80 if (!likelihood.getLikelihoodKnown()) {
81 // if (true) {
82 final int processor = i + 1;
83 // MPIServices.requestLikelihood(processor);
84 // ((AbstractModel) getLikelihood(i).getModel()).sendState(processor);
85 likelihoodsDistributed.add(likelihood);
86 processorList.add(processor);
87 } else {
88 logLikelihood += likelihood.getLogLikelihood();
89 }
90 }
91
92 final int size = likelihoodsDistributed.size();
93
94 if (size == 1) { // only one, so do locally
95
96 logLikelihood += likelihoodsDistributed.get(0).getLogLikelihood();
97
98 } else if (size > 1) {
99
100 // Distribute calculations
101 int index = 0;
102 for (ParallelLikelihood likelihood : likelihoodsDistributed) {
103 int processor = processorList.get(index++);
104 MPIServices.requestLikelihood(processor);
105 ((AbstractModel) likelihood.getModel()).sendState(processor);
106 }
107
108 // Implicit barrier
109
110 // Collect calculations
111 index = 0;
112 for (ParallelLikelihood likelihood : likelihoodsDistributed) {
113 int processor = processorList.get(index++);
114 double l = MPIServices.receiveDouble(processor);
115 logLikelihood += l;
116 likelihood.setLikelihood(l); // todo don't we need to set all of the submodels ????
117 }
118
119 // todo Use Gather instead of blocking loop
120
121 }
122
123 return logLikelihood;
124 }
125
126
127 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
128
129 public String getParserName() {
130 return PARALLEL_COMPOUND_LIKELIHOOD;
131 }
132
133 // public String[] getParserNames() { return new String[] { getParserName(), "posterior", "prior", "likelihood" }; }
134
135 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
136
137 boolean doParallel = true;
138 boolean checkLocal = false;
139
140 if (xo.hasAttribute(LOCAL_CHECK)) {
141 checkLocal = xo.getBooleanAttribute(LOCAL_CHECK);
142 }
143
144 if (xo.hasAttribute(RUN_PARALLEL)) {
145 doParallel = xo.getBooleanAttribute(RUN_PARALLEL);
146 }
147 List<Likelihood> likelihoods = new ArrayList<Likelihood>();
148 for (int i = 0; i < xo.getChildCount(); i++) {
149 if (xo.getChild(i) instanceof Likelihood) {
150 likelihoods.add((Likelihood) xo.getChild(i));
151 } else {
152 throw new XMLParseException("An element which is not a likelihood has been added to a " + PARALLEL_COMPOUND_LIKELIHOOD + " element");
153 }
154 }
155
156 ParallelCompoundLikelihood compoundLikelihood = new ParallelCompoundLikelihood(likelihoods, doParallel, checkLocal);
157
158 return compoundLikelihood;
159 }
160
161 //************************************************************************
162 // AbstractXMLObjectParser implementation
163 //************************************************************************
164
165 public String getParserDescription() {
166 return "A likelihood function which is simply the product of its component likelihood functions.";
167 }
168
169 public XMLSyntaxRule[] getSyntaxRules() {
170 return rules;
171 }
172
173 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
174 new ElementRule(Likelihood.class, 1, Integer.MAX_VALUE),
175 };
176
177 public Class getReturnType() {
178 return ParallelCompoundLikelihood.class;
179 }
180 };
181 }