|
| 1 | +package scratch.kevin; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayDeque; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.concurrent.CompletableFuture; |
| 8 | + |
| 9 | +import org.opensha.commons.data.Site; |
| 10 | +import org.opensha.commons.data.function.ArbitrarilyDiscretizedFunc; |
| 11 | +import org.opensha.commons.data.function.DiscretizedFunc; |
| 12 | +import org.opensha.commons.geo.GriddedRegion; |
| 13 | +import org.opensha.commons.geo.Location; |
| 14 | +import org.opensha.commons.param.Parameter; |
| 15 | +import org.opensha.sha.calc.HazardCurveCalculator; |
| 16 | +import org.opensha.sha.calc.sourceFilters.SourceFilterManager; |
| 17 | +import org.opensha.sha.calc.sourceFilters.SourceFilters; |
| 18 | +import org.opensha.sha.earthquake.rupForecastImpl.nshm23.erf.NSHM23_WUS_BranchAveragedERF; |
| 19 | +import org.opensha.sha.earthquake.rupForecastImpl.nshm23.util.NSHM23_RegionLoader; |
| 20 | +import org.opensha.sha.gui.infoTools.IMT_Info; |
| 21 | +import org.opensha.sha.imr.AttenRelRef; |
| 22 | +import org.opensha.sha.imr.ScalarIMR; |
| 23 | +import org.opensha.sha.imr.param.IntensityMeasureParams.PGA_Param; |
| 24 | + |
| 25 | +public class ThreadedHazardCurveCalcExample { |
| 26 | + |
| 27 | + public static void main(String[] args) throws IOException { |
| 28 | + // ERF |
| 29 | + NSHM23_WUS_BranchAveragedERF erf = new NSHM23_WUS_BranchAveragedERF(); |
| 30 | + erf.getTimeSpan().setDuration(1d); |
| 31 | + erf.updateForecast(); |
| 32 | + |
| 33 | + // GMM, can't share an instance across multiple threads so we keep the reference to build instances |
| 34 | + AttenRelRef gmmRef = AttenRelRef.ASK_2014; |
| 35 | + |
| 36 | + // sites, I'll use a gridded region to build the site list for this example |
| 37 | + GriddedRegion sitesGridded = new GriddedRegion(NSHM23_RegionLoader.loadFullConterminousWUS(), 1d, GriddedRegion.ANCHOR_0_0); |
| 38 | + // create the site list |
| 39 | + List<Site> sites = new ArrayList<>(); |
| 40 | + // need one GMM in order to get the site parameter list |
| 41 | + ScalarIMR gmm0 = gmmRef.get(); |
| 42 | + for (Location loc : sitesGridded.getNodeList()) { |
| 43 | + Site site = new Site(loc); |
| 44 | + for (Parameter<?> param : gmm0.getSiteParams()) { |
| 45 | + // need to clone the site parameter so that it's not shared across multiple sites (in case you want |
| 46 | + // different site data per site) |
| 47 | + site.addParameter((Parameter<?>)param.clone()); |
| 48 | + } |
| 49 | + sites.add(site); |
| 50 | + } |
| 51 | + |
| 52 | + // source filters to use for the calculator |
| 53 | + SourceFilterManager sourceFilters = new SourceFilterManager(SourceFilters.FIXED_DIST_CUTOFF); |
| 54 | + |
| 55 | + // this will cache and keep track of our GMM instances |
| 56 | + ArrayDeque<ScalarIMR> gmmDeque = new ArrayDeque<>(); |
| 57 | + // and our hazard curve calculator instances |
| 58 | + ArrayDeque<HazardCurveCalculator> calcDeque = new ArrayDeque<>(); |
| 59 | + |
| 60 | + // need x values |
| 61 | + DiscretizedFunc xVals = new IMT_Info().getDefaultHazardCurve(PGA_Param.NAME); |
| 62 | + // need them in ln spacing |
| 63 | + DiscretizedFunc logXVals = new ArbitrarilyDiscretizedFunc(); |
| 64 | + for (int i=0; i<xVals.size(); i++) |
| 65 | + logXVals.set(Math.log(xVals.getX(i)), 0d); |
| 66 | + |
| 67 | + // now spin it up in parallel |
| 68 | + List<CompletableFuture<DiscretizedFunc>> futures = new ArrayList<>(sites.size()); |
| 69 | + |
| 70 | + System.out.println("Calculating "+sites.size()+" curves"); |
| 71 | + for (Site site : sites) { |
| 72 | + futures.add(CompletableFuture.supplyAsync(()-> { |
| 73 | + // this runs in parallel |
| 74 | + |
| 75 | + // get GMM instance for this thread |
| 76 | + ScalarIMR gmm = null; |
| 77 | + synchronized (gmmDeque) { |
| 78 | + if (!gmmDeque.isEmpty()) |
| 79 | + gmm = gmmDeque.pop(); |
| 80 | + } |
| 81 | + if (gmm == null) { |
| 82 | + gmm = gmmRef.get(); |
| 83 | + // could set any custom params here |
| 84 | + } |
| 85 | + // get hazard curve calculator instance for this thread |
| 86 | + HazardCurveCalculator calc = null; |
| 87 | + synchronized (calcDeque) { |
| 88 | + if (!calcDeque.isEmpty()) |
| 89 | + calc = calcDeque.pop(); |
| 90 | + } |
| 91 | + if (calc == null) { |
| 92 | + calc = new HazardCurveCalculator(sourceFilters); |
| 93 | + } |
| 94 | + |
| 95 | + DiscretizedFunc logCurve = logXVals.deepClone(); |
| 96 | + calc.getHazardCurve(logCurve, site, gmm, erf); |
| 97 | + |
| 98 | + // return those instances for reuse by later threads |
| 99 | + synchronized (gmmDeque) { |
| 100 | + gmmDeque.push(gmm); |
| 101 | + } |
| 102 | + synchronized (calcDeque) { |
| 103 | + calcDeque.push(calc); |
| 104 | + } |
| 105 | + |
| 106 | + // return linear curve |
| 107 | + DiscretizedFunc linearCurve = xVals.deepClone(); |
| 108 | + for (int i=0; i<linearCurve.size(); i++) |
| 109 | + linearCurve.set(i, logCurve.getY(i)); |
| 110 | + return logCurve; |
| 111 | + })); |
| 112 | + } |
| 113 | + |
| 114 | + // now all of the tasks have been submitted, need to "join" them (wait for them to finish) |
| 115 | + for (int s=0; s<sites.size(); s++) { |
| 116 | + Site site = sites.get(s); |
| 117 | + DiscretizedFunc curve = futures.get(s).join(); |
| 118 | + // do what you need to do here |
| 119 | + |
| 120 | + // this print helps keep track of progress |
| 121 | + System.out.print("."); |
| 122 | + if (s > 0 && (s % 100 == 0 || s == sites.size()-1)) |
| 123 | + System.out.println(" "+s+"/"+sites.size()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments