You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@systemds.apache.org by ma...@apache.org on 2021/02/05 01:27:41 UTC

[systemds] 03/05: [SYSTEMDS-2825] Avoid recompiling generated cuda operators

This is an automated email from the ASF dual-hosted git repository.

markd pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/systemds.git

commit d269cb905f057dff134256ac43c61e39308e91f6
Author: Mark Dokter <ma...@dokter.cc>
AuthorDate: Fri Feb 5 02:09:22 2021 +0100

    [SYSTEMDS-2825] Avoid recompiling generated cuda operators
---
 .../apache/sysds/hops/codegen/SpoofCompiler.java   | 88 ++++++++++++++--------
 .../org/apache/sysds/hops/codegen/cplan/CNode.java |  6 +-
 .../apache/sysds/hops/codegen/cplan/CNodeCell.java |  8 +-
 .../apache/sysds/hops/codegen/cplan/CNodeTpl.java  |  2 +
 .../java/org/apache/sysds/lops/SpoofFused.java     | 12 ++-
 5 files changed, 77 insertions(+), 39 deletions(-)

diff --git a/src/main/java/org/apache/sysds/hops/codegen/SpoofCompiler.java b/src/main/java/org/apache/sysds/hops/codegen/SpoofCompiler.java
index 06bedbc..5cb0bdc 100644
--- a/src/main/java/org/apache/sysds/hops/codegen/SpoofCompiler.java
+++ b/src/main/java/org/apache/sysds/hops/codegen/SpoofCompiler.java
@@ -191,6 +191,22 @@ public class SpoofCompiler {
 			native_contexts = new HashMap<>();
 
 		if(!native_contexts.containsKey(generator)) {
+			String local_tmp = ConfigurationManager.getDMLConfig().getTextValue(DMLConfig.LOCAL_TMP_DIR);
+			String jar_path = SpoofCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath();
+			if(jar_path.contains(".jar")) {
+				try {
+					extractCodegenSources(local_tmp, jar_path);
+				}
+				catch (IOException e){
+					LOG.error("Could not extract spoof files from jar: " + e);
+					API = GeneratorAPI.JAVA;
+					return;
+				}
+			}
+			else {
+				local_tmp = System.getProperty("user.dir") + "/src/main".replace("/", File.separator);
+			}
+			
 			if(generator == GeneratorAPI.CUDA) {
 				// init GPUs with jCuda to avoid double initialization problems
 				GPUContextPool.initializeGPU();
@@ -222,21 +238,7 @@ public class SpoofCompiler {
 					isLoaded = NativeHelper.loadLibraryHelperFromResource(libName);
 
 				if(isLoaded) {
-					String local_tmp = ConfigurationManager.getDMLConfig().getTextValue(DMLConfig.LOCAL_TMP_DIR);
-					String jar_path = SpoofCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath();
-					if(jar_path.contains(".jar")) {
-						try {
-							extractCodegenSources(local_tmp, jar_path);
-						}
-						catch (IOException e){
-							LOG.error("Could not extract spoof files from jar: " + e);
-							API = GeneratorAPI.JAVA;
-							return;
-						}
-					}
-					else {
-						local_tmp = System.getProperty("user.dir") + "/src/main".replace("/", File.separator);
-					}
+
 
 					long ctx_ptr = initialize_cuda_context(0, local_tmp);
 					if(ctx_ptr != 0) {
@@ -272,7 +274,8 @@ public class SpoofCompiler {
 	
 			while (files_in_jar.hasMoreElements()) {
 				JarEntry in_file = files_in_jar.nextElement();
-				if (in_file.getName().startsWith("cuda/") && !in_file.isDirectory()) {
+				if ((in_file.getName().startsWith("cuda/") || in_file.getName().startsWith("java/")) &&
+						!in_file.isDirectory()) {
 					File out_file = new File(resource_path, in_file.getName());
 					out_file.deleteOnExit();
 					File parent = out_file.getParentFile();
@@ -512,20 +515,24 @@ public class SpoofCompiler {
 				
 				if( cla == null ) {
 					String src = "";
+					String src_cuda = "";
 					boolean native_compiled_successfully = false;
+					src = tmp.getValue().codegen(false, GeneratorAPI.JAVA);
+					cla = CodegenUtils.compileClass("codegen."+ tmp.getValue().getClassname(), src);
 
-					if(API == GeneratorAPI.CUDA && tmp.getValue().isSupported(API)) {
-						src = tmp.getValue().codegen(false, GeneratorAPI.CUDA);
-						native_compiled_successfully = compile_cuda(tmp.getValue().getVarname(), src);
-						if (native_compiled_successfully)
-							CodegenUtils.putNativeOpData(new SpoofCUDA(tmp.getValue()));
+					if(API == GeneratorAPI.CUDA) {
+						if(tmp.getValue().isSupported(API)) {
+							src_cuda = tmp.getValue().codegen(false, GeneratorAPI.CUDA);
+							native_compiled_successfully = compile_cuda(tmp.getValue().getVarname(), src_cuda);
+							if(native_compiled_successfully)
+								CodegenUtils.putNativeOpData(new SpoofCUDA(tmp.getValue()));
+							else {
+								LOG.warn("CUDA compilation failed, falling back to JAVA");
+								tmp.getValue().setGeneratorAPI(GeneratorAPI.JAVA);
+							}
+						}
 						else
-							LOG.warn("CUDA compilation failed, falling back to JAVA");
-					}
-
-					if(API == GeneratorAPI.JAVA || !native_compiled_successfully) {
-							src = tmp.getValue().codegen(false, GeneratorAPI.JAVA);
-							cla = CodegenUtils.compileClass("codegen."+ tmp.getValue().getClassname(), src);
+							LOG.warn("CPlan " + tmp.getValue().getVarname() + " not supported by SPOOF CUDA");
 					}
 
 					//explain debug output cplans or generated source code
@@ -536,9 +543,16 @@ public class SpoofCompiler {
 							+ Explain.explainCPlan(cplan.getValue().getValue()));
 					}
 					if( LOG.isTraceEnabled() || DMLScript.EXPLAIN.isRuntimeType(recompile) ) {
-						LOG.info("Codegen EXPLAIN (generated code for HopID: " + cplan.getKey() + 
+						LOG.info("JAVA Codegen EXPLAIN (generated code for HopID: " + cplan.getKey() +
 							", line "+tmp.getValue().getBeginLine() + ", hash="+tmp.getValue().hashCode()+"):");
-						LOG.info(src);
+						LOG.info(CodegenUtils.printWithLineNumber(src));
+						
+						if(API == GeneratorAPI.CUDA) {
+							LOG.info("CUDA Codegen EXPLAIN (generated code for HopID: " + cplan.getKey() +
+									", line " + tmp.getValue().getBeginLine() + ", hash=" + tmp.getValue().hashCode() + "):");
+
+							LOG.info(CodegenUtils.printWithLineNumber(src_cuda));
+						}
 					}
 
 					//maintain plan cache
@@ -550,8 +564,20 @@ public class SpoofCompiler {
 				}
 				
 				//make class available and maintain hits
-				if(cla != null || API != GeneratorAPI.JAVA)
-					clas.put(cplan.getKey(), new Pair<Hop[],Class<?>>(tmp.getKey(),cla));
+				if(cla != null) {
+					if(CodegenUtils.getNativeOpData(cla.getName()) != null) {
+						if(tmp.getValue().getVarname() == null) {
+							tmp.getValue().setVarName(cla.getName());
+							if(tmp.getValue().getGeneratorAPI() != CodegenUtils.getNativeOpData(cla.getName())
+								.getCNodeTemplate().getGeneratorAPI())
+							{
+								tmp.getValue().setGeneratorAPI(CodegenUtils.getNativeOpData(cla.getName())
+									.getCNodeTemplate().getGeneratorAPI());
+							}
+						}
+					}
+					clas.put(cplan.getKey(), new Pair<Hop[], Class<?>>(tmp.getKey(), cla));
+				}
 				if( DMLScript.STATISTICS )
 					Statistics.incrementCodegenOpCacheTotal();
 			}
diff --git a/src/main/java/org/apache/sysds/hops/codegen/cplan/CNode.java b/src/main/java/org/apache/sysds/hops/codegen/cplan/CNode.java
index 6cdd225..bbe5454 100644
--- a/src/main/java/org/apache/sysds/hops/codegen/cplan/CNode.java
+++ b/src/main/java/org/apache/sysds/hops/codegen/cplan/CNode.java
@@ -73,7 +73,8 @@ public abstract class CNode
 	}
 	
 	public String createVarname() {
-		_genVar = "TMP"+_seqVar.getNextID();
+		if(_genVar == null)
+			_genVar = "TMP"+_seqVar.getNextID();
 		return _genVar; 
 	}
 	
@@ -83,7 +84,6 @@ public abstract class CNode
 
 	public String getVarname(GeneratorAPI api) { return getVarname(); }
 
-
 	public String getVectorLength() {
 		if( getVarname().startsWith("a") )
 			return "len";
@@ -264,4 +264,6 @@ public abstract class CNode
 	}
 
 	public abstract boolean isSupported(GeneratorAPI api);
+	
+	public void setVarName(String name) { _genVar = name; }
 }
diff --git a/src/main/java/org/apache/sysds/hops/codegen/cplan/CNodeCell.java b/src/main/java/org/apache/sysds/hops/codegen/cplan/CNodeCell.java
index 99fcbdc..74292b6 100644
--- a/src/main/java/org/apache/sysds/hops/codegen/cplan/CNodeCell.java
+++ b/src/main/java/org/apache/sysds/hops/codegen/cplan/CNodeCell.java
@@ -124,8 +124,12 @@ public class CNodeCell extends CNodeTpl
 		//generate dense/sparse bodies
 		String tmpDense = _output.codegen(false, api);
 		_output.resetGenerated();
-
-		tmp = tmp.replace("%TMP%", createVarname());
+		
+		if(getVarname() == null)
+			tmp = tmp.replace("%TMP%", createVarname());
+		else
+			tmp = tmp.replace("%TMP%", getVarname());
+		
 		tmp = tmp.replace("%BODY_dense%", tmpDense);
 		
 		//return last TMP
diff --git a/src/main/java/org/apache/sysds/hops/codegen/cplan/CNodeTpl.java b/src/main/java/org/apache/sysds/hops/codegen/cplan/CNodeTpl.java
index 2026eb3..552b951 100644
--- a/src/main/java/org/apache/sysds/hops/codegen/cplan/CNodeTpl.java
+++ b/src/main/java/org/apache/sysds/hops/codegen/cplan/CNodeTpl.java
@@ -236,4 +236,6 @@ public abstract class CNodeTpl extends CNode implements Cloneable
 	}
 
 	public GeneratorAPI getGeneratorAPI() { return api; }
+	
+	public void setGeneratorAPI(GeneratorAPI _api) { api = _api; }
 }
diff --git a/src/main/java/org/apache/sysds/lops/SpoofFused.java b/src/main/java/org/apache/sysds/lops/SpoofFused.java
index 0795f8f..b114f4d 100644
--- a/src/main/java/org/apache/sysds/lops/SpoofFused.java
+++ b/src/main/java/org/apache/sysds/lops/SpoofFused.java
@@ -34,7 +34,7 @@ public class SpoofFused extends Lop
 	private final int _numThreads;
 	private final String _genVarName;
 
-	private GeneratorAPI _api;
+	private final GeneratorAPI _api;
 	public SpoofFused(ArrayList<Lop> inputs, DataType dt, ValueType vt, Class<?> cla, GeneratorAPI api,
 					  String genVarName, int k, ExecType etype) {
 		super(Type.SpoofFused, dt, vt);
@@ -110,10 +110,14 @@ public class SpoofFused extends Lop
 		sb.append( OPERAND_DELIMITOR );
 		sb.append( _api);
 		sb.append( OPERAND_DELIMITOR );
-		if(_class != null)
-			sb.append( _class.getName() );
+		if(_api == GeneratorAPI.CUDA)
+			if(_genVarName.contains("codegen"))
+				sb.append(_genVarName);
+			else
+				sb.append("codegen.").append(_genVarName);
 		else
-			sb.append("codegen." + _genVarName);
+			sb.append( _class.getName() );
+			
 
 
 		for(int i=0; i < inputs.length; i++) {