Repository: incubator-systemml Updated Branches: refs/heads/master d0ab2af19 -> 7a3e95716
[SYSTEMML-591] Initial attempt to consolidate replicated compilation chain This only consolidates MLContext and DMLScript, but not JMLC and old MLContext (as it is going to be removed soon). Closes #471. Project: http://git-wip-us.apache.org/repos/asf/incubator-systemml/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-systemml/commit/7a3e9571 Tree: http://git-wip-us.apache.org/repos/asf/incubator-systemml/tree/7a3e9571 Diff: http://git-wip-us.apache.org/repos/asf/incubator-systemml/diff/7a3e9571 Branch: refs/heads/master Commit: 7a3e95716e81c1fc3415c36c357c9d24b710e739 Parents: d0ab2af Author: Niketan Pansare <npan...@us.ibm.com> Authored: Sun Apr 30 12:15:48 2017 -0800 Committer: Niketan Pansare <npan...@us.ibm.com> Committed: Sun Apr 30 13:15:48 2017 -0700 ---------------------------------------------------------------------- .../java/org/apache/sysml/api/DMLScript.java | 47 ++--------- .../apache/sysml/api/ScriptExecutorUtils.java | 83 ++++++++++++++++++++ .../sysml/api/mlcontext/ScriptExecutor.java | 55 +++++-------- 3 files changed, 107 insertions(+), 78 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/7a3e9571/src/main/java/org/apache/sysml/api/DMLScript.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/api/DMLScript.java b/src/main/java/org/apache/sysml/api/DMLScript.java index bfac48d..7988ff0 100644 --- a/src/main/java/org/apache/sysml/api/DMLScript.java +++ b/src/main/java/org/apache/sysml/api/DMLScript.java @@ -88,11 +88,8 @@ import org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext; import org.apache.sysml.runtime.controlprogram.parfor.ProgramConverter; import org.apache.sysml.runtime.controlprogram.parfor.stat.InfrastructureAnalyzer; import org.apache.sysml.runtime.controlprogram.parfor.util.IDHandler; -import org.apache.sysml.runtime.instructions.gpu.context.GPUContext; -import org.apache.sysml.runtime.instructions.gpu.context.GPUContextPool; import org.apache.sysml.runtime.io.IOUtilFunctions; import org.apache.sysml.runtime.matrix.CleanupMR; -import org.apache.sysml.runtime.matrix.data.LibMatrixDNN; import org.apache.sysml.runtime.matrix.mapred.MRConfigurationNames; import org.apache.sysml.runtime.matrix.mapred.MRJobConfiguration; import org.apache.sysml.runtime.util.LocalFileUtils; @@ -100,7 +97,6 @@ import org.apache.sysml.runtime.util.MapReduceTool; import org.apache.sysml.utils.Explain; import org.apache.sysml.utils.Explain.ExplainCounts; import org.apache.sysml.utils.Explain.ExplainType; -import org.apache.sysml.utils.GPUStatistics; import org.apache.sysml.utils.Statistics; import org.apache.sysml.yarn.DMLAppMasterUtils; import org.apache.sysml.yarn.DMLYarnClientProxy; @@ -825,53 +821,20 @@ public class DMLScript //double costs = CostEstimationWrapper.getTimeEstimate(rtprog, ExecutionContextFactory.createContext()); //System.out.println("Estimated costs: "+costs); - - // Whether extra statistics useful for developers and others interested in digging - // into performance problems are recorded and displayed - GPUStatistics.DISPLAY_STATISTICS = dmlconf.getBooleanValue(DMLConfig.EXTRA_GPU_STATS); - LibMatrixDNN.DISPLAY_STATISTICS = dmlconf.getBooleanValue(DMLConfig.EXTRA_DNN_STATS); - - // Sets the maximum number of GPUs per process, -1 for all available GPUs - GPUContextPool.PER_PROCESS_MAX_GPUS = dmlconf.getIntValue(DMLConfig.MAX_GPUS_PER_PROCESS); //Step 10: execute runtime program - Statistics.startRunTimer(); ExecutionContext ec = null; - GPUContext gCtx = null; - try - { - //run execute (w/ exception handling to ensure proper shutdown) + try { ec = ExecutionContextFactory.createContext(rtprog); - if (DMLScript.USE_ACCELERATOR && ec != null){ - gCtx = GPUContextPool.getFromPool(); - if (gCtx == null) { - throw new DMLRuntimeException("GPU : Could not create GPUContext, either no GPU or all GPUs currently in use"); - } - gCtx.initializeThread(); - ec.setGPUContext(gCtx); - } - rtprog.execute( ec ); - + ScriptExecutorUtils.executeRuntimeProgram(rtprog, ec, dmlconf); } - finally //ensure cleanup/shutdown - { - if(DMLScript.USE_ACCELERATOR && ec.getGPUContext() != null) { - GPUContextPool.returnToPool(ec.getGPUContext()); - } - - if( dmlconf.getBooleanValue(DMLConfig.CODEGEN) ) - SpoofCompiler.cleanupCodeGenerator(); + finally { if(ec != null && ec instanceof SparkExecutionContext) ((SparkExecutionContext) ec).close(); - - //display statistics (incl caching stats if enabled) - Statistics.stopRunTimer(); - LOG.info(Statistics.display()); LOG.info("END DML run " + getDateTime() ); - //cleanup scratch_space and all working dirs - cleanupHadoopExecution( dmlconf ); - } + cleanupHadoopExecution( dmlconf ); + } } /** http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/7a3e9571/src/main/java/org/apache/sysml/api/ScriptExecutorUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/api/ScriptExecutorUtils.java b/src/main/java/org/apache/sysml/api/ScriptExecutorUtils.java new file mode 100644 index 0000000..543e218 --- /dev/null +++ b/src/main/java/org/apache/sysml/api/ScriptExecutorUtils.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sysml.api; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.sysml.conf.DMLConfig; +import org.apache.sysml.hops.codegen.SpoofCompiler; +import org.apache.sysml.runtime.DMLRuntimeException; +import org.apache.sysml.runtime.controlprogram.Program; +import org.apache.sysml.runtime.controlprogram.context.ExecutionContext; +import org.apache.sysml.runtime.instructions.gpu.context.GPUContext; +import org.apache.sysml.runtime.instructions.gpu.context.GPUContextPool; +import org.apache.sysml.runtime.matrix.data.LibMatrixDNN; +import org.apache.sysml.utils.GPUStatistics; +import org.apache.sysml.utils.Statistics; + +public class ScriptExecutorUtils { + private static final Log LOG = LogFactory.getLog(ScriptExecutorUtils.class.getName()); + + /** + * Execute the runtime program. This involves execution of the program + * blocks that make up the runtime program and may involve dynamic + * recompilation. + * + * @param rtprog runtime program + * @param ec execution context + * @param dmlconf dml configuration + * @throws DMLRuntimeException if error occurs + */ + public static void executeRuntimeProgram(Program rtprog, ExecutionContext ec, DMLConfig dmlconf) throws DMLRuntimeException { + // Whether extra statistics useful for developers and others interested in digging + // into performance problems are recorded and displayed + GPUStatistics.DISPLAY_STATISTICS = dmlconf.getBooleanValue(DMLConfig.EXTRA_GPU_STATS); + LibMatrixDNN.DISPLAY_STATISTICS = dmlconf.getBooleanValue(DMLConfig.EXTRA_DNN_STATS); + + // Sets the maximum number of GPUs per process, -1 for all available GPUs + GPUContextPool.PER_PROCESS_MAX_GPUS = dmlconf.getIntValue(DMLConfig.MAX_GPUS_PER_PROCESS); + Statistics.startRunTimer(); + GPUContext gCtx = null; + try { + //run execute (w/ exception handling to ensure proper shutdown) + if (DMLScript.USE_ACCELERATOR && ec != null){ + gCtx = GPUContextPool.getFromPool(); + if (gCtx == null) { + throw new DMLRuntimeException("GPU : Could not create GPUContext, either no GPU or all GPUs currently in use"); + } + gCtx.initializeThread(); + ec.setGPUContext(gCtx); + } + rtprog.execute( ec ); + } + finally //ensure cleanup/shutdown + { + if(DMLScript.USE_ACCELERATOR && ec.getGPUContext() != null) { + GPUContextPool.returnToPool(ec.getGPUContext()); + } + if( dmlconf.getBooleanValue(DMLConfig.CODEGEN) ) + SpoofCompiler.cleanupCodeGenerator(); + + //display statistics (incl caching stats if enabled) + Statistics.stopRunTimer(); + LOG.info(Statistics.display()); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/7a3e9571/src/main/java/org/apache/sysml/api/mlcontext/ScriptExecutor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/api/mlcontext/ScriptExecutor.java b/src/main/java/org/apache/sysml/api/mlcontext/ScriptExecutor.java index 2368c90..bb891ca 100644 --- a/src/main/java/org/apache/sysml/api/mlcontext/ScriptExecutor.java +++ b/src/main/java/org/apache/sysml/api/mlcontext/ScriptExecutor.java @@ -25,6 +25,7 @@ import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.apache.sysml.api.DMLScript; +import org.apache.sysml.api.ScriptExecutorUtils; import org.apache.sysml.api.jmlc.JMLCUtils; import org.apache.sysml.api.mlcontext.MLContext.ExplainLevel; import org.apache.sysml.conf.ConfigurationManager; @@ -47,8 +48,6 @@ import org.apache.sysml.runtime.controlprogram.LocalVariableMap; import org.apache.sysml.runtime.controlprogram.Program; import org.apache.sysml.runtime.controlprogram.context.ExecutionContext; import org.apache.sysml.runtime.controlprogram.context.ExecutionContextFactory; -import org.apache.sysml.runtime.instructions.gpu.context.GPUContext; -import org.apache.sysml.runtime.instructions.gpu.context.GPUContextPool; import org.apache.sysml.utils.Explain; import org.apache.sysml.utils.Explain.ExplainCounts; import org.apache.sysml.utils.Explain.ExplainType; @@ -118,7 +117,9 @@ public class ScriptExecutor { protected boolean init = false; protected boolean explain = false; protected boolean gpu = false; + protected boolean oldGPU = false; protected boolean statistics = false; + protected boolean oldStatistics = false; protected ExplainLevel explainLevel; protected int statisticsMaxHeavyHitters = 10; protected boolean maintainSymbolTable = false; @@ -245,18 +246,10 @@ public class ScriptExecutor { if (symbolTable != null) { executionContext.setVariables(symbolTable); } - try { - if (gpu) { - GPUContext gCtx = GPUContextPool.getFromPool(); - if (gCtx == null) - throw new MLContextException("GPU : no GPUs or no more free GPUs available"); - executionContext.setGPUContext(gCtx); - gCtx.initializeThread(); - } - } catch (DMLRuntimeException e) { - throw new MLContextException("GPU : Exception occurred during initialization"); - } - + oldGPU = DMLScript.USE_ACCELERATOR; + oldStatistics = DMLScript.STATISTICS; + DMLScript.USE_ACCELERATOR = gpu; + DMLScript.STATISTICS = statistics; } /** @@ -290,9 +283,6 @@ public class ScriptExecutor { public MLResults execute(Script script) { // main steps in script execution - if(statistics) { - Statistics.startRunTimer(); - } setup(script); parseScript(); liveVariableAnalysis(); @@ -307,19 +297,19 @@ public class ScriptExecutor { countCompiledMRJobsAndSparkInstructions(); initializeCachingAndScratchSpace(); cleanupRuntimeProgram(); - createAndInitializeExecutionContext(); - executeRuntimeProgram(); - cleanupAfterExecution(); - + + try { + createAndInitializeExecutionContext(); + executeRuntimeProgram(); + } + finally { + cleanupAfterExecution(); + } + // add symbol table to MLResults MLResults mlResults = new MLResults(script); script.setResults(mlResults); - if (statistics) { - Statistics.stopRunTimer(); - System.out.println(Statistics.display(statisticsMaxHeavyHitters)); - } - return mlResults; } @@ -344,14 +334,8 @@ public class ScriptExecutor { */ protected void cleanupAfterExecution() { restoreInputsInSymbolTable(); - try { - if (gpu) { - GPUContext gCtx = executionContext.getGPUContext(); - GPUContextPool.returnToPool(gCtx); - } - } catch (DMLRuntimeException e) { - throw new MLContextException("Exception occurred during cleanup of GPU related resources", e); - } + DMLScript.USE_ACCELERATOR = oldGPU; + DMLScript.STATISTICS = oldStatistics; } /** @@ -394,7 +378,7 @@ public class ScriptExecutor { */ protected void executeRuntimeProgram() { try { - runtimeProgram.execute(executionContext); + ScriptExecutorUtils.executeRuntimeProgram(runtimeProgram, executionContext, config); } catch (DMLRuntimeException e) { throw new MLContextException("Exception occurred while executing runtime program", e); } @@ -667,7 +651,6 @@ public class ScriptExecutor { */ public void setGPU(boolean enabled) { this.gpu = enabled; - DMLScript.USE_ACCELERATOR = enabled; } }