You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@systemml.apache.org by na...@apache.org on 2017/04/21 23:23:16 UTC

[1/5] incubator-systemml git commit: Refactored GPU{Contex, Object} to make it friendlier for parfor

Repository: incubator-systemml
Updated Branches:
  refs/heads/master 9ed27ad60 -> 129f0f6b0


http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/test/java/org/apache/sysml/test/integration/AutomatedTestBase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/sysml/test/integration/AutomatedTestBase.java b/src/test/java/org/apache/sysml/test/integration/AutomatedTestBase.java
index f7071ba..b20f66a 100644
--- a/src/test/java/org/apache/sysml/test/integration/AutomatedTestBase.java
+++ b/src/test/java/org/apache/sysml/test/integration/AutomatedTestBase.java
@@ -1515,7 +1515,7 @@ public abstract class AutomatedTestBase
 	/**
 	 * Enables expection of a line in standard output stream.
 	 * 
-	 * @param expected
+	 * @param expectedLine
 	 */
 	public void setExpectedStdOut(String expectedLine) {
 		this.expectedStdOut = expectedLine;


[2/5] incubator-systemml git commit: Refactored GPU{Contex, Object} to make it friendlier for parfor

Posted by na...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixCUDA.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixCUDA.java b/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixCUDA.java
index c363ab1..3c32137 100644
--- a/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixCUDA.java
+++ b/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixCUDA.java
@@ -19,23 +19,49 @@
 
 package org.apache.sysml.runtime.matrix.data;
 
-import jcuda.Pointer;
-import jcuda.Sizeof;
-import jcuda.jcublas.JCublas2;
-import jcuda.jcublas.cublasFillMode;
-import jcuda.jcublas.cublasHandle;
-import jcuda.jcublas.cublasOperation;
-import jcuda.jcudnn.cudnnActivationDescriptor;
-import jcuda.jcudnn.cudnnBatchNormMode;
-import jcuda.jcudnn.cudnnConvolutionDescriptor;
-import jcuda.jcudnn.cudnnConvolutionFwdPreference;
-import jcuda.jcudnn.cudnnFilterDescriptor;
-import jcuda.jcudnn.cudnnHandle;
-import jcuda.jcudnn.cudnnPoolingDescriptor;
-import jcuda.jcudnn.cudnnStatus;
-import jcuda.jcudnn.cudnnTensorDescriptor;
-import jcuda.jcusparse.JCusparse;
-import jcuda.jcusparse.cusparseHandle;
+import static jcuda.jcublas.cublasOperation.CUBLAS_OP_N;
+import static jcuda.jcublas.cublasOperation.CUBLAS_OP_T;
+import static jcuda.jcudnn.JCudnn.cudnnActivationForward;
+import static jcuda.jcudnn.JCudnn.cudnnBatchNormalizationBackward;
+import static jcuda.jcudnn.JCudnn.cudnnBatchNormalizationForwardInference;
+import static jcuda.jcudnn.JCudnn.cudnnBatchNormalizationForwardTraining;
+import static jcuda.jcudnn.JCudnn.cudnnConvolutionBackwardData;
+import static jcuda.jcudnn.JCudnn.cudnnConvolutionBackwardFilter;
+import static jcuda.jcudnn.JCudnn.cudnnConvolutionForward;
+import static jcuda.jcudnn.JCudnn.cudnnCreateActivationDescriptor;
+import static jcuda.jcudnn.JCudnn.cudnnCreateConvolutionDescriptor;
+import static jcuda.jcudnn.JCudnn.cudnnCreateFilterDescriptor;
+import static jcuda.jcudnn.JCudnn.cudnnCreatePoolingDescriptor;
+import static jcuda.jcudnn.JCudnn.cudnnCreateTensorDescriptor;
+import static jcuda.jcudnn.JCudnn.cudnnDestroyConvolutionDescriptor;
+import static jcuda.jcudnn.JCudnn.cudnnDestroyFilterDescriptor;
+import static jcuda.jcudnn.JCudnn.cudnnDestroyPoolingDescriptor;
+import static jcuda.jcudnn.JCudnn.cudnnGetConvolutionBackwardDataWorkspaceSize;
+import static jcuda.jcudnn.JCudnn.cudnnGetConvolutionBackwardFilterWorkspaceSize;
+import static jcuda.jcudnn.JCudnn.cudnnGetConvolutionForwardWorkspaceSize;
+import static jcuda.jcudnn.JCudnn.cudnnPoolingBackward;
+import static jcuda.jcudnn.JCudnn.cudnnPoolingForward;
+import static jcuda.jcudnn.JCudnn.cudnnSetActivationDescriptor;
+import static jcuda.jcudnn.JCudnn.cudnnSetConvolution2dDescriptor;
+import static jcuda.jcudnn.JCudnn.cudnnSetFilter4dDescriptor;
+import static jcuda.jcudnn.JCudnn.cudnnSetPooling2dDescriptor;
+import static jcuda.jcudnn.JCudnn.cudnnSetTensor4dDescriptor;
+import static jcuda.jcudnn.cudnnActivationMode.CUDNN_ACTIVATION_RELU;
+import static jcuda.jcudnn.cudnnConvolutionMode.CUDNN_CROSS_CORRELATION;
+import static jcuda.jcudnn.cudnnDataType.CUDNN_DATA_DOUBLE;
+import static jcuda.jcudnn.cudnnNanPropagation.CUDNN_PROPAGATE_NAN;
+import static jcuda.jcudnn.cudnnPoolingMode.CUDNN_POOLING_MAX;
+import static jcuda.jcudnn.cudnnTensorFormat.CUDNN_TENSOR_NCHW;
+import static jcuda.jcusparse.JCusparse.cusparseDcsrgemm;
+import static jcuda.jcusparse.JCusparse.cusparseDcsrmv;
+import static jcuda.jcusparse.cusparseOperation.CUSPARSE_OPERATION_NON_TRANSPOSE;
+import static jcuda.jcusparse.cusparseOperation.CUSPARSE_OPERATION_TRANSPOSE;
+import static jcuda.runtime.JCuda.cudaDeviceSynchronize;
+import static jcuda.runtime.JCuda.cudaMemcpy;
+import static jcuda.runtime.cudaMemcpyKind.cudaMemcpyDeviceToDevice;
+import static jcuda.runtime.cudaMemcpyKind.cudaMemcpyDeviceToHost;
+import static jcuda.runtime.cudaMemcpyKind.cudaMemcpyHostToDevice;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.sysml.api.DMLScript;
@@ -72,10 +98,9 @@ import org.apache.sysml.runtime.instructions.cp.DoubleObject;
 import org.apache.sysml.runtime.instructions.gpu.GPUInstruction;
 import org.apache.sysml.runtime.instructions.gpu.context.ExecutionConfig;
 import org.apache.sysml.runtime.instructions.gpu.context.GPUContext;
-import org.apache.sysml.runtime.instructions.gpu.context.JCudaContext;
+import org.apache.sysml.runtime.instructions.gpu.context.GPUObject;
+import org.apache.sysml.runtime.instructions.gpu.context.CSRPointer;
 import org.apache.sysml.runtime.instructions.gpu.context.JCudaKernels;
-import org.apache.sysml.runtime.instructions.gpu.context.JCudaObject;
-import org.apache.sysml.runtime.instructions.gpu.context.JCudaObject.CSRPointer;
 import org.apache.sysml.runtime.matrix.operators.AggregateOperator;
 import org.apache.sysml.runtime.matrix.operators.AggregateUnaryOperator;
 import org.apache.sysml.runtime.matrix.operators.BinaryOperator;
@@ -86,127 +111,123 @@ import org.apache.sysml.runtime.matrix.operators.ScalarOperator;
 import org.apache.sysml.utils.GPUStatistics;
 import org.apache.sysml.utils.Statistics;
 
-import static jcuda.jcublas.cublasOperation.CUBLAS_OP_N;
-import static jcuda.jcublas.cublasOperation.CUBLAS_OP_T;
-import static jcuda.jcudnn.JCudnn.cudnnActivationForward;
-import static jcuda.jcudnn.JCudnn.cudnnBatchNormalizationBackward;
-import static jcuda.jcudnn.JCudnn.cudnnBatchNormalizationForwardInference;
-import static jcuda.jcudnn.JCudnn.cudnnBatchNormalizationForwardTraining;
-import static jcuda.jcudnn.JCudnn.cudnnConvolutionBackwardData;
-import static jcuda.jcudnn.JCudnn.cudnnConvolutionBackwardFilter;
-import static jcuda.jcudnn.JCudnn.cudnnConvolutionForward;
-import static jcuda.jcudnn.JCudnn.cudnnCreateActivationDescriptor;
-import static jcuda.jcudnn.JCudnn.cudnnCreateConvolutionDescriptor;
-import static jcuda.jcudnn.JCudnn.cudnnCreateFilterDescriptor;
-import static jcuda.jcudnn.JCudnn.cudnnCreatePoolingDescriptor;
-import static jcuda.jcudnn.JCudnn.cudnnCreateTensorDescriptor;
-import static jcuda.jcudnn.JCudnn.cudnnDestroyConvolutionDescriptor;
-import static jcuda.jcudnn.JCudnn.cudnnDestroyFilterDescriptor;
-import static jcuda.jcudnn.JCudnn.cudnnDestroyPoolingDescriptor;
-import static jcuda.jcudnn.JCudnn.cudnnGetConvolutionBackwardDataWorkspaceSize;
-import static jcuda.jcudnn.JCudnn.cudnnGetConvolutionBackwardFilterWorkspaceSize;
-import static jcuda.jcudnn.JCudnn.cudnnGetConvolutionForwardWorkspaceSize;
-import static jcuda.jcudnn.JCudnn.cudnnPoolingBackward;
-import static jcuda.jcudnn.JCudnn.cudnnPoolingForward;
-import static jcuda.jcudnn.JCudnn.cudnnSetActivationDescriptor;
-import static jcuda.jcudnn.JCudnn.cudnnSetConvolution2dDescriptor;
-import static jcuda.jcudnn.JCudnn.cudnnSetFilter4dDescriptor;
-import static jcuda.jcudnn.JCudnn.cudnnSetPooling2dDescriptor;
-import static jcuda.jcudnn.JCudnn.cudnnSetTensor4dDescriptor;
-import static jcuda.jcudnn.cudnnActivationMode.CUDNN_ACTIVATION_RELU;
-import static jcuda.jcudnn.cudnnConvolutionMode.CUDNN_CROSS_CORRELATION;
-import static jcuda.jcudnn.cudnnDataType.CUDNN_DATA_DOUBLE;
-import static jcuda.jcudnn.cudnnNanPropagation.CUDNN_PROPAGATE_NAN;
-import static jcuda.jcudnn.cudnnPoolingMode.CUDNN_POOLING_MAX;
-import static jcuda.jcudnn.cudnnTensorFormat.CUDNN_TENSOR_NCHW;
-import static jcuda.jcusparse.JCusparse.cusparseDcsrgemm;
-import static jcuda.jcusparse.JCusparse.cusparseDcsrmv;
-import static jcuda.jcusparse.cusparseOperation.CUSPARSE_OPERATION_NON_TRANSPOSE;
-import static jcuda.jcusparse.cusparseOperation.CUSPARSE_OPERATION_TRANSPOSE;
-import static jcuda.runtime.JCuda.cudaDeviceSynchronize;
-import static jcuda.runtime.JCuda.cudaMemcpy;
-import static jcuda.runtime.cudaMemcpyKind.cudaMemcpyDeviceToDevice;
-import static jcuda.runtime.cudaMemcpyKind.cudaMemcpyDeviceToHost;
-import static jcuda.runtime.cudaMemcpyKind.cudaMemcpyHostToDevice;
-import static org.apache.sysml.runtime.instructions.gpu.context.JCudaObject.allocate;
-import static org.apache.sysml.runtime.instructions.gpu.context.JCudaObject.cudaFreeHelper;
+import jcuda.CudaException;
+import jcuda.Pointer;
+import jcuda.Sizeof;
+import jcuda.jcublas.JCublas2;
+import jcuda.jcublas.cublasFillMode;
+import jcuda.jcublas.cublasHandle;
+import jcuda.jcublas.cublasOperation;
+import jcuda.jcudnn.cudnnActivationDescriptor;
+import jcuda.jcudnn.cudnnBatchNormMode;
+import jcuda.jcudnn.cudnnConvolutionDescriptor;
+import jcuda.jcudnn.cudnnConvolutionFwdPreference;
+import jcuda.jcudnn.cudnnFilterDescriptor;
+import jcuda.jcudnn.cudnnHandle;
+import jcuda.jcudnn.cudnnPoolingDescriptor;
+import jcuda.jcudnn.cudnnStatus;
+import jcuda.jcudnn.cudnnTensorDescriptor;
+import jcuda.jcusparse.JCusparse;
+import jcuda.jcusparse.cusparseHandle;
 
-//FIXME move could to respective instructions, this is not a block library
+/**
+ * All CUDA kernels and library calls are redirected through this class
+ * @see GPUContext
+ * @see GPUObject
+ */
 public class LibMatrixCUDA {
 
-	// Assume Compute Capability 3.0
+	private static final Log LOG = LogFactory.getLog(LibMatrixCUDA.class.getName());
+
+    // Assume Compute Capability 3.0
 	// MAX BLOCKS is 2^31 - 1 For compute capability > 3.0
 	// MAX_THREADS is 1024 For compute capability > 3.0
 	private static int _MAX_THREADS = -1;
 	private static int _MAX_BLOCKS  = -1;
 	private static int _WARP_SIZE 	= -1;
 
+	//********************************************************************/
+	//***************************** UTILS ********************************/
+	//********************************************************************/
+
+	/*
+	static GPUContext gCtx throws DMLRuntimeException {
+			return GPUContext.gCtx;
+	}
+	*/
+
 	/**
 	 * Utility function to get maximum number of threads supported by the active CUDA device.
-	 * This is put into a singleton style method because the GPUContext is not fully initialized when
-	 * the LibMatrixCUDA class is loaded. If the {@link GPUContext#getGPUContext()} is invoked in a
-	 * static block in this class, it will access the {@link JCudaContext} instance when it is not
-	 * properly initialized. Due to the proper checks in place, a deadlock occurs.
+	 * @param gCtx a valid {@link GPUContext}
 	 * @return max threads
 	 * @throws DMLRuntimeException if exception occurs
 	 */
-	static int getMaxThreads() throws DMLRuntimeException{
+	static int getMaxThreads(GPUContext gCtx) throws DMLRuntimeException{
 		if (_MAX_THREADS == -1){
-			_MAX_THREADS = JCudaContext.getMaxThreadsPerBlock();
+			_MAX_THREADS = gCtx.getMaxThreadsPerBlock();
 		}
 		return _MAX_THREADS;
 	}
 
 	/**
 	 * Utility function to get maximum number of blocks supported by the active CUDA device.
-	 * This is put into a singleton style method because the GPUContext is not fully initialized when
-	 * the LibMatrixCUDA class is loaded. If the {@link GPUContext#getGPUContext()} is invoked in a
-	 * static block in this class, it will access the {@link JCudaContext} instance when it is not
-	 * properly initialized. Due to the proper checks in place, a deadlock occurs.
+	 * @param gCtx a valid {@link GPUContext}
 	 * @return max blocks
 	 * @throws DMLRuntimeException if exception occurs
 	 */
-	static int getMaxBlocks() throws DMLRuntimeException{
+	static int getMaxBlocks(GPUContext gCtx) throws DMLRuntimeException{
 		if (_MAX_BLOCKS == -1){
-			_MAX_BLOCKS = JCudaContext.getMaxBlocks();
+			_MAX_BLOCKS = gCtx.getMaxBlocks();
 		}
 		return _MAX_BLOCKS;
 	}
 
 	/**
 	 * Utility function to get the warp size supported by the active CUDA device.
-	 * This is put into a singleton style method because the GPUContext is not fully initialized when
-	 * the LibMatrixCUDA class is loaded. If the {@link GPUContext#getGPUContext()} is invoked in a
-	 * static block in this class, it will access the {@link JCudaContext} instance when it is not
-	 * properly initialized. Due to the proper checks in place, a deadlock occurs.
+	 * @param gCtx a valid {@link GPUContext}
 	 * @return warp size
 	 * @throws DMLRuntimeException if exception occurs
 	 */
-	static int getWarpSize() throws DMLRuntimeException {
+	static int getWarpSize(GPUContext gCtx) throws DMLRuntimeException {
 		if (_WARP_SIZE == -1) {
-			_WARP_SIZE = JCudaContext.getWarpSize();
+			_WARP_SIZE = gCtx.getWarpSize();
 		}
 		return _WARP_SIZE;
 	}
 
-	public static boolean isInSparseFormat(MatrixObject mo) {
-		if(mo.getGPUObject() != null && mo.getGPUObject().isAllocated())
-			return mo.getGPUObject().isInSparseFormat();
+	public static boolean isInSparseFormat(GPUContext gCtx, MatrixObject mo) {
+		if(mo.getGPUObject(gCtx) != null && mo.getGPUObject(gCtx).isAllocated())
+			return mo.getGPUObject(gCtx).isSparse();
 		return MatrixBlock.evalSparseFormatInMemory(mo.getNumRows(), mo.getNumColumns(), mo.getNnz());
 	}
 
 
+	private static cusparseHandle getCusparseHandle(GPUContext gCtx) throws DMLRuntimeException{
+		return gCtx.getCusparseHandle();
+	}
+
+	private static cublasHandle getCublasHandle(GPUContext gCtx) throws DMLRuntimeException {
+		return gCtx.getCublasHandle();
+	}
+
+	private static cudnnHandle getCudnnHandle(GPUContext gCtx) throws DMLRuntimeException {
+		return gCtx.getCudnnHandle();
+	}
+
+	private static JCudaKernels getCudaKernels(GPUContext gCtx) throws DMLRuntimeException {
+		return gCtx.getKernels();
+	}
+
+
 	//********************************************************************/
-	//***************** DEEP LEARNING Operators **************************/
+	//************************ End of UTILS ******************************/
 	//********************************************************************/
 
+	//********************************************************************/
+	//***************** DEEP LEARNING Operators **************************/
+	//********************************************************************/
 
-	public static cudnnHandle cudnnHandle;
-	public static cublasHandle cublasHandle;
-	public static cusparseHandle cusparseHandle;
-	public static JCudaKernels kernels; // Used to launch custom kernels
 
-	private static final Log LOG = LogFactory.getLog(LibMatrixCUDA.class.getName());
 
 	private static int CONVOLUTION_PREFERENCE = cudnnConvolutionFwdPreference.CUDNN_CONVOLUTION_FWD_NO_WORKSPACE;
 	
@@ -234,7 +255,8 @@ public class LibMatrixCUDA {
 	}
 	
 	/**
-	 * Convenience method to get tensor descriptor from underlying JCudaObject
+	 * Convenience method to get tensor descriptor from underlying GPUObject
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param mat matrix object
 	 * @param N number of images
 	 * @param C number of channels
@@ -243,39 +265,57 @@ public class LibMatrixCUDA {
 	 * @return cudnn tensor descriptor
 	 * @throws DMLRuntimeException if the input descriptor and matrix dimensions don't match
 	 */
-	private static cudnnTensorDescriptor allocateTensorDescriptor(MatrixObject mat, int N, int C, int H, int W) throws DMLRuntimeException {
+	private static cudnnTensorDescriptor allocateTensorDescriptor(GPUContext gCtx, MatrixObject mat, int N, int C, int H, int W) throws DMLRuntimeException {
 		if(mat.getNumRows() != N || mat.getNumColumns() != C*H*W) {
 			throw new DMLRuntimeException("Mismatch descriptor-matrix dimensions:" + mat.getNumRows() + " != " + N 
 					+ " || " + mat.getNumColumns() + " != " + (C*H*W));
 		}
-		return ((JCudaObject)mat.getGPUObject()).allocateTensorDescriptor(N, C, H, W);
+		return mat.getGPUObject(gCtx).allocateTensorDescriptor(N, C, H, W);
+	}
+
+	/**
+	 * Convenience method to get tensor descriptor
+	 * @param N number of images
+	 * @param C number of channels
+	 * @param H height
+	 * @param W width
+	 * @return cudnn tensor descriptor
+	 * @throws DMLRuntimeException if the input descriptor and matrix dimensions don't match
+	 */
+	private static cudnnTensorDescriptor allocateTensorDescriptor(int N, int C, int H, int W) throws DMLRuntimeException {
+		cudnnTensorDescriptor tensorDescriptor = new cudnnTensorDescriptor();
+		cudnnCreateTensorDescriptor(tensorDescriptor);
+		cudnnSetTensor4dDescriptor(tensorDescriptor, CUDNN_TENSOR_NCHW, CUDNN_DATA_DOUBLE, N, C, H, W);
+		return tensorDescriptor;
 	}
 	
 	/**
 	 * Convenience method to get jcudaDenseMatrixPtr. This method explicitly converts sparse to dense format, so use it judiciously.
+	 * @param gCtx a valid {@link GPUContext}
 	 * @param image input matrix object
 	 * @param isForCuDNN true if the dense pointer is to be used by a CuDNN kernel
 	 * @return jcuda pointer
 	 * @throws DMLRuntimeException if error occurs while sparse to dense conversion
 	 */
-	private static Pointer getDensePointer(MatrixObject image, boolean isForCuDNN, String instName) throws DMLRuntimeException {
+	private static Pointer getDensePointer(GPUContext gCtx, MatrixObject image, boolean isForCuDNN, String instName) throws DMLRuntimeException {
 		if(isForCuDNN && image.getNumRows()*image.getNumColumns() > numDoublesIn2GB) {
 			throw new DMLRuntimeException("CuDNN restriction: the size of input tensor cannot be greater than 2GB. Hint: try reducing the mini-batch size.");
 		}
-		return getDensePointer(image, instName);
+		return getDensePointer(gCtx, image, instName);
 	}
 	
 	/**
 	 * Convenience method to get jcudaDenseMatrixPtr. This method explicitly converts sparse to dense format, so use it judiciously.
+	 * @param gCtx a valid {@link GPUContext}
 	 * @param image input matrix object
 	 * @return jcuda pointer
 	 * @throws DMLRuntimeException if error occurs while sparse to dense conversion
 	 */
-	private static Pointer getDensePointer(MatrixObject image, String instName) throws DMLRuntimeException {
-		if(isInSparseFormat(image)) {
-			((JCudaObject)image.getGPUObject()).sparseToDense(instName);
+	private static Pointer getDensePointer(GPUContext gCtx, MatrixObject image, String instName) throws DMLRuntimeException {
+		if(isInSparseFormat(gCtx, image)) {
+			image.getGPUObject(gCtx).sparseToDense(instName);
 		}
-		return ((JCudaObject)image.getGPUObject()).jcudaDenseMatrixPtr;
+		return image.getGPUObject(gCtx).getJcudaDenseMatrixPtr();
 	}
 	
 	/**
@@ -289,34 +329,67 @@ public class LibMatrixCUDA {
 			throw new DMLRuntimeException("Error status returned by CuDNN:" + jcuda.jcudnn.cudnnStatus.stringFor(status));
 	}
 
-	public static void conv2dBiasAdd(String instName, MatrixObject image, MatrixObject bias, MatrixObject filter, MatrixObject outputBlock, int N, int C, int H, int W,
+	public static void conv2dBiasAdd(GPUContext gCtx, String instName, MatrixObject image, MatrixObject bias, MatrixObject filter, MatrixObject outputBlock, int N, int C, int H, int W,
 			int K, int R, int S, int pad_h, int pad_w, int stride_h, int stride_w, int P, int Q)
 					throws DMLRuntimeException {
-		conv2d(instName, image, filter, outputBlock, N, C, H, W, K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
-		biasAdd(instName, outputBlock, bias, outputBlock);
+		/*
+		int rows = (int) outputBlock.getNumRows();
+		int cols = (int) outputBlock.getNumColumns();
+		long size  = rows * cols * Sizeof.DOUBLE;
+
+		Pointer imagePointer = getDensePointer(image, instName);
+		Pointer biasPointer = getDensePointer(bias, instName);
+		Pointer outputPointer = getDensePointer(outputBlock, instName);
+		Pointer filterPointer = getDensePointer(filter, instName);
+
+		Pointer tmp = allocate(size);
+
+		conv2d(instName, imagePointer, filterPointer, tmp, N, C, H, W, K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
+		cudaDeviceSynchronize();
+
+		long k1 = bias.getNumColumns();
+		if(k1 != bias.getNumColumns() || bias.getNumColumns() != 1 || cols % k1 != 0) {
+			throw new DMLRuntimeException("Incorrect inputs for bias_add: input[" + rows + " X " + cols + "] and bias[" + K + " X " + bias.getNumColumns() + "]");
+		}
+		// biasAdd(instName, outputBlock, bias, outputBlock);
+		biasAdd(instName, tmp, biasPointer, outputPointer, rows, cols, (int)k1);
+
+		cudaFreeHelper(tmp);
+		*/
+		LOG.trace("GPU : conv2dBiasAdd" + ", GPUContext=" + gCtx);
+		conv2d(gCtx, instName, image, filter, outputBlock, N, C, H, W, K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
+		//cudaDeviceSynchronize;
+		biasAdd(gCtx, instName, outputBlock, bias, outputBlock);
 	}
 	
-	public static void conv2d(String instName, MatrixObject image, MatrixObject filter, MatrixObject outputBlock, int N, int C, int H, int W,
+	public static void conv2d(GPUContext gCtx, String instName, MatrixObject image, MatrixObject filter, MatrixObject outputBlock, int N, int C, int H, int W,
 														int K, int R, int S, int pad_h, int pad_w, int stride_h, int stride_w, int P, int Q)
 					throws DMLRuntimeException {
-		cudnnFilterDescriptor filterDesc = null;
+		Pointer imagePointer = getDensePointer(gCtx, image, true, instName);
+		Pointer filterPointer = getDensePointer(gCtx, filter, true, instName);
+		Pointer dstPointer = getDensePointer(gCtx, outputBlock, true, instName);
+
+		conv2d(gCtx, instName, imagePointer, filterPointer, dstPointer, N, C, H, W, K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
+	}
+
+	public static void conv2d(GPUContext gCtx, String instName, Pointer image, Pointer filter, Pointer output, int N,
+														 int C, int H, int W, int K, int R, int S, int pad_h, int pad_w, int stride_h, int stride_w, int P, int Q)
+					throws DMLRuntimeException {
+        LOG.trace("GPU : conv2d" + ", GPUContext=" + gCtx);
+        cudnnFilterDescriptor filterDesc = null;
 		cudnnConvolutionDescriptor convDesc = null;
 		Pointer workSpace = null;
 		long sizeInBytes = 0;
 		try {
-			long t1=0, t2=0;
+			long t1 = 0, t2 = 0;
 			// Allocate descriptors
 			if (GPUStatistics.DISPLAY_STATISTICS) t1 = System.nanoTime();
-			cudnnTensorDescriptor srcTensorDesc = allocateTensorDescriptor(image, N, C, H, W);
-			cudnnTensorDescriptor dstTensorDesc = allocateTensorDescriptor(outputBlock, N, K, P, Q);
+			cudnnTensorDescriptor srcTensorDesc = allocateTensorDescriptor(N, C, H, W);
+			cudnnTensorDescriptor dstTensorDesc = allocateTensorDescriptor(N, K, P, Q);
 			filterDesc = allocateFilterDescriptor(K, C, R, S);
 
-			Pointer imagePointer = getDensePointer(image, true, instName);
-			Pointer filterPointer = getDensePointer(filter, true, instName);
-			Pointer dstPointer = getDensePointer(outputBlock, true, instName);
-
-			int padding [] = { pad_h, pad_w };
-			int strides [] = { stride_h, stride_w };
+			int padding[] = {pad_h, pad_w};
+			int strides[] = {stride_h, stride_w};
 			convDesc = allocateConvolutionDescriptor(padding, strides);
 
 			// Select the best algorithm depending on the data and supported CUDA
@@ -324,53 +397,54 @@ public class LibMatrixCUDA {
 			int algo = -1;
 			workSpace = new Pointer();
 
-			if(CONVOLUTION_PREFERENCE == cudnnConvolutionFwdPreference.CUDNN_CONVOLUTION_FWD_NO_WORKSPACE) {
+			if (CONVOLUTION_PREFERENCE == cudnnConvolutionFwdPreference.CUDNN_CONVOLUTION_FWD_NO_WORKSPACE) {
 				algo = jcuda.jcudnn.cudnnConvolutionFwdAlgo.CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_GEMM;
-			}
-			else if(CONVOLUTION_PREFERENCE == cudnnConvolutionFwdPreference.CUDNN_CONVOLUTION_FWD_PREFER_FASTEST) {
-				int [] algos = {
+			} else if (CONVOLUTION_PREFERENCE == cudnnConvolutionFwdPreference.CUDNN_CONVOLUTION_FWD_PREFER_FASTEST) {
+				int[] algos = {
 								jcuda.jcudnn.cudnnConvolutionFwdAlgo.CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_GEMM,
 								jcuda.jcudnn.cudnnConvolutionFwdAlgo.CUDNN_CONVOLUTION_FWD_ALGO_GEMM,
 								jcuda.jcudnn.cudnnConvolutionFwdAlgo.CUDNN_CONVOLUTION_FWD_ALGO_IMPLICIT_PRECOMP_GEMM
 				};
 				// TODO: Look into FFt, Winograd, etc
 				// Also ensure that GPU has enough memory to allocate memory
-				long sizeInBytesArray[] = { 0 };
-				algo = jcuda.jcudnn.JCudnn.cudnnGetConvolutionForwardAlgorithm(cudnnHandle, srcTensorDesc, filterDesc, convDesc, dstTensorDesc,
+				long sizeInBytesArray[] = {0};
+				algo = jcuda.jcudnn.JCudnn.cudnnGetConvolutionForwardAlgorithm(getCudnnHandle(gCtx), srcTensorDesc, filterDesc, convDesc, dstTensorDesc,
 								CONVOLUTION_PREFERENCE, sizeInBytesArray[0], algos);
-				cudnnGetConvolutionForwardWorkspaceSize(cudnnHandle, srcTensorDesc, filterDesc, convDesc, dstTensorDesc, algo, sizeInBytesArray);
-				if(sizeInBytesArray[0] != 0)
-					workSpace = allocate(sizeInBytesArray[0]);
+				cudnnGetConvolutionForwardWorkspaceSize(getCudnnHandle(gCtx), srcTensorDesc, filterDesc, convDesc, dstTensorDesc, algo, sizeInBytesArray);
+				if (sizeInBytesArray[0] != 0)
+					workSpace = gCtx.allocate(sizeInBytesArray[0]);
 				sizeInBytes = sizeInBytesArray[0];
-			}
-			else if(CONVOLUTION_PREFERENCE == cudnnConvolutionFwdPreference.CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT) {
+			} else if (CONVOLUTION_PREFERENCE == cudnnConvolutionFwdPreference.CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT) {
 				throw new DMLRuntimeException("CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT is not implemented");
-			}
-			else {
+			} else {
 				throw new DMLRuntimeException("Unsupported preference criteria for convolution");
 			}
-			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CUDNN_INIT, System.nanoTime() - t1);
+			if (GPUStatistics.DISPLAY_STATISTICS)
+				GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CUDNN_INIT, System.nanoTime() - t1);
 			if (GPUStatistics.DISPLAY_STATISTICS) t2 = System.nanoTime();
-			int status = cudnnConvolutionForward(cudnnHandle, one(),
-							srcTensorDesc, imagePointer,
-							filterDesc, filterPointer,
+			int status = cudnnConvolutionForward(getCudnnHandle(gCtx), one(),
+							srcTensorDesc, image,
+							filterDesc, filter,
 							convDesc, algo, workSpace, sizeInBytes, zero(),
-							dstTensorDesc, dstPointer);
-			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CONVOLUTION_FORWARD_LIB, System.nanoTime() - t2);
-			if(status != jcuda.jcudnn.cudnnStatus.CUDNN_STATUS_SUCCESS) {
-				throw new DMLRuntimeException("Could not executed cudnnConvolutionForward: " + jcuda.jcudnn.cudnnStatus.stringFor(status));
+							dstTensorDesc, output);
+			if (GPUStatistics.DISPLAY_STATISTICS)
+				GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CONVOLUTION_FORWARD_LIB, System.nanoTime() - t2);
+			if (status != cudnnStatus.CUDNN_STATUS_SUCCESS) {
+				throw new DMLRuntimeException("Could not executed cudnnConvolutionForward: " + cudnnStatus.stringFor(status));
 			}
-		}
-		finally {
-			long t3=0;
+		} catch (CudaException e) {
+			throw new DMLRuntimeException("Error in conv2d in GPUContext " + gCtx.toString() + " from Thread " + Thread.currentThread().toString(), e);
+		} finally {
+			long t3 = 0;
 			if (GPUStatistics.DISPLAY_STATISTICS) t3 = System.nanoTime();
-			if(filterDesc != null)
+			if (filterDesc != null)
 				cudnnDestroyFilterDescriptor(filterDesc);
-			if(convDesc != null)
+			if (convDesc != null)
 				cudnnDestroyConvolutionDescriptor(convDesc);
-			if(workSpace != null && sizeInBytes != 0)
-				cudaFreeHelper(instName, workSpace);
-			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CUDNN_CLEANUP, System.nanoTime() - t3);
+			if (workSpace != null && sizeInBytes != 0)
+				gCtx.cudaFreeHelper(instName, workSpace);
+			if (GPUStatistics.DISPLAY_STATISTICS)
+				GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CUDNN_CLEANUP, System.nanoTime() - t3);
 		}
 	}
 
@@ -381,7 +455,7 @@ public class LibMatrixCUDA {
 		return convDesc;
 	}
 
-	public static  Pointer pointerTo(double value) {
+	private static Pointer pointerTo(double value) {
 		return Pointer.to(new double[] { value });
 	}
 
@@ -411,22 +485,24 @@ public class LibMatrixCUDA {
 
 	/**
 	 * This method computes the backpropagation errors for previous layer of relu operation
+	 * @param gCtx a valid {@link GPUContext}
 	 * @param instName the invoking instruction's name for record {@link Statistics}.
 	 * @param input input image
 	 * @param dout  next layer error propogation
 	 * @param outputBlock output
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public static void reluBackward(String instName, MatrixObject input, MatrixObject dout, MatrixObject outputBlock) throws DMLRuntimeException {
-		long rows = input.getNumRows();
+	public static void reluBackward(GPUContext gCtx, String instName, MatrixObject input, MatrixObject dout, MatrixObject outputBlock) throws DMLRuntimeException {
+        LOG.trace("GPU : reluBackward" + ", GPUContext=" + gCtx);
+        long rows = input.getNumRows();
 		long cols = input.getNumColumns();
-		Pointer imagePointer = getDensePointer(input, instName);
-		Pointer doutPointer = getDensePointer(dout, instName);
-		Pointer outputPointer = getDensePointer(outputBlock, instName);
+		Pointer imagePointer = getDensePointer(gCtx, input, instName);
+		Pointer doutPointer = getDensePointer(gCtx, dout, instName);
+		Pointer outputPointer = getDensePointer(gCtx, outputBlock, instName);
 
 		long t1=0;
 		if (GPUStatistics.DISPLAY_STATISTICS) t1 = System.nanoTime();
-		kernels.launchKernel("relu_backward",
+		getCudaKernels(gCtx).launchKernel("relu_backward",
 						ExecutionConfig.getConfigForSimpleMatrixOperations((int)rows, (int)cols),
 						imagePointer, doutPointer, outputPointer, (int)rows, (int)cols);
 		if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_BIAS_ADD_LIB, System.nanoTime() - t1);
@@ -438,18 +514,20 @@ public class LibMatrixCUDA {
 	 * ones = matrix(1, rows=1, cols=Hout*Wout)
 	 * output = input * matrix(bias %*% ones, rows=1, cols=F*Hout*Wout)
 	 * This operation is often followed by conv2d and hence we have introduced bias_add(input, bias) built-in function
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName the invoking instruction's name for record {@link Statistics}.
 	 * @param input input image
 	 * @param bias bias
 	 * @param outputBlock output
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public static void biasMultiply(String instName, MatrixObject input, MatrixObject bias, MatrixObject outputBlock) throws DMLRuntimeException {
-		if(isInSparseFormat(input)) {
-			((JCudaObject)input.getGPUObject()).sparseToDense(instName);
+	public static void biasMultiply(GPUContext gCtx, String instName, MatrixObject input, MatrixObject bias, MatrixObject outputBlock) throws DMLRuntimeException {
+        LOG.trace("GPU : biasMultiply" + ", GPUContext=" + gCtx);
+        if(isInSparseFormat(gCtx, input)) {
+			input.getGPUObject(gCtx).sparseToDense(instName);
 		}
-		if(isInSparseFormat(bias)) {
-			((JCudaObject)bias.getGPUObject()).sparseToDense(instName);
+		if(isInSparseFormat(gCtx, bias)) {
+			bias.getGPUObject(gCtx).sparseToDense(instName);
 		}
 		long rows = input.getNumRows();
 		long cols = input.getNumColumns();
@@ -458,12 +536,12 @@ public class LibMatrixCUDA {
 		if(bias.getNumColumns() != 1 || cols % K != 0) {
 			throw new DMLRuntimeException("Incorrect inputs for bias_multiply: input[" + rows + " X " + cols + "] and bias[" + K + " X " + bias.getNumColumns() + "]");
 		}
-		Pointer imagePointer = ((JCudaObject)input.getGPUObject()).jcudaDenseMatrixPtr;
-		Pointer biasPointer = ((JCudaObject)bias.getGPUObject()).jcudaDenseMatrixPtr;
-		Pointer outputPointer = ((JCudaObject)outputBlock.getGPUObject()).jcudaDenseMatrixPtr;
+		Pointer imagePointer = input.getGPUObject(gCtx).getJcudaDenseMatrixPtr();
+		Pointer biasPointer = bias.getGPUObject(gCtx).getJcudaDenseMatrixPtr();
+		Pointer outputPointer = outputBlock.getGPUObject(gCtx).getJcudaDenseMatrixPtr();
 		long t1 = 0;
 		if (GPUStatistics.DISPLAY_STATISTICS) t1 = System.nanoTime();
-		kernels.launchKernel("bias_multiply",
+		getCudaKernels(gCtx).launchKernel("bias_multiply",
 						ExecutionConfig.getConfigForSimpleMatrixOperations((int)rows, (int)cols),
 						imagePointer, biasPointer, outputPointer, (int)rows, (int)cols, (int) PQ);
 		if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_RELU_BACKWARD_KERNEL, System.nanoTime() - t1);
@@ -475,32 +553,52 @@ public class LibMatrixCUDA {
 	 * ones = matrix(1, rows=1, cols=Hout*Wout)
 	 * output = input + matrix(bias %*% ones, rows=1, cols=F*Hout*Wout)
 	 * This operation is often followed by conv2d and hence we have introduced bias_add(input, bias) built-in function
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName the invoking instruction's name for record {@link Statistics}.
 	 * @param input input image
 	 * @param bias bias
 	 * @param outputBlock output
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public static void biasAdd(String instName, MatrixObject input, MatrixObject bias, MatrixObject outputBlock) throws DMLRuntimeException {
-		long rows = input.getNumRows();
-		long cols = input.getNumColumns();
-		long K = bias.getNumRows();
-		long PQ = cols / K;
+	public static void biasAdd(GPUContext gCtx, String instName, MatrixObject input, MatrixObject bias, MatrixObject outputBlock) throws DMLRuntimeException {
+		Pointer imagePointer = getDensePointer(gCtx, input, instName);
+		Pointer biasPointer = getDensePointer(gCtx, bias, instName);
+		Pointer outputPointer = getDensePointer(gCtx, outputBlock, instName);
+		int rows = (int) input.getNumRows();
+		int cols = (int) input.getNumColumns();
+		int K = (int) bias.getNumRows();
 		if(bias.getNumColumns() != 1 || cols % K != 0) {
 			throw new DMLRuntimeException("Incorrect inputs for bias_add: input[" + rows + " X " + cols + "] and bias[" + K + " X " + bias.getNumColumns() + "]");
 		}
-		Pointer imagePointer = getDensePointer(input, instName);
-		Pointer biasPointer = getDensePointer(bias, instName);
-		Pointer outputPointer = getDensePointer(outputBlock, instName);
+		biasAdd(gCtx, instName, imagePointer, biasPointer, outputPointer, rows, cols, K);
+	}
+
+	/**
+	 * Performs the operation corresponding to the DML script:
+	 * ones = matrix(1, rows=1, cols=Hout*Wout)
+	 * output = input + matrix(bias %*% ones, rows=1, cols=F*Hout*Wout)
+	 * This operation is often followed by conv2d and hence we have introduced bias_add(input, bias) built-in function
+	 * @param gCtx   a valid {@link GPUContext}
+	 * @param instName the invoking instruction's name for record {@link Statistics}.
+	 * @param image input image
+	 * @param bias bias
+	 * @param output output
+	 * @param rows rows in input image
+	 * @param cols cols in input image
+	 * @param k rows in bias
+	 * @throws DMLRuntimeException
+	 */
+	private static void biasAdd(GPUContext gCtx, String instName, Pointer image, Pointer bias, Pointer output, int rows, int cols, int k) throws DMLRuntimeException {
+        LOG.trace("GPU : biasAdd" + ", GPUContext=" + gCtx);
+        int PQ = cols / k;
 		long t1 = 0;
 		if (GPUStatistics.DISPLAY_STATISTICS) t1 = System.nanoTime();
-		kernels.launchKernel("bias_add",
-						ExecutionConfig.getConfigForSimpleMatrixOperations((int)rows, (int)cols),
-						imagePointer, biasPointer, outputPointer, (int)rows, (int)cols, (int) PQ);
+		getCudaKernels(gCtx).launchKernel("bias_add",
+						ExecutionConfig.getConfigForSimpleMatrixOperations(rows, cols),
+						image, bias, output, rows, cols, PQ);
 		if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_RELU_BACKWARD_KERNEL, System.nanoTime() - t1);
-
 	}
-	
+
 	private static void validateBatchNormalizationDimensions(MatrixObject scale, MatrixObject bias, MatrixObject runningMean, MatrixObject runningVar, int C) throws DMLRuntimeException {
 		if(scale.getNumRows() != 1 || scale.getNumColumns() != C) {
 			throw new DMLRuntimeException("Incorrect dimensions for scale");
@@ -518,7 +616,7 @@ public class LibMatrixCUDA {
 	
 	/**
 	 * Performs the forward BatchNormalization layer computation for inference
-	 * 
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName name of the instruction
 	 * @param image input image
 	 * @param scale scale (as per CuDNN) and gamma as per original paper: shape [1, C, 1, 1]
@@ -529,10 +627,11 @@ public class LibMatrixCUDA {
 	 * @param epsilon epsilon value used in the batch normalization formula
 	 * @throws DMLRuntimeException if error occurs
 	 */
-	public static void batchNormalizationForwardInference(String instName, MatrixObject image, 
+	public static void batchNormalizationForwardInference(GPUContext gCtx, String instName, MatrixObject image,
 			MatrixObject scale, MatrixObject bias, MatrixObject runningMean, MatrixObject runningVar, 
 			MatrixObject ret, double epsilon) throws DMLRuntimeException {
-		int mode = cudnnBatchNormMode.CUDNN_BATCHNORM_SPATIAL;
+        LOG.trace("GPU : batchNormalizationForwardInference" + ", GPUContext=" + gCtx);
+        int mode = cudnnBatchNormMode.CUDNN_BATCHNORM_SPATIAL;
 		
 		int N = (int) image.getNumRows();
 		int C = (int) scale.getNumColumns();
@@ -540,19 +639,19 @@ public class LibMatrixCUDA {
 		validateBatchNormalizationDimensions(scale, bias, runningMean, runningVar, C);
 		
 		// Allocate descriptors
-		cudnnTensorDescriptor nCHWDescriptor = allocateNCHWDescriptors(N, C, CHW, 
+		cudnnTensorDescriptor nCHWDescriptor = allocateNCHWDescriptors(gCtx, N, C, CHW,
 				new MatrixObject[] {image},  new MatrixObject[] {ret});
-		cudnnTensorDescriptor scaleTensorDesc = allocateTensorDescriptor(scale, 1, C, 1, 1);
+		cudnnTensorDescriptor scaleTensorDesc = allocateTensorDescriptor(gCtx, scale, 1, C, 1, 1);
 		
 		// Get underlying dense pointer
-		Pointer imagePtr = getDensePointer(image, true, instName);
-		Pointer retPtr = getDensePointer(ret, true, instName);
-		Pointer biasPtr = getDensePointer(bias, true, instName);
-		Pointer scalePtr = getDensePointer(scale, true, instName);
-		Pointer runningMeanPtr = getDensePointer(runningMean, true, instName);
-		Pointer runningVarPtr = getDensePointer(runningVar, true, instName);
+		Pointer imagePtr = getDensePointer(gCtx, image, true, instName);
+		Pointer retPtr = getDensePointer(gCtx, ret, true, instName);
+		Pointer biasPtr = getDensePointer(gCtx, bias, true, instName);
+		Pointer scalePtr = getDensePointer(gCtx, scale, true, instName);
+		Pointer runningMeanPtr = getDensePointer(gCtx, runningMean, true, instName);
+		Pointer runningVarPtr = getDensePointer(gCtx, runningVar, true, instName);
 		
-		checkStatus(cudnnBatchNormalizationForwardInference(cudnnHandle, mode, one(), zero(),
+		checkStatus(cudnnBatchNormalizationForwardInference(getCudnnHandle(gCtx), mode, one(), zero(),
 				nCHWDescriptor, imagePtr, nCHWDescriptor, retPtr,
 			scaleTensorDesc, scalePtr, biasPtr,
 			runningMeanPtr, runningVarPtr, epsilon));
@@ -560,7 +659,7 @@ public class LibMatrixCUDA {
 	
 	/**
 	 * Performs the forward BatchNormalization layer computation for training
-	 * 
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName name of the instruction
 	 * @param image input image
 	 * @param scale scale (as per CuDNN) and gamma as per original paper: shape [1, C, 1, 1]
@@ -574,10 +673,11 @@ public class LibMatrixCUDA {
 	 * @param exponentialAverageFactor factor used in the moving average computation
 	 * @throws DMLRuntimeException if error occurs
 	 */
-	public static void batchNormalizationForwardTraining(String instName, MatrixObject image, 
+	public static void batchNormalizationForwardTraining(GPUContext gCtx, String instName, MatrixObject image,
 			MatrixObject scale,  MatrixObject bias, MatrixObject runningMean, MatrixObject runningVar, 
 			MatrixObject ret, MatrixObject retRunningMean, MatrixObject retRunningVar, double epsilon, double exponentialAverageFactor) throws DMLRuntimeException {
-		int mode = cudnnBatchNormMode.CUDNN_BATCHNORM_SPATIAL;
+        LOG.trace("GPU : batchNormalizationForwardTraining" + ", GPUContext=" + gCtx);
+        int mode = cudnnBatchNormMode.CUDNN_BATCHNORM_SPATIAL;
 		
 		int N = (int) image.getNumRows();
 		int C = (int) scale.getNumColumns();
@@ -585,26 +685,26 @@ public class LibMatrixCUDA {
 		validateBatchNormalizationDimensions(scale, bias, runningMean, runningVar, C);
 		
 		// Allocate descriptors
-		cudnnTensorDescriptor nCHWDescriptor = allocateNCHWDescriptors(N, C, CHW, 
+		cudnnTensorDescriptor nCHWDescriptor = allocateNCHWDescriptors(gCtx, N, C, CHW,
 				new MatrixObject[] {image},  new MatrixObject[] {ret});
-		cudnnTensorDescriptor scaleTensorDesc = allocateTensorDescriptor(scale, 1, C, 1, 1);
+		cudnnTensorDescriptor scaleTensorDesc = allocateTensorDescriptor(gCtx, scale, 1, C, 1, 1);
 		
 		// Get underlying dense pointer
-		Pointer imagePtr = getDensePointer(image, true, instName);
-		Pointer retPtr = getDensePointer(ret, true, instName);
-		Pointer biasPtr = getDensePointer(bias, true, instName);
-		Pointer scalePtr = getDensePointer(scale, true, instName);
-		Pointer runningMeanPtr = getDensePointer(runningMean, true, instName);
-		Pointer runningVarPtr = getDensePointer(runningVar, true, instName);
+		Pointer imagePtr = getDensePointer(gCtx, image, true, instName);
+		Pointer retPtr = getDensePointer(gCtx, ret, true, instName);
+		Pointer biasPtr = getDensePointer(gCtx, bias, true, instName);
+		Pointer scalePtr = getDensePointer(gCtx, scale, true, instName);
+		Pointer runningMeanPtr = getDensePointer(gCtx, runningMean, true, instName);
+		Pointer runningVarPtr = getDensePointer(gCtx, runningVar, true, instName);
 		
 		// To allow for copy-on-write
-		Pointer retRunningMeanPtr = getDensePointer(retRunningMean, true, instName);
-		Pointer retRunningVarPtr = getDensePointer(retRunningVar, true, instName);
+		Pointer retRunningMeanPtr = getDensePointer(gCtx, retRunningMean, true, instName);
+		Pointer retRunningVarPtr = getDensePointer(gCtx, retRunningVar, true, instName);
 		cudaMemcpy(retRunningMeanPtr, runningMeanPtr, C * Sizeof.DOUBLE, cudaMemcpyDeviceToDevice);
 		cudaMemcpy(retRunningVarPtr, runningVarPtr, C * Sizeof.DOUBLE, cudaMemcpyDeviceToDevice);
 		
 		// ignoring resultSaveMean and resultSaveVariance as it requires state management
-		checkStatus(cudnnBatchNormalizationForwardTraining(cudnnHandle, mode, one(), zero(),
+		checkStatus(cudnnBatchNormalizationForwardTraining(getCudnnHandle(gCtx), mode, one(), zero(),
 				nCHWDescriptor, imagePtr, nCHWDescriptor, retPtr,
 			scaleTensorDesc, scalePtr, biasPtr, exponentialAverageFactor,
 			retRunningMeanPtr, retRunningVarPtr, epsilon, new Pointer(), new Pointer()));
@@ -612,7 +712,7 @@ public class LibMatrixCUDA {
 	
 	/**
 	 * Convenient utility for batch normalization that returns a NCHW descriptor
-	 * 
+	 * @param gCtx a valid {@link GPUContext}
 	 * @param N number of images
 	 * @param C number of channels
 	 * @param CHW channels*height*width
@@ -621,7 +721,7 @@ public class LibMatrixCUDA {
 	 * @return one of the NCHW descriptor
 	 * @throws DMLRuntimeException if error occurs
 	 */
-	private static cudnnTensorDescriptor allocateNCHWDescriptors(int N, int C, long CHW, MatrixObject [] input, MatrixObject [] output) throws DMLRuntimeException {
+	private static cudnnTensorDescriptor allocateNCHWDescriptors(GPUContext gCtx, int N, int C, long CHW, MatrixObject [] input, MatrixObject [] output) throws DMLRuntimeException {
 		cudnnTensorDescriptor ret  = null; // Return any one
 		if(CHW > ((long)Integer.MAX_VALUE)*C) {
 			throw new DMLRuntimeException("image size (height*width) should be less than " + Integer.MAX_VALUE);
@@ -629,9 +729,9 @@ public class LibMatrixCUDA {
 		cudnnTensorDescriptor knownNCHWdescriptor = null;
 		int H = -1; int W = -1;
 		for(int i = 0; i < input.length; i++) {
-			knownNCHWdescriptor = ((JCudaObject)input[i].getGPUObject()).getTensorDescriptor();
+			knownNCHWdescriptor = input[i].getGPUObject(gCtx).getTensorDescriptor();
 			if(knownNCHWdescriptor != null) {
-				int [] shape = ((JCudaObject)input[i].getGPUObject()).getTensorShape();
+				int [] shape = input[i].getGPUObject(gCtx).getTensorShape();
 				if(shape[0] != N || shape[1] != C) {
 					throw new DMLRuntimeException("Incorrect N and C:" + shape[0]  + " != " + N + " || " + shape[1]  + " != " +  C);
 				}
@@ -643,10 +743,10 @@ public class LibMatrixCUDA {
 		if(knownNCHWdescriptor != null) {
 			// We precisely know N, C, H, W
 			for(int i = 0; i < input.length; i++) {
-				ret = allocateTensorDescriptor(input[i], N, C, H, W);
+				ret = allocateTensorDescriptor(gCtx, input[i], N, C, H, W);
 			}
 			for(int i = 0; i < output.length; i++) {
-				ret = allocateTensorDescriptor(output[i], N, C, H, W);
+				ret = allocateTensorDescriptor(gCtx, output[i], N, C, H, W);
 			}
 		}
 		else {
@@ -667,7 +767,7 @@ public class LibMatrixCUDA {
 	
 	/**
 	 * This method computes the backpropagation errors for image, scale and bias of batch normalization layer
-	 * 
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName name of the instruction
 	 * @param image input image
 	 * @param dout input errors of shape C, H, W
@@ -678,30 +778,31 @@ public class LibMatrixCUDA {
 	 * @param epsilon epsilon value used in the batch normalization formula
 	 * @throws DMLRuntimeException if error occurs
 	 */
-	public static void batchNormalizationBackward(String instName, MatrixObject image, MatrixObject dout, 
+	public static void batchNormalizationBackward(GPUContext gCtx, String instName, MatrixObject image, MatrixObject dout,
 			MatrixObject scale, MatrixObject ret, MatrixObject retScale, MatrixObject retBias,
 			double epsilon) throws DMLRuntimeException {
-		int mode = cudnnBatchNormMode.CUDNN_BATCHNORM_SPATIAL;
+        LOG.trace("GPU : batchNormalizationBackward" + ", GPUContext=" + gCtx);
+        int mode = cudnnBatchNormMode.CUDNN_BATCHNORM_SPATIAL;
 		
 		int N = (int) image.getNumRows();
 		int C = (int) scale.getNumColumns();
 		long CHW = image.getNumColumns();
 		
 		// Allocate descriptors
-		cudnnTensorDescriptor nCHWDescriptor = allocateNCHWDescriptors(N, C, CHW, 
+		cudnnTensorDescriptor nCHWDescriptor = allocateNCHWDescriptors(gCtx, N, C, CHW,
 				new MatrixObject[] {image, dout},  new MatrixObject[] {ret});
-		cudnnTensorDescriptor scaleTensorDesc = allocateTensorDescriptor(scale, 1, C, 1, 1);
+		cudnnTensorDescriptor scaleTensorDesc = allocateTensorDescriptor(gCtx, scale, 1, C, 1, 1);
 		
 		// Get underlying dense pointer
-		Pointer imagePtr = getDensePointer(image, true, instName);
-		Pointer doutPtr = getDensePointer(dout, true, instName);
-		Pointer scalePtr = getDensePointer(scale, true, instName);
-		Pointer retPtr = getDensePointer(ret, true, instName);
-		Pointer retScalePtr = getDensePointer(retScale, true, instName);
-		Pointer retBiasPtr = getDensePointer(retBias, true, instName);
+		Pointer imagePtr = getDensePointer(gCtx, image, true, instName);
+		Pointer doutPtr = getDensePointer(gCtx, dout, true, instName);
+		Pointer scalePtr = getDensePointer(gCtx, scale, true, instName);
+		Pointer retPtr = getDensePointer(gCtx, ret, true, instName);
+		Pointer retScalePtr = getDensePointer(gCtx, retScale, true, instName);
+		Pointer retBiasPtr = getDensePointer(gCtx, retBias, true, instName);
 		
 		// ignoring resultSaveMean and resultSaveVariance as it requires state management
-		checkStatus(cudnnBatchNormalizationBackward(cudnnHandle, mode,  one(), zero(), one(), zero(),
+		checkStatus(cudnnBatchNormalizationBackward(getCudnnHandle(gCtx), mode,  one(), zero(), one(), zero(),
 				nCHWDescriptor,  imagePtr, nCHWDescriptor, doutPtr, nCHWDescriptor, retPtr,
 				scaleTensorDesc, scalePtr, retScalePtr, retBiasPtr, epsilon, new Pointer(), new Pointer()));
 	}
@@ -709,7 +810,7 @@ public class LibMatrixCUDA {
 
 	/**
 	 * This method computes the backpropogation errors for filter of convolution operation
-	 * 
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName the invoking instruction's name for record {@link Statistics}.
 	 * @param image input image
 	 * @param dout errors from next layer
@@ -729,56 +830,60 @@ public class LibMatrixCUDA {
 	 * @param Q output activation width
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public static void conv2dBackwardFilter(String instName, MatrixObject image, MatrixObject dout,
+	public static void conv2dBackwardFilter(GPUContext gCtx, String instName, MatrixObject image, MatrixObject dout,
 																					MatrixObject outputBlock, int N, int C, int H, int W, int K, int R,
 																					int S, int pad_h, int pad_w, int stride_h, int stride_w, int P,
 																					int Q) throws DMLRuntimeException {
-		cudnnFilterDescriptor dwDesc = null;
+        LOG.trace("GPU : conv2dBackwardFilter" + ", GPUContext=" + gCtx);
+        cudnnFilterDescriptor dwDesc = null;
 		cudnnConvolutionDescriptor convDesc = null;
 
 		Pointer workSpace = null;
 		long sizeInBytes = 0;
 		try {
 
-			long t1=0, t2=0;
+			long t1 = 0, t2 = 0;
 			if (GPUStatistics.DISPLAY_STATISTICS) t1 = System.nanoTime();
 			// Allocate descriptors
-			cudnnTensorDescriptor xTensorDesc = allocateTensorDescriptor(image, N, C, H, W);
-			cudnnTensorDescriptor doutTensorDesc = allocateTensorDescriptor(dout, N, K, P, Q);
+			cudnnTensorDescriptor xTensorDesc = allocateTensorDescriptor(gCtx, image, N, C, H, W);
+			cudnnTensorDescriptor doutTensorDesc = allocateTensorDescriptor(gCtx, dout, N, K, P, Q);
 			dwDesc = allocateFilterDescriptor(K, C, R, S);
 
 			// Allocate data
-			Pointer imagePointer = getDensePointer(image, true, instName);
-			Pointer doutPointer = getDensePointer(dout, true, instName);
-			Pointer dwPointer = getDensePointer(outputBlock, true, instName);
-			int padding [] = { pad_h, pad_w };
-			int strides [] = { stride_h, stride_w };
+			Pointer imagePointer = getDensePointer(gCtx, image, true, instName);
+			Pointer doutPointer = getDensePointer(gCtx, dout, true, instName);
+			Pointer dwPointer = getDensePointer(gCtx, outputBlock, true, instName);
+			int padding[] = {pad_h, pad_w};
+			int strides[] = {stride_h, stride_w};
 			convDesc = allocateConvolutionDescriptor(padding, strides);
-			long sizeInBytesArray[] = { 0 };
+			long sizeInBytesArray[] = {0};
 
 			// TODO: Select the best algorithm depending on the data and supported CUDA
 			int algo = jcuda.jcudnn.cudnnConvolutionBwdFilterAlgo.CUDNN_CONVOLUTION_BWD_FILTER_ALGO_0;
 
 			workSpace = new Pointer();
-			cudnnGetConvolutionBackwardFilterWorkspaceSize(cudnnHandle,
+			cudnnGetConvolutionBackwardFilterWorkspaceSize(getCudnnHandle(gCtx),
 							xTensorDesc, doutTensorDesc, convDesc, dwDesc, algo, sizeInBytesArray);
-			if (GPUStatistics.DISPLAY_STATISTICS)GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CUDNN_INIT, System.nanoTime() - t1);
+			if (GPUStatistics.DISPLAY_STATISTICS)
+				GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CUDNN_INIT, System.nanoTime() - t1);
 
 			if (GPUStatistics.DISPLAY_STATISTICS) t2 = System.nanoTime();
-			int status = cudnnConvolutionBackwardFilter(cudnnHandle, one(), xTensorDesc, imagePointer,
+			int status = cudnnConvolutionBackwardFilter(getCudnnHandle(gCtx), one(), xTensorDesc, imagePointer,
 							doutTensorDesc, doutPointer, convDesc, algo, workSpace, sizeInBytes, zero(), dwDesc, dwPointer);
-			if (GPUStatistics.DISPLAY_STATISTICS)GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CONVOLUTION_BACKWARD_FILTER_LIB, System.nanoTime() - t2);
+			if (GPUStatistics.DISPLAY_STATISTICS)
+				GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CONVOLUTION_BACKWARD_FILTER_LIB, System.nanoTime() - t2);
 
-			if(status != jcuda.jcudnn.cudnnStatus.CUDNN_STATUS_SUCCESS) {
+			if (status != jcuda.jcudnn.cudnnStatus.CUDNN_STATUS_SUCCESS) {
 				throw new DMLRuntimeException("Could not executed cudnnConvolutionBackwardFilter: " + jcuda.jcudnn.cudnnStatus.stringFor(status));
 			}
-		}
-		finally {
+		} catch (CudaException e) {
+				throw new DMLRuntimeException("Error in conv2d in GPUContext " + gCtx.toString() + " from Thread " + Thread.currentThread().toString(), e);
+		} finally {
 			long t3=0;
 			if (GPUStatistics.DISPLAY_STATISTICS) t3 = System.nanoTime();
 
 			if(workSpace != null && sizeInBytes != 0)
-				cudaFreeHelper(instName, workSpace);
+				gCtx.cudaFreeHelper(instName, workSpace);
 			if(dwDesc != null)
 				cudnnDestroyFilterDescriptor(dwDesc);
 
@@ -792,6 +897,7 @@ public class LibMatrixCUDA {
 
 	/**
 	 * This method computes the backpropogation errors for previous layer of convolution operation
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName the invoking instruction's name for record {@link Statistics}.
 	 * @param filter filter used in conv2d
 	 * @param dout errors from next layer
@@ -811,11 +917,12 @@ public class LibMatrixCUDA {
 	 * @param Q output activation width
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public static void conv2dBackwardData(String instName, MatrixObject filter, MatrixObject dout,
+	public static void conv2dBackwardData(GPUContext gCtx, String instName, MatrixObject filter, MatrixObject dout,
 																				MatrixObject output, int N, int C, int H, int W, int K, int R,
 																				int S, int pad_h, int pad_w, int stride_h, int stride_w, int P,
 																				int Q) throws DMLRuntimeException {
-		cudnnFilterDescriptor wDesc = null;
+        LOG.trace("GPU : conv2dBackwardData" + ", GPUContext=" + gCtx);
+        cudnnFilterDescriptor wDesc = null;
 		cudnnConvolutionDescriptor convDesc = null;
 
 		Pointer workSpace = null;
@@ -825,13 +932,13 @@ public class LibMatrixCUDA {
 			if (GPUStatistics.DISPLAY_STATISTICS) t1 = System.nanoTime();
 			// Allocate descriptors
 			wDesc = allocateFilterDescriptor(K, C, R, S);
-			cudnnTensorDescriptor dyDesc = allocateTensorDescriptor(dout, N, K, P, Q);
-			cudnnTensorDescriptor dxDesc = allocateTensorDescriptor(output, N, C, H, W);
+			cudnnTensorDescriptor dyDesc = allocateTensorDescriptor(gCtx, dout, N, K, P, Q);
+			cudnnTensorDescriptor dxDesc = allocateTensorDescriptor(gCtx, output, N, C, H, W);
 
 			// Allocate data
-			Pointer w = getDensePointer(filter, true, instName);
-			Pointer dy = getDensePointer(dout, true, instName);
-			Pointer dx = getDensePointer(output, true, instName);
+			Pointer w = getDensePointer(gCtx, filter, true, instName);
+			Pointer dy = getDensePointer(gCtx, dout, true, instName);
+			Pointer dx = getDensePointer(gCtx, output, true, instName);
 			
 			int padding [] = { pad_h, pad_w };
 			int strides [] = { stride_h, stride_w };
@@ -841,25 +948,27 @@ public class LibMatrixCUDA {
 			// TODO: Select the best algorithm depending on the data and supported CUDA
 			int algo = jcuda.jcudnn.cudnnConvolutionBwdDataAlgo.CUDNN_CONVOLUTION_BWD_DATA_ALGO_0;
 			workSpace = new Pointer();
-			cudnnGetConvolutionBackwardDataWorkspaceSize(cudnnHandle,
+			cudnnGetConvolutionBackwardDataWorkspaceSize(getCudnnHandle(gCtx),
 							wDesc, dyDesc, convDesc, dxDesc, algo, sizeInBytesArray);
 			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CUDNN_INIT, System.nanoTime() - t1);
 
 			if (GPUStatistics.DISPLAY_STATISTICS) t2 = System.nanoTime();
-			int status = cudnnConvolutionBackwardData(cudnnHandle, one(), wDesc, w,
+			int status = cudnnConvolutionBackwardData(getCudnnHandle(gCtx), one(), wDesc, w,
 							dyDesc, dy, convDesc, algo, workSpace, sizeInBytes, zero(), dxDesc, dx);
 			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CONVOLUTION_BACKWARD_DATA_LIB, System.nanoTime() - t2);
 
 			if(status != jcuda.jcudnn.cudnnStatus.CUDNN_STATUS_SUCCESS) {
 				throw new DMLRuntimeException("Could not executed cudnnConvolutionBackwardData: " + jcuda.jcudnn.cudnnStatus.stringFor(status));
 			}
+		} catch (CudaException e) {
+			throw new DMLRuntimeException("Error in conv2d in GPUContext " + gCtx.toString() + " from Thread " + Thread.currentThread().toString(), e);
 		}
 		finally {
 			long t3=0;
 			if (GPUStatistics.DISPLAY_STATISTICS) t3 = System.nanoTime();
 
 			if(workSpace != null && sizeInBytes != 0)
-				cudaFreeHelper(instName, workSpace);
+				gCtx.cudaFreeHelper(instName, workSpace);
 			if(wDesc != null)
 				cudnnDestroyFilterDescriptor(wDesc);
 			if(convDesc != null)
@@ -871,6 +980,7 @@ public class LibMatrixCUDA {
 	
 	/**
 	 * performs maxpooling on GPU by exploiting cudnnPoolingForward(...)
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName the invoking instruction's name for record {@link Statistics}.
 	 * @param image image as matrix object
 	 * @param outputBlock output matrix
@@ -889,37 +999,40 @@ public class LibMatrixCUDA {
 	 * @param Q				(W - S + 1 + 2*pad_w)/stride_w
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public static void maxpooling(String instName, MatrixObject image,
+	public static void maxpooling(GPUContext gCtx, String instName, MatrixObject image,
 			MatrixObject outputBlock, int N, int C, int H, int W, int K, int R,
 			int S, int pad_h, int pad_w, int stride_h, int stride_w, int P,
 			int Q) throws DMLRuntimeException {
-		Pointer x = getDensePointer(image, true, instName);
-		cudnnTensorDescriptor xDesc = allocateTensorDescriptor(image, N, C, H, W);
-		performMaxpooling(instName, x, xDesc, outputBlock, N, C, H, W, K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
+        Pointer x = getDensePointer(gCtx, image, true, instName);
+		cudnnTensorDescriptor xDesc = allocateTensorDescriptor(gCtx, image, N, C, H, W);
+		performMaxpooling(gCtx, instName, x, xDesc, outputBlock, N, C, H, W, K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
 	}
 	
-	public static void performMaxpooling(String instName, Pointer x, cudnnTensorDescriptor xDesc,
+	public static void performMaxpooling(GPUContext gCtx, String instName, Pointer x, cudnnTensorDescriptor xDesc,
 			MatrixObject outputBlock, int N, int C, int H, int W, int K, int R,
 			int S, int pad_h, int pad_w, int stride_h, int stride_w, int P,
 			int Q) throws DMLRuntimeException {
-		Pointer y = getDensePointer(outputBlock, true, instName);
+        LOG.trace("GPU : performMaxpooling" + ", GPUContext=" + gCtx);
+        Pointer y = getDensePointer(gCtx, outputBlock, true, instName);
 		cudnnPoolingDescriptor poolingDesc = null;
 
 		try {
 			long t1=0,t2=0;
 			if (GPUStatistics.DISPLAY_STATISTICS) t1 = System.nanoTime();
 			// Allocate descriptors
-			cudnnTensorDescriptor yDesc = allocateTensorDescriptor(outputBlock, N, C, P, Q);
+			cudnnTensorDescriptor yDesc = allocateTensorDescriptor(gCtx, outputBlock, N, C, P, Q);
 			poolingDesc = allocatePoolingDescriptor(R, S, pad_h, pad_w, stride_h, stride_w);
 			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CUDNN_INIT, System.nanoTime() - t1);
 
 			if (GPUStatistics.DISPLAY_STATISTICS) t2 = System.nanoTime();
-			int status = cudnnPoolingForward(cudnnHandle, poolingDesc, one(), xDesc, x, zero(), yDesc, y);
+			int status = cudnnPoolingForward(getCudnnHandle(gCtx), poolingDesc, one(), xDesc, x, zero(), yDesc, y);
 			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_MAXPOOLING_FORWARD_LIB, System.nanoTime() - t2);
 
 			if(status != jcuda.jcudnn.cudnnStatus.CUDNN_STATUS_SUCCESS) {
 				throw new DMLRuntimeException("Could not executed cudnnPoolingForward: " + jcuda.jcudnn.cudnnStatus.stringFor(status));
 			}
+		} catch (CudaException e) {
+			throw new DMLRuntimeException("Error in conv2d in GPUContext " + gCtx.toString() + " from Thread " + Thread.currentThread().toString(), e);
 		}
 		finally {
 			long t3=0;
@@ -932,6 +1045,7 @@ public class LibMatrixCUDA {
 	
 	/**
 	 * performs relu followed by maxpooling on GPU by exploiting cudnnPoolingForward(...)
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName the invoking instruction's name for record {@link Statistics}.
 	 * @param image image as matrix object
 	 * @param outputBlock output matrix
@@ -950,22 +1064,24 @@ public class LibMatrixCUDA {
 	 * @param Q				(W - S + 1 + 2*pad_w)/stride_w
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public static void reluMaxpooling(String instName, MatrixObject image,
+	public static void reluMaxpooling(GPUContext gCtx, String instName, MatrixObject image,
 			MatrixObject outputBlock, int N, int C, int H, int W, int K, int R,
 			int S, int pad_h, int pad_w, int stride_h, int stride_w, int P,
 			int Q) throws DMLRuntimeException {
-		cudnnTensorDescriptor srcTensorDesc = allocateTensorDescriptor(image, N, C, H, W);
+        LOG.trace("GPU : reluMaxpooling" + ", GPUContext=" + gCtx);
+        cudnnTensorDescriptor srcTensorDesc = allocateTensorDescriptor(gCtx, image, N, C, H, W);
 		long size  = image.getNumRows() * image.getNumColumns() * Sizeof.DOUBLE;
-		Pointer tmp = allocate(size);
-		performCuDNNReLU(instName, image, tmp, srcTensorDesc);
-		cudaDeviceSynchronize(); // It seemed like the cudnn operation in performCuDNNReLU was being done aysnchronously, this adds the neccesary sync
-		performMaxpooling(instName, tmp, srcTensorDesc, outputBlock, N, C, H, W, K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
-		cudaFreeHelper(tmp);
+		Pointer tmp = gCtx.allocate(size);
+		performCuDNNReLU(gCtx, instName, image, tmp, srcTensorDesc);
+		//cudaDeviceSynchronize; // It seemed like the cudnn operation in performCuDNNReLU was being done aysnchronously, this adds the neccesary sync
+		performMaxpooling(gCtx, instName, tmp, srcTensorDesc, outputBlock, N, C, H, W, K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
+		gCtx.cudaFreeHelper(tmp);
 	}
 
 	/**
 	 * Performs maxpoolingBackward on GPU by exploiting cudnnPoolingBackward(...)
 	 * This method computes the backpropogation errors for previous layer of maxpooling operation
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName the invoking instruction's name for record {@link Statistics}.
 	 * @param image image as matrix object
 	 * @param dout			delta matrix, output of previous layer
@@ -985,38 +1101,39 @@ public class LibMatrixCUDA {
 	 * @param Q				(W - S + 1 + 2*pad_w)/stride_w
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public static void maxpoolingBackward(String instName, MatrixObject image, MatrixObject dout,
+	public static void maxpoolingBackward(GPUContext gCtx, String instName, MatrixObject image, MatrixObject dout,
 																				MatrixObject outputBlock, int N, int C, int H, int W, int K, int R,
 																				int S, int pad_h, int pad_w, int stride_h, int stride_w, int P,
 																				int Q) throws DMLRuntimeException {
-		Pointer y = null;
+        LOG.trace("GPU : maxpoolingBackward" + ", GPUContext=" + gCtx);
+        Pointer y = null;
 		cudnnPoolingDescriptor poolingDesc = null;
 
 		try {
 			long t1=0, t2=0, t3=0;
 			if (GPUStatistics.DISPLAY_STATISTICS) t1 = System.nanoTime();
 			// Allocate descriptors
-			cudnnTensorDescriptor xDesc = allocateTensorDescriptor(image, N, C, H, W);
-			cudnnTensorDescriptor yDesc = allocateTensorDescriptor(dout, N, C, P, Q);
-			cudnnTensorDescriptor dxDesc = allocateTensorDescriptor(outputBlock, N, C, H, W);
-			cudnnTensorDescriptor dyDesc = allocateTensorDescriptor(dout, N, C, P, Q);
+			cudnnTensorDescriptor xDesc = allocateTensorDescriptor(gCtx, image, N, C, H, W);
+			cudnnTensorDescriptor yDesc = allocateTensorDescriptor(gCtx, dout, N, C, P, Q);
+			cudnnTensorDescriptor dxDesc = allocateTensorDescriptor(gCtx, outputBlock, N, C, H, W);
+			cudnnTensorDescriptor dyDesc = allocateTensorDescriptor(gCtx, dout, N, C, P, Q);
 
 			poolingDesc = allocatePoolingDescriptor(R, S, pad_h, pad_w, stride_h, stride_w);
 
 			// Calling PoolForward first, y is one of the inputs for poolBackward
 			// TODO: Remove calling poolForward after necessary changes at language level for poolBackward
 			long numBytes = N*C*P*Q*Sizeof.DOUBLE;
-			y = allocate(numBytes);
+			y = gCtx.allocate(numBytes);
 
 			// Allocate data
-			Pointer x = getDensePointer(image, true, instName);
-			Pointer dx = getDensePointer(outputBlock, true, instName);
-			Pointer dy = getDensePointer(dout, true, instName);
+			Pointer x = getDensePointer(gCtx, image, true, instName);
+			Pointer dx = getDensePointer(gCtx, outputBlock, true, instName);
+			Pointer dy = getDensePointer(gCtx, dout, true, instName);
 
 			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CUDNN_INIT, System.nanoTime() - t1);
 
 			if (GPUStatistics.DISPLAY_STATISTICS) t2 = System.nanoTime();
-			int status = cudnnPoolingForward(cudnnHandle, poolingDesc, one(), xDesc, x, zero(), yDesc, y);
+			int status = cudnnPoolingForward(getCudnnHandle(gCtx), poolingDesc, one(), xDesc, x, zero(), yDesc, y);
 			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_MAXPOOLING_FORWARD_LIB, System.nanoTime() - t2);
 
 			if(status != jcuda.jcudnn.cudnnStatus.CUDNN_STATUS_SUCCESS) {
@@ -1024,19 +1141,21 @@ public class LibMatrixCUDA {
 			}
 
 			if (GPUStatistics.DISPLAY_STATISTICS) t3 = System.nanoTime();
-			status = cudnnPoolingBackward(cudnnHandle, poolingDesc, one(), yDesc, y, dyDesc, dy, xDesc, x, zero(), dxDesc, dx);
+			status = cudnnPoolingBackward(getCudnnHandle(gCtx), poolingDesc, one(), yDesc, y, dyDesc, dy, xDesc, x, zero(), dxDesc, dx);
 			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_MAXPOOLING_BACKWARD_LIB, System.nanoTime() - t3);
 
 			if(status != jcuda.jcudnn.cudnnStatus.CUDNN_STATUS_SUCCESS) {
 				throw new DMLRuntimeException("Could not executed cudnnPoolingBackward: " + jcuda.jcudnn.cudnnStatus.stringFor(status));
 			}
+		} catch (CudaException e) {
+			throw new DMLRuntimeException("Error in conv2d in GPUContext " + gCtx.toString() + " from Thread " + Thread.currentThread().toString(), e);
 		}
 		finally {
 			long t4=0;
 			if (GPUStatistics.DISPLAY_STATISTICS) t4 = System.nanoTime();
 
 			if(y != null)
-				cudaFreeHelper(instName, y);
+				gCtx.cudaFreeHelper(instName, y);
 			if(poolingDesc != null)
 				cudnnDestroyPoolingDescriptor(poolingDesc);
 
@@ -1044,21 +1163,24 @@ public class LibMatrixCUDA {
 		}
 	}
 	
-	private static void performCuDNNReLU(String instName, MatrixObject in, Pointer dstData, cudnnTensorDescriptor srcTensorDesc) throws DMLRuntimeException {
+	private static void performCuDNNReLU(GPUContext gCtx, String instName, MatrixObject in, Pointer dstData, cudnnTensorDescriptor srcTensorDesc) throws DMLRuntimeException {
 		long t0=0;
 		try {
-			cudnnTensorDescriptor dstTensorDesc = srcTensorDesc;
+            LOG.trace("GPU : performCuDNNReLU" + ", GPUContext=" + gCtx);
+            cudnnTensorDescriptor dstTensorDesc = srcTensorDesc;
 			
-			Pointer srcData = getDensePointer(in, true, instName);
+			Pointer srcData = getDensePointer(gCtx, in, true, instName);
 			cudnnActivationDescriptor activationDescriptor = new cudnnActivationDescriptor();
 			cudnnCreateActivationDescriptor(activationDescriptor);
 			double dummy = -1;
 			cudnnSetActivationDescriptor(activationDescriptor, CUDNN_ACTIVATION_RELU, CUDNN_PROPAGATE_NAN, dummy);
 			if (GPUStatistics.DISPLAY_STATISTICS) t0 = System.nanoTime();
-			cudnnActivationForward(cudnnHandle, activationDescriptor,
+			cudnnActivationForward(getCudnnHandle(gCtx), activationDescriptor,
 							one(), srcTensorDesc, srcData,
 							zero(), dstTensorDesc, dstData);
 			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_ACTIVATION_FORWARD_LIB, System.nanoTime() - t0);
+		} catch (CudaException e) {
+			throw new DMLRuntimeException("Error in conv2d in GPUContext " + gCtx.toString() + " from Thread " + Thread.currentThread().toString(), e);
 		}
 		finally {
 			long t1=0;
@@ -1071,30 +1193,34 @@ public class LibMatrixCUDA {
 	/**
 	 * Performs the relu operation on the GPU.
 	 * @param ec currently active {@link ExecutionContext}
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName the invoking instruction's name for record {@link Statistics}.
 	 * @param in input matrix
 	 * @param outputName	name of the output matrix
 	 * @throws DMLRuntimeException	if an error occurs
 	 */
-	public static void relu(ExecutionContext ec, String instName, MatrixObject in, String outputName) throws DMLRuntimeException {
+	public static void relu(ExecutionContext ec, GPUContext gCtx, String instName, MatrixObject in, String outputName) throws DMLRuntimeException {
+		if (ec.getGPUContext() != gCtx)
+			throw new DMLRuntimeException("GPU : Invalid internal state, the GPUContext set with the ExecutionContext is not the same used to run this LibMatrixCUDA function");
 		long N = in.getNumRows();
 		long CHW = in.getNumColumns();
 		MatrixObject output = ec.getMatrixObject(outputName);
 		getDenseMatrixOutputForGPUInstruction(ec, instName, outputName);	// Allocated the dense output matrix
 		long t0=0;
-		cudnnTensorDescriptor srcTensorDesc = ((JCudaObject)in.getGPUObject()).getTensorDescriptor();
+		cudnnTensorDescriptor srcTensorDesc = in.getGPUObject(gCtx).getTensorDescriptor();
 		if(N*CHW >= numDoublesIn2GB ||  srcTensorDesc == null) {
-			// Invokes relu(double* A,  double* ret, int rlen, int clen)
+            LOG.trace("GPU : relu custom kernel" + ", GPUContext=" + gCtx);
+            // Invokes relu(double* A,  double* ret, int rlen, int clen)
 			if (GPUStatistics.DISPLAY_STATISTICS) t0 = System.nanoTime();
-			Pointer dstData = getDensePointer(output, instName);
-			Pointer srcData = getDensePointer(in, instName); // TODO: FIXME: Add sparse kernel support for relu
-			kernels.launchKernel("relu",
+			Pointer dstData = getDensePointer(gCtx, output, instName);
+			Pointer srcData = getDensePointer(gCtx, in, instName); // TODO: FIXME: Add sparse kernel support for relu
+			getCudaKernels(gCtx).launchKernel("relu",
 							ExecutionConfig.getConfigForSimpleMatrixOperations((int)N, (int)CHW),
 							srcData, dstData, (int)N, (int) CHW);
 			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_RELU_KERNEL, System.nanoTime() - t0);
 		}
 		else {
-			performCuDNNReLU(instName, in, getDensePointer(output, true, instName), srcTensorDesc);
+			performCuDNNReLU(gCtx, instName, in, getDensePointer(gCtx, output, true, instName), srcTensorDesc);
 		}
 	}
 
@@ -1114,17 +1240,21 @@ public class LibMatrixCUDA {
 	 * Performs tsmm, A %*% A' or A' %*% A, on GPU by exploiting cublasDsyrk(...)
 	 *
 	 * @param ec execution context
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName the invoking instruction's name for record {@link Statistics}.
 	 * @param left input matrix, as in a tsmm expression like A %*% A' or A' %*% A, we just need to check whether the left one is transposed or not, I named it 'left'
 	 * @param outputName output matrix name
 	 * @param isLeftTransposed if true, left transposed
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public static void matmultTSMM(ExecutionContext ec, String instName, MatrixObject left, String outputName,
+	public static void matmultTSMM(ExecutionContext ec, GPUContext gCtx, String instName, MatrixObject left, String outputName,
 																 boolean isLeftTransposed) throws DMLRuntimeException {
-		if(isInSparseFormat(left)) {
+		LOG.trace("GPU : matmultTSMM" + ", GPUContext=" + gCtx);
+		if (ec.getGPUContext() != gCtx)
+			throw new DMLRuntimeException("GPU : Invalid internal state, the GPUContext set with the ExecutionContext is not the same used to run this LibMatrixCUDA function");
+		if(isInSparseFormat(gCtx, left)) {
 			// For sparse TSMM, invoke matmult (TODO: possible performance improvement)
-			matmult(ec, instName, left, left, outputName, isLeftTransposed, !isLeftTransposed);
+			matmult(ec, gCtx, instName, left, left, outputName, isLeftTransposed, !isLeftTransposed);
 			return;
 		}
 
@@ -1145,22 +1275,22 @@ public class LibMatrixCUDA {
 		int lda = (int) (isLeftTransposed ? m : k);
 		int ldc = m;
 
-		if(!left.getGPUObject().isAllocated())
-			throw new DMLRuntimeException("Input is not allocated:" + left.getGPUObject().isAllocated());
-		if(!output.getGPUObject().isAllocated())
-			throw new DMLRuntimeException("Output is not allocated:" + output.getGPUObject().isAllocated());
+		if(!left.getGPUObject(gCtx).isAllocated())
+			throw new DMLRuntimeException("Input is not allocated:" + left.getGPUObject(gCtx).isAllocated());
+		if(!output.getGPUObject(gCtx).isAllocated())
+			throw new DMLRuntimeException("Output is not allocated:" + output.getGPUObject(gCtx).isAllocated());
 
-		Pointer A = getDensePointer(left, instName);
-		Pointer C = getDensePointer(output, instName);
+		Pointer A = getDensePointer(gCtx, left, instName);
+		Pointer C = getDensePointer(gCtx, output, instName);
 
 		long t0=0, t1=0;
 
 		if (GPUStatistics.DISPLAY_STATISTICS) t0 = System.nanoTime();
-		JCublas2.cublasDsyrk(cublasHandle, cublasFillMode.CUBLAS_FILL_MODE_LOWER,transa, m, k, one(), A, lda, zero(), C, ldc);
+		JCublas2.cublasDsyrk(getCublasHandle(gCtx), cublasFillMode.CUBLAS_FILL_MODE_LOWER,transa, m, k, one(), A, lda, zero(), C, ldc);
 		if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_SYRK_LIB, System.nanoTime() - t0);
 
 		if (GPUStatistics.DISPLAY_STATISTICS) t1 = System.nanoTime();
-		copyUpperToLowerTriangle(instName, output);
+		copyUpperToLowerTriangle(gCtx, instName, output);
 		if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_UPPER_TO_LOWER_TRIANGLE_KERNEL, System.nanoTime() - t1);
 	}
 
@@ -1168,22 +1298,23 @@ public class LibMatrixCUDA {
 	 * Used for all version of TSMM where the result is known to be symmetric.
 	 * Hence, we compute only the upper triangular matrix and copy this partial
 	 * result down to lower triangular matrix once.
-	 *
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName instruction name
 	 * @param ret upper triangular matrix
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	private static void copyUpperToLowerTriangle(String instName, MatrixObject ret) throws DMLRuntimeException {
-		if(isInSparseFormat(ret)) {
+	private static void copyUpperToLowerTriangle(GPUContext gCtx, String instName, MatrixObject ret) throws DMLRuntimeException {
+        LOG.trace("GPU : copyUpperToLowerTriangle" + ", GPUContext=" + gCtx);
+        if(isInSparseFormat(gCtx, ret)) {
 			throw new DMLRuntimeException("Sparse GPU copyUpperToLowerTriangle is not implemented");
 		}
 		if(ret.getNumRows() != ret.getNumColumns()) {
 			throw new DMLRuntimeException("Only square matrix kernel is implemented for copyUpperToLowerTriangle");
 		}
 		int dim = (int) ret.getNumRows();
-		kernels.launchKernel("copy_u2l_dense",
+		getCudaKernels(gCtx).launchKernel("copy_u2l_dense",
 						ExecutionConfig.getConfigForSimpleMatrixOperations(dim, dim),
-						getDensePointer(ret, instName), dim, dim*dim);
+						getDensePointer(gCtx, ret, instName), dim, dim*dim);
 	}
 
 
@@ -1201,40 +1332,43 @@ public class LibMatrixCUDA {
 	 * Examines sparsity and shapes and routes call to appropriate method
 	 * from cuBLAS or cuSparse
 	 * C = op(A) x op(B)
-	 * @param ec									Current {@link ExecutionContext} instance
-	 * @param instName name of the invoking instruction to record{@link Statistics}.
-	 * @param left1								Matrix A
-	 * @param right1							Matrix B
-	 * @param outputName					Name of the output matrix C (in code generated after LOP layer)
-	 * @param isLeftTransposed1		op for A, transposed or not
-	 * @param isRightTransposed1	op for B, tranposed or not
+	 * @param ec                    Current {@link ExecutionContext} instance
+	 * @param gCtx                  a valid {@link GPUContext}
+	 * @param instName              name of the invoking instruction to record{@link Statistics}.
+	 * @param left                  Matrix A
+	 * @param right                 Matrix B
+	 * @param outputName            Name of the output matrix C (in code generated after LOP layer)
+	 * @param isLeftTransposed      op for A, transposed or not
+	 * @param isRightTransposed     op for B, tranposed or not
 	 * @return	output of matrix multiply
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public static MatrixObject matmult(ExecutionContext ec, String instName, MatrixObject left1, MatrixObject right1, String outputName,
-																		 boolean isLeftTransposed1, boolean isRightTransposed1) throws DMLRuntimeException {
-
-		if(!left1.getGPUObject().isAllocated() || !right1.getGPUObject().isAllocated())
-			throw new DMLRuntimeException("One of input is not allocated:" + left1.getGPUObject().isAllocated() + " " + right1.getGPUObject().isAllocated());
+	public static MatrixObject matmult(ExecutionContext ec, GPUContext gCtx, String instName, MatrixObject left, MatrixObject right, String outputName,
+																		 boolean isLeftTransposed, boolean isRightTransposed) throws DMLRuntimeException {
+		if (ec.getGPUContext() != gCtx)
+			throw new DMLRuntimeException("GPU : Invalid internal state, the GPUContext set with the ExecutionContext is not the same used to run this LibMatrixCUDA function");
+		LOG.trace("GPU : matmult" + ", GPUContext=" + gCtx);
+		if(!left.getGPUObject(gCtx).isAllocated() || !right.getGPUObject(gCtx).isAllocated())
+			throw new DMLRuntimeException("One of input is not allocated:" + left.getGPUObject(gCtx).isAllocated() + " " + right.getGPUObject(gCtx).isAllocated());
 
-		boolean bothDense = !left1.getGPUObject().isInSparseFormat() && !right1.getGPUObject().isInSparseFormat();
-		boolean bothSparse = left1.getGPUObject().isInSparseFormat() && right1.getGPUObject().isInSparseFormat();
+		boolean bothDense = !left.getGPUObject(gCtx).isSparse() && !right.getGPUObject(gCtx).isSparse();
+		boolean bothSparse = left.getGPUObject(gCtx).isSparse() && right.getGPUObject(gCtx).isSparse();
 
 		MatrixObject output = ec.getMatrixObject(outputName);
 
 		if (bothDense) {		// Dense C = Dense A * Dense B
 			// For both dense, do cuBLAS
 			getDenseMatrixOutputForGPUInstruction(ec, instName, outputName);	// Allocated the dense output matrix
-			denseDenseMatmult(instName, output, left1, right1, isLeftTransposed1, isRightTransposed1);
+			denseDenseMatmult(gCtx, instName, output, left, right, isLeftTransposed, isRightTransposed);
 		}
 		else if (bothSparse){	// Sparse C = Sparse A * Sparse B
 			ec.allocateGPUMatrixObject(outputName);
-			bothSparseMatmult(instName, output, left1, right1, isLeftTransposed1, isRightTransposed1);
+			bothSparseMatmult(gCtx, instName, output, left, right, isLeftTransposed, isRightTransposed);
 		}
 		else {	// Either of A or B is sparse, Sparse C = Sparse/Dense A * Dense/Sparse B
 			// Convert the dense to sparse and use the cusparseDcsrgemm routine
 			ec.allocateGPUMatrixObject(outputName);
-			eitherSparseMatmult(instName, output, left1, right1, isLeftTransposed1, isRightTransposed1);
+			eitherSparseMatmult(gCtx, instName, output, left, right, isLeftTransposed, isRightTransposed);
 		}
 
 		return output;
@@ -1243,20 +1377,18 @@ public class LibMatrixCUDA {
 	/**
 	 * One of the matrices is sparse, the other dense
 	 * C = op(A) x op(B)
-	 * @param instName the invoking instruction's name for record {@link Statistics}.
-	 * @param output				allocated output object for C on host to which GPU output will be attached
-	 * @param left					Matrix A on host
-	 * @param right					Matrix B on host
-	 * @param isLeftTransposed		op for A, tranposed or not
-	 * @param isRightTransposed		op for B, transposed or not
+	 * @param gCtx              a valid {@link GPUContext}
+	 * @param instName          the invoking instruction's name for record {@link Statistics}.
+	 * @param output            allocated output object for C on host to which GPU output will be attached
+	 * @param left              Matrix A on host
+	 * @param right             Matrix B on host
+	 * @param isLeftTransposed  op for A, tranposed or not
+	 * @param isRightTransposed op for B, transposed or not
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	protected static void eitherSparseMatmult(String instName, MatrixObject output, MatrixObject left, MatrixObject right,
+	private static void eitherSparseMatmult(GPUContext gCtx, String instName, MatrixObject output, MatrixObject left, MatrixObject right,
 																						boolean isLeftTransposed, boolean isRightTransposed) throws DMLRuntimeException {
 
-		int transA = isLeftTransposed ? CUSPARSE_OPERATION_TRANSPOSE : CUSPARSE_OPERATION_NON_TRANSPOSE;
-		int transB = isRightTransposed ? CUSPARSE_OPERATION_TRANSPOSE : CUSPARSE_OPERATION_NON_TRANSPOSE;
-
 		int m = (int) (isLeftTransposed ? left.getNumColumns() : left.getNumRows()) ;
 		int n = (int) (isRightTransposed ? right.getNumRows() : right.getNumColumns());
 		int k = (int) (isLeftTransposed ? left.getNumRows() :  left.getNumColumns());
@@ -1268,12 +1400,12 @@ public class LibMatrixCUDA {
 			throw new DMLRuntimeException("Incorrect dimensions");
 
 
-		if (left.getGPUObject().isInSparseFormat()) {
+		if (left.getGPUObject(gCtx).isSparse()) {
 			// Left sparse, right dense
-			sparseDenseMatmult(instName, output, left, right, isLeftTransposed, isRightTransposed, transA, transB, m, n, k);
+			sparseDenseMatmult(gCtx, instName, output, left, right, isLeftTransposed, isRightTransposed, m, n, k);
 		} else {
 			// Left dense, right sparse
-			denseSparseMatmult(instName, output, right, left, isLeftTransposed, isRightTransposed, transA, transB, m, n, k);
+			denseSparseMatmult(gCtx, instName, left, right, output, isLeftTransposed, isRightTransposed, m, n, k);
 		}
 	}
 
@@ -1281,73 +1413,72 @@ public class LibMatrixCUDA {
 	 * C = op(A) * op(B) where A is dense and B is sparse
 	 * If B is ultrasparse, A is converted to a sparse matrix and {@code sparseSparseMatmult(MatrixObject, int, int, int, int, int, CSRPointer, CSRPointer)} is invoked
 	 * otherwise B is converted to a dense matrix and {@code denseDenseMatmult(Pointer, int, int, int, int, boolean, boolean, Pointer, Pointer)} is invoked.
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName the invoking instruction's name for record {@link Statistics}.
-	 * @param output ?
-	 * @param right ?
-	 * @param left ?
-	 * @param isLeftTransposed ?
-	 * @param isRightTransposed ?
-	 * @param transA ?
-	 * @param transB ?
+	 * @param left {@link MatrixObject} of A
+	 * @param right {@link MatrixObject} of B
+	 * @param output {@link MatrixObject} of the output matrix C
+	 * @param isLeftTransposed whether matrix A needs to be transposed
+	 * @param isRightTransposed whether matrix B needs to be transposed
 	 * @param m ?
 	 * @param n ?
 	 * @param k ?
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	protected static void denseSparseMatmult(String instName, MatrixObject output, MatrixObject right, MatrixObject left,
-																					 boolean isLeftTransposed, boolean isRightTransposed, int transA, int transB, int m, int n, int k)
+	private static void denseSparseMatmult(GPUContext gCtx, String instName, MatrixObject left, MatrixObject right, MatrixObject output,
+                                             boolean isLeftTransposed, boolean isRightTransposed, int m, int n, int k)
 					throws DMLRuntimeException {
 		// right sparse, left dense
-		CSRPointer B = ((JCudaObject)right.getGPUObject()).jcudaSparseMatrixPtr;
-		Pointer ADense = getDensePointer(left, instName);
+		CSRPointer B = right.getGPUObject(gCtx).getJcudaSparseMatrixPtr();
+		Pointer ADense = getDensePointer(gCtx, left, instName);
 		if (B.isUltraSparse(k, n)){
-			LOG.debug(" GPU Dense-Sparse Matrix Multiplication (Converted to Sparse-Sparse)");
-			// Convert left to CSR and do cuSparse matmul
+            LOG.trace(" GPU : Convert d M %*% sp M --> sp M %*% sp M)" + ", GPUContext=" + gCtx);
+
+            // Convert left to CSR and do cuSparse matmul
 			int rowsA = (int)left.getNumRows();
 			int colsA = (int)left.getNumColumns();
 
-
 			long t0=0,t1=0, t2=0;
 			if (DMLScript.STATISTICS) t0 = System.nanoTime();
-			Pointer AT = JCudaObject.transpose(ADense, rowsA, colsA, colsA, rowsA);
+			Pointer AT = GPUObject.transpose(gCtx, ADense, rowsA, colsA, colsA, rowsA);
 			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_TRANSPOSE_LIB, System.nanoTime() - t0);
 
 			if (GPUStatistics.DISPLAY_STATISTICS) t1 = System.nanoTime();
-			CSRPointer A = JCudaObject.columnMajorDenseToRowMajorSparse(cusparseHandle, rowsA, colsA, AT);
+			CSRPointer A = GPUObject.columnMajorDenseToRowMajorSparse(gCtx, getCusparseHandle(gCtx), AT, rowsA, colsA);
 			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_DENSE_TO_SPARSE, System.nanoTime() - t1);
 
 			if (DMLScript.STATISTICS) GPUStatistics.cudaDenseToSparseTime.getAndAdd(System.nanoTime() - t0);
 			if (DMLScript.STATISTICS) GPUStatistics.cudaDenseToSparseCount.getAndAdd(1);
-			sparseSparseMatmult(instName, output, transA, transB, m, n, k, A, B);
+			sparseSparseMatmult(gCtx, instName, A, B, output, isLeftTransposed, isRightTransposed, m, n, k);
 
 			if (GPUStatistics.DISPLAY_STATISTICS) t2 = System.nanoTime();
 			A.deallocate();
-			cudaFreeHelper(AT);
+			gCtx.cudaFreeHelper(AT);
 			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_CUDA_FREE, System.nanoTime() - t2, 2);
 
 		} else {
-			LOG.debug(" GPU Dense-Sparse Matrix Multiplication (Converted to Dense-Dense)");
+            LOG.trace(" GPU : Convert d M %*% sp M --> d M %*% d M" + ", GPUContext=" + gCtx);
 			// Convert right to dense and do a cuBlas matmul
 			// BDenseTransposed is a column major matrix
 			// Note the arguments to denseDenseMatmult to accommodate for this.
 			long t0=0, t1=0;
 			if (DMLScript.STATISTICS) t0 = System.nanoTime();
-			Pointer BDenseTransposed = B.toColumnMajorDenseMatrix(cusparseHandle, cublasHandle, (int)right.getNumRows(), (int)right.getNumColumns());
+			Pointer BDenseTransposed = B.toColumnMajorDenseMatrix(getCusparseHandle(gCtx), getCublasHandle(gCtx), (int)right.getNumRows(), (int)right.getNumColumns());
 			if (GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_SPARSE_TO_DENSE, System.nanoTime() - t0);
 			if (DMLScript.STATISTICS) GPUStatistics.cudaSparseToDenseTime.getAndAdd(System.nanoTime() - t0);
 			if (DMLScript.STATISTICS) GPUStatistics.cudaSparseToDenseCount.getAndAdd(System.nanoTime() - t0);
 
 			if (GPUStatistics.DISPLAY_STATISTICS) t1 = System.nanoTime();
-			boolean allocated = output.getGPUObject().acquireDeviceModifyDense();	// To allocate the dense matrix
+			boolean allocated = output.getGPUObject(gCtx).acquireDeviceModifyDense();	// To allocate the dense matrix
 			if (GPUStatistics.DISPLAY_STATISTICS && allocated) GPUStatistics.maintainCPMiscTimes(instName, GPUInstruction.MISC_TIMER_ALLOCATE_DENSE_OUTPUT, System.nanoTime() - t1);
-			Pointer C = getDensePointer(output, instName);
-			denseDenseMatmult(instName, C,
+			Pointer C = getDensePointer(gCtx, output, instName);
+			denseDenseMatmult(gCtx, instName, C,
 							(int) left.getNumRows(), (int) left.getNumColumns(),
 							(int) right.getNumColumns(), (int) right.getNumRows(),
 							isLeftTransposed, !isRightTransposed,
 							ADense, BDenseTransposed);
 
-			cudaFreeHelper(instName, BDenseTransposed);
+			gCtx.cudaFreeHelper(instName, BDenseTransposed);
 		}
 	}
 
@@ -1355,81 +1486,79 @@ public class LibMatrixCUDA {
 	 * * C = op(A) * op(B) where A is sparse and B is dense
 	 * If A is ultrasparse, B is converted to a sparse matrix and {@code sparseSparseMatmult(MatrixObject, int, int, int, int, int, CSRPointer, CSRPointer)} is invoked
 	 * otherwise A is converted to a dense matrix and {@code denseDenseMatmult(Pointer, int, int, int, int, boolean, boolean, Pointer, Pointer)} is invoked.
+	 * @param gCtx   a valid {@link GPUContext}
 	 * @param instName the invoking instruction's name for record {@link Statistics}.
-	 * @param output ?
-	 * @param left ?
-	 * @param right ?
-	 * @param isLeftTransposed ?
-	 * @param isRightTransposed ?
-	 * @param transA ?
-	 * @param transB ?
+	 * @param output the output matrix object
+	 * @param left matrix A
+	 * @param right matrix B
+	 * @param isLeftTransposed if A needs to be transposed
+	 * @param isRightTransposed if B needs to be transposed
 	 * @param m ?
 	 * @param n ?
 	 * @param k ?
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	protected static void sparseDenseMatmult(String instName, MatrixObject output, MatrixObject left, MatrixObject right,
-																					 boolean isLeftTransposed, boolean isRightTransposed, int transA, int transB, int m, int n, int k)
+	private static void sparseDenseMatmult(GPUContext gCtx, String instName, MatrixObject output, MatrixObject left, MatrixObject right,
+																					 boolean isLeftTransposed, boole

<TRUNCATED>


[3/5] incubator-systemml git commit: Refactored GPU{Contex, Object} to make it friendlier for parfor

Posted by na...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/JCudaKernels.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/JCudaKernels.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/JCudaKernels.java
index d858b0b..b4cb87d 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/JCudaKernels.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/JCudaKernels.java
@@ -18,34 +18,25 @@
  */
 package org.apache.sysml.runtime.instructions.gpu.context;
 
-import static jcuda.driver.JCudaDriver.cuCtxCreate;
-import static jcuda.driver.JCudaDriver.cuCtxGetCurrent;
-import static jcuda.driver.JCudaDriver.cuDeviceGet;
-import static jcuda.driver.JCudaDriver.cuInit;
 import static jcuda.driver.JCudaDriver.cuLaunchKernel;
 import static jcuda.driver.JCudaDriver.cuModuleGetFunction;
 import static jcuda.driver.JCudaDriver.cuModuleLoadDataEx;
-import static jcuda.driver.JCudaDriver.cuModuleUnload;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.HashMap;
 
-import jcuda.runtime.JCuda;
 import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.io.IOUtilFunctions;
 
-import jcuda.CudaException;
 import jcuda.Pointer;
-import jcuda.driver.CUcontext;
-import jcuda.driver.CUdevice;
 import jcuda.driver.CUfunction;
 import jcuda.driver.CUmodule;
 import jcuda.driver.CUresult;
 
 /**
- * Utility class that allows LibMatrixCUDA as well as JCudaObject to invoke custom CUDA kernels.
+ * Utility class that allows LibMatrixCUDA as well as GPUObject to invoke custom CUDA kernels.
  * 
  * The utility org.apache.sysml.runtime.instructions.gpu.context.JCudaKernels simplifies the launching of the kernels. 
  * For example: to launch a kernel 
@@ -54,70 +45,23 @@ import jcuda.driver.CUresult;
  */
 public class JCudaKernels {
 
-	private static String ptxFileName = "/kernels/SystemML.ptx";
+	private final static String ptxFileName = "/kernels/SystemML.ptx";
 	private HashMap<String, CUfunction> kernels = new HashMap<String, CUfunction>();
 	private CUmodule module;
+	private final int deviceNum;
 	
 	/**
 	 * Loads the kernels in the file ptxFileName. Though cubin files are also supported, we will stick with
 	 * ptx file as they are target-independent similar to Java's .class files.
-	 * 
+	 * @param deviceNum  the device number for which to initiate the driver API
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public JCudaKernels() throws DMLRuntimeException {
-		shutdown();
-		initCUDA();
+	JCudaKernels(int deviceNum) throws DMLRuntimeException {
+		this.deviceNum = deviceNum;
 		module = new CUmodule();
 		// Load the kernels specified in the ptxFileName file
 		checkResult(cuModuleLoadDataEx(module, initKernels(ptxFileName), 0, new int[0], Pointer.to(new int[0])));
 	}
-	
-	/**
-     * Initializes the JCuda driver API. Then it will try to attach to the 
-     * current CUDA context. If no active CUDA context exists, then it will 
-     * try to create one, for the device which is specified by the current 
-     * deviceNumber.
-     * 
-	 * @throws DMLRuntimeException If it is neither possible to attach to an 
-     * existing context, nor to create a new context.
-     */
-    private static void initCUDA() throws DMLRuntimeException {
-        checkResult(cuInit(0));
-
-        // Try to obtain the current context
-        CUcontext context = new CUcontext();
-        checkResult(cuCtxGetCurrent(context));
-        
-        // If the context is 'null', then a new context
-        // has to be created.
-        CUcontext nullContext = new CUcontext(); 
-        if (context.equals(nullContext)) {
-            createContext();
-        }
-    }
-    
-    /**
-     * Tries to create a context for device 'deviceNumber'.
-     * @throws DMLRuntimeException 
-     * 
-     * @throws CudaException If the device can not be 
-     * accessed or the context can not be created
-     */
-    private static void createContext() throws DMLRuntimeException {
-    	int deviceNumber = 0;
-        CUdevice device = new CUdevice();
-        checkResult(cuDeviceGet(device, deviceNumber));
-        CUcontext context = new CUcontext();
-        checkResult(cuCtxCreate(context, 0, device));
-    }
-    
-	/**
-	 * Performs cleanup actions such as unloading the module
-	 */
-	public void shutdown() {
-		if(module != null)
-			cuModuleUnload(module);
-	}
 
 	/**
 	 * Setups the kernel parameters and launches the kernel using cuLaunchKernel API. 
@@ -167,7 +111,7 @@ public class JCudaKernels {
 				config.gridDimX, config.gridDimY, config.gridDimZ, 
 				config.blockDimX, config.blockDimY, config.blockDimZ, 
 				config.sharedMemBytes, config.stream, Pointer.to(kernelParams), null));
-		JCuda.cudaDeviceSynchronize();
+		//JCuda.cudaDeviceSynchronize();
 	}
 	
     public static void checkResult(int cuResult) throws DMLRuntimeException {

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/JCudaObject.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/JCudaObject.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/JCudaObject.java
deleted file mode 100644
index bc0031b..0000000
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/JCudaObject.java
+++ /dev/null
@@ -1,1330 +0,0 @@
-/*
- * 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.runtime.instructions.gpu.context;
-
-import jcuda.Pointer;
-import jcuda.jcublas.JCublas2;
-import jcuda.jcublas.cublasHandle;
-import jcuda.jcudnn.cudnnTensorDescriptor;
-import jcuda.jcusparse.JCusparse;
-import jcuda.jcusparse.cusparseDirection;
-import jcuda.jcusparse.cusparseHandle;
-import jcuda.jcusparse.cusparseMatDescr;
-import jcuda.jcusparse.cusparsePointerMode;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.sysml.api.DMLScript;
-import org.apache.sysml.runtime.DMLRuntimeException;
-import org.apache.sysml.runtime.controlprogram.caching.CacheException;
-import org.apache.sysml.runtime.controlprogram.caching.MatrixObject;
-import org.apache.sysml.runtime.instructions.gpu.GPUInstruction;
-import org.apache.sysml.runtime.matrix.data.LibMatrixCUDA;
-import org.apache.sysml.runtime.matrix.data.MatrixBlock;
-import org.apache.sysml.runtime.matrix.data.SparseBlock;
-import org.apache.sysml.runtime.matrix.data.SparseBlockCOO;
-import org.apache.sysml.runtime.matrix.data.SparseBlockCSR;
-import org.apache.sysml.runtime.matrix.data.SparseBlockMCSR;
-import org.apache.sysml.utils.GPUStatistics;
-import org.apache.sysml.utils.LRUCacheMap;
-
-import java.util.HashMap;
-import java.util.LinkedList;
-
-import static jcuda.jcublas.cublasOperation.CUBLAS_OP_T;
-import static jcuda.jcudnn.JCudnn.cudnnCreateTensorDescriptor;
-import static jcuda.jcudnn.JCudnn.cudnnDestroyTensorDescriptor;
-import static jcuda.jcudnn.JCudnn.cudnnSetTensor4dDescriptor;
-import static jcuda.jcudnn.cudnnTensorFormat.CUDNN_TENSOR_NCHW;
-import static jcuda.jcudnn.cudnnDataType.CUDNN_DATA_DOUBLE;
-import static jcuda.jcusparse.JCusparse.cusparseCreateMatDescr;
-import static jcuda.jcusparse.JCusparse.cusparseDcsr2dense;
-import static jcuda.jcusparse.JCusparse.cusparseDdense2csr;
-import static jcuda.jcusparse.JCusparse.cusparseDnnz;
-import static jcuda.jcusparse.JCusparse.cusparseSetMatIndexBase;
-import static jcuda.jcusparse.JCusparse.cusparseSetMatType;
-import static jcuda.jcusparse.JCusparse.cusparseSetPointerMode;
-import static jcuda.jcusparse.JCusparse.cusparseXcsrgeamNnz;
-import static jcuda.jcusparse.JCusparse.cusparseXcsrgemmNnz;
-import static jcuda.jcusparse.cusparseIndexBase.CUSPARSE_INDEX_BASE_ZERO;
-import static jcuda.jcusparse.cusparseMatrixType.CUSPARSE_MATRIX_TYPE_GENERAL;
-import static jcuda.runtime.JCuda.cudaDeviceSynchronize;
-import static jcuda.runtime.JCuda.cudaFree;
-import static jcuda.runtime.JCuda.cudaMalloc;
-import static jcuda.runtime.JCuda.cudaMemcpy;
-import static jcuda.runtime.JCuda.cudaMemset;
-import static jcuda.runtime.cudaMemcpyKind.cudaMemcpyDeviceToHost;
-import static jcuda.runtime.cudaMemcpyKind.cudaMemcpyHostToDevice;
-
-/**
- * Handle to a matrix block on the GPU
- */
-public class JCudaObject extends GPUObject {
-
-	private static final Log LOG = LogFactory.getLog(JCudaObject.class.getName());
-	
-	// An optional tensor descriptor (and shape) that can be set by a tensor instruction such as convolution, maxpooling
-	// and exploited by a subsequent non-tensor instruction such as relu
-	private cudnnTensorDescriptor tensorDescriptor = null;
-	private int [] tensorShape = null;
-	
-	/**
-	 * Returns a previously allocated tensor shape or null
-	 * @return int array of four elements or null
-	 */
-	public int [] getTensorShape() {
-		return tensorShape;
-	}
-	
-	/**
-	 * Returns a previously allocated tensor descriptor or null
-	 * @return cudnn tensor descriptor
-	 */
-	public cudnnTensorDescriptor getTensorDescriptor() {
-		return tensorDescriptor;
-	}
-	
-	/**
-	 * Returns a previously allocated or allocates and returns a tensor descriptor
-	 * @param N number of images
-	 * @param C number of channels
-	 * @param H height
-	 * @param W width
-	 * @return cudnn tensor descriptor
-	 */
-	public cudnnTensorDescriptor allocateTensorDescriptor(int N, int C, int H, int W) {
-		if(tensorDescriptor == null) {
-			tensorDescriptor = new cudnnTensorDescriptor();
-			cudnnCreateTensorDescriptor(tensorDescriptor);
-			cudnnSetTensor4dDescriptor(tensorDescriptor, CUDNN_TENSOR_NCHW, CUDNN_DATA_DOUBLE, N, C, H, W);
-			tensorShape = new int[4];
-			tensorShape[0] = N;
-			tensorShape[1] = C;
-			tensorShape[2] = H;
-			tensorShape[3] = W;
-		}
-		return tensorDescriptor;
-	}
-	
-	/**
-	 * Compressed Sparse Row (CSR) format for CUDA
-	 * Generalized matrix multiply is implemented for CSR format in the cuSparse library among other operations
-	 */
-	public static class CSRPointer {
-		
-		public static cusparseMatDescr matrixDescriptor;
-		
-		/**
-		 * @return Singleton default matrix descriptor object 
-		 * 			(set with CUSPARSE_MATRIX_TYPE_GENERAL, CUSPARSE_INDEX_BASE_ZERO)
-		 */
-		public static cusparseMatDescr getDefaultCuSparseMatrixDescriptor(){
-			if (matrixDescriptor == null){
-				// Code from JCuda Samples - http://www.jcuda.org/samples/JCusparseSample.java
-				matrixDescriptor = new cusparseMatDescr();
-				cusparseCreateMatDescr(matrixDescriptor);
-				cusparseSetMatType(matrixDescriptor, CUSPARSE_MATRIX_TYPE_GENERAL);
-				cusparseSetMatIndexBase(matrixDescriptor, CUSPARSE_INDEX_BASE_ZERO);
-			}
-			return matrixDescriptor;
-		}
-		
-		private static final double ULTRA_SPARSITY_TURN_POINT = 0.0004;
-
-		/**
-		 * Default constructor to help with Factory method {@link #allocateEmpty(long, long)}
-		 */
-		private CSRPointer() {
-			val = new Pointer();
-			rowPtr = new Pointer();
-			colInd = new Pointer();
-			allocateMatDescrPointer();
-		}
-		
-		/** Number of non zeroes	 									*/
-		public long nnz;
-		/** double array of non zero values 							*/
-		public Pointer val;
-		/** integer array of start of all rows and end of last row + 1 	*/
-		public Pointer rowPtr;
-		/** integer array of nnz values' column indices					*/
-		public Pointer colInd;
-		/** descriptor of matrix, only CUSPARSE_MATRIX_TYPE_GENERAL supported	*/
-		public cusparseMatDescr descr;
-		
-		/** 
-		 * Check for ultra sparsity
-		 * 
-		 * @param rows number of rows
-		 * @param cols number of columns
-		 * @return true if ultra sparse
-		 */
-		public boolean isUltraSparse(int rows, int cols) {
-			double sp = ((double)nnz/rows/cols);
-			return sp<ULTRA_SPARSITY_TURN_POINT;
-		}
-		
-		/**
-		 * Initializes {@link #descr} to CUSPARSE_MATRIX_TYPE_GENERAL,
-		 * the default that works for DGEMM.
-		 */
-		private void allocateMatDescrPointer() {			
-			this.descr = getDefaultCuSparseMatrixDescriptor();
-		}
-		
-		/**
-		 * Estimate the size of a CSR matrix in GPU memory
-		 * Size of pointers is not needed and is not added in
-		 * @param nnz2	number of non zeroes
-		 * @param rows	number of rows 
-		 * @return size estimate
-		 */
-		public static long estimateSize(long nnz2, long rows) {
-			long sizeofValArray = getDoubleSizeOf(nnz2);
-			long sizeofRowPtrArray  = getIntSizeOf(rows + 1);
-			long sizeofColIndArray = getIntSizeOf(nnz2);
-			long sizeofDescr = getIntSizeOf(4);
-			// From the CUSPARSE documentation, the cusparseMatDescr in native code is represented as: 
-			// typedef struct {
-			// 	cusparseMatrixType_t MatrixType;
-			//	cusparseFillMode_t FillMode;
-			//	cusparseDiagType_t DiagType;
-			// 	cusparseIndexBase_t IndexBase;
-			// } cusparseMatDescr_t;
-			long tot = sizeofValArray + sizeofRowPtrArray + sizeofColIndArray + sizeofDescr;
-			return tot;
-		}
-		
-		/** 
-		 * Factory method to allocate an empty CSR Sparse matrix on the GPU
-		 * @param nnz2	number of non-zeroes
-		 * @param rows 	number of rows
-		 * @return a {@link CSRPointer} instance that encapsulates the CSR matrix on GPU
-		 * @throws DMLRuntimeException if DMLRuntimeException occurs
-		 */
-		public static CSRPointer allocateEmpty(long nnz2, long rows) throws DMLRuntimeException {
-			assert nnz2 > -1 : "Incorrect usage of internal API, number of non zeroes is less than 0 when trying to allocate sparse data on GPU";
-			CSRPointer r = new CSRPointer();
-			r.nnz = nnz2;
-			if(nnz2 == 0) {
-				// The convention for an empty sparse matrix is to just have an instance of the CSRPointer object
-				// with no memory allocated on the GPU.
-				return r;
-			}
-			ensureFreeSpace(getDoubleSizeOf(nnz2) + getIntSizeOf(rows + 1) + getIntSizeOf(nnz2));
-			// increment the cudaCount by 1 for the allocation of all 3 arrays
-			r.val = allocate(null, getDoubleSizeOf(nnz2), 0);
-			r.rowPtr = allocate(null, getIntSizeOf(rows + 1), 0);
-			r.colInd = allocate(null, getIntSizeOf(nnz2), 1);
-			return r;
-		}
-		
-		/**
-		 * Static method to copy a CSR sparse matrix from Host to Device
-		 * @param dest	[input] destination location (on GPU)
-		 * @param rows	number of rows
-		 * @param nnz 	number of non-zeroes
-		 * @param rowPtr	integer array of row pointers
-		 * @param colInd	integer array of column indices
-		 * @param values	double array of non zero values
-		 */
-		public static void copyToDevice(CSRPointer dest, int rows, long nnz, int[] rowPtr, int[] colInd, double[] values) {
-			CSRPointer r = dest;
-			long t0=0;
-			if (DMLScript.STATISTICS) t0 = System.nanoTime();
-			r.nnz = nnz;
-			cudaMemcpy(r.rowPtr, Pointer.to(rowPtr), getIntSizeOf(rows + 1), cudaMemcpyHostToDevice);
-			cudaMemcpy(r.colInd, Pointer.to(colInd), getIntSizeOf(nnz), cudaMemcpyHostToDevice);
-			cudaMemcpy(r.val, Pointer.to(values), getDoubleSizeOf(nnz), cudaMemcpyHostToDevice);
-			if (DMLScript.STATISTICS) GPUStatistics.cudaToDevTime.addAndGet(System.nanoTime()-t0);
-			if (DMLScript.STATISTICS) GPUStatistics.cudaToDevCount.addAndGet(3);
-		}
-		
-		/**
-		 * Static method to copy a CSR sparse matrix from Device to host
-		 * @param src	[input] source location (on GPU)
-		 * @param rows	[input] number of rows
-		 * @param nnz	[input] number of non-zeroes
-		 * @param rowPtr	[output] pre-allocated integer array of row pointers of size (rows+1)
-		 * @param colInd	[output] pre-allocated integer array of column indices of size nnz
-		 * @param values	[output] pre-allocated double array of values of size nnz
-		 */
-		public static void copyToHost(CSRPointer src, int rows, long nnz, int[] rowPtr, int[] colInd, double[] values){
-			CSRPointer r = src;
-			long t0=0;
-			if (DMLScript.STATISTICS) t0 = System.nanoTime();
-			cudaMemcpy(Pointer.to(rowPtr), r.rowPtr, getIntSizeOf(rows + 1), cudaMemcpyDeviceToHost);
-			cudaMemcpy(Pointer.to(colInd), r.colInd, getIntSizeOf(nnz), cudaMemcpyDeviceToHost);
-			cudaMemcpy(Pointer.to(values), r.val, getDoubleSizeOf(nnz), cudaMemcpyDeviceToHost);
-			if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevTime.addAndGet(System.nanoTime()-t0);
-			if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevCount.addAndGet(3);
-		}
-		
-		// ==============================================================================================
-
-		// The following methods estimate the memory needed for sparse matrices that are
-		// results of operations on other sparse matrices using the cuSparse Library.
-		// The operation is C = op(A) binaryOperation op(B), C is the output and A & B are the inputs
-		// op = whether to transpose or not
-		// binaryOperation = For cuSparse, +, - are *(matmul) are supported
-
-		// From CuSparse Manual,
-		// Since A and B have different sparsity patterns, cuSPARSE adopts a two-step approach
-		// to complete sparse matrix C. In the first step, the user allocates csrRowPtrC of m+1
-		// elements and uses function cusparseXcsrgeamNnz() to determine csrRowPtrC
-		// and the total number of nonzero elements. In the second step, the user gathers nnzC
-		//(number of nonzero elements of matrix C) from either (nnzC=*nnzTotalDevHostPtr)
-		// or (nnzC=csrRowPtrC(m)-csrRowPtrC(0)) and allocates csrValC, csrColIndC of
-		// nnzC elements respectively, then finally calls function cusparse[S|D|C|Z]csrgeam()
-		// to complete matrix C.
-
-		/**
-		 * Allocate row pointers of m+1 elements
-		 * 
-		 * @param handle	a valid {@link cusparseHandle}
-		 * @param C			Output matrix
-		 * @param rowsC			number of rows in C
-		 * @throws DMLRuntimeException ?
-		 */
-		private static void step1AllocateRowPointers(cusparseHandle handle, CSRPointer C, int rowsC) throws DMLRuntimeException {
-			cusparseSetPointerMode(handle, cusparsePointerMode.CUSPARSE_POINTER_MODE_HOST);
-            cudaDeviceSynchronize();
-			// Do not increment the cudaCount of allocations on GPU
-			C.rowPtr = allocate(null, getIntSizeOf((long)rowsC+1), 0);
-		}
-		
-		/**
-		 * Determine total number of nonzero element for the cusparseDgeam  operation.
-		 * This is done from either (nnzC=*nnzTotalDevHostPtr) or (nnzC=csrRowPtrC(m)-csrRowPtrC(0))
-		 * 
-		 * @param handle	a valid {@link cusparseHandle}
-		 * @param A			Sparse Matrix A on GPU
-		 * @param B			Sparse Matrix B on GPU
-		 * @param C			Output Sparse Matrix C on GPU
-		 * @param m			Rows in C
-		 * @param n			Columns in C
-		 * @throws DMLRuntimeException ?
-		 */
-		private static void step2GatherNNZGeam(cusparseHandle handle, CSRPointer A, CSRPointer B, CSRPointer C, int m, int n) throws DMLRuntimeException {
-			int[] CnnzArray = { -1 };
-			cusparseXcsrgeamNnz(handle, m, n,  
-					A.descr, toIntExact(A.nnz), A.rowPtr, A.colInd, 
-					B.descr, toIntExact(B.nnz), B.rowPtr, B.colInd, 
-					C.descr, C.rowPtr, Pointer.to(CnnzArray));
-            cudaDeviceSynchronize();
-			if (CnnzArray[0] != -1){
-				C.nnz = CnnzArray[0];
-			}
-			else {
-		        int baseArray[] = { 0 };
-		        cudaMemcpy(Pointer.to(CnnzArray), C.rowPtr.withByteOffset(getIntSizeOf(m)), getIntSizeOf(1), cudaMemcpyDeviceToHost);
-	            cudaMemcpy(Pointer.to(baseArray), C.rowPtr,								   getIntSizeOf(1), cudaMemcpyDeviceToHost);
-	            C.nnz = CnnzArray[0] - baseArray[0];
-			}
-		}
-
-		/**
-		 *	Determine total number of nonzero element for the cusparseDgemm operation.
-		 *
-		 * @param handle	a valid {@link cusparseHandle}
-		 * @param A			Sparse Matrix A on GPU
-		 * @param transA	op - whether A is transposed
-		 * @param B			Sparse Matrix B on GPU
-		 * @param transB	op - whether B is transposed
-		 * @param C			Output Sparse Matrix C on GPU
-		 * @param m			Number of rows of sparse matrix op ( A ) and C
-		 * @param n			Number of columns of sparse matrix op ( B ) and C
-		 * @param k			Number of columns/rows of sparse matrix op ( A ) / op ( B )
-		 * @throws DMLRuntimeException ?
-		 */
-		private static void step2GatherNNZGemm(cusparseHandle handle, CSRPointer A, int transA, CSRPointer B, int transB, CSRPointer C, int m, int n, int k) throws DMLRuntimeException {
-			int[] CnnzArray = { -1 };
-			if (A.nnz >= Integer.MAX_VALUE || B.nnz >= Integer.MAX_VALUE) { 
-				throw new DMLRuntimeException("Number of non zeroes is larger than supported by cuSparse"); 
-			}
-			cusparseXcsrgemmNnz(handle, transA, transB, m, n, k, 
-					A.descr, toIntExact(A.nnz), A.rowPtr, A.colInd, 
-					B.descr, toIntExact(B.nnz), B.rowPtr, B.colInd, 
-					C.descr, C.rowPtr, Pointer.to(CnnzArray));
-            cudaDeviceSynchronize();
-			if (CnnzArray[0] != -1){
-				C.nnz = CnnzArray[0];
-			}
-			else {
-		        int baseArray[] = { 0 };
-		        cudaMemcpy(Pointer.to(CnnzArray), C.rowPtr.withByteOffset(getIntSizeOf(m)), getIntSizeOf(1), cudaMemcpyDeviceToHost);
-	            cudaMemcpy(Pointer.to(baseArray), C.rowPtr,								   getIntSizeOf(1), cudaMemcpyDeviceToHost);
-	            C.nnz = CnnzArray[0] - baseArray[0];
-			}
-		}
-
-		/**
-		 * Allocate val and index pointers.
-		 * 
-		 * @param handle	a valid {@link cusparseHandle}
-		 * @param C			Output sparse matrix on GPU
-		 * @throws DMLRuntimeException ?
-		 */
-		private static void step3AllocateValNInd(cusparseHandle handle, CSRPointer C) throws DMLRuntimeException {
-			// Increment cudaCount by one when all three arrays of CSR sparse array are allocated
-			C.val = allocate(null, getDoubleSizeOf(C.nnz), 0);
-			C.colInd = allocate(null, getIntSizeOf(C.nnz), 1);
-		}
-
-		// ==============================================================================================
-
-
-		/**
-		 * Estimates the number of non zero elements from the results of a sparse cusparseDgeam operation
-		 * C = a op(A) + b op(B)
-		 * @param handle 	a valid {@link cusparseHandle}
-		 * @param A			Sparse Matrix A on GPU
-		 * @param B			Sparse Matrix B on GPU
-		 * @param m			Rows in A
-		 * @param n			Columns in Bs
-		 * @return CSR (compressed sparse row) pointer
-		 * @throws DMLRuntimeException if DMLRuntimeException occurs
-		 */
-		public static CSRPointer allocateForDgeam(cusparseHandle handle, CSRPointer A, CSRPointer B, int m, int n) 
-				throws DMLRuntimeException{
-			if (A.nnz >= Integer.MAX_VALUE || B.nnz >= Integer.MAX_VALUE) { 
-				throw new DMLRuntimeException("Number of non zeroes is larger than supported by cuSparse"); 
-			}
-			CSRPointer C = new CSRPointer();
-			step1AllocateRowPointers(handle, C, m);
-			step2GatherNNZGeam(handle, A, B, C, m, n);
-			step3AllocateValNInd(handle, C);
-			return C;
-		}
-		
-		/**
-		 * Estimates the number of non-zero elements from the result of a sparse matrix multiplication C = A * B
-		 * and returns the {@link CSRPointer} to C with the appropriate GPU memory.
-		 * @param handle	a valid {@link cusparseHandle}
-		 * @param A			Sparse Matrix A on GPU
-		 * @param transA	'T' if A is to be transposed, 'N' otherwise
-		 * @param B			Sparse Matrix B on GPU
-		 * @param transB	'T' if B is to be transposed, 'N' otherwise
-		 * @param m			Rows in A
-		 * @param n			Columns in B
-		 * @param k			Columns in A / Rows in B
-		 * @return a {@link CSRPointer} instance that encapsulates the CSR matrix on GPU
-		 * @throws DMLRuntimeException if DMLRuntimeException occurs
-		 */
-		public static CSRPointer allocateForMatrixMultiply(cusparseHandle handle, CSRPointer A, int transA, CSRPointer B, int transB, int m, int n, int k) 
-				throws DMLRuntimeException{
-			// Following the code example at http://docs.nvidia.com/cuda/cusparse/#cusparse-lt-t-gt-csrgemm and at
-			// https://github.com/jcuda/jcuda-matrix-utils/blob/master/JCudaMatrixUtils/src/test/java/org/jcuda/matrix/samples/JCusparseSampleDgemm.java
-			CSRPointer C = new CSRPointer();
-			step1AllocateRowPointers(handle, C, m);
-			step2GatherNNZGemm(handle, A, transA, B, transB, C, m, n, k);
-			step3AllocateValNInd(handle, C);
-			return C;
-		}
-		
-		/**
-		 * Copies this CSR matrix on the GPU to a dense column-major matrix
-		 * on the GPU. This is a temporary matrix for operations such as 
-		 * cusparseDcsrmv.
-		 * Since the allocated matrix is temporary, bookkeeping is not updated.
-		 * The caller is responsible for calling "free" on the returned Pointer object
-		 * @param cusparseHandle	a valid {@link cusparseHandle}
-		 * @param cublasHandle 		a valid {@link cublasHandle}
-		 * @param rows		number of rows in this CSR matrix
-		 * @param cols		number of columns in this CSR matrix
-		 * @return			A {@link Pointer} to the allocated dense matrix (in column-major format)
-		 * @throws DMLRuntimeException if DMLRuntimeException occurs
-		 */
-		public Pointer toColumnMajorDenseMatrix(cusparseHandle cusparseHandle, cublasHandle cublasHandle, int rows, int cols) throws DMLRuntimeException {
-			long size = ((long)rows) * getDoubleSizeOf((long)cols);
-			Pointer A = JCudaObject.allocate(size);
-			// If this sparse block is empty, the allocated dense matrix, initialized to zeroes, will be returned.
-			if (val != null && rowPtr != null && colInd != null && nnz > 0) {
-				// Note: cusparseDcsr2dense method cannot handle empty blocks
-				cusparseDcsr2dense(cusparseHandle, rows, cols, descr, val, rowPtr, colInd, A, rows);
-                cudaDeviceSynchronize();
-			} else {
-				LOG.warn("in CSRPointer, the values array, row pointers array or column indices array was null");
-			}
-			return A;
-		}
-		
-		/**
-		 * Calls cudaFree lazily on the allocated {@link Pointer} instances
-		 */
-		public void deallocate() {
-			deallocate(false);
-		}
-
-		/**
-		 * Calls cudaFree lazily or eagerly on the allocated {@link Pointer} instances
-		 * @param eager whether to do eager or lazy cudaFrees
-		 */
-		public void deallocate(boolean eager){
-			if (nnz > 0) {
-				cudaFreeHelper(val, eager);
-				cudaFreeHelper(rowPtr, eager);
-				cudaFreeHelper(colInd, eager);
-			}
-		}
-	};
-	
-	private static long getDoubleSizeOf(long numElems) {
-		return numElems * ((long)jcuda.Sizeof.DOUBLE);
-	}
-	
-	private static long getIntSizeOf(long numElems) {
-		return numElems * ((long)jcuda.Sizeof.INT);
-	}
-	
-	public synchronized boolean isAllocated() {
-		return (jcudaDenseMatrixPtr != null || jcudaSparseMatrixPtr != null);
-	}
-	
-	/** Pointer to dense matrix */
-	public Pointer jcudaDenseMatrixPtr = null;
-	/** Pointer to sparse matrix */
-	public CSRPointer jcudaSparseMatrixPtr = null;
-
-	public long numBytes;							/** Number of bytes occupied by this block on GPU */
-
-	/**
-	 * Initializes this JCudaObject with a {@link MatrixObject} instance which will contain metadata about the enclosing matrix block
-	 * @param m
-	 */
-	JCudaObject(MatrixObject m) {
-		super(m);
-	}
-
-	/**
-	 * Allocates a sparse and empty {@link JCudaObject}
-	 * This is the result of operations that are both non zero matrices.
-	 * 
-	 * @throws DMLRuntimeException if DMLRuntimeException occurs
-	 */
-	public void allocateSparseAndEmpty() throws DMLRuntimeException{
-		setSparseMatrixCudaPointer(CSRPointer.allocateEmpty(0, mat.getNumRows()));
-		setDeviceModify(0);
-	}
-
-
-	/**
-	 * Allocates a dense matrix of size obtained from the attached matrix metadata
-	 * and fills it up with a single value
-	 * 
-	 * @param v value to fill up the dense matrix
-	 * @throws DMLRuntimeException if DMLRuntimeException occurs
-	 */
-	public void allocateAndFillDense(double v) throws DMLRuntimeException {
-		long rows = mat.getNumRows();
-		long cols = mat.getNumColumns();
-		int numElems = toIntExact(rows * cols);
-		long size = getDoubleSizeOf(numElems);
-		setDenseMatrixCudaPointer(allocate(size));
-		setDeviceModify(size);
-		// The "fill" kernel is called which treats the matrix "jcudaDensePtr" like a vector and fills it with value "v"
-		LibMatrixCUDA.kernels.launchKernel("fill", ExecutionConfig.getConfigForSimpleVectorOperations(numElems), jcudaDenseMatrixPtr, v, numElems);
-	}
-
-	/**
-	 * If this {@link JCudaObject} is sparse and empty
-	 * Being allocated is a prerequisite to being sparse and empty.
-	 * 
-	 * @return true if sparse and empty
-	 */
-	public boolean isSparseAndEmpty() {
-		boolean isSparseAndAllocated = isAllocated()&& LibMatrixCUDA.isInSparseFormat(mat);
-		boolean isEmptyAndSparseAndAllocated = isSparseAndAllocated && jcudaSparseMatrixPtr.nnz == 0;
-		return isEmptyAndSparseAndAllocated;
-	}
-
-	@Override
-	public synchronized boolean acquireDeviceRead() throws DMLRuntimeException {
-		boolean transferred = false;
-		if(!isAllocated()) {
-			copyFromHostToDevice();
-			transferred = true;
-		} else {
-			numLocks.addAndGet(1);
-		}
-		if(!isAllocated())
-			throw new DMLRuntimeException("Expected device data to be allocated");
-		return transferred;
-	}
-	
-	@Override
-	public synchronized boolean acquireDeviceModifyDense() throws DMLRuntimeException {
-		boolean allocated = false;
-		if(!isAllocated()) {
-			mat.setDirty(true);
-			// Dense block, size = numRows * numCols
-			allocateDenseMatrixOnDevice();
-			allocated = true;
-			synchronized(evictionLock) {
-				JCudaContext.allocatedPointers.add(this);
-			}
-		}
-		isDeviceCopyModified = true;
-		if(!isAllocated()) 
-			throw new DMLRuntimeException("Expected device data to be allocated");
-		return allocated;
-	}
-	
-	@Override
-	public synchronized boolean acquireDeviceModifySparse() throws DMLRuntimeException {
-		boolean allocated = false;
-		isInSparseFormat = true;
-		if(!isAllocated()) {
-			mat.setDirty(true);
-			allocateSparseMatrixOnDevice();
-			allocated = true;
-			synchronized(evictionLock) {
-				JCudaContext.allocatedPointers.add(this);
-			}
-		}
-		isDeviceCopyModified = true;
-		if(!isAllocated()) 
-			throw new DMLRuntimeException("Expected device data to be allocated");
-		return allocated;
-	}
-	
-	@Override
-	public synchronized boolean acquireHostRead() throws CacheException {
-		boolean copied = false;
-		if(isAllocated()) {
-			try {
-				if(isDeviceCopyModified) {
-					copyFromDeviceToHost();
-					copied = true;
-				}
-			} catch (DMLRuntimeException e) {
-				throw new CacheException(e);
-			}
-		}
-		else {
-			throw new CacheException("Cannot perform acquireHostRead as the GPU data is not allocated:" + mat.getVarName());
-		}
-		return copied;
-	}
-	
-	/**
-	 * updates the locks depending on the eviction policy selected
-	 * @throws CacheException if there is no locked GPU Object
-	 */
-	private void updateReleaseLocks() throws CacheException {
-		if(numLocks.addAndGet(-1) < 0) {
-            throw new CacheException("Redundant release of GPU object");
-		}
-		if(evictionPolicy == EvictionPolicy.LRU) {
-            timestamp.set(System.nanoTime());
-		}
-		else if(evictionPolicy == EvictionPolicy.LFU) {
-            timestamp.addAndGet(1);
-		}
-		else if(evictionPolicy == EvictionPolicy.MIN_EVICT) {
-            // Do Nothing
-		}
-		else {
-            throw new CacheException("The eviction policy is not supported:" + evictionPolicy.name());
-		}
-	}
-	
-	/**
-	 * releases input allocated on GPU
-	 * @throws CacheException if data is not allocated
-	 */
-	public synchronized void releaseInput() throws CacheException {
-		updateReleaseLocks();
-		if(!isAllocated())
-			throw new CacheException("Attempting to release an input before allocating it");
-	}
-
-	/**
-	@Override
-	void allocateMemoryOnDevice(long numElemToAllocate) throws DMLRuntimeException {
-		if(!isAllocated()) {
-			long start = System.nanoTime();
-			if(numElemToAllocate == -1 && LibMatrixCUDA.isInSparseFormat(mat)) {
-				setSparseMatrixCudaPointer(CSRPointer.allocateEmpty(mat.getNnz(), mat.getNumRows()));
-				numBytes = CSRPointer.estimateSize(mat.getNnz(), mat.getNumRows());
-				JCudaContext.deviceMemBytes.addAndGet(-numBytes);
-				isInSparseFormat = true;
-				//throw new DMLRuntimeException("Sparse format not implemented");
-			} else if(numElemToAllocate == -1) {
-				// Called for dense input
-				setDenseMatrixCudaPointer(new Pointer());
-				numBytes = mat.getNumRows()*getDoubleSizeOf(mat.getNumColumns());
-				cudaMalloc(jcudaDenseMatrixPtr, numBytes);
-				JCudaContext.deviceMemBytes.addAndGet(-numBytes);
-			}
-			else {
-				// Called for dense output
-				setDenseMatrixCudaPointer(new Pointer());
-				numBytes = getDoubleSizeOf(numElemToAllocate);
-				if(numElemToAllocate <= 0 || numBytes <= 0)
-					throw new DMLRuntimeException("Cannot allocate dense matrix object with " + numElemToAllocate + " elements and size " + numBytes);
-				cudaMalloc(jcudaDenseMatrixPtr,  numBytes);
-				JCudaContext.deviceMemBytes.addAndGet(-numBytes);
-			}
-
-			GPUStatistics.cudaAllocTime.addAndGet(System.nanoTime()-start);
-			GPUStatistics.cudaAllocCount.addAndGet(1);
-
-		}
-	}
-	 */
-
-	@Override
-	void allocateDenseMatrixOnDevice() throws DMLRuntimeException {
-		assert !isAllocated() : "Internal error - trying to allocated dense matrix to a JCudaObject that is already allocated";
-		long rows = mat.getNumRows();
-		long cols = mat.getNumColumns();
-		assert rows > 0 : "Internal error - invalid number of rows when allocating dense matrix";
-		assert cols > 0 : "Internal error - invalid number of columns when allocating dense matrix;";
-        long size = getDoubleSizeOf(rows * cols);
-		Pointer tmp = allocate(size);
-		setDenseMatrixCudaPointer(tmp);
-		setDeviceModify(size);
-	}
-
-	@Override
-	void allocateSparseMatrixOnDevice() throws DMLRuntimeException {
-		assert !isAllocated() : "Internal error = trying to allocated sparse matrix to a JCudaObject that is already allocated";
-		long rows = mat.getNumRows();
-		long nnz = mat.getNnz();
-		assert rows > 0 : "Internal error - invalid number of rows when allocating a sparse matrix";
-		assert nnz > 0 : "Internal error - invalid number of non zeroes when allocating a sparse matrix";
-		CSRPointer tmp = CSRPointer.allocateEmpty(nnz, rows);
-		setSparseMatrixCudaPointer(tmp);
-		long size = CSRPointer.estimateSize(nnz, rows);
-		setDeviceModify(size);
-	}
-
-	/**
-	 * releases output allocated on GPU
-	 * @throws CacheException if data is not allocated
-	 */
-    @Override
-	public synchronized void releaseOutput() throws CacheException {
-		updateReleaseLocks();
-		isDeviceCopyModified = true;
-		if(!isAllocated())
-			throw new CacheException("Attempting to release an output before allocating it");
-	}
-
-	@Override
-	public void setDeviceModify(long numBytes) {
-		this.numLocks.addAndGet(1);
-		this.numBytes = numBytes;
-		((JCudaContext)GPUContext.currContext).getAndAddAvailableMemory(-numBytes);
-	}
-
-	@Override
-	void deallocateMemoryOnDevice(boolean eager) {
-		if(jcudaDenseMatrixPtr != null) {
-			cudaFreeHelper(null, jcudaDenseMatrixPtr, eager);
-			((JCudaContext)GPUContext.currContext).getAndAddAvailableMemory(numBytes);
-		}
-		if (jcudaSparseMatrixPtr != null) {
-			jcudaSparseMatrixPtr.deallocate(eager);
-			((JCudaContext)GPUContext.currContext).getAndAddAvailableMemory(numBytes);
-		}
-		jcudaDenseMatrixPtr = null;
-		jcudaSparseMatrixPtr = null;
-		if(tensorDescriptor != null) {
-			cudnnDestroyTensorDescriptor(tensorDescriptor);
-			tensorDescriptor = null;
-		}
-		numLocks.set(0);
-	}
-	
-	/** 
-	 * Thin wrapper over {@link #evict(long)}
-	 * @param size size to check
-	 * @throws DMLRuntimeException if DMLRuntimeException occurs
-	 */
-	static void ensureFreeSpace(long size) throws DMLRuntimeException {
-		ensureFreeSpace(null, size);
-	}
-
-	/**
-	 * Thin wrapper over {@link #evict(long)}
-	 * @param instructionName instructionName name of the instruction for which performance measurements are made
-	 * @param size size to check
-	 * @throws DMLRuntimeException if DMLRuntimeException occurs
-	 */
-	static void ensureFreeSpace(String instructionName, long size) throws DMLRuntimeException {
-		if(size >= getAvailableMemory()) {
-			evict(instructionName, size);
-		}
-	}
-	
-	@Override
-	void copyFromHostToDevice() 
-		throws DMLRuntimeException 
-	{
-		printCaller();
-		long start=0;
-		if (DMLScript.STATISTICS) start = System.nanoTime();
-		
-		MatrixBlock tmp = mat.acquireRead();
-		if(tmp.isInSparseFormat()) {
-			
-			int rowPtr[] = null;
-			int colInd[] = null;
-			double[] values = null;
-			
-			tmp.recomputeNonZeros();
-			long nnz = tmp.getNonZeros();
-			mat.getMatrixCharacteristics().setNonZeros(nnz);
-			
-			SparseBlock block = tmp.getSparseBlock();
-			boolean copyToDevice = true;
-			if(block == null && tmp.getNonZeros() == 0) {
-//				// Allocate empty block --> not necessary
-//				// To reproduce this, see org.apache.sysml.test.integration.applications.dml.ID3DMLTest
-//				rowPtr = new int[0];
-//				colInd = new int[0];
-//				values = new double[0];
-				copyToDevice = false;
-			}
-			else if(block == null && tmp.getNonZeros() != 0) {
-				throw new DMLRuntimeException("Expected CP sparse block to be not null.");
-			}
-			else {
-				// CSR is the preferred format for cuSparse GEMM
-				// Converts MCSR and COO to CSR
-				SparseBlockCSR csrBlock = null;
-				long t0=0;
-				if (block instanceof SparseBlockCSR){ 
-					csrBlock = (SparseBlockCSR)block;
-				} else if (block instanceof SparseBlockCOO) {
-					// TODO - should we do this on the GPU using cusparse<t>coo2csr() ?
-					if (DMLScript.STATISTICS) t0 = System.nanoTime();
-					SparseBlockCOO cooBlock = (SparseBlockCOO)block;
-					csrBlock = new SparseBlockCSR(toIntExact(mat.getNumRows()), cooBlock.rowIndexes(), cooBlock.indexes(), cooBlock.values());
-					if (DMLScript.STATISTICS) GPUStatistics.cudaSparseConversionTime.addAndGet(System.nanoTime() - t0);
-					if (DMLScript.STATISTICS) GPUStatistics.cudaSparseConversionCount.incrementAndGet();
-				} else if (block instanceof SparseBlockMCSR) {
-					if (DMLScript.STATISTICS) t0 = System.nanoTime();
-					SparseBlockMCSR mcsrBlock = (SparseBlockMCSR)block;
-					csrBlock = new SparseBlockCSR(mcsrBlock.getRows(), toIntExact(mcsrBlock.size()));
-					if (DMLScript.STATISTICS) GPUStatistics.cudaSparseConversionTime.addAndGet(System.nanoTime() - t0);
-					if (DMLScript.STATISTICS) GPUStatistics.cudaSparseConversionCount.incrementAndGet();
-				} else {
-					throw new DMLRuntimeException("Unsupported sparse matrix format for CUDA operations");
-				}
-				rowPtr = csrBlock.rowPointers();
-				colInd = csrBlock.indexes();
-				values = csrBlock.values();	
-			}
-			allocateSparseMatrixOnDevice();
-			synchronized(evictionLock) {
-				JCudaContext.allocatedPointers.add(this);
-			}
-			if(copyToDevice) {
-				CSRPointer.copyToDevice(jcudaSparseMatrixPtr, tmp.getNumRows(), tmp.getNonZeros(), rowPtr, colInd, values);
-			}
-		}
-		else {
-			double[] data = tmp.getDenseBlock();
-			
-			if( data == null && tmp.getSparseBlock() != null )
-				throw new DMLRuntimeException("Incorrect sparsity calculation");
-			else if( data==null && tmp.getNonZeros() != 0 )
-				throw new DMLRuntimeException("MatrixBlock is not allocated");
-			else if( tmp.getNonZeros() == 0 )
-				data = new double[tmp.getNumRows()*tmp.getNumColumns()];
-			
-			// Copy dense block
-			allocateDenseMatrixOnDevice();
-			synchronized(evictionLock) {
-				JCudaContext.allocatedPointers.add(this);
-			}
-			cudaMemcpy(jcudaDenseMatrixPtr, Pointer.to(data), getDoubleSizeOf(mat.getNumRows()*mat.getNumColumns()), cudaMemcpyHostToDevice);
-		}
-		
-		mat.release();
-
-		if (DMLScript.STATISTICS) GPUStatistics.cudaToDevTime.addAndGet(System.nanoTime()-start);
-		if (DMLScript.STATISTICS) GPUStatistics.cudaToDevCount.addAndGet(1);
-	}
-	
-	public static int toIntExact(long l) throws DMLRuntimeException {
-	    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
-	        throw new DMLRuntimeException("Cannot be cast to int:" + l);
-	    }
-	    return (int) l;
-	}
-
-	@Override
-	protected void copyFromDeviceToHost() throws DMLRuntimeException {
-		if (jcudaDenseMatrixPtr != null && jcudaSparseMatrixPtr != null){
-			throw new DMLRuntimeException("Invalid state : JCuda dense/sparse pointer are both allocated");
-		}
-
-		if(jcudaDenseMatrixPtr != null) {
-			printCaller();
-			long start=0;
-			if (DMLScript.STATISTICS) start = System.nanoTime();
-			MatrixBlock tmp = new MatrixBlock(toIntExact(mat.getNumRows()), toIntExact(mat.getNumColumns()), false);
-			tmp.allocateDenseBlock();
-			double [] data = tmp.getDenseBlock();
-			
-			cudaMemcpy(Pointer.to(data), jcudaDenseMatrixPtr, getDoubleSizeOf(data.length), cudaMemcpyDeviceToHost);
-			
-			tmp.recomputeNonZeros();
-			mat.acquireModify(tmp);
-			mat.release();
-
-			if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevTime.addAndGet(System.nanoTime()-start);
-			if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevCount.addAndGet(1);
-		}
-		else if (jcudaSparseMatrixPtr != null){
-			printCaller();
-			if(!LibMatrixCUDA.isInSparseFormat(mat))
-				throw new DMLRuntimeException("Block not in sparse format on host yet the device sparse matrix pointer is not null");
-
-			if(this.isSparseAndEmpty()){
-				MatrixBlock tmp = new MatrixBlock();	// Empty Block
-				mat.acquireModify(tmp);
-				mat.release();
-			} else {
-				long start=0;
-				if (DMLScript.STATISTICS) start = System.nanoTime();
-
-				int rows = toIntExact(mat.getNumRows());
-				int cols = toIntExact(mat.getNumColumns());
-				int nnz = toIntExact(jcudaSparseMatrixPtr.nnz);
-				int[] rowPtr = new int[rows + 1];
-				int[] colInd = new int[nnz];
-				double[] values = new double[nnz];
-				CSRPointer.copyToHost(jcudaSparseMatrixPtr, rows, nnz, rowPtr, colInd, values);
-
-				SparseBlockCSR sparseBlock = new SparseBlockCSR(rowPtr, colInd, values, nnz);
-				MatrixBlock tmp = new MatrixBlock(rows, cols, nnz, sparseBlock);
-				mat.acquireModify(tmp);
-				mat.release();
-				if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevTime.addAndGet(System.nanoTime() - start);
-				if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevCount.addAndGet(1);
-			}
-		}
-		else {
-			throw new DMLRuntimeException("Cannot copy from device to host as JCuda dense/sparse pointer is not allocated");
-		}
-		isDeviceCopyModified = false;
-	}
-
-	@Override
-	protected long getSizeOnDevice() throws DMLRuntimeException {
-		long GPUSize = 0;
-		long rlen = mat.getNumRows();
-		long clen = mat.getNumColumns();
-		long nnz = mat.getNnz();
-
-		if(LibMatrixCUDA.isInSparseFormat(mat)) {
-			GPUSize = CSRPointer.estimateSize(nnz, rlen);
-		}
-		else {
-			GPUSize = getDoubleSizeOf(rlen * clen);
-		}
-		return GPUSize;
-	}
-	
-	private String getClassAndMethod(StackTraceElement st) {
-		String [] str = st.getClassName().split("\\.");
-		return str[str.length - 1] + "." + st.getMethodName();
-	}
-	
-	/**
-	 * Convenience debugging method.
-	 * Checks {@link JCudaContext#DEBUG} flag before printing to System.out
-	 */
-	private void printCaller() {
-		if(JCudaContext.DEBUG) {
-			StackTraceElement[] st = Thread.currentThread().getStackTrace();
-			String ret = getClassAndMethod(st[1]);
-			for (int i = 2; i < st.length && i < 7; i++) {
-				ret += "->" + getClassAndMethod(st[i]);
-			}
-			System.out.println("CALL_STACK:" + ret);
-		}
-	}
-	
-	/**
-	 * Convenience method to directly examine the Sparse matrix on GPU
-	 * 
-	 * @return CSR (compressed sparse row) pointer
-	 */
-	public CSRPointer getSparseMatrixCudaPointer() {
-		return jcudaSparseMatrixPtr;
-	}
-	
-	/**
-	 * Convenience method to directly set the sparse matrix on GPU
-	 * Make sure to call {@link #setDeviceModify(long)} after this to set appropriate state, if you are not sure what you are doing.
-	 * Needed for operations like {@link JCusparse#cusparseDcsrgemm(cusparseHandle, int, int, int, int, int, cusparseMatDescr, int, Pointer, Pointer, Pointer, cusparseMatDescr, int, Pointer, Pointer, Pointer, cusparseMatDescr, Pointer, Pointer, Pointer)}
-	 * @param sparseMatrixPtr CSR (compressed sparse row) pointer
-	 */
-	public synchronized void setSparseMatrixCudaPointer(CSRPointer sparseMatrixPtr) {
-		this.jcudaSparseMatrixPtr = sparseMatrixPtr;
-		this.isInSparseFormat = true;
-		if(jcudaDenseMatrixPtr != null) {
-			cudaFreeHelper(jcudaDenseMatrixPtr);
-			jcudaDenseMatrixPtr = null;
-		}
-	}
-
-	/**
-	 * Convenience method to directly set the dense matrix pointer on GPU
-	 * Make sure to call {@link #setDeviceModify(long)} after this to set appropriate state, if you are not sure what you are doing.
-	 * 
-	 * @param densePtr dense pointer
-	 */
-	public synchronized void setDenseMatrixCudaPointer(Pointer densePtr){
-		this.jcudaDenseMatrixPtr = densePtr;
-		this.isInSparseFormat = false;
-		if(jcudaSparseMatrixPtr != null) {
-			jcudaSparseMatrixPtr.deallocate();
-			jcudaSparseMatrixPtr = null;
-		}
-	}
-	
-	/**
-	 * Converts this JCudaObject from dense to sparse format.
-	 * 
-	 * @throws DMLRuntimeException if DMLRuntimeException occurs
-	 */
-	public void denseToSparse() throws DMLRuntimeException {
-		long t0=0;
-		if (DMLScript.STATISTICS) t0 = System.nanoTime();
-		cusparseHandle cusparseHandle = LibMatrixCUDA.cusparseHandle;
-		if(cusparseHandle == null)
-			throw new DMLRuntimeException("Expected cusparse to be initialized");
-		int rows = toIntExact(mat.getNumRows());
-		int cols = toIntExact(mat.getNumColumns());
-		
-		if(jcudaDenseMatrixPtr == null || !isAllocated())
-			throw new DMLRuntimeException("Expected allocated dense matrix before denseToSparse() call");
-
-		convertDensePtrFromRowMajorToColumnMajor();
-		setSparseMatrixCudaPointer(columnMajorDenseToRowMajorSparse(cusparseHandle, rows, cols, jcudaDenseMatrixPtr));
-		// TODO: What if mat.getNnz() is -1 ?
-		numBytes = CSRPointer.estimateSize(mat.getNnz(), rows);
-		if (DMLScript.STATISTICS) GPUStatistics.cudaDenseToSparseTime.addAndGet(System.nanoTime() - t0);
-		if (DMLScript.STATISTICS) GPUStatistics.cudaDenseToSparseCount.addAndGet(1);
-	}
-
-	/**
-	 * Transposes a dense matrix on the GPU by calling the cublasDgeam operation
-	 * @param densePtr	Pointer to dense matrix on the GPU
-	 * @param m			rows in ouput matrix
-	 * @param n			columns in output matrix
-	 * @param lda		rows in input matrix
-	 * @param ldc		columns in output matrix
-	 * @return			transposed matrix
-	 * @throws DMLRuntimeException if operation failed
-	 */
-	public static Pointer transpose(Pointer densePtr, int m, int n, int lda, int ldc) throws DMLRuntimeException {
-		Pointer alpha = LibMatrixCUDA.pointerTo(1.0);
-		Pointer beta = LibMatrixCUDA.pointerTo(0.0);
-		Pointer A = densePtr;
-		Pointer C = JCudaObject.allocate(((long)m)*getDoubleSizeOf(n));
-
-		// Transpose the matrix to get a dense matrix
-		JCublas2.cublasDgeam(LibMatrixCUDA.cublasHandle, CUBLAS_OP_T, CUBLAS_OP_T, m, n, alpha, A, lda, beta, new Pointer(), lda, C, ldc);
-		return C;
-	}
-
-	/**
-	 * Convenience method. Converts Row Major Dense Matrix --> Column Major Dense Matrix
-	 * @throws DMLRuntimeException if DMLRuntimeException occurs
-	 */
-	private void convertDensePtrFromRowMajorToColumnMajor() throws DMLRuntimeException {
-		int m = toIntExact(mat.getNumRows());
-		int n = toIntExact(mat.getNumColumns());
-		int lda = n;
-		int ldc = m;
-		if(!isAllocated()) {
-			throw new DMLRuntimeException("Error in converting row major to column major : data is not allocated");
-		}
-
-		Pointer tmp = transpose(jcudaDenseMatrixPtr, m, n, lda, ldc);
-		cudaFreeHelper(jcudaDenseMatrixPtr);
-		setDenseMatrixCudaPointer(tmp);
-	}
-
-	private void convertDensePtrFromColMajorToRowMajor() throws DMLRuntimeException {
-		int n = toIntExact(mat.getNumRows());
-		int m = toIntExact(mat.getNumColumns());
-		int lda = n;
-	    int ldc = m;
-		if(!isAllocated()) {
-			throw new DMLRuntimeException("Error in converting column major to row major : data is not allocated");
-		}
-
-		Pointer tmp = transpose(jcudaDenseMatrixPtr, m, n, lda, ldc);
-		cudaFreeHelper(jcudaDenseMatrixPtr);
-		setDenseMatrixCudaPointer(tmp);
-	}
-	
-	/**
-	 * Convert sparse to dense (Performs transpose, use sparseToColumnMajorDense if the kernel can deal with column major format)
-	 * 
-	 * @throws DMLRuntimeException if DMLRuntimeException occurs
-	 */
-	public void sparseToDense() throws DMLRuntimeException {
-		sparseToDense(null);
-	}
-
-	/**
-	 * Convert sparse to dense (Performs transpose, use sparseToColumnMajorDense if the kernel can deal with column major format)
-	 * Also records per instruction invokation of sparseToDense.
-	 * @param instructionName	Name of the instruction for which statistics are recorded in {@link GPUStatistics}
-	 * @throws DMLRuntimeException ?
-	 */
-	public void sparseToDense(String instructionName) throws DMLRuntimeException {
-		long start=0, end=0;
-		if (DMLScript.STATISTICS) start = System.nanoTime();
-		if(jcudaSparseMatrixPtr == null || !isAllocated())
-			throw new DMLRuntimeException("Expected allocated sparse matrix before sparseToDense() call");
-
-		sparseToColumnMajorDense();
-		convertDensePtrFromColMajorToRowMajor();
-		if (DMLScript.STATISTICS) end = System.nanoTime();
-		if (instructionName != null && GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instructionName, GPUInstruction.MISC_TIMER_SPARSE_TO_DENSE, end - start);
-		if (DMLScript.STATISTICS) GPUStatistics.cudaSparseToDenseTime.addAndGet(end - start);
-		if (DMLScript.STATISTICS) GPUStatistics.cudaSparseToDenseCount.addAndGet(1);
-	}
-	
-	/**
-	 * More efficient method to convert sparse to dense but returns dense in column major format
-	 * 
-	 * @throws DMLRuntimeException if DMLRuntimeException occurs
-	 */
-	public void sparseToColumnMajorDense() throws DMLRuntimeException {
-		if(jcudaSparseMatrixPtr == null || !isAllocated())
-			throw new DMLRuntimeException("Expected allocated sparse matrix before sparseToDense() call");
-		
-		cusparseHandle cusparseHandle = LibMatrixCUDA.cusparseHandle;
-		if(cusparseHandle == null)
-			throw new DMLRuntimeException("Expected cusparse to be initialized");
-		int rows = toIntExact(mat.getNumRows());
-		int cols = toIntExact(mat.getNumColumns());
-		setDenseMatrixCudaPointer(jcudaSparseMatrixPtr.toColumnMajorDenseMatrix(cusparseHandle, null, rows, cols));
-		numBytes = ((long)rows)*getDoubleSizeOf(cols);
-	}
-	
-	/**
-	 * Convenience method to convert a CSR matrix to a dense matrix on the GPU
-	 * Since the allocated matrix is temporary, bookkeeping is not updated.
-	 * Also note that the input dense matrix is expected to be in COLUMN MAJOR FORMAT
-	 * Caller is responsible for deallocating memory on GPU.
-	 * 
-	 * @param cusparseHandle handle to cusparse library
-	 * @param rows number of rows
-	 * @param cols number of columns
-	 * @param densePtr [in] dense matrix pointer on the GPU in row major
-	 * @return CSR (compressed sparse row) pointer
-	 * @throws DMLRuntimeException if DMLRuntimeException occurs
-	 */
-	public static CSRPointer columnMajorDenseToRowMajorSparse(cusparseHandle cusparseHandle, int rows, int cols, Pointer densePtr) throws DMLRuntimeException {
-		cusparseMatDescr matDescr = CSRPointer.getDefaultCuSparseMatrixDescriptor();
-		Pointer nnzPerRowPtr = null;
-		Pointer nnzTotalDevHostPtr = null;
-		
-		ensureFreeSpace(getIntSizeOf(rows + 1));
-		nnzPerRowPtr = allocate(getIntSizeOf(rows));
-		nnzTotalDevHostPtr = allocate(getIntSizeOf(1));
-		
-		// Output is in dense vector format, convert it to CSR
-		cusparseDnnz(cusparseHandle, cusparseDirection.CUSPARSE_DIRECTION_ROW, rows, cols, matDescr, densePtr, rows, nnzPerRowPtr, nnzTotalDevHostPtr);
-		cudaDeviceSynchronize();
-		int[] nnzC = {-1};
-
-		long t2=0;
-		if (DMLScript.STATISTICS) t2 = System.nanoTime();
-		cudaMemcpy(Pointer.to(nnzC), nnzTotalDevHostPtr, getIntSizeOf(1), cudaMemcpyDeviceToHost);
-		if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevTime.addAndGet(System.nanoTime() - t2);
-		if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevCount.addAndGet(1);
-		
-		if (nnzC[0] == -1){
-			throw new DMLRuntimeException("cusparseDnnz did not calculate the correct number of nnz from the sparse-matrix vector mulitply on the GPU");
-		}
-		
-		CSRPointer C = CSRPointer.allocateEmpty(nnzC[0], rows);		
-		cusparseDdense2csr(cusparseHandle, rows, cols, matDescr, densePtr, rows, nnzPerRowPtr, C.val, C.rowPtr, C.colInd);
-		cudaDeviceSynchronize();
-
-		cudaFreeHelper(nnzPerRowPtr);
-		cudaFreeHelper(nnzTotalDevHostPtr);
-		
-		return C;
-	}
-
-
-	/** Map of free blocks allocate on GPU. maps size_of_block -> pointer on GPU */
-	static LRUCacheMap<Long, LinkedList<Pointer>> freeCUDASpaceMap = new LRUCacheMap<Long, LinkedList<Pointer>>();
-	/** To record size of allocated blocks */
-	static HashMap<Pointer, Long> cudaBlockSizeMap = new HashMap<Pointer, Long>();
-
-
-	/**
-	 * Convenience method for {@link #allocate(String, long, int)}, defaults statsCount to 1.
-	 * @param size size of data (in bytes) to allocate
-	 * @return jcuda pointer
-	 * @throws DMLRuntimeException if DMLRuntimeException occurs
-	 */
-	public static Pointer allocate(long size) throws DMLRuntimeException {
-		return allocate(null, size, 1);
-	}
-
-	/**
-	 * Convenience method for {@link #allocate(String, long, int)}, defaults statsCount to 1.
-	 * @param instructionName name of instruction for which to record per instruction performance statistics, null if don't want to record
-	 * @param size size of data (in bytes) to allocate
-	 * @return jcuda pointer
-	 * @throws DMLRuntimeException if DMLRuntimeException occurs
-	 */
-	public static Pointer allocate(String instructionName, long size) throws DMLRuntimeException {
-		return allocate(instructionName, size, 1);
-	}
-
-	/**
-	 * Allocates temporary space on the device.
-	 * Does not update bookkeeping.
-	 * The caller is responsible for freeing up after usage.
-	 * @param instructionName name of instruction for which to record per instruction performance statistics, null if don't want to record
-	 * @param size   			Size of data (in bytes) to allocate
-	 * @param statsCount	amount to increment the cudaAllocCount by
-	 * @return jcuda Pointer
-	 * @throws DMLRuntimeException if DMLRuntimeException occurs
-	 */
-	public static Pointer allocate(String instructionName, long size, int statsCount) throws DMLRuntimeException{
-		long t0=0, t1=0, end=0;
-		synchronized (JCudaContext.syncObj) {
-			Pointer A;
-			if (freeCUDASpaceMap.containsKey(size)) {
-				if (instructionName != null && GPUStatistics.DISPLAY_STATISTICS) t0 = System.nanoTime();
-				LinkedList<Pointer> freeList = freeCUDASpaceMap.get(size);
-				A = freeList.pop();
-				if (freeList.isEmpty())
-					freeCUDASpaceMap.remove(size);
-				if (instructionName != null && GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instructionName, GPUInstruction.MISC_TIMER_REUSE, System.nanoTime() - t0);
-			} else {
-				if (DMLScript.STATISTICS) t0 = System.nanoTime();
-				ensureFreeSpace(instructionName, size);
-				A = new Pointer();
-				cudaMalloc(A, size);
-				((JCudaContext)(JCudaContext.currContext)).deviceMemBytes.addAndGet(size);
-				if (DMLScript.STATISTICS) GPUStatistics.cudaAllocTime.getAndAdd(System.nanoTime() - t0);
-				if (DMLScript.STATISTICS) GPUStatistics.cudaAllocCount.getAndAdd(statsCount);
-				if (instructionName != null && GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instructionName, GPUInstruction.MISC_TIMER_ALLOCATE, System.nanoTime() - t0);
-			}
-			// Set all elements to 0 since newly allocated space will contain garbage
-			if (DMLScript.STATISTICS) t1 = System.nanoTime();
-			cudaMemset(A, 0, size);
-			if (DMLScript.STATISTICS) end = System.nanoTime();
-			if (instructionName != null && GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instructionName, GPUInstruction.MISC_TIMER_SET_ZERO, end - t1);
-			if (DMLScript.STATISTICS) GPUStatistics.cudaMemSet0Time.getAndAdd(end - t1);
-			if (DMLScript.STATISTICS) GPUStatistics.cudaMemSet0Count.getAndAdd(1);
-			cudaBlockSizeMap.put(A, size);
-			return A;
-		}
-	}
-
-	/**
-	 * Does lazy cudaFree calls
-	 * @param toFree {@link Pointer} instance to be freed
-	 */
-	public static void cudaFreeHelper(final Pointer toFree) {
-		cudaFreeHelper(null, toFree, false);
-	}
-
-	/**
-	 * does lazy/eager cudaFree calls
-	 * @param toFree {@link Pointer} instance to be freed
-	 * @param eager true if to be done eagerly
-	 */
-	public static void cudaFreeHelper(final Pointer toFree, boolean eager) {
-		cudaFreeHelper(null, toFree, eager);
-	}
-
-	/**
-	 * Does lazy cudaFree calls
-	 * @param instructionName name of the instruction for which to record per instruction free time, null if do not want to record
-	 * @param toFree {@link Pointer} instance to be freed
-	 */
-	public static void cudaFreeHelper(String instructionName, final Pointer toFree) {
-		cudaFreeHelper(instructionName, toFree, false);
-	}
-
-	/**
-	 * Does cudaFree calls, lazily
-	 * @param instructionName name of the instruction for which to record per instruction free time, null if do not want to record
-	 * @param toFree {@link Pointer} instance to be freed
-	 * @param eager true if to be done eagerly
-	 */
-	public static void cudaFreeHelper(String instructionName, final Pointer toFree, boolean eager){
-		long t0 = 0;
-		assert cudaBlockSizeMap.containsKey(toFree) : "ERROR : Internal state corrupted, cache block size map is not aware of a block it trying to free up";
-		long size = cudaBlockSizeMap.get(toFree);
-		if (eager) {
-			if (DMLScript.STATISTICS) t0 = System.nanoTime();
-			((JCudaContext)(JCudaContext.currContext)).deviceMemBytes.addAndGet(-size);
-			cudaFree(toFree);
-			cudaBlockSizeMap.remove(toFree);
-			if (DMLScript.STATISTICS) GPUStatistics.cudaDeAllocTime.addAndGet(System.nanoTime() - t0);
-			if (DMLScript.STATISTICS) GPUStatistics.cudaDeAllocCount.addAndGet(1);
-			if (instructionName != null && GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instructionName, GPUInstruction.MISC_TIMER_CUDA_FREE, System.nanoTime() - t0);
-		} else {
-			LinkedList<Pointer> freeList = freeCUDASpaceMap.get(size);
-			if (freeList == null) {
-				freeList = new LinkedList<Pointer>();
-				freeCUDASpaceMap.put(size, freeList);
-			}
-			freeList.add(toFree);
-		}
-	}
-
-
-	/**
-	 * Gets the double array from GPU memory onto host memory and returns string.
-	 * @param A Pointer to memory on device (GPU), assumed to point to a double array
-	 * @param rows rows in matrix A
-     * @param cols columns in matrix A
-	 * @return the debug string
-	 * @throws DMLRuntimeException  if DMLRuntimeException occurs
-	 */
-	public static String debugString(Pointer A, long rows, long cols) throws DMLRuntimeException {
-		StringBuffer sb = new StringBuffer();
-        int len = toIntExact(rows * cols);
-		double[] tmp = new double[len];
-		cudaMemcpy(Pointer.to(tmp), A, getDoubleSizeOf(len), cudaMemcpyDeviceToHost);
-        int k = 0;
-		for (int i=0; i<rows; i++){
-            for (int j=0; j<cols; j++){
-			   sb.append(tmp[k]).append(' ');
-               k++;
-            }
-            sb.append('\n');
-		}
-		return sb.toString();
-	}
-}


[4/5] incubator-systemml git commit: Refactored GPU{Contex, Object} to make it friendlier for parfor

Posted by na...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/GPUContext.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/GPUContext.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/GPUContext.java
index d2309b0..708f291 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/GPUContext.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/GPUContext.java
@@ -18,65 +18,584 @@
  */
 package org.apache.sysml.runtime.instructions.gpu.context;
 
+import static jcuda.jcublas.JCublas2.cublasCreate;
+import static jcuda.jcublas.JCublas2.cublasDestroy;
+import static jcuda.jcudnn.JCudnn.cudnnCreate;
+import static jcuda.jcudnn.JCudnn.cudnnDestroy;
+import static jcuda.jcusparse.JCusparse.cusparseCreate;
+import static jcuda.jcusparse.JCusparse.cusparseDestroy;
+import static jcuda.runtime.JCuda.cudaDeviceScheduleBlockingSync;
+import static jcuda.runtime.JCuda.cudaFree;
+import static jcuda.runtime.JCuda.cudaGetDeviceCount;
+import static jcuda.runtime.JCuda.cudaMalloc;
+import static jcuda.runtime.JCuda.cudaMemGetInfo;
+import static jcuda.runtime.JCuda.cudaMemset;
+import static jcuda.runtime.JCuda.cudaSetDevice;
+import static jcuda.runtime.JCuda.cudaSetDeviceFlags;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.sysml.api.DMLScript;
-import org.apache.sysml.hops.OptimizerUtils;
+import org.apache.sysml.conf.ConfigurationManager;
+import org.apache.sysml.conf.DMLConfig;
 import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.controlprogram.caching.MatrixObject;
+import org.apache.sysml.runtime.instructions.gpu.GPUInstruction;
+import org.apache.sysml.utils.GPUStatistics;
+import org.apache.sysml.utils.LRUCacheMap;
 
-//FIXME merge JCudaContext into GPUContext as this context is anyway CUDA specific
+import jcuda.Pointer;
+import jcuda.jcublas.cublasHandle;
+import jcuda.jcudnn.cudnnHandle;
+import jcuda.jcusparse.cusparseHandle;
+import jcuda.runtime.JCuda;
+import jcuda.runtime.cudaDeviceProp;
 
-public abstract class GPUContext {
+/**
+ * Represents a context per GPU accessible through the same JVM
+ * Each context holds cublas, cusparse, cudnn... handles which are separate for each GPU
+ */
+public class GPUContext {
 
-	protected static GPUContext currContext;
-	public static volatile Boolean isGPUContextCreated = false;
+	protected static final Log LOG = LogFactory.getLog(GPUContext.class.getName());
 
-	protected GPUContext() {}
+  /** Eviction policies for {@link GPUContext#evict(long)} */
+	public enum EvictionPolicy {
+		LRU, LFU, MIN_EVICT
+	}
 
-	/**
-	 * Gets device memory available for SystemML operations
-	 * 
-	 * @return available memory
-	 */
-	public abstract long getAvailableMemory();
+	/** currently employed eviction policy */
+	public final EvictionPolicy evictionPolicy = EvictionPolicy.LRU;
+
+	/** Map of free blocks allocate on GPU. maps size_of_block -> pointer on GPU */
+	private LRUCacheMap<Long, LinkedList<Pointer>> freeCUDASpaceMap = new LRUCacheMap<>();
+
+	/** To record size of allocated blocks */
+	private HashMap<Pointer, Long> cudaBlockSizeMap = new HashMap<>();
+
+  /** active device assigned to this GPUContext instance */
+  private final int deviceNum;
+
+  /** list of allocated {@link GPUObject} instances allocated on {@link GPUContext#deviceNum} GPU
+   * These are matrices allocated on the GPU on which rmvar hasn't been called yet.
+   * If a {@link GPUObject} has more than one lock on it, it cannot be freed
+   * If it has zero locks on it, it can be freed, but it is preferrable to keep it around
+   * so that an extraneous host to dev transfer can be avoided */
+  private ArrayList<GPUObject> allocatedGPUObjects = new ArrayList<>();
+
+  /** cudnnHandle specific to the active GPU for this GPUContext */
+  private cudnnHandle cudnnHandle;
+
+  /** cublasHandle specific to the active GPU for this GPUContext */
+  private cublasHandle cublasHandle;
+
+  /** cusparseHandle specific to the active GPU for this GPUContext */
+  private cusparseHandle cusparseHandle;
+
+  /** to launch custom CUDA kernel, specific to the active GPU for this GPUContext */
+  private JCudaKernels kernels;
 
 	/**
-	 * Ensures that all the CUDA cards on the current system are
-	 * of the minimum required compute capability.
-	 * (The minimum required compute capability is hard coded in {@link JCudaContext}.
-	 * 
-	 * @throws DMLRuntimeException if DMLRuntimeException occurs
-	 */
-	public abstract void ensureComputeCapability() throws DMLRuntimeException;
-	
-	/**
-	 * Singleton Factory method for creation of {@link GPUContext}
-	 * @return GPU context
-	 * @throws DMLRuntimeException if DMLRuntimeException occurs
+	 * The minimum CUDA Compute capability needed for SystemML.
+	 * After compute capability 3.0, 2^31 - 1 blocks and 1024 threads per block are supported.
+	 * If SystemML needs to run on an older card, this logic can be revisited.
 	 */
-	public static GPUContext getGPUContext() throws DMLRuntimeException {
-		if(currContext == null && DMLScript.USE_ACCELERATOR) {
-			synchronized(isGPUContextCreated) {
-				currContext = new JCudaContext();
-				currContext.ensureComputeCapability();
-				OptimizerUtils.GPU_MEMORY_BUDGET = currContext.getAvailableMemory();
-				isGPUContextCreated = true;
-			}
-		}
-		return currContext;
-	}
-	
-	public static GPUObject createGPUObject(MatrixObject mo) {
-		if(DMLScript.USE_ACCELERATOR) {
-			synchronized(isGPUContextCreated) {
-				if(currContext == null)
-					throw new RuntimeException("GPUContext is not created");
-				if(currContext instanceof JCudaContext)
-					return new JCudaObject(mo);
-			}
-		}
-		throw new RuntimeException("Cannot create createGPUObject when USE_ACCELERATOR is off");
-	}
-	public abstract void destroy() throws DMLRuntimeException;
-	
-	
+	final int MAJOR_REQUIRED = 3;
+	final int MINOR_REQUIRED = 0;
+
+  // Invoke cudaMemGetInfo to get available memory information. Useful if GPU is shared among multiple application.
+  public double GPU_MEMORY_UTILIZATION_FACTOR = ConfigurationManager.getDMLConfig().getDoubleValue(DMLConfig.GPU_MEMORY_UTILIZATION_FACTOR);
+
+  protected GPUContext(int deviceNum) throws DMLRuntimeException {
+    this.deviceNum = deviceNum;
+    cudaSetDevice(deviceNum);
+
+    cudaSetDeviceFlags(cudaDeviceScheduleBlockingSync);
+
+    long free[] = {0};
+    long total[] = {0};
+    cudaMemGetInfo(free, total);
+
+    long start = System.nanoTime();
+    cudnnHandle = new cudnnHandle();
+    cudnnCreate(cudnnHandle);
+    cublasHandle = new cublasHandle();
+    cublasCreate(cublasHandle);
+    // For cublas v2, cublasSetPointerMode tells Cublas whether to expect scalar arguments on device or on host
+    // This applies to arguments like "alpha" in Dgemm, and "y" in Ddot.
+    // cublasSetPointerMode(LibMatrixCUDA.cublasHandle, cublasPointerMode.CUBLAS_POINTER_MODE_DEVICE);
+    cusparseHandle = new cusparseHandle();
+    cusparseCreate(cusparseHandle);
+    kernels = new JCudaKernels(deviceNum);
+
+    GPUStatistics.cudaLibrariesInitTime = System.nanoTime() - start;
+    LOG.info(" GPU memory - Total: " + (total[0] * (1e-6)) + " MB, Available: " + (free[0] * (1e-6)) + " MB on " + this);
+
+  }
+
+  public int getDeviceNum() {
+    return deviceNum;
+  }
+
+  /**
+   * Sets the device for the calling thread.
+   * This method must be called after {@link GPUContextPool#getFromPool()}
+   * is called.
+   * If in a multi-threaded env like parfor, this method must be called when in the
+   * appropriate thread
+   */
+  public void initializeThread() {
+    cudaSetDevice(deviceNum);
+  }
+
+  @SuppressWarnings("unused")
+  public static int cudaGetDevice() {
+    int[] device = new int[1];
+    JCuda.cudaGetDevice(device);
+    return device[0];
+  }
+
+  /**
+   * Convenience method for {@link #allocate(String, long, int)}, defaults statsCount to 1.
+   *
+   * @param size size of data (in bytes) to allocate
+   * @return jcuda pointer
+   * @throws DMLRuntimeException if DMLRuntimeException occurs
+   */
+  public Pointer allocate(long size) throws DMLRuntimeException {
+    return allocate(null, size, 1);
+  }
+
+  /**
+   * Convenience method for {@link #allocate(String, long, int)}, defaults statsCount to 1.
+   *
+   * @param instructionName name of instruction for which to record per instruction performance statistics, null if don't want to record
+   * @param size            size of data (in bytes) to allocate
+   * @return jcuda pointer
+   * @throws DMLRuntimeException if DMLRuntimeException occurs
+   */
+  public Pointer allocate(String instructionName, long size) throws DMLRuntimeException {
+    return allocate(instructionName, size, 1);
+  }
+
+  /**
+   * Allocates temporary space on the device.
+   * Does not update bookkeeping.
+   * The caller is responsible for freeing up after usage.
+   *
+   * @param instructionName name of instruction for which to record per instruction performance statistics, null if don't want to record
+   * @param size            Size of data (in bytes) to allocate
+   * @param statsCount      amount to increment the cudaAllocCount by
+   * @return jcuda Pointer
+   * @throws DMLRuntimeException if DMLRuntimeException occurs
+   */
+  public Pointer allocate(String instructionName, long size, int statsCount) throws DMLRuntimeException {
+    long t0 = 0, t1 = 0, end = 0;
+    Pointer A;
+    if (freeCUDASpaceMap.containsKey(size)) {
+      LOG.trace("GPU : in allocate from instruction " + instructionName + ", found free block of size " + (size / 1024.0) + " Kbytes from previously allocated block on " + this);
+      if (instructionName != null && GPUStatistics.DISPLAY_STATISTICS) t0 = System.nanoTime();
+      LinkedList<Pointer> freeList = freeCUDASpaceMap.get(size);
+      A = freeList.pop();
+      if (freeList.isEmpty())
+        freeCUDASpaceMap.remove(size);
+      if (instructionName != null && GPUStatistics.DISPLAY_STATISTICS)
+        GPUStatistics.maintainCPMiscTimes(instructionName, GPUInstruction.MISC_TIMER_REUSE, System.nanoTime() - t0);
+    } else {
+      LOG.trace("GPU : in allocate from instruction " + instructionName + ", allocating new block of size " + (size / 1024.0) + " Kbytes on " + this);
+      if (DMLScript.STATISTICS) t0 = System.nanoTime();
+      ensureFreeSpace(instructionName, size);
+      A = new Pointer();
+      cudaMalloc(A, size);
+      if (DMLScript.STATISTICS) GPUStatistics.cudaAllocTime.getAndAdd(System.nanoTime() - t0);
+      if (DMLScript.STATISTICS) GPUStatistics.cudaAllocCount.getAndAdd(statsCount);
+      if (instructionName != null && GPUStatistics.DISPLAY_STATISTICS)
+        GPUStatistics.maintainCPMiscTimes(instructionName, GPUInstruction.MISC_TIMER_ALLOCATE, System.nanoTime() - t0);
+    }
+    // Set all elements to 0 since newly allocated space will contain garbage
+    if (DMLScript.STATISTICS) t1 = System.nanoTime();
+    LOG.trace("GPU : in allocate from instruction " + instructionName + ", setting block of size " + (size / 1024.0) + " Kbytes to zero on " + this);
+    cudaMemset(A, 0, size);
+    if (DMLScript.STATISTICS) end = System.nanoTime();
+    if (instructionName != null && GPUStatistics.DISPLAY_STATISTICS)
+      GPUStatistics.maintainCPMiscTimes(instructionName, GPUInstruction.MISC_TIMER_SET_ZERO, end - t1);
+    if (DMLScript.STATISTICS) GPUStatistics.cudaMemSet0Time.getAndAdd(end - t1);
+    if (DMLScript.STATISTICS) GPUStatistics.cudaMemSet0Count.getAndAdd(1);
+    cudaBlockSizeMap.put(A, size);
+    return A;
+
+  }
+
+  /**
+   * Does lazy cudaFree calls
+   *
+   * @param toFree {@link Pointer} instance to be freed
+   */
+  public void cudaFreeHelper(final Pointer toFree) {
+    cudaFreeHelper(null, toFree, false);
+  }
+
+  /**
+   * does lazy/eager cudaFree calls
+   *
+   * @param toFree {@link Pointer} instance to be freed
+   * @param eager  true if to be done eagerly
+   */
+  public void cudaFreeHelper(final Pointer toFree, boolean eager) {
+    cudaFreeHelper(null, toFree, eager);
+  }
+
+  /**
+   * Does lazy cudaFree calls
+   *
+   * @param instructionName name of the instruction for which to record per instruction free time, null if do not want to record
+   * @param toFree          {@link Pointer} instance to be freed
+   */
+  public void cudaFreeHelper(String instructionName, final Pointer toFree) {
+    cudaFreeHelper(instructionName, toFree, false);
+  }
+
+  /**
+   * Does cudaFree calls, lazily
+   *
+   * @param instructionName name of the instruction for which to record per instruction free time, null if do not want to record
+   * @param toFree          {@link Pointer} instance to be freed
+   * @param eager           true if to be done eagerly
+   */
+  public void cudaFreeHelper(String instructionName, final Pointer toFree, boolean eager) {
+    long t0 = 0;
+    assert cudaBlockSizeMap.containsKey(toFree) : "ERROR : Internal state corrupted, cache block size map is not aware of a block it trying to free up";
+    long size = cudaBlockSizeMap.get(toFree);
+    if (eager) {
+      LOG.trace("GPU : eagerly freeing cuda memory [ " + toFree + " ] for instruction " + instructionName + " on " + this);
+      if (DMLScript.STATISTICS) t0 = System.nanoTime();
+      cudaFree(toFree);
+      cudaBlockSizeMap.remove(toFree);
+      if (DMLScript.STATISTICS) GPUStatistics.cudaDeAllocTime.addAndGet(System.nanoTime() - t0);
+      if (DMLScript.STATISTICS) GPUStatistics.cudaDeAllocCount.addAndGet(1);
+      if (instructionName != null && GPUStatistics.DISPLAY_STATISTICS)
+        GPUStatistics.maintainCPMiscTimes(instructionName, GPUInstruction.MISC_TIMER_CUDA_FREE, System.nanoTime() - t0);
+    } else {
+      LOG.trace("GPU : lazily freeing cuda memory for instruction " + instructionName + " on " + this);
+      LinkedList<Pointer> freeList = freeCUDASpaceMap.get(size);
+      if (freeList == null) {
+        freeList = new LinkedList<Pointer>();
+        freeCUDASpaceMap.put(size, freeList);
+      }
+      freeList.add(toFree);
+    }
+  }
+
+  /**
+   * Thin wrapper over {@link GPUContext#evict(long)}
+   *
+   * @param size size to check
+   * @throws DMLRuntimeException if DMLRuntimeException occurs
+   */
+  void ensureFreeSpace(long size) throws DMLRuntimeException {
+    ensureFreeSpace(null, size);
+  }
+
+  /**
+   * Thin wrapper over {@link GPUContext#evict(long)}
+   *
+   * @param instructionName instructionName name of the instruction for which performance measurements are made
+   * @param size            size to check
+   * @throws DMLRuntimeException if DMLRuntimeException occurs
+   */
+  void ensureFreeSpace(String instructionName, long size) throws DMLRuntimeException {
+    if (size >= getAvailableMemory()) {
+      evict(instructionName, size);
+    }
+  }
+
+  /**
+   * Convenience wrapper over {@link GPUContext#evict(String, long)}
+   *
+   * @param GPUSize Desired size to be freed up on the GPU
+   * @throws DMLRuntimeException If no blocks to free up or if not enough blocks with zero locks on them.
+   */
+  protected void evict(final long GPUSize) throws DMLRuntimeException {
+    evict(null, GPUSize);
+  }
+
+  /**
+   * Memory on the GPU is tried to be freed up until either a chunk of needed size is freed up
+   * or it fails.
+   * First the set of reusable blocks is freed up. If that isn't enough, the set of allocated matrix
+   * blocks with zero locks on them is freed up.
+   * The process cycles through the sorted list of allocated {@link GPUObject} instances. Sorting is based on
+   * number of (read) locks that have been obtained on it (reverse order). It repeatedly frees up
+   * blocks on which there are zero locks until the required size has been freed up.
+   * // TODO: update it with hybrid policy
+   *
+   * @param instructionName name of the instruction for which performance measurements are made
+   * @param neededSize      desired size to be freed up on the GPU
+   * @throws DMLRuntimeException If no reusable memory blocks to free up or if not enough matrix blocks with zero locks on them.
+   */
+  protected void evict(String instructionName, final long neededSize) throws DMLRuntimeException {
+    LOG.trace("GPU : evict called from " + instructionName + " for size " + neededSize + " on " + this);
+    GPUStatistics.cudaEvictionCount.addAndGet(1);
+    // Release the set of free blocks maintained in a GPUObject.freeCUDASpaceMap
+    // to free up space
+    LRUCacheMap<Long, LinkedList<Pointer>> lruCacheMap = freeCUDASpaceMap;
+    while (lruCacheMap.size() > 0) {
+      if (neededSize <= getAvailableMemory())
+        break;
+      Map.Entry<Long, LinkedList<Pointer>> toFreeListPair = lruCacheMap.removeAndGetLRUEntry();
+      LinkedList<Pointer> toFreeList = toFreeListPair.getValue();
+      Long size = toFreeListPair.getKey();
+      Pointer toFree = toFreeList.pop();
+      if (toFreeList.isEmpty())
+        lruCacheMap.remove(size);
+      cudaFreeHelper(instructionName, toFree, true);
+    }
+
+    if (neededSize <= getAvailableMemory())
+      return;
+
+    if (allocatedGPUObjects.size() == 0) {
+      throw new DMLRuntimeException("There is not enough memory on device for this matrix!");
+    }
+
+    Collections.sort(allocatedGPUObjects, new Comparator<GPUObject>() {
+      @Override
+      public int compare(GPUObject p1, GPUObject p2) {
+        long p1Val = p1.readLocks.get();
+        long p2Val = p2.readLocks.get();
+
+        if (p1Val > 0 && p2Val > 0) {
+          // Both are locked, so don't sort
+          return 0;
+        } else if (p1Val > 0 || p2Val > 0) {
+          // Put the unlocked one to RHS
+          return Long.compare(p2Val, p1Val);
+        } else {
+          // Both are unlocked
+
+          if (evictionPolicy == EvictionPolicy.MIN_EVICT) {
+            long p1Size = 0;
+            long p2Size = 0;
+            try {
+              p1Size = p1.getSizeOnDevice() - neededSize;
+              p2Size = p2.getSizeOnDevice() - neededSize;
+            } catch (DMLRuntimeException e) {
+              throw new RuntimeException(e);
+            }
+
+            if (p1Size >= 0 && p2Size >= 0) {
+              return Long.compare(p2Size, p1Size);
+            } else {
+              return Long.compare(p1Size, p2Size);
+            }
+          } else if (evictionPolicy == EvictionPolicy.LRU || evictionPolicy == EvictionPolicy.LFU) {
+            return Long.compare(p2.timestamp.get(), p1.timestamp.get());
+          } else {
+            throw new RuntimeException("Unsupported eviction policy:" + evictionPolicy.name());
+          }
+        }
+      }
+    });
+
+    while (neededSize > getAvailableMemory() && allocatedGPUObjects.size() > 0) {
+      GPUObject toBeRemoved = allocatedGPUObjects.get(allocatedGPUObjects.size() - 1);
+      if (toBeRemoved.readLocks.get() > 0) {
+        throw new DMLRuntimeException("There is not enough memory on device for this matrix!");
+      }
+      if (toBeRemoved.dirty) {
+        toBeRemoved.copyFromDeviceToHost();
+      }
+
+      toBeRemoved.clearData(true);
+    }
+  }
+
+  /**
+   * Whether the GPU associated with this {@link GPUContext} has recorded the usage of a certain block
+   *
+   * @param o the block
+   * @return true if present, false otherwise
+   */
+  public boolean isBlockRecorded(GPUObject o) {
+    return allocatedGPUObjects.contains(o);
+  }
+
+  /**
+   * @param o {@link GPUObject} instance to record
+   * @see GPUContext#allocatedGPUObjects
+   * Records the usage of a matrix block
+   */
+  public void recordBlockUsage(GPUObject o) {
+    allocatedGPUObjects.add(o);
+  }
+
+  /**
+   * @param o {@link GPUObject} instance to remove from the list of allocated GPU objects
+   * @see GPUContext#allocatedGPUObjects
+   * Records that a block is not used anymore
+   */
+  public void removeRecordedUsage(GPUObject o) {
+    allocatedGPUObjects.remove(o);
+  }
+
+  /**
+   * Gets the available memory on GPU that SystemML can use
+   *
+   * @return the available memory in bytes
+   */
+  public long getAvailableMemory() {
+    long free[] = {0};
+    long total[] = {0};
+    cudaMemGetInfo(free, total);
+    return (long) (free[0] * GPU_MEMORY_UTILIZATION_FACTOR);
+  }
+
+  /**
+   * Makes sure that GPU that SystemML is trying to use has the minimum compute capability needed
+   *
+   * @throws DMLRuntimeException if the compute capability is less than what is required
+   */
+  public void ensureComputeCapability() throws DMLRuntimeException {
+    int[] devices = {-1};
+    cudaGetDeviceCount(devices);
+    if (devices[0] == -1) {
+      throw new DMLRuntimeException("Call to cudaGetDeviceCount returned 0 devices");
+    }
+    boolean isComputeCapable = true;
+    for (int i = 0; i < devices[0]; i++) {
+      cudaDeviceProp properties = GPUContextPool.getGPUProperties(i);
+      int major = properties.major;
+      int minor = properties.minor;
+      if (major < MAJOR_REQUIRED) {
+        isComputeCapable = false;
+      } else if (major == MAJOR_REQUIRED && minor < MINOR_REQUIRED) {
+        isComputeCapable = false;
+      }
+    }
+    if (!isComputeCapable) {
+      throw new DMLRuntimeException("One of the CUDA cards on the system has compute capability lower than " + MAJOR_REQUIRED + "." + MINOR_REQUIRED);
+    }
+  }
+
+  public GPUObject createGPUObject(MatrixObject mo) {
+    return new GPUObject(this, mo);
+  }
+
+  /**
+   * Gets the device properties for the active GPU (set with cudaSetDevice())
+   *
+   * @return the device properties
+   */
+  public cudaDeviceProp getGPUProperties() throws DMLRuntimeException {
+    return GPUContextPool.getGPUProperties(deviceNum);
+  }
+
+  /**
+   * Gets the maximum number of threads per block for "active" GPU
+   *
+   * @return the maximum number of threads per block
+   */
+  public int getMaxThreadsPerBlock() throws DMLRuntimeException {
+    cudaDeviceProp deviceProps = getGPUProperties();
+    return deviceProps.maxThreadsPerBlock;
+  }
+
+  /**
+   * Gets the maximum number of blocks supported by the active cuda device
+   *
+   * @return the maximum number of blocks supported
+   */
+  public int getMaxBlocks() throws DMLRuntimeException {
+    cudaDeviceProp deviceProp = getGPUProperties();
+    return deviceProp.maxGridSize[0];
+  }
+
+  /**
+   * Gets the shared memory per block supported by the active cuda device
+   *
+   * @return the shared memory per block
+   */
+  public long getMaxSharedMemory() throws DMLRuntimeException {
+    cudaDeviceProp deviceProp = getGPUProperties();
+    return deviceProp.sharedMemPerBlock;
+  }
+
+  /**
+   * Gets the warp size supported by the active cuda device
+   *
+   * @return the warp size
+   */
+  public int getWarpSize() throws DMLRuntimeException {
+    cudaDeviceProp deviceProp = getGPUProperties();
+    return deviceProp.warpSize;
+  }
+
+  public cudnnHandle getCudnnHandle() {
+    return cudnnHandle;
+  }
+
+  public cublasHandle getCublasHandle() {
+    return cublasHandle;
+  }
+
+  public cusparseHandle getCusparseHandle() {
+    return cusparseHandle;
+  }
+
+  public JCudaKernels getKernels() {
+    return kernels;
+  }
+
+  /**
+   * Destroys this GPUContext object
+   * This method MUST BE called so that the GPU is available to be used again
+   *
+   * @throws DMLRuntimeException if error
+   */
+  public void destroy() throws DMLRuntimeException {
+    LOG.trace("GPU : this context was destroyed, this = " + this.toString());
+    clearMemory();
+    cudnnDestroy(cudnnHandle);
+    cublasDestroy(cublasHandle);
+    cusparseDestroy(cusparseHandle);
+    cudnnHandle = null;
+    cublasHandle = null;
+    cusparseHandle = null;
+
+  }
+
+  /**
+   * Clears all memory used by this {@link GPUContext}
+   * Be careful to ensure that no memory is currently being used before invoking this
+   * @throws DMLRuntimeException
+   */
+  public void clearMemory() throws DMLRuntimeException {
+    while (allocatedGPUObjects.isEmpty()) {
+      GPUObject o = allocatedGPUObjects.get(0);
+      o.clearData();
+    }
+    for (LinkedList<Pointer> l : freeCUDASpaceMap.values()) {
+      for (Pointer p : l) {
+        cudaFreeHelper(p, true);
+      }
+    }
+    cudaBlockSizeMap.clear();
+    freeCUDASpaceMap.clear();
+    allocatedGPUObjects.clear();
+  }
+
+  @Override
+  public String toString() {
+    return "GPUContext{" +
+            "deviceNum=" + deviceNum +
+            '}';
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/GPUContextPool.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/GPUContextPool.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/GPUContextPool.java
new file mode 100644
index 0000000..6452651
--- /dev/null
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/GPUContextPool.java
@@ -0,0 +1,158 @@
+/*
+ * 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.runtime.instructions.gpu.context;
+
+import static jcuda.driver.JCudaDriver.cuDeviceGetCount;
+import static jcuda.driver.JCudaDriver.cuInit;
+import static jcuda.runtime.JCuda.cudaGetDeviceProperties;
+
+import java.util.LinkedList;
+import java.util.Queue;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.sysml.runtime.DMLRuntimeException;
+import org.apache.sysml.utils.GPUStatistics;
+
+import jcuda.driver.JCudaDriver;
+import jcuda.jcublas.JCublas2;
+import jcuda.jcudnn.JCudnn;
+import jcuda.jcusparse.JCusparse;
+import jcuda.runtime.JCuda;
+import jcuda.runtime.cudaDeviceProp;
+
+public class GPUContextPool {
+
+  protected static final Log LOG = LogFactory.getLog(GPUContextPool.class.getName());
+
+  /** Maximum number of gpus to use, -1 for all */
+  public static int PER_PROCESS_MAX_GPUS = -1;
+
+  /** Whether cuda has been initialized */
+  static boolean initialized = false;
+
+  /** The total number of cuda devices on this machine */
+  static int deviceCount = -1;
+
+  /** Stores the cached deviceProperties */
+  static cudaDeviceProp[] deviceProperties;
+
+  /** Set of free GPUContexts */
+  static Queue<GPUContext> freePool = new LinkedList<>();
+
+  /**
+   * Static initialization of the number of devices
+   * Also sets behaviour for J{Cuda, Cudnn, Cublas, Cusparse} in case of error
+   * Initializes the CUDA driver
+   * All these need be done once, and not per GPU
+   */
+  public synchronized static void initializeGPU() throws DMLRuntimeException {
+    GPUContext.LOG.info("Initializing CUDA");
+    long start = System.nanoTime();
+    JCuda.setExceptionsEnabled(true);
+    JCudnn.setExceptionsEnabled(true);
+    JCublas2.setExceptionsEnabled(true);
+    JCusparse.setExceptionsEnabled(true);
+    JCudaDriver.setExceptionsEnabled(true);
+    cuInit(0); // Initialize the driver
+
+    int deviceCountArray[] = {0};
+    cuDeviceGetCount(deviceCountArray);        // Obtain the number of devices
+    deviceCount = deviceCountArray[0];
+    deviceProperties = new cudaDeviceProp[deviceCount];
+
+    if (PER_PROCESS_MAX_GPUS > 0)
+       deviceCount = Math.min(PER_PROCESS_MAX_GPUS, deviceCount);
+
+    // Initialize the list of devices
+    for (int i = 0; i < deviceCount; i++) {
+      cudaDeviceProp properties = new cudaDeviceProp();
+      cudaGetDeviceProperties(properties, i);
+      deviceProperties[i] = properties;
+    }
+
+    // Initialize the pool of GPUContexts
+    for (int i=0; i<deviceCount; i++){
+      GPUContext gCtx = new GPUContext(i);
+      freePool.add(gCtx);
+    }
+
+    GPUContext.LOG.info("Total number of GPUs on the machine: " + deviceCount);
+    //int[] device = {-1};
+    //cudaGetDevice(device);
+    //cudaDeviceProp prop = getGPUProperties(device[0]);
+    //int maxBlocks = prop.maxGridSize[0];
+    //int maxThreadsPerBlock = prop.maxThreadsPerBlock;
+    //long sharedMemPerBlock = prop.sharedMemPerBlock;
+    //LOG.debug("Active CUDA device number : " + device[0]);
+    //LOG.debug("Max Blocks/Threads/SharedMem on active device: " + maxBlocks + "/" + maxThreadsPerBlock + "/" + sharedMemPerBlock);
+    initialized = true;
+    GPUStatistics.cudaInitTime = System.nanoTime() - start;
+  }
+
+  /**
+   * Gets an initialized GPUContext from a pool of GPUContexts, each linked to a GPU
+   * @return null if not more GPUContexts in pool, a valid GPUContext otherwise
+   */
+  public static synchronized GPUContext getFromPool() throws DMLRuntimeException {
+    if (!initialized) initializeGPU();
+    GPUContext gCtx = freePool.poll();
+    LOG.trace("GPU : got GPUContext (" + gCtx + ") from freePool. New sizes - FreePool[" + freePool.size() + "]");
+    return gCtx;
+  }
+
+  /**
+   * Get the number of free GPUContexts
+   * @return number of free GPUContexts
+   */
+  public static synchronized int getAvailableCount() {
+    return freePool.size();
+  }
+
+  /**
+   * Gets the device properties
+   * @param device the device number (on a machine with more than 1 GPU)
+   * @return the device properties
+   * @throws DMLRuntimeException if there is problem initializing the GPUContexts
+   */
+  static cudaDeviceProp getGPUProperties(int device) throws DMLRuntimeException {
+    // do once - initialization of GPU
+    if (!initialized) initializeGPU();
+    return deviceProperties[device];
+  }
+
+  public static int getDeviceCount() throws DMLRuntimeException {
+    if (!initialized) initializeGPU();
+    return deviceCount;
+  }
+
+  /**
+   * Returns a {@link GPUContext} back to the pool of {@link GPUContext}s
+   * @param gCtx the GPUContext instance to return. If null, nothing happens
+   * @throws DMLRuntimeException if error
+   */
+  public static synchronized void returnToPool(GPUContext gCtx) throws DMLRuntimeException {
+    if (gCtx == null)
+      return;
+    freePool.add(gCtx);
+    LOG.trace("GPU : returned GPUContext (" + gCtx + ") to freePool. New sizes - FreePool[" + freePool.size() + "]");
+
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/GPUObject.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/GPUObject.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/GPUObject.java
index c116475..3a1fafa 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/GPUObject.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/GPUObject.java
@@ -18,206 +18,799 @@
  */
 package org.apache.sysml.runtime.instructions.gpu.context;
 
-import jcuda.Pointer;
+import static jcuda.jcublas.cublasOperation.CUBLAS_OP_T;
+import static jcuda.jcudnn.JCudnn.cudnnCreateTensorDescriptor;
+import static jcuda.jcudnn.JCudnn.cudnnDestroyTensorDescriptor;
+import static jcuda.jcudnn.JCudnn.cudnnSetTensor4dDescriptor;
+import static jcuda.jcudnn.cudnnDataType.CUDNN_DATA_DOUBLE;
+import static jcuda.jcudnn.cudnnTensorFormat.CUDNN_TENSOR_NCHW;
+import static jcuda.jcusparse.JCusparse.cusparseDdense2csr;
+import static jcuda.jcusparse.JCusparse.cusparseDnnz;
+import static jcuda.runtime.JCuda.cudaMemcpy;
+import static jcuda.runtime.cudaMemcpyKind.cudaMemcpyDeviceToHost;
+import static jcuda.runtime.cudaMemcpyKind.cudaMemcpyHostToDevice;
+
+import java.util.Arrays;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.sysml.api.DMLScript;
 import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.controlprogram.caching.CacheException;
 import org.apache.sysml.runtime.controlprogram.caching.MatrixObject;
+import org.apache.sysml.runtime.instructions.gpu.GPUInstruction;
+import org.apache.sysml.runtime.matrix.data.LibMatrixCUDA;
 import org.apache.sysml.runtime.matrix.data.MatrixBlock;
+import org.apache.sysml.runtime.matrix.data.SparseBlock;
+import org.apache.sysml.runtime.matrix.data.SparseBlockCOO;
+import org.apache.sysml.runtime.matrix.data.SparseBlockCSR;
+import org.apache.sysml.runtime.matrix.data.SparseBlockMCSR;
 import org.apache.sysml.utils.GPUStatistics;
-import org.apache.sysml.utils.LRUCacheMap;
 
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.LinkedList;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.atomic.AtomicLong;
+import jcuda.Pointer;
+import jcuda.jcublas.JCublas2;
+import jcuda.jcudnn.cudnnTensorDescriptor;
+import jcuda.jcusparse.JCusparse;
+import jcuda.jcusparse.cusparseDirection;
+import jcuda.jcusparse.cusparseHandle;
+import jcuda.jcusparse.cusparseMatDescr;
+
+/**
+ * Handle to a matrix block on the GPU
+ */
+public class GPUObject {
+
+	private static final Log LOG = LogFactory.getLog(GPUObject.class.getName());
+
+	/** GPUContext that owns this GPUObject */
+	private final GPUContext gpuContext;
+
+	/** Pointer to the underlying dense matrix block on GPU */
+	private Pointer jcudaDenseMatrixPtr = null;
+
+    /** Pointer to the underlying sparse matrix block on GPU */
+	private CSRPointer jcudaSparseMatrixPtr = null;
 
-//FIXME merge JCudaObject into GPUObject to avoid unnecessary complexity
-public abstract class GPUObject 
-{
-	public enum EvictionPolicy {
-        LRU, LFU, MIN_EVICT
-    }
-	public static final EvictionPolicy evictionPolicy = EvictionPolicy.LRU;
-	protected boolean isDeviceCopyModified = false;
-	protected AtomicInteger numLocks = new AtomicInteger(0);
+	/** An optional tensor descriptor (and shape) that can be set by a tensor instruction such as convolution,
+	 * maxpooling and exploited by a subsequent non-tensor instruction such as relu
+	 */
+	private cudnnTensorDescriptor tensorDescriptor = null;
+
+	/** the shape of this tensor, if in fact this is a tensor */
+	private int [] tensorShape = null;
+
+	/** whether the block attached to this {@link GPUContext} is dirty on the device and needs to be copied back to host */
+	protected boolean dirty = false;
+
+	/** number of read locks on this object */
+	protected AtomicInteger readLocks = new AtomicInteger(0);
+
+	/** Timestamp, needed by {@link GPUContext#evict(long)} */
 	AtomicLong timestamp = new AtomicLong(0);
-	
-	protected boolean isInSparseFormat = false;
+
+	/** Whether this block is in sparse format */
+	protected boolean isSparse = false;
+
+	/** Enclosing {@link MatrixObject} instance */
 	protected MatrixObject mat = null;
-	
-	protected GPUObject(MatrixObject mat2)  {
-		this.mat = mat2;
+
+	private Pointer allocate(String instName, long size) throws DMLRuntimeException {
+		return getGPUContext().allocate(instName, size);
+	}
+
+	private Pointer allocate(long size) throws DMLRuntimeException {
+		return getGPUContext().allocate(size);
+	}
+
+	private void cudaFreeHelper(Pointer toFree) throws DMLRuntimeException {
+		getGPUContext().cudaFreeHelper(toFree);
+	}
+
+	private void cudaFreeHelper(Pointer toFree, boolean eager) throws DMLRuntimeException {
+		getGPUContext().cudaFreeHelper(toFree, eager);
+	}
+
+	private void cudaFreeHelper(String instName, Pointer toFree, boolean eager) throws DMLRuntimeException {
+		getGPUContext().cudaFreeHelper(instName, toFree, eager);
 	}
-	
-	public boolean isInSparseFormat() {
-		return isInSparseFormat;
+
+	private GPUContext getGPUContext() throws DMLRuntimeException {
+		return gpuContext;
 	}
-	
-	public abstract boolean isAllocated();
 
 	/**
-	 * Signal intent that a matrix block will be read (as input) on the GPU
-	 * @return	true if a host memory to device memory transfer happened
-	 * @throws DMLRuntimeException ?
+	 * Transposes a dense matrix on the GPU by calling the cublasDgeam operation
+	 * @param gCtx   a valid {@link GPUContext}
+	 * @param densePtr	Pointer to dense matrix on the GPU
+	 * @param m			rows in ouput matrix
+	 * @param n			columns in output matrix
+	 * @param lda		rows in input matrix
+	 * @param ldc		columns in output matrix
+	 * @return			transposed matrix
+	 * @throws DMLRuntimeException if operation failed
 	 */
-	public abstract boolean acquireDeviceRead() throws DMLRuntimeException;
+	public static Pointer transpose(GPUContext gCtx, Pointer densePtr, int m, int n, int lda, int ldc) throws DMLRuntimeException {
+		LOG.trace("GPU : transpose of block of size [" + m + "," + n + "]" + ", GPUContext=" + gCtx);
+		Pointer alpha = Pointer.to(new double[]{1.0});
+		Pointer beta = Pointer.to(new double[]{0.0});
+		Pointer A = densePtr;
+		Pointer C = gCtx.allocate(((long)m)*getDoubleSizeOf(n));
+
+		// Transpose the matrix to get a dense matrix
+		JCublas2.cublasDgeam(gCtx.getCublasHandle(), CUBLAS_OP_T, CUBLAS_OP_T, m, n, alpha, A, lda, beta, new Pointer(), lda, C, ldc);
+		return C;
+	}
+
 	/**
-	 * To signal intent that a matrix block will be written to on the GPU
-	 * @return	true if memory was allocated on the GPU as a result of this call
+	 * Convenience method to convert a CSR matrix to a dense matrix on the GPU
+	 * Since the allocated matrix is temporary, bookkeeping is not updated.
+	 * Also note that the input dense matrix is expected to be in COLUMN MAJOR FORMAT
+	 * Caller is responsible for deallocating memory on GPU.
+	 * @param gCtx   a valid {@link GPUContext}
+	 * @param cusparseHandle handle to cusparse library
+	 * @param densePtr [in] dense matrix pointer on the GPU in row major
+	 * @param rows number of rows
+	 * @param cols number of columns
+	 * @return CSR (compressed sparse row) pointer
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public abstract boolean acquireDeviceModifyDense() throws DMLRuntimeException;
+	public static CSRPointer columnMajorDenseToRowMajorSparse(GPUContext gCtx, cusparseHandle cusparseHandle, Pointer densePtr, int rows, int cols) throws DMLRuntimeException {
+		cusparseMatDescr matDescr = CSRPointer.getDefaultCuSparseMatrixDescriptor();
+		Pointer nnzPerRowPtr = null;
+		Pointer nnzTotalDevHostPtr = null;
+
+		gCtx.ensureFreeSpace(getIntSizeOf(rows + 1));
+		nnzPerRowPtr = gCtx.allocate(getIntSizeOf(rows));
+		nnzTotalDevHostPtr = gCtx.allocate(getIntSizeOf(1));
+
+		// Output is in dense vector format, convert it to CSR
+		cusparseDnnz(cusparseHandle, cusparseDirection.CUSPARSE_DIRECTION_ROW, rows, cols, matDescr, densePtr, rows, nnzPerRowPtr, nnzTotalDevHostPtr);
+		//cudaDeviceSynchronize();
+		int[] nnzC = {-1};
+
+		long t2=0;
+		if (DMLScript.STATISTICS) t2 = System.nanoTime();
+		cudaMemcpy(Pointer.to(nnzC), nnzTotalDevHostPtr, getIntSizeOf(1), cudaMemcpyDeviceToHost);
+		if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevTime.addAndGet(System.nanoTime() - t2);
+		if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevCount.addAndGet(1);
+
+		if (nnzC[0] == -1){
+			throw new DMLRuntimeException("cusparseDnnz did not calculate the correct number of nnz from the sparse-matrix vector mulitply on the GPU");
+		}
+
+		LOG.trace("GPU : col-major dense size[" + rows + "," + cols + "] to row-major sparse of with nnz = " + nnzC[0] + ", GPUContext=" + gCtx);
+
+		CSRPointer C = CSRPointer.allocateEmpty(gCtx, nnzC[0], rows);
+		cusparseDdense2csr(cusparseHandle, rows, cols, matDescr, densePtr, rows, nnzPerRowPtr, C.val, C.rowPtr, C.colInd);
+		//cudaDeviceSynchronize();
+
+		gCtx.cudaFreeHelper(nnzPerRowPtr);
+		gCtx.cudaFreeHelper(nnzTotalDevHostPtr);
+
+		return C;
+	}
+
+	/**
+	 * Gets the double array from GPU memory onto host memory and returns string.
+	 * @param A Pointer to memory on device (GPU), assumed to point to a double array
+	 * @param rows rows in matrix A
+	 * @param cols columns in matrix A
+	 * @return the debug string
+	 * @throws DMLRuntimeException  if DMLRuntimeException occurs
+	 */
+	@SuppressWarnings("unused")
+	public static String debugString(Pointer A, long rows, long cols) throws DMLRuntimeException {
+		StringBuffer sb = new StringBuffer();
+		int len = toIntExact(rows * cols);
+		double[] tmp = new double[len];
+		cudaMemcpy(Pointer.to(tmp), A, getDoubleSizeOf(len), cudaMemcpyDeviceToHost);
+		int k = 0;
+		for (int i=0; i<rows; i++){
+			for (int j=0; j<cols; j++){
+				sb.append(tmp[k]).append(' ');
+				k++;
+			}
+			sb.append('\n');
+		}
+		return sb.toString();
+	}
+
 	/**
-	 * To signal intent that a sparse matrix block will be written to on the GPU
-	 * @return	true if memory was allocated on the GPU as a result of this call
+	 * Convenience method to directly examine the Sparse matrix on GPU
+	 * @return CSR (compressed sparse row) pointer
+	 */
+	public CSRPointer getSparseMatrixCudaPointer() {
+		return getJcudaSparseMatrixPtr();
+	}
+
+	/**
+	 * Convenience method to directly set the sparse matrix on GPU
+	 * Make sure to call {@link #addReadLock()} after this to set appropriate state, if you are not sure what you are doing.
+	 * Needed for operations like {@link JCusparse#cusparseDcsrgemm(cusparseHandle, int, int, int, int, int, cusparseMatDescr, int, Pointer, Pointer, Pointer, cusparseMatDescr, int, Pointer, Pointer, Pointer, cusparseMatDescr, Pointer, Pointer, Pointer)}
+	 * @param sparseMatrixPtr CSR (compressed sparse row) pointer
+	 */
+	public void setSparseMatrixCudaPointer(CSRPointer sparseMatrixPtr) throws DMLRuntimeException {
+		this.jcudaSparseMatrixPtr = sparseMatrixPtr;
+		this.isSparse = true;
+		if(getJcudaDenseMatrixPtr() != null) {
+			cudaFreeHelper(getJcudaDenseMatrixPtr());
+			jcudaDenseMatrixPtr = null;
+		}
+	}
+
+	/**
+	 * Convenience method to directly set the dense matrix pointer on GPU
+	 * Make sure to call {@link #addReadLock()} after this to set appropriate state, if you are not sure what you are doing.
+	 *
+	 * @param densePtr dense pointer
+	 */
+	public void setDenseMatrixCudaPointer(Pointer densePtr) throws DMLRuntimeException{
+		this.jcudaDenseMatrixPtr = densePtr;
+		this.isSparse = false;
+		if(getJcudaSparseMatrixPtr() != null) {
+			getJcudaSparseMatrixPtr().deallocate();
+			jcudaSparseMatrixPtr = null;
+		}
+	}
+
+	/**
+	 * Converts this GPUObject from dense to sparse format.
+	 *
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public abstract boolean acquireDeviceModifySparse() throws DMLRuntimeException;
-	
+	public void denseToSparse() throws DMLRuntimeException {
+		LOG.trace("GPU : dense -> sparse on " + this + ", GPUContext=" + getGPUContext());
+		long t0=0;
+		if (DMLScript.STATISTICS) t0 = System.nanoTime();
+		cusparseHandle cusparseHandle = getGPUContext().getCusparseHandle();
+		if(cusparseHandle == null)
+			throw new DMLRuntimeException("Expected cusparse to be initialized");
+		int rows = toIntExact(mat.getNumRows());
+		int cols = toIntExact(mat.getNumColumns());
+
+		if(getJcudaDenseMatrixPtr() == null || !isAllocated())
+			throw new DMLRuntimeException("Expected allocated dense matrix before denseToSparse() call");
+
+		convertDensePtrFromRowMajorToColumnMajor();
+		setSparseMatrixCudaPointer(columnMajorDenseToRowMajorSparse(getGPUContext(), cusparseHandle, getJcudaDenseMatrixPtr(), rows, cols));
+		// TODO: What if mat.getNnz() is -1 ?
+		if (DMLScript.STATISTICS) GPUStatistics.cudaDenseToSparseTime.addAndGet(System.nanoTime() - t0);
+		if (DMLScript.STATISTICS) GPUStatistics.cudaDenseToSparseCount.addAndGet(1);
+	}
+
 	/**
-	 * If memory on GPU has been allocated from elsewhere, this method 
-	 * updates the internal bookkeeping
-	 * @param numBytes number of bytes
+	 * Convenience method. Converts Row Major Dense Matrix --> Column Major Dense Matrix
+	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public abstract void setDeviceModify(long numBytes);
+	private void convertDensePtrFromRowMajorToColumnMajor() throws DMLRuntimeException {
+		LOG.trace("GPU : dense Ptr row-major -> col-major on " + this + ", GPUContext=" + getGPUContext());
+		int m = toIntExact(mat.getNumRows());
+		int n = toIntExact(mat.getNumColumns());
+		int lda = n;
+		int ldc = m;
+		if(!isAllocated()) {
+			throw new DMLRuntimeException("Error in converting row major to column major : data is not allocated");
+		}
+
+		Pointer tmp = transpose(getGPUContext(), getJcudaDenseMatrixPtr(), m, n, lda, ldc);
+		cudaFreeHelper(getJcudaDenseMatrixPtr());
+		setDenseMatrixCudaPointer(tmp);
+	}
+
+	private void convertDensePtrFromColMajorToRowMajor() throws DMLRuntimeException {
+		LOG.trace("GPU : dense Ptr row-major -> col-major on " + this + ", GPUContext=" + getGPUContext());
+
+		int n = toIntExact(mat.getNumRows());
+		int m = toIntExact(mat.getNumColumns());
+		int lda = n;
+		int ldc = m;
+		if(!isAllocated()) {
+			throw new DMLRuntimeException("Error in converting column major to row major : data is not allocated");
+		}
+
+		Pointer tmp = transpose(getGPUContext(), getJcudaDenseMatrixPtr(), m, n, lda, ldc);
+		cudaFreeHelper(getJcudaDenseMatrixPtr());
+		setDenseMatrixCudaPointer(tmp);
+	}
 
 	/**
-	 * Signal intent that a block needs to be read on the host
-	 * @return true if copied from device to host
-	 * @throws CacheException ?
+	 * Convert sparse to dense (Performs transpose, use sparseToColumnMajorDense if the kernel can deal with column major format)
+	 *
+	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public abstract boolean acquireHostRead() throws CacheException;
-
-	public abstract void releaseInput() throws CacheException;
-	public abstract void releaseOutput() throws CacheException;
-	
-	// package-level visibility as these methods are guarded by underlying GPUContext
-
-	abstract void allocateDenseMatrixOnDevice() throws DMLRuntimeException;
-	abstract void allocateSparseMatrixOnDevice() throws DMLRuntimeException;
-	abstract void deallocateMemoryOnDevice(boolean eager) throws DMLRuntimeException;
-	abstract long getSizeOnDevice() throws DMLRuntimeException;
-	
-	abstract void copyFromHostToDevice() throws DMLRuntimeException;
-	
+	public void sparseToDense() throws DMLRuntimeException {
+		sparseToDense(null);
+	}
+
 	/**
-	 * Copies a matrix block (dense or sparse) from GPU Memory to Host memory.
-	 * A {@link MatrixBlock} instance is allocated, data from the GPU is copied in,
-	 * the current one in Host memory is deallocated by calling MatrixObject's acquireHostModify(MatrixBlock) (??? does not exist)
-	 * and overwritten with the newly allocated instance.
-	 * TODO : re-examine this to avoid spurious allocations of memory for optimizations
+	 * Convert sparse to dense (Performs transpose, use sparseToColumnMajorDense if the kernel can deal with column major format)
+	 * Also records per instruction invokation of sparseToDense.
+	 * @param instructionName	Name of the instruction for which statistics are recorded in {@link GPUStatistics}
+	 * @throws DMLRuntimeException ?
+	 */
+	public void sparseToDense(String instructionName) throws DMLRuntimeException {
+		LOG.trace("GPU : sparse -> dense on " + this + ", GPUContext=" + getGPUContext());
+		long start=0, end=0;
+		if (DMLScript.STATISTICS) start = System.nanoTime();
+		if(getJcudaSparseMatrixPtr() == null || !isAllocated())
+			throw new DMLRuntimeException("Expected allocated sparse matrix before sparseToDense() call");
+
+		sparseToColumnMajorDense();
+		convertDensePtrFromColMajorToRowMajor();
+		if (DMLScript.STATISTICS) end = System.nanoTime();
+		if (instructionName != null && GPUStatistics.DISPLAY_STATISTICS) GPUStatistics.maintainCPMiscTimes(instructionName, GPUInstruction.MISC_TIMER_SPARSE_TO_DENSE, end - start);
+		if (DMLScript.STATISTICS) GPUStatistics.cudaSparseToDenseTime.addAndGet(end - start);
+		if (DMLScript.STATISTICS) GPUStatistics.cudaSparseToDenseCount.addAndGet(1);
+	}
+
+	/**
+	 * More efficient method to convert sparse to dense but returns dense in column major format
+	 *
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	abstract void copyFromDeviceToHost() throws DMLRuntimeException; // Called by export()
+	public void sparseToColumnMajorDense() throws DMLRuntimeException {
+		LOG.trace("GPU : sparse -> col-major dense on " + this + ", GPUContext=" + getGPUContext());
+		if(getJcudaSparseMatrixPtr() == null || !isAllocated())
+			throw new DMLRuntimeException("Expected allocated sparse matrix before sparseToDense() call");
+
+		cusparseHandle cusparseHandle = getGPUContext().getCusparseHandle();
+		if(cusparseHandle == null)
+			throw new DMLRuntimeException("Expected cusparse to be initialized");
+		int rows = toIntExact(mat.getNumRows());
+		int cols = toIntExact(mat.getNumColumns());
+		setDenseMatrixCudaPointer(getJcudaSparseMatrixPtr().toColumnMajorDenseMatrix(cusparseHandle, null, rows, cols));
+	}
+
+	/**
+	 * Initializes this GPUObject with a {@link MatrixObject} instance which will contain metadata about the enclosing matrix block
+	 * @param mat2 the matrix block that owns this {@link GPUObject}
+	 */
+	GPUObject(GPUContext gCtx, MatrixObject mat2)  {
+		gpuContext = gCtx;
+		this.mat = mat2;
+	}
 
+	public boolean isSparse() {
+		return isSparse;
+	}
 
 	/**
-	 * Convenience wrapper over {@link GPUObject#evict(String, long)}
-	 * @param GPUSize Desired size to be freed up on the GPU
-	 * @throws DMLRuntimeException If no blocks to free up or if not enough blocks with zero locks on them.
+	 * Returns a previously allocated tensor shape or null
+	 * @return int array of four elements or null
 	 */
-	protected static void evict(final long GPUSize) throws DMLRuntimeException {
-		evict(null, GPUSize);
+	public int [] getTensorShape() {
+		return tensorShape;
 	}
 
 	/**
-	 * Cycles through the sorted list of allocated {@link GPUObject} instances. Sorting is based on
-	 * number of (read) locks that have been obtained on it (reverse order). It repeatedly frees up 
-	 * blocks on which there are zero locks until the required size has been freed up.
-	 * // TODO: update it with hybrid policy
-	 * @param instructionName name of the instruction for which performance measurements are made
-	 * @param GPUSize Desired size to be freed up on the GPU
-	 * @throws DMLRuntimeException If no blocks to free up or if not enough blocks with zero locks on them.	 
+	 * Returns a previously allocated tensor descriptor or null
+	 * @return cudnn tensor descriptor
 	 */
-	protected static void evict(String instructionName, final long GPUSize) throws DMLRuntimeException {
-		synchronized (JCudaContext.syncObj) {
-
-			GPUStatistics.cudaEvictionCount.addAndGet(1);
-			// Release the set of free blocks maintained in a JCudaObject.freeCUDASpaceMap
-			// to free up space
-			LRUCacheMap<Long, LinkedList<Pointer>> lruCacheMap = JCudaObject.freeCUDASpaceMap;
-			while (lruCacheMap.size() > 0) {
-				if (GPUSize <= getAvailableMemory())
-					break;
-				Map.Entry<Long, LinkedList<Pointer>> toFreeListPair = lruCacheMap.removeAndGetLRUEntry();
-				LinkedList<Pointer> toFreeList = toFreeListPair.getValue();
-				Long size = toFreeListPair.getKey();
-				Pointer toFree = toFreeList.pop();
-				if (toFreeList.isEmpty())
-					lruCacheMap.remove(size);
-				JCudaObject.cudaFreeHelper(instructionName, toFree, true);
+	public cudnnTensorDescriptor getTensorDescriptor() {
+		return tensorDescriptor;
+	}
+
+	/**
+	 * Returns a previously allocated or allocates and returns a tensor descriptor
+	 * @param N number of images
+	 * @param C number of channels
+	 * @param H height
+	 * @param W width
+	 * @return cudnn tensor descriptor
+	 */
+	public cudnnTensorDescriptor allocateTensorDescriptor(int N, int C, int H, int W) {
+		LOG.trace("GPU : allocateTensorDescriptor with [N="+N+",C="+C+",H="+H+",W="+W+"] on " + this);
+		if(tensorDescriptor == null) {
+			tensorDescriptor = new cudnnTensorDescriptor();
+			cudnnCreateTensorDescriptor(tensorDescriptor);
+			cudnnSetTensor4dDescriptor(tensorDescriptor, CUDNN_TENSOR_NCHW, CUDNN_DATA_DOUBLE, N, C, H, W);
+			tensorShape = new int[4];
+			tensorShape[0] = N;
+			tensorShape[1] = C;
+			tensorShape[2] = H;
+			tensorShape[3] = W;
+		}
+		return tensorDescriptor;
+	}
+
+	private static long getDoubleSizeOf(long numElems) {
+		return numElems * ((long)jcuda.Sizeof.DOUBLE);
+	}
+
+	private static long getIntSizeOf(long numElems) {
+		return numElems * ((long)jcuda.Sizeof.INT);
+	}
+
+	public boolean isAllocated() {
+		boolean eitherAllocated = (getJcudaDenseMatrixPtr() != null || getJcudaSparseMatrixPtr() != null);
+		return eitherAllocated;
+	}
+
+	public boolean isInputAllocated() {
+		try {
+			boolean eitherAllocated = (getJcudaDenseMatrixPtr() != null || getJcudaSparseMatrixPtr() != null);
+			boolean isAllocatedOnThisGPUContext = getGPUContext().isBlockRecorded(this);
+			if (eitherAllocated && !isAllocatedOnThisGPUContext) {
+				LOG.warn("GPU : A block was allocated but was not on this GPUContext, GPUContext=" + getGPUContext());
 			}
+			return eitherAllocated && isAllocatedOnThisGPUContext;
+		} catch (DMLRuntimeException e){
+			LOG.info("GPU : System is in an inconsistent state");
+			throw new RuntimeException(e);
+		}
+	}
+
+	/**
+	 * Allocates a sparse and empty {@link GPUObject}
+	 * This is the result of operations that are both non zero matrices.
+	 *
+	 * @throws DMLRuntimeException if DMLRuntimeException occurs
+	 */
+	public void allocateSparseAndEmpty() throws DMLRuntimeException{
+		LOG.trace("GPU : allocate sparse and empty block on " + this + ", GPUContext=" + getGPUContext());
+		setSparseMatrixCudaPointer(CSRPointer.allocateEmpty(getGPUContext(), 0, mat.getNumRows()));
+		addReadLock();
+	}
+
+	/**
+	 * Allocates a dense matrix of size obtained from the attached matrix metadata
+	 * and fills it up with a single value
+	 *
+	 * @param v value to fill up the dense matrix
+	 * @throws DMLRuntimeException if DMLRuntimeException occurs
+	 */
+	public void allocateAndFillDense(double v) throws DMLRuntimeException {
+		LOG.trace("GPU : allocate and fill dense with value " + v + " on " + this + ", GPUContext=" + getGPUContext());
+		long rows = mat.getNumRows();
+		long cols = mat.getNumColumns();
+		int numElems = toIntExact(rows * cols);
+		long size = getDoubleSizeOf(numElems);
+		setDenseMatrixCudaPointer(allocate(size));
+		addReadLock();
+		// The "fill" kernel is called which treats the matrix "jcudaDensePtr" like a vector and fills it with value "v"
+		getGPUContext().getKernels().launchKernel("fill", ExecutionConfig.getConfigForSimpleVectorOperations(numElems), getJcudaDenseMatrixPtr(), v, numElems);
+	}
+
+	/**
+	 * If this {@link GPUObject} is sparse and empty
+	 * Being allocated is a prerequisite to being sparse and empty.
+	 *
+	 * @return true if sparse and empty
+	 * @throws DMLRuntimeException if error
+	 */
+	public boolean isSparseAndEmpty() throws DMLRuntimeException{
+		boolean isSparseAndAllocated = isAllocated()&& LibMatrixCUDA.isInSparseFormat(getGPUContext(), mat);
+		boolean isEmptyAndSparseAndAllocated = isSparseAndAllocated && getJcudaSparseMatrixPtr().nnz == 0;
+		return isEmptyAndSparseAndAllocated;
+	}
+
+	public boolean acquireDeviceRead() throws DMLRuntimeException {
+		LOG.trace("GPU : acquireDeviceRead on " + this);
+		boolean transferred = false;
+		if(!isAllocated()) {
+			LOG.trace("GPU : in acquireDeviceRead, data is not allocated, copying from host, on " + this + ", GPUContext=" + getGPUContext());
+			copyFromHostToDevice();
+			transferred = true;
+		} else {
+			addReadLock();
+		}
+		if(!isAllocated())
+			throw new DMLRuntimeException("Expected device data to be allocated");
+		return transferred;
+	}
+
+	public boolean acquireDeviceModifyDense() throws DMLRuntimeException {
+		LOG.trace("GPU : acquireDeviceModifyDense on " + this + ", GPUContext=" + getGPUContext());
+		boolean allocated = false;
+		if(!isAllocated()) {
+			mat.setDirty(true);
+			LOG.trace("GPU : data is not allocated, allocating a dense block, on " + this);
+			// Dense block, size = numRows * numCols
+			allocateDenseMatrixOnDevice();
+			allocated = true;
+			getGPUContext().recordBlockUsage(this);
+		}
+		dirty = true;
+		if(!isAllocated())
+			throw new DMLRuntimeException("Expected device data to be allocated");
+		return allocated;
+	}
 
-			if (GPUSize <= getAvailableMemory())
-				return;
+	public boolean acquireDeviceModifySparse() throws DMLRuntimeException {
+		LOG.trace("GPU : acquireDeviceModifySparse on " + this + ", GPUContext=" + getGPUContext());
+		boolean allocated = false;
+		isSparse = true;
+		if(!isAllocated()) {
+			LOG.trace("GPU : data is not allocated, allocating a sparse block, on " + this);
+			mat.setDirty(true);
+			allocateSparseMatrixOnDevice();
+			allocated = true;
+			getGPUContext().recordBlockUsage(this);
+
+		}
+		dirty = true;
+		if(!isAllocated())
+			throw new DMLRuntimeException("Expected device data to be allocated");
+		return allocated;
+	}
+
+	public void addReadLock() {
+		readLocks.addAndGet(1);
+	}
 
-			if (JCudaContext.allocatedPointers.size() == 0) {
-				throw new DMLRuntimeException("There is not enough memory on device for this matrix!");
+	/**
+	 * if the data is allocated on the GPU and is dirty, it is copied back to the host memory
+	 * @return true if a copy to host happened, false otherwise
+	 * @throws CacheException
+	 */
+	public boolean acquireHostRead() throws CacheException {
+		boolean copied = false;
+		try {
+			LOG.trace("GPU : acquireDeviceModifySparse on " + this + ", GPUContext=" + getGPUContext());
+			if (isAllocated() && dirty) {
+				LOG.trace("GPU : data is dirty on device, copying to host, on " + this + ", GPUContext=" + getGPUContext());
+				copyFromDeviceToHost();
+				copied = true;
 			}
+		} catch (DMLRuntimeException e) {
+			throw new CacheException(e);
+		}
+		return copied;
+	}
+
+	/**
+	 * Updates the locks depending on the eviction policy selected
+	 * @throws DMLRuntimeException if there is no locked GPU Object or if could not obtain a {@link GPUContext}
+	 */
+	private void updateReleaseLocks() throws DMLRuntimeException {
+		if (readLocks.addAndGet(-1) < 0) {
+			throw new CacheException("Redundant release of GPU object");
+		}
+		LOG.trace("GPU : updateReleaseLocks, new number of read locks is " + readLocks.get() + ", on " + this + ", GPUContext=" + getGPUContext());
+		GPUContext.EvictionPolicy evictionPolicy = getGPUContext().evictionPolicy;
+		switch (evictionPolicy){
+			case LRU : timestamp.set(System.nanoTime()); break;
+			case LFU : timestamp.addAndGet(1); break;
+			case MIN_EVICT : /* Do Nothing */ break;
+			default : throw new CacheException("The eviction policy is not supported:" + evictionPolicy.name());
+		}
+	}
+
+	/**
+	 * Releases input allocated on GPU
+	 * @throws DMLRuntimeException if data is not allocated or if there is no locked GPU Object or if could not obtain a {@link GPUContext}
+	 */
+	public void releaseInput() throws DMLRuntimeException {
+		updateReleaseLocks();
+		if(!isAllocated())
+			throw new CacheException("Attempting to release an input before allocating it");
+	}
+
+	/**
+	 * releases output allocated on GPU
+	 * @throws DMLRuntimeException if data is not allocated or if there is no locked GPU Object or if could not obtain a {@link GPUContext}
+	 */
+	public void releaseOutput() throws DMLRuntimeException {
+		updateReleaseLocks();
+		dirty = true;
+		if(!isAllocated())
+			throw new CacheException("Attempting to release an output before allocating it");
+	}
 
-			synchronized (evictionLock) {
-				Collections.sort(JCudaContext.allocatedPointers, new Comparator<GPUObject>() {
-
-					@Override
-					public int compare(GPUObject p1, GPUObject p2) {
-						long p1Val = p1.numLocks.get();
-						long p2Val = p2.numLocks.get();
-
-						if (p1Val > 0 && p2Val > 0) {
-							// Both are locked, so don't sort
-							return 0;
-						} else if (p1Val > 0 || p2Val > 0) {
-							// Put the unlocked one to RHS
-							return Long.compare(p2Val, p1Val);
-						} else {
-							// Both are unlocked
-
-							if (evictionPolicy == EvictionPolicy.MIN_EVICT) {
-								long p1Size = 0;
-								long p2Size = 0;
-								try {
-									p1Size = p1.getSizeOnDevice() - GPUSize;
-									p2Size = p2.getSizeOnDevice() - GPUSize;
-								} catch (DMLRuntimeException e) {
-									throw new RuntimeException(e);
-								}
-
-								if (p1Size >= 0 && p2Size >= 0) {
-									return Long.compare(p2Size, p1Size);
-								} else {
-									return Long.compare(p1Size, p2Size);
-								}
-							} else if (evictionPolicy == EvictionPolicy.LRU || evictionPolicy == EvictionPolicy.LFU) {
-								return Long.compare(p2.timestamp.get(), p1.timestamp.get());
-							} else {
-								throw new RuntimeException("Unsupported eviction policy:" + evictionPolicy.name());
-							}
-						}
-					}
-				});
-
-				while (GPUSize > getAvailableMemory() && JCudaContext.allocatedPointers.size() > 0) {
-					GPUObject toBeRemoved = JCudaContext.allocatedPointers.get(JCudaContext.allocatedPointers.size() - 1);
-					if (toBeRemoved.numLocks.get() > 0) {
-						throw new DMLRuntimeException("There is not enough memory on device for this matrix!");
-					}
-					if (toBeRemoved.isDeviceCopyModified) {
-						toBeRemoved.copyFromDeviceToHost();
-					}
-
-					toBeRemoved.clearData(true);
+	void allocateDenseMatrixOnDevice() throws DMLRuntimeException {
+		LOG.trace("GPU : allocateDenseMatrixOnDevice, on " + this + ", GPUContext=" + getGPUContext());
+		assert !isAllocated() : "Internal error - trying to allocated dense matrix to a GPUObject that is already allocated";
+		long rows = mat.getNumRows();
+		long cols = mat.getNumColumns();
+		assert rows > 0 : "Internal error - invalid number of rows when allocating dense matrix";
+		assert cols > 0 : "Internal error - invalid number of columns when allocating dense matrix;";
+		long size = getDoubleSizeOf(rows * cols);
+		Pointer tmp = allocate(size);
+		setDenseMatrixCudaPointer(tmp);
+		addReadLock();
+	}
+
+	void allocateSparseMatrixOnDevice() throws DMLRuntimeException {
+		LOG.trace("GPU : allocateSparseMatrixOnDevice, on " + this + ", GPUContext=" + getGPUContext());
+		assert !isAllocated() : "Internal error = trying to allocated sparse matrix to a GPUObject that is already allocated";
+		long rows = mat.getNumRows();
+		long nnz = mat.getNnz();
+		assert rows > 0 : "Internal error - invalid number of rows when allocating a sparse matrix";
+		assert nnz > 0 : "Internal error - invalid number of non zeroes when allocating a sparse matrix";
+		CSRPointer tmp = CSRPointer.allocateEmpty(getGPUContext(), nnz, rows);
+		setSparseMatrixCudaPointer(tmp);
+		addReadLock();
+	}
+
+	void deallocateMemoryOnDevice(boolean eager) throws DMLRuntimeException {
+		LOG.trace("GPU : deallocateMemoryOnDevice, on " + this + ", GPUContext=" + getGPUContext());
+		if(getJcudaDenseMatrixPtr() != null) {
+			cudaFreeHelper(null, getJcudaDenseMatrixPtr(), eager);
+		}
+		if (getJcudaSparseMatrixPtr() != null) {
+			getJcudaSparseMatrixPtr().deallocate(eager);
+		}
+		jcudaDenseMatrixPtr = null;
+		jcudaSparseMatrixPtr = null;
+		if(tensorDescriptor != null) {
+			cudnnDestroyTensorDescriptor(tensorDescriptor);
+			tensorDescriptor = null;
+		}
+		readLocks.set(0);
+	}
+
+	protected long getSizeOnDevice() throws DMLRuntimeException {
+		long GPUSize = 0;
+		long rlen = mat.getNumRows();
+		long clen = mat.getNumColumns();
+		long nnz = mat.getNnz();
+
+		if(LibMatrixCUDA.isInSparseFormat(getGPUContext(), mat)) {
+			GPUSize = CSRPointer.estimateSize(nnz, rlen);
+		}
+		else {
+			GPUSize = getDoubleSizeOf(rlen * clen);
+		}
+		return GPUSize;
+	}
+
+	void copyFromHostToDevice() throws DMLRuntimeException {
+		LOG.trace("GPU : copyFromHostToDevice, on " + this + ", GPUContext=" + getGPUContext());
+		long start=0;
+		if (DMLScript.STATISTICS) start = System.nanoTime();
+
+		MatrixBlock tmp = mat.acquireRead();
+		if(tmp.isInSparseFormat()) {
+
+			int rowPtr[] = null;
+			int colInd[] = null;
+			double[] values = null;
+
+			tmp.recomputeNonZeros();
+			long nnz = tmp.getNonZeros();
+			mat.getMatrixCharacteristics().setNonZeros(nnz);
+
+			SparseBlock block = tmp.getSparseBlock();
+			boolean copyToDevice = true;
+			if(block == null && tmp.getNonZeros() == 0) {
+//				// Allocate empty block --> not necessary
+//				// To reproduce this, see org.apache.sysml.test.integration.applications.dml.ID3DMLTest
+//				rowPtr = new int[0];
+//				colInd = new int[0];
+//				values = new double[0];
+				copyToDevice = false;
+			}
+			else if(block == null && tmp.getNonZeros() != 0) {
+				throw new DMLRuntimeException("Expected CP sparse block to be not null.");
+			}
+			else {
+				// CSR is the preferred format for cuSparse GEMM
+				// Converts MCSR and COO to CSR
+				SparseBlockCSR csrBlock = null;
+				long t0=0;
+				if (block instanceof SparseBlockCSR){
+					csrBlock = (SparseBlockCSR)block;
+				} else if (block instanceof SparseBlockCOO) {
+					// TODO - should we do this on the GPU using cusparse<t>coo2csr() ?
+					if (DMLScript.STATISTICS) t0 = System.nanoTime();
+					SparseBlockCOO cooBlock = (SparseBlockCOO)block;
+					csrBlock = new SparseBlockCSR(toIntExact(mat.getNumRows()), cooBlock.rowIndexes(), cooBlock.indexes(), cooBlock.values());
+					if (DMLScript.STATISTICS) GPUStatistics.cudaSparseConversionTime.addAndGet(System.nanoTime() - t0);
+					if (DMLScript.STATISTICS) GPUStatistics.cudaSparseConversionCount.incrementAndGet();
+				} else if (block instanceof SparseBlockMCSR) {
+					if (DMLScript.STATISTICS) t0 = System.nanoTime();
+					SparseBlockMCSR mcsrBlock = (SparseBlockMCSR)block;
+					csrBlock = new SparseBlockCSR(mcsrBlock.getRows(), toIntExact(mcsrBlock.size()));
+					if (DMLScript.STATISTICS) GPUStatistics.cudaSparseConversionTime.addAndGet(System.nanoTime() - t0);
+					if (DMLScript.STATISTICS) GPUStatistics.cudaSparseConversionCount.incrementAndGet();
+				} else {
+					throw new DMLRuntimeException("Unsupported sparse matrix format for CUDA operations");
 				}
+				rowPtr = csrBlock.rowPointers();
+				colInd = csrBlock.indexes();
+				values = csrBlock.values();
+			}
+			allocateSparseMatrixOnDevice();
+			getGPUContext().recordBlockUsage(this);
+
+			if(copyToDevice) {
+				CSRPointer.copyToDevice(getJcudaSparseMatrixPtr(), tmp.getNumRows(), tmp.getNonZeros(), rowPtr, colInd, values);
 			}
 		}
+		else {
+			double[] data = tmp.getDenseBlock();
+
+			if( data == null && tmp.getSparseBlock() != null )
+				throw new DMLRuntimeException("Incorrect sparsity calculation");
+			else if( data==null && tmp.getNonZeros() != 0 )
+				throw new DMLRuntimeException("MatrixBlock is not allocated");
+			else if( tmp.getNonZeros() == 0 )
+				data = new double[tmp.getNumRows()*tmp.getNumColumns()];
+
+			// Copy dense block
+			allocateDenseMatrixOnDevice();
+			getGPUContext().recordBlockUsage(this);
+
+			cudaMemcpy(getJcudaDenseMatrixPtr(), Pointer.to(data), getDoubleSizeOf(mat.getNumRows()*mat.getNumColumns()), cudaMemcpyHostToDevice);
+		}
+
+		mat.release();
+
+		if (DMLScript.STATISTICS) GPUStatistics.cudaToDevTime.addAndGet(System.nanoTime()-start);
+		if (DMLScript.STATISTICS) GPUStatistics.cudaToDevCount.addAndGet(1);
+	}
+
+	public static int toIntExact(long l) throws DMLRuntimeException {
+		if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
+			throw new DMLRuntimeException("Cannot be cast to int:" + l);
+		}
+		return (int) l;
 	}
 
+	protected void copyFromDeviceToHost() throws DMLRuntimeException {
+		LOG.trace("GPU : copyFromDeviceToHost, on " + this + ", GPUContext=" + getGPUContext());
+		if (getJcudaDenseMatrixPtr() != null && getJcudaSparseMatrixPtr() != null){
+			throw new DMLRuntimeException("Invalid state : JCuda dense/sparse pointer are both allocated");
+		}
+
+		if(getJcudaDenseMatrixPtr() != null) {
+			long start=0;
+			if (DMLScript.STATISTICS) start = System.nanoTime();
+			MatrixBlock tmp = new MatrixBlock(toIntExact(mat.getNumRows()), toIntExact(mat.getNumColumns()), false);
+			tmp.allocateDenseBlock();
+			double [] data = tmp.getDenseBlock();
+
+			cudaMemcpy(Pointer.to(data), getJcudaDenseMatrixPtr(), getDoubleSizeOf(data.length), cudaMemcpyDeviceToHost);
+
+			tmp.recomputeNonZeros();
+			mat.acquireModify(tmp);
+			mat.release();
+
+			if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevTime.addAndGet(System.nanoTime()-start);
+			if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevCount.addAndGet(1);
+		}
+		else if (getJcudaSparseMatrixPtr() != null){
+			if(!LibMatrixCUDA.isInSparseFormat(getGPUContext(), mat))
+				throw new DMLRuntimeException("Block not in sparse format on host yet the device sparse matrix pointer is not null");
+
+			if(this.isSparseAndEmpty()){
+				MatrixBlock tmp = new MatrixBlock();	// Empty Block
+				mat.acquireModify(tmp);
+				mat.release();
+			} else {
+				long start=0;
+				if (DMLScript.STATISTICS) start = System.nanoTime();
+
+				int rows = toIntExact(mat.getNumRows());
+				int cols = toIntExact(mat.getNumColumns());
+				int nnz = toIntExact(getJcudaSparseMatrixPtr().nnz);
+				int[] rowPtr = new int[rows + 1];
+				int[] colInd = new int[nnz];
+				double[] values = new double[nnz];
+				CSRPointer.copyToHost(getJcudaSparseMatrixPtr(), rows, nnz, rowPtr, colInd, values);
+
+				SparseBlockCSR sparseBlock = new SparseBlockCSR(rowPtr, colInd, values, nnz);
+				MatrixBlock tmp = new MatrixBlock(rows, cols, nnz, sparseBlock);
+				mat.acquireModify(tmp);
+				mat.release();
+				if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevTime.addAndGet(System.nanoTime() - start);
+				if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevCount.addAndGet(1);
+			}
+		}
+		else {
+			throw new DMLRuntimeException("Cannot copy from device to host as JCuda dense/sparse pointer is not allocated");
+		}
+		dirty = false;
+	}
+
+
 	/**
 	 * lazily clears the data associated with this {@link GPUObject} instance
 	 * @throws CacheException ?
 	 */
-	public void clearData() throws CacheException {
+	public void clearData() throws DMLRuntimeException {
 		clearData(false);
 	}
 
@@ -226,36 +819,38 @@ public abstract class GPUObject
 	 * @param eager whether to be done synchronously or asynchronously
 	 * @throws CacheException ?
 	 */
-	public void clearData(boolean eager) throws CacheException {
-		synchronized(evictionLock) {
-			JCudaContext.allocatedPointers.remove(this);
-		}
-		try {
-			deallocateMemoryOnDevice(eager);
-		} catch (DMLRuntimeException e) {
-			throw new CacheException(e);
-		}
+	public void clearData(boolean eager) throws DMLRuntimeException {
+		getGPUContext().removeRecordedUsage(this);
+		deallocateMemoryOnDevice(eager);
+
+	}
+
+	/** Pointer to dense matrix */
+	public Pointer getJcudaDenseMatrixPtr() {
+		return jcudaDenseMatrixPtr;
+	}
+
+	/** Pointer to sparse matrix */
+	public CSRPointer getJcudaSparseMatrixPtr() {
+		return jcudaSparseMatrixPtr;
 	}
-	
-	static Boolean evictionLock = new Boolean(true);
-	
-	protected static long getAvailableMemory() {
-		return GPUContext.currContext.getAvailableMemory();
-	}
-	
-//	// Copying from device -> host occurs here
-//	// Called by MatrixObject's exportData
-//	public void exportData() throws CacheException {
-//		boolean isDeviceCopyModified = mat.getGPUObject() != null && mat.getGPUObject().isDeviceCopyModified;
-//		boolean isHostCopyUnavailable = mat.getMatrixBlock() == null || 
-//				(mat.getMatrixBlock().getDenseBlock() == null && mat.getMatrixBlock().getSparseBlock() == null);
-//		
-//		if(mat.getGPUObject() != null && (isDeviceCopyModified || isHostCopyUnavailable)) {
-//			try {
-//				mat.getGPUObject().copyFromDeviceToHost();
-//			} catch (DMLRuntimeException e) {
-//				throw new CacheException(e);
-//			}
-//		}
-//	}
+
+	/** Whether this block is dirty on the GPU */
+	public boolean isDirty() {
+		return dirty;
+	}
+
+	@Override
+	public String toString() {
+		final StringBuilder sb = new StringBuilder("GPUObject{");
+		sb.append(", tensorShape=").append(Arrays.toString(tensorShape));
+		sb.append(", dirty=").append(dirty);
+		sb.append(", readLocks=").append(readLocks);
+		sb.append(", sparse? ").append(isSparse);
+		sb.append(", dims=[").append(mat.getNumRows()).append(",").append(mat.getNumColumns()).append("]");
+		sb.append('}');
+		return sb.toString();
+	}
+
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/JCudaContext.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/JCudaContext.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/JCudaContext.java
deleted file mode 100644
index bb73f4b..0000000
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/JCudaContext.java
+++ /dev/null
@@ -1,286 +0,0 @@
-/*
- * 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.runtime.instructions.gpu.context;
-
-import jcuda.driver.JCudaDriver;
-import jcuda.jcublas.JCublas2;
-import jcuda.jcublas.cublasHandle;
-import jcuda.jcudnn.JCudnn;
-import jcuda.jcudnn.cudnnHandle;
-import jcuda.jcusparse.JCusparse;
-import jcuda.jcusparse.cusparseHandle;
-import jcuda.runtime.JCuda;
-import jcuda.runtime.cudaDeviceProp;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.sysml.conf.ConfigurationManager;
-import org.apache.sysml.conf.DMLConfig;
-import org.apache.sysml.runtime.DMLRuntimeException;
-import org.apache.sysml.runtime.matrix.data.LibMatrixCUDA;
-import org.apache.sysml.utils.GPUStatistics;
-
-import java.util.ArrayList;
-import java.util.concurrent.atomic.AtomicLong;
-
-import static jcuda.driver.JCudaDriver.cuDeviceGetCount;
-import static jcuda.driver.JCudaDriver.cuInit;
-import static jcuda.jcublas.JCublas2.cublasCreate;
-import static jcuda.jcublas.JCublas2.cublasDestroy;
-import static jcuda.jcudnn.JCudnn.cudnnCreate;
-import static jcuda.jcudnn.JCudnn.cudnnDestroy;
-import static jcuda.jcusparse.JCusparse.cusparseCreate;
-import static jcuda.jcusparse.JCusparse.cusparseDestroy;
-import static jcuda.runtime.JCuda.*;
-import static jcuda.runtime.cudaError.cudaSuccess;
-
-
-public class JCudaContext extends GPUContext {
-
-	/** Synchronization object to make sure no allocations happen when something is being evicted from memory */
-	public static final Object syncObj = new Object();
-	private static final Log LOG = LogFactory.getLog(JCudaContext.class.getName());
-
-	/** Global list of allocated {@link GPUObject} instances. This list must be accessed in a synchronized way */
-	public static ArrayList<GPUObject> allocatedPointers = new ArrayList<GPUObject>();
-
-	// The minimum CUDA Compute capability needed for SystemML.
-	// After compute capability 3.0, 2^31 - 1 blocks and 1024 threads per block are supported.
-	// If SystemML needs to run on an older card, this logic can be revisited.
-	final int MAJOR_REQUIRED = 3;
-	final int MINOR_REQUIRED = 0;
-
-	/** The total number of cuda devices on this machine */
-	public static int deviceCount = -1;
-
-	/** enable this to print debug information before code pertaining to the GPU is executed  */
-	public static boolean DEBUG = false;
-
-	/** total bytes available on currently active cude device, please be careful with its bookkeeping */
-	AtomicLong deviceMemBytes = new AtomicLong(0);
-
-	/** Stores the cached deviceProperties */
-	private static cudaDeviceProp[] deviceProperties;
-
-	// Invoke cudaMemGetInfo to get available memory information. Useful if GPU is shared among multiple application.
-	public double GPU_MEMORY_UTILIZATION_FACTOR = ConfigurationManager.getDMLConfig().getDoubleValue(DMLConfig.GPU_MEMORY_UTILIZATION_FACTOR);
-	// Whether to invoke cudaMemGetInfo for available memory or rely on internal bookkeeping for memory info.
-	public boolean REFRESH_AVAILABLE_MEMORY_EVERY_TIME = ConfigurationManager.getDMLConfig().getBooleanValue(DMLConfig.REFRESH_AVAILABLE_MEMORY_EVERY_TIME);
-	static {
-		long start = System.nanoTime();
-		JCuda.setExceptionsEnabled(true);
-		JCudnn.setExceptionsEnabled(true);
-		JCublas2.setExceptionsEnabled(true);
-		JCusparse.setExceptionsEnabled(true);
-		JCudaDriver.setExceptionsEnabled(true);
-		cuInit(0); // Initialize the driver
-
-		int deviceCountArray[] = { 0 };
-		cuDeviceGetCount(deviceCountArray);				// Obtain the number of devices
-		deviceCount = deviceCountArray[0];
-		deviceProperties = new cudaDeviceProp[deviceCount];
-
-		LOG.info("Total number of GPUs on the machine: " + deviceCount);
-		int maxBlocks = getMaxBlocks();
-		int maxThreadsPerBlock = getMaxThreadsPerBlock();
-		long sharedMemPerBlock = getMaxSharedMemory();
-		int[] device = {-1};
-		cudaGetDevice(device);
-		LOG.info("Active CUDA device number : " + device[0]);
-		LOG.info("Max Blocks/Threads/SharedMem : " + maxBlocks + "/" + maxThreadsPerBlock + "/" + sharedMemPerBlock);
-
-		GPUStatistics.cudaInitTime = System.nanoTime() - start;
-	}
-
-	@Override
-	public long getAvailableMemory() {
-		if (REFRESH_AVAILABLE_MEMORY_EVERY_TIME) {
-			long free[] = {0};
-			long total[] = {0};
-			if (cudaMemGetInfo(free, total) == cudaSuccess) {
-				//long totalNumBytes = total[0];
-				deviceMemBytes.set(free[0]);
-			} else {
-				throw new RuntimeException("ERROR: Unable to get memory information of the GPU.");
-			}
-		}
-		return (long) (deviceMemBytes.get()*GPU_MEMORY_UTILIZATION_FACTOR);
-	}
-
-	@Override
-	public void ensureComputeCapability() throws DMLRuntimeException {
-		int[] devices =  {-1};
-		cudaGetDeviceCount(devices);
-		if (devices[0] == -1){
-			throw new DMLRuntimeException("Call to cudaGetDeviceCount returned 0 devices");
-		}
-		boolean isComputeCapable = true;
-		for (int i=0; i<devices[0]; i++) {
-			cudaDeviceProp properties = getGPUProperties(i);
-			int major = properties.major;
-			int minor = properties.minor;
-			if (major < MAJOR_REQUIRED) {
-				isComputeCapable = false;
-			} else if (major == MAJOR_REQUIRED && minor < MINOR_REQUIRED) {
-				isComputeCapable = false;
-			}
-		}
-		if (!isComputeCapable) {
-			throw new DMLRuntimeException("One of the CUDA cards on the system has compute capability lower than " + MAJOR_REQUIRED + "." + MINOR_REQUIRED);
-		}
-	}
-
-	/**
-	 * Gets the device properties for the active GPU (set with cudaSetDevice())
-	 * @return the device properties
-	 */
-	public static cudaDeviceProp getGPUProperties() {
-		int[] device = {-1};
-		cudaGetDevice(device);	// Get currently active device
-		return getGPUProperties(device[0]);
-	}
-
-	/**
-	 * Gets the device properties
-	 * @param device the device number (on a machine with more than 1 GPU)
-	 * @return the device properties
-	 */
-	public static cudaDeviceProp getGPUProperties(int device){
-		if (deviceProperties[device] == null) {
-			cudaDeviceProp properties = new cudaDeviceProp();
-			cudaGetDeviceProperties(properties, device);
-			deviceProperties[device] = properties;
-		}
-		return deviceProperties[device];
-	}
-
-
-	/**
-	 * Gets the maximum number of threads per block for "active" GPU
-	 * @return the maximum number of threads per block
-	 */
-	public static int getMaxThreadsPerBlock() {
-		cudaDeviceProp deviceProps = getGPUProperties();
-		return deviceProps.maxThreadsPerBlock;
-	}
-
-	/**
-	 * Gets the maximum number of blocks supported by the active cuda device
-	 * @return the maximum number of blocks supported
-	 */
-	public static int getMaxBlocks() {
-		cudaDeviceProp deviceProp = getGPUProperties();
-		return deviceProp.maxGridSize[0];
-	}
-
-	/**
-	 * Gets the shared memory per block supported by the active cuda device
-	 * @return the shared memory per block
-	 */
-	public static long getMaxSharedMemory() {
-		cudaDeviceProp deviceProp = getGPUProperties();
-		return deviceProp.sharedMemPerBlock;
-	}
-
-	/**
-	 * Gets the warp size supported by the active cuda device
-	 * @return the warp size
-	 */
-	public static int getWarpSize() {
-		cudaDeviceProp deviceProp = getGPUProperties();
-		return deviceProp.warpSize;
-	}
-
-	/**
-	 * Gets the available memory and then adds value to it
-	 * @param v the value to add
-	 * @return the current available memory before adding value to it
-	 */
-	public long getAndAddAvailableMemory(long v){
-		return deviceMemBytes.getAndAdd(v);
-	}
-
-	public JCudaContext() throws DMLRuntimeException {
-		if(isGPUContextCreated) {
-			// Wait until it is deleted. This case happens during multi-threaded testing.
-			// This also allows for multi-threaded execute calls
-			long startTime = System.currentTimeMillis();
-			do {
-				try {
-					Thread.sleep(100);
-				} catch (InterruptedException e) {}
-			} while(isGPUContextCreated && (System.currentTimeMillis() - startTime) < 60000);
-			synchronized(isGPUContextCreated) {
-				if(GPUContext.currContext != null) {
-					throw new RuntimeException("Cannot create multiple JCudaContext. Waited for 10 min to close previous GPUContext");
-				}
-			}
-		}
-		synchronized (isGPUContextCreated){
-			GPUContext.currContext = this;
-		}
-
-		long free [] = { 0 };
-		long total [] = { 0 };
-		long totalNumBytes = 0;
-		if(cudaMemGetInfo(free, total) == cudaSuccess) {
-			totalNumBytes = total[0];
-			deviceMemBytes.set(free[0]);
-		}
-		else {
-			throw new RuntimeException("ERROR: Unable to get memory information of the GPU.");
-		}
-		LOG.info("Total GPU memory: " + (totalNumBytes*(1e-6)) + " MB");
-		LOG.info("Available GPU memory: " + (deviceMemBytes.get()*(1e-6)) + " MB");
-
-		long start = System.nanoTime();
-		LibMatrixCUDA.cudnnHandle = new cudnnHandle();
-		cudnnCreate(LibMatrixCUDA.cudnnHandle);
-		LibMatrixCUDA.cublasHandle = new cublasHandle();
-		cublasCreate(LibMatrixCUDA.cublasHandle);
-		// For cublas v2, cublasSetPointerMode tells Cublas whether to expect scalar arguments on device or on host
-		// This applies to arguments like "alpha" in Dgemm, and "y" in Ddot.
-		// cublasSetPointerMode(LibMatrixCUDA.cublasHandle, cublasPointerMode.CUBLAS_POINTER_MODE_DEVICE);
-		LibMatrixCUDA.cusparseHandle = new cusparseHandle();
-		cusparseCreate(LibMatrixCUDA.cusparseHandle);
-		try {
-			LibMatrixCUDA.kernels = new JCudaKernels();
-		} catch (DMLRuntimeException e) {
-			System.err.println("ERROR - Unable to initialize JCudaKernels. System in an inconsistent state");
-			LibMatrixCUDA.kernels = null;
-		}
-		GPUStatistics.cudaLibrariesInitTime = System.nanoTime() - start;
-	}
-
-	@Override
-	public void destroy() throws DMLRuntimeException {
-		if(currContext != null) {
-			synchronized(isGPUContextCreated) {
-				cudnnDestroy(LibMatrixCUDA.cudnnHandle);
-				cublasDestroy(LibMatrixCUDA.cublasHandle);
-				cusparseDestroy(LibMatrixCUDA.cusparseHandle);
-				currContext = null;
-				isGPUContextCreated = false;
-			}
-		}
-		else if(LibMatrixCUDA.cudnnHandle != null || LibMatrixCUDA.cublasHandle != null) {
-			throw new DMLRuntimeException("Error while destroying the GPUContext");
-		}
-	}
-
-}


[5/5] incubator-systemml git commit: Refactored GPU{Contex, Object} to make it friendlier for parfor

Posted by na...@apache.org.
Refactored GPU{Contex,Object} to make it friendlier for parfor

- Folded JCuda{Context,Object} to GPU{Context,Object}
- Removed "deviceMemBytes", it was redundant
- Removed all synchronized in GPU{Object,Contex}
- print GPUContext from everywhere in log.trace
- LibMatrixCUDA functions expect a GPUContext instead of getting it statically
- Restructured GPUContext to use a pool of already initialized GPUContexts
- Call cudaSetDevice when on different thread
- TODO FIXME Disabled cublasDgeam for scalarMatrixArithmetic
- TODO FIXME revisit the need to always force gpu to be used, mem est
  broken
- Ability to restrict parfor from picking up all GPUs on the machine,
  from a system property

Closes #462


Project: http://git-wip-us.apache.org/repos/asf/incubator-systemml/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-systemml/commit/129f0f6b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-systemml/tree/129f0f6b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-systemml/diff/129f0f6b

Branch: refs/heads/master
Commit: 129f0f6b0e0f6167e4137c6d47374ab96501b888
Parents: 9ed27ad
Author: Nakul Jindal <na...@gmail.com>
Authored: Fri Apr 21 16:22:19 2017 -0700
Committer: Nakul Jindal <na...@gmail.com>
Committed: Fri Apr 21 16:22:19 2017 -0700

----------------------------------------------------------------------
 conf/SystemML-config.xml.template               |    3 +
 .../java/org/apache/sysml/api/DMLScript.java    |   27 +-
 .../api/mlcontext/MLContextConversionUtil.java  |    4 +-
 .../sysml/api/mlcontext/ScriptExecutor.java     |   25 +-
 .../java/org/apache/sysml/conf/DMLConfig.java   |   17 +-
 .../controlprogram/ParForProgramBlock.java      |   25 +
 .../controlprogram/caching/CacheableData.java   |   80 +-
 .../context/ExecutionContext.java               |   56 +-
 .../controlprogram/parfor/LocalParWorker.java   |    5 +
 .../controlprogram/parfor/ParWorker.java        |    5 +
 .../parfor/opt/OptTreeConverter.java            |    2 +-
 .../cp/FunctionCallCPInstruction.java           |   13 +-
 .../gpu/AggregateBinaryGPUInstruction.java      |    4 +-
 .../gpu/AggregateUnaryGPUInstruction.java       |    3 +-
 .../gpu/ConvolutionGPUInstruction.java          |   25 +-
 .../instructions/gpu/GPUInstruction.java        |    3 +-
 .../instructions/gpu/MMTSJGPUInstruction.java   |    3 +-
 .../gpu/MatrixBuiltinGPUInstruction.java        |    5 +-
 .../MatrixMatrixArithmeticGPUInstruction.java   |    5 +-
 .../gpu/MatrixMatrixAxpyGPUInstruction.java     |    5 +-
 .../instructions/gpu/ReorgGPUInstruction.java   |    5 +-
 .../ScalarMatrixArithmeticGPUInstruction.java   |    4 +-
 .../instructions/gpu/context/CSRPointer.java    |  457 ++++++
 .../instructions/gpu/context/GPUContext.java    |  619 ++++++-
 .../gpu/context/GPUContextPool.java             |  158 ++
 .../instructions/gpu/context/GPUObject.java     |  957 ++++++++---
 .../instructions/gpu/context/JCudaContext.java  |  286 ----
 .../instructions/gpu/context/JCudaKernels.java  |   70 +-
 .../instructions/gpu/context/JCudaObject.java   | 1330 ---------------
 .../runtime/matrix/data/LibMatrixCUDA.java      | 1509 ++++++++++--------
 .../test/integration/AutomatedTestBase.java     |    2 +-
 31 files changed, 3035 insertions(+), 2677 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/conf/SystemML-config.xml.template
----------------------------------------------------------------------
diff --git a/conf/SystemML-config.xml.template b/conf/SystemML-config.xml.template
index a4c7b2f..fe4437f 100644
--- a/conf/SystemML-config.xml.template
+++ b/conf/SystemML-config.xml.template
@@ -71,4 +71,7 @@
 
    <!-- prints extra statistics information for Deep Neural Networks done in CP mode -->
    <systemml.stats.extraDNN>false</systemml.stats.extraDNN>
+
+   <!-- sets the maximum number of GPUs per process, -1 for all GPUs -->
+   <systemml.gpu.perProcessMax>-1</systemml.gpu.perProcessMax>
 </root>

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/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 ce60d55..febbf13 100644
--- a/src/main/java/org/apache/sysml/api/DMLScript.java
+++ b/src/main/java/org/apache/sysml/api/DMLScript.java
@@ -89,6 +89,7 @@ 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;
@@ -111,7 +112,7 @@ public class DMLScript
 		HADOOP, 	    // execute all matrix operations in MR
 		SINGLE_NODE,    // execute all matrix operations in CP
 		HYBRID,         // execute matrix operations in CP or MR
-		HYBRID_SPARK,   // execute matrix operations in CP or Spark   
+		HYBRID_SPARK,   // execute matrix operations in CP or Spark
 		SPARK			// execute matrix operations in Spark
 	}
 
@@ -476,10 +477,6 @@ public class DMLScript
 				return true;
 			}
 
-			if (USE_ACCELERATOR){
-				GPUContext.getGPUContext();
-			}
-
 			if (dmlOptions.clean) {
 				cleanSystemMLWorkspace();
 				return true;
@@ -834,21 +831,35 @@ public class DMLScript
 		// 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)
 			ec = ExecutionContextFactory.createContext(rtprog);
+			if (DMLScript.USE_ACCELERATOR && ec != null){
+				gCtx = GPUContextPool.getFromPool();
+				gCtx.initializeThread();
+				ec.setGPUContext(gCtx);
+				if (gCtx == null) {
+					throw new DMLRuntimeException("GPU : Could not create GPUContext, either no GPU or all GPUs currently in use");
+				}
+			}
 			rtprog.execute( ec );  
 			
 		}
 		finally //ensure cleanup/shutdown
-		{	
-			if(DMLScript.USE_ACCELERATOR && ec != null)
-				ec.destroyGPUContext();
+		{
+			if(DMLScript.USE_ACCELERATOR && ec.getGPUContext() != null) {
+				GPUContextPool.returnToPool(ec.getGPUContext());
+			}
+
 			if( dmlconf.getBooleanValue(DMLConfig.CODEGEN) )
 				SpoofCompiler.cleanupCodeGenerator();
 			if(ec != null && ec instanceof SparkExecutionContext)

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/api/mlcontext/MLContextConversionUtil.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/api/mlcontext/MLContextConversionUtil.java b/src/main/java/org/apache/sysml/api/mlcontext/MLContextConversionUtil.java
index dc20108..b37f037 100644
--- a/src/main/java/org/apache/sysml/api/mlcontext/MLContextConversionUtil.java
+++ b/src/main/java/org/apache/sysml/api/mlcontext/MLContextConversionUtil.java
@@ -169,7 +169,7 @@ public class MLContextConversionUtil {
 			matrixObject.acquireModify(matrixBlock);
 			matrixObject.release();
 			return matrixObject;
-		} catch (CacheException e) {
+		} catch (DMLRuntimeException e) {
 			throw new MLContextException("Exception converting MatrixBlock to MatrixObject", e);
 		}
 	}
@@ -197,7 +197,7 @@ public class MLContextConversionUtil {
 			frameObject.acquireModify(frameBlock);
 			frameObject.release();
 			return frameObject;
-		} catch (CacheException e) {
+		} catch (DMLRuntimeException e) {
 			throw new MLContextException("Exception converting FrameBlock to FrameObject", e);
 		}
 	}

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/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 ac2b92c..2368c90 100644
--- a/src/main/java/org/apache/sysml/api/mlcontext/ScriptExecutor.java
+++ b/src/main/java/org/apache/sysml/api/mlcontext/ScriptExecutor.java
@@ -48,6 +48,7 @@ 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;
@@ -244,6 +245,18 @@ 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");
+		}
+
 	}
 
 	/**
@@ -324,12 +337,6 @@ public class ScriptExecutor {
 		script.setScriptExecutor(this);
 		// Set global variable indicating the script type
 		DMLScript.SCRIPT_TYPE = script.getScriptType();
-		try {
-			if (gpu)
-				GPUContext.getGPUContext();
-		} catch (DMLRuntimeException e) {
-			throw new MLContextException("Exception occurred during initialization of GPU", e);
-		}
 	}
 
 	/**
@@ -338,8 +345,10 @@ public class ScriptExecutor {
 	protected void cleanupAfterExecution() {
 		restoreInputsInSymbolTable();
 		try {
-			if (gpu)
-				executionContext.destroyGPUContext();
+			if (gpu) {
+				GPUContext gCtx = executionContext.getGPUContext();
+				GPUContextPool.returnToPool(gCtx);
+			}
 		} catch (DMLRuntimeException e) {
 			throw new MLContextException("Exception occurred during cleanup of GPU related resources", e);
 		}

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/conf/DMLConfig.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/conf/DMLConfig.java b/src/main/java/org/apache/sysml/conf/DMLConfig.java
index a42b1ca..e974a71 100644
--- a/src/main/java/org/apache/sysml/conf/DMLConfig.java
+++ b/src/main/java/org/apache/sysml/conf/DMLConfig.java
@@ -39,17 +39,17 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
-import org.xml.sax.SAXException;
 import org.apache.sysml.hops.OptimizerUtils;
 import org.apache.sysml.parser.ParseException;
 import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.util.LocalFileUtils;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
 
 
-public class DMLConfig 
+public class DMLConfig
 {
 
 	public static final String DEFAULT_SYSTEMML_CONFIG_FILEPATH = "./SystemML-config.xml";
@@ -76,12 +76,11 @@ public class DMLConfig
 	public static final String CODEGEN_LITERALS     = "codegen.literals"; //1..heuristic, 2..always
 	public static final String EXTRA_GPU_STATS			= "systemml.stats.extraGPU"; //boolean
 	public static final String EXTRA_DNN_STATS			= "systemml.stats.extraDNN"; //boolean
+	public static final String MAX_GPUS_PER_PROCESS = "systemml.gpu.perProcessMax"; // boolean, maximum number of gpus to use, -1 for all
 
-	// Fraction of available memory to use. The available memory is computer when the JCudaContext is created
+	// Fraction of available memory to use. The available memory is computer when the GPUContext is created
 	// to handle the tradeoff on calling cudaMemGetInfo too often.
 	public static final String GPU_MEMORY_UTILIZATION_FACTOR    = "gpu.memory.util.factor";
-	// Invoke cudaMemGetInfo to get available memory information. Useful if GPU is shared among multiple application.
-	public static final String REFRESH_AVAILABLE_MEMORY_EVERY_TIME    = "gpu.memory.refresh";
 
 	// supported prefixes for custom map/reduce configurations
 	public static final String PREFIX_MAPRED = "mapred";
@@ -121,7 +120,7 @@ public class DMLConfig
 		_defaultVals.put(EXTRA_DNN_STATS,       "false" );
 
 		_defaultVals.put(GPU_MEMORY_UTILIZATION_FACTOR,      "0.9" );
-		_defaultVals.put(REFRESH_AVAILABLE_MEMORY_EVERY_TIME,      "true" );
+		_defaultVals.put(MAX_GPUS_PER_PROCESS,	"-1");
 	}
 	
 	public DMLConfig()

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/controlprogram/ParForProgramBlock.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/controlprogram/ParForProgramBlock.java b/src/main/java/org/apache/sysml/runtime/controlprogram/ParForProgramBlock.java
index d6186f3..f3de422 100644
--- a/src/main/java/org/apache/sysml/runtime/controlprogram/ParForProgramBlock.java
+++ b/src/main/java/org/apache/sysml/runtime/controlprogram/ParForProgramBlock.java
@@ -102,6 +102,8 @@ import org.apache.sysml.runtime.instructions.cp.DoubleObject;
 import org.apache.sysml.runtime.instructions.cp.IntObject;
 import org.apache.sysml.runtime.instructions.cp.StringObject;
 import org.apache.sysml.runtime.instructions.cp.VariableCPInstruction;
+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.MatrixCharacteristics;
 import org.apache.sysml.runtime.matrix.data.OutputInfo;
@@ -629,6 +631,11 @@ public class ParForProgramBlock extends ForProgramBlock
 			switch( _execMode )
 			{
 				case LOCAL: //create parworkers as local threads
+					if (DMLScript.USE_ACCELERATOR) {
+						GPUContextPool.returnToPool(ec.getGPUContext());
+						ec.setGPUContext(null);
+						setDegreeOfParallelism(GPUContextPool.getDeviceCount());
+					}
 					executeLocalParFor(ec, iterVar, from, to, incr);
 					break;
 					
@@ -718,6 +725,7 @@ public class ParForProgramBlock extends ForProgramBlock
 	private void executeLocalParFor( ExecutionContext ec, IntObject itervar, IntObject from, IntObject to, IntObject incr ) 
 		throws DMLRuntimeException, InterruptedException
 	{
+		LOG.trace("Local Par For (multi-threaded) with degree of parallelism : " + _numThreads);
 		/* Step 1) init parallel workers, task queue and threads
 		 *         start threads (from now on waiting for tasks)
 		 * Step 2) create tasks
@@ -820,6 +828,17 @@ public class ParForProgramBlock extends ForProgramBlock
 						_prog.removeFunctionProgramBlock(parts[0], parts[1]);
 					}
 			}
+
+			// Frees up the GPUContexts used in the threaded Parfor and sets
+			// the main thread to use the GPUContext
+			if (DMLScript.USE_ACCELERATOR) {
+				for (int i = 0; i < _numThreads; i++) {
+					GPUContext gCtx = workers[i].getExecutionContext().getGPUContext();
+					GPUContextPool.returnToPool(gCtx);
+				}
+				ec.setGPUContext(GPUContextPool.getFromPool());
+				ec.getGPUContext().initializeThread();
+			}
 		}
 		finally 
 		{
@@ -1399,6 +1418,12 @@ public class ParForProgramBlock extends ForProgramBlock
 			
 			//deep copy execution context (including prepare parfor update-in-place)
 			ExecutionContext cpEc = ProgramConverter.createDeepCopyExecutionContext(ec);
+
+			// If GPU mode is enabled, gets a GPUContext from the pool of GPUContexts
+			// and sets it in the ExecutionContext
+			if (DMLScript.USE_ACCELERATOR){
+				cpEc.setGPUContext(GPUContextPool.getFromPool());
+			}
 			
 			//prepare basic update-in-place variables (vars dropped on result merge)
 			prepareUpdateInPlaceVariables(cpEc, pwID);

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/controlprogram/caching/CacheableData.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/controlprogram/caching/CacheableData.java b/src/main/java/org/apache/sysml/runtime/controlprogram/caching/CacheableData.java
index 054b333..904eb87 100644
--- a/src/main/java/org/apache/sysml/runtime/controlprogram/caching/CacheableData.java
+++ b/src/main/java/org/apache/sysml/runtime/controlprogram/caching/CacheableData.java
@@ -22,6 +22,8 @@ package org.apache.sysml.runtime.controlprogram.caching;
 import java.io.File;
 import java.io.IOException;
 import java.lang.ref.SoftReference;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.commons.lang.mutable.MutableBoolean;
@@ -34,9 +36,10 @@ import org.apache.sysml.parser.Expression.DataType;
 import org.apache.sysml.parser.Expression.ValueType;
 import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.controlprogram.caching.LazyWriteBuffer.RPolicy;
-import org.apache.sysml.runtime.instructions.gpu.context.GPUObject;
 import org.apache.sysml.runtime.controlprogram.parfor.util.IDSequence;
 import org.apache.sysml.runtime.instructions.cp.Data;
+import org.apache.sysml.runtime.instructions.gpu.context.GPUContext;
+import org.apache.sysml.runtime.instructions.gpu.context.GPUObject;
 import org.apache.sysml.runtime.instructions.spark.data.BroadcastObject;
 import org.apache.sysml.runtime.instructions.spark.data.RDDObject;
 import org.apache.sysml.runtime.matrix.MatrixCharacteristics;
@@ -50,6 +53,7 @@ import org.apache.sysml.runtime.matrix.data.OutputInfo;
 import org.apache.sysml.runtime.util.LocalFileUtils;
 import org.apache.sysml.runtime.util.MapReduceTool;
 
+
 /**
  * Each object of this class is a cache envelope for some large piece of data
  * called "cache block". For example, the body of a matrix can be the cache block.  
@@ -187,7 +191,7 @@ public abstract class CacheableData<T extends CacheBlock> extends Data
 	//for lazily evaluated RDDs, and (2) as abstraction for environments that do not necessarily have spark libraries available
 	private RDDObject _rddHandle = null; //RDD handle
 	private BroadcastObject<T> _bcHandle = null; //Broadcast handle
-	protected GPUObject _gpuHandle = null;
+	protected HashMap<GPUContext, GPUObject> _gpuObjects = null; //Per GPUContext object allocated on GPU
 	
 	/**
 	 * Basic constructor for any cacheable data.
@@ -200,6 +204,7 @@ public abstract class CacheableData<T extends CacheBlock> extends Data
 		_uniqueID = (int)_seq.getNextID();		
 		_cacheStatus = CacheStatus.EMPTY;
 		_numReadThreads = 0;
+		_gpuObjects = new HashMap<>();
 	}
 	
 	/**
@@ -213,7 +218,7 @@ public abstract class CacheableData<T extends CacheBlock> extends Data
 		_hdfsFileName = that._hdfsFileName;
 		_hdfsFileExists = that._hdfsFileExists; 
 		_varName = that._varName;
-		_gpuHandle = that._gpuHandle;
+		_gpuObjects = that._gpuObjects;
 	}
 
 	
@@ -341,15 +346,16 @@ public abstract class CacheableData<T extends CacheBlock> extends Data
 			bc.setBackReference(this);
 	}
 
-	public GPUObject getGPUObject() {
-		return _gpuHandle;
+	public synchronized GPUObject getGPUObject(GPUContext gCtx) {
+		return _gpuObjects.get(gCtx);
 	}
-	
-	public void setGPUObject(GPUObject handle) {
-		_gpuHandle = handle;
+
+	public synchronized void setGPUObject(GPUContext gCtx, GPUObject gObj) throws DMLRuntimeException {
+		GPUObject old = _gpuObjects.put(gCtx, gObj);
+		if (old != null)
+				throw new DMLRuntimeException("GPU : Inconsistent internal state - this CacheableData already has a GPUObject assigned to the current GPUContext (" + gCtx + ")");
 	}
 	
-	
 	// *********************************************
 	// ***                                       ***
 	// ***    HIGH-LEVEL METHODS THAT SPECIFY    ***
@@ -384,12 +390,20 @@ public abstract class CacheableData<T extends CacheBlock> extends Data
 		if( _data == null )
 			getCache();
 			
-		//call acquireHostRead if gpuHandle is set as well as is allocated  
-		if( _gpuHandle != null && _gpuHandle.isAllocated()) {
-			_gpuHandle.acquireHostRead();
-			if( _data == null )
-				getCache();
-		}
+		//call acquireHostRead if gpuHandle is set as well as is allocated
+        boolean copiedFromGPU = false;
+        for (Map.Entry<GPUContext, GPUObject> kv : _gpuObjects.entrySet()) {
+            GPUObject gObj = kv.getValue();
+            if (gObj != null && copiedFromGPU && gObj.isDirty()) {
+                LOG.error("Inconsistent internal state - A copy of this CacheableData was dirty on more than 1 GPU");
+                throw new CacheException("Internal Error : Inconsistent internal state, A copy of this CacheableData was dirty on more than 1 GPU");
+            } else if (gObj != null){
+                copiedFromGPU = gObj.acquireHostRead();
+                if( _data == null )
+                    getCache();
+            }
+        }
+
 		//read data from HDFS/RDD if required
 		//(probe data for cache_nowrite / jvm_reuse)  
 		if( isEmpty(true) && _data==null ) 
@@ -517,10 +531,10 @@ public abstract class CacheableData<T extends CacheBlock> extends Data
 	 * 
 	 * @param newData new data
 	 * @return cacheable data
-	 * @throws CacheException if CacheException occurs
+	 * @throws DMLRuntimeException if error occurs
 	 */
 	public synchronized T acquireModify(T newData)
-		throws CacheException
+		throws DMLRuntimeException
 	{
 		if( LOG.isTraceEnabled() )
 			LOG.trace("Acquire modify newdata "+getVarName());
@@ -631,10 +645,10 @@ public abstract class CacheableData<T extends CacheBlock> extends Data
 	 * In-Status:  EMPTY, EVICTABLE, EVICTED;
 	 * Out-Status: EMPTY.
 	 * 
-	 * @throws CacheException if CacheException occurs
+	 * @throws DMLRuntimeException if error occurs
 	 */
 	public synchronized void clearData() 
-		throws CacheException
+		throws DMLRuntimeException
 	{
 		if( LOG.isTraceEnabled() )
 			LOG.trace("Clear data "+getVarName());
@@ -661,9 +675,14 @@ public abstract class CacheableData<T extends CacheBlock> extends Data
 			_rddHandle.setBackReference(null);
 		if( _bcHandle != null )
 			_bcHandle.setBackReference(null);
-		if( _gpuHandle != null )
-			_gpuHandle.clearData();
-		
+		if( _gpuObjects != null ) {
+		    for (GPUObject gObj : _gpuObjects.values()){
+		        if (gObj != null) {
+                    gObj.clearData();
+                }
+            }
+        }
+
 		// change object state EMPTY
 		setDirty(false);
 		setEmpty();
@@ -731,10 +750,19 @@ public abstract class CacheableData<T extends CacheBlock> extends Data
 
 		LOG.trace("Exporting " + this.getDebugName() + " to " + fName + " in format " + outputFormat);
 		
-		//TODO remove 
-		if( getGPUObject() != null && getGPUObject().isAllocated() ) {
-			getGPUObject().acquireHostRead();
-		}
+		//TODO remove
+        boolean copiedFromGPU = false;
+        for (Map.Entry<GPUContext, GPUObject> kv : _gpuObjects.entrySet()) {
+            GPUObject gObj = kv.getValue();
+            if (gObj != null && copiedFromGPU && gObj.isDirty()) {
+                LOG.error("Inconsistent internal state - A copy of this CacheableData was dirty on more than 1 GPU");
+                throw new CacheException("Internal Error : Inconsistent internal state, A copy of this CacheableData was dirty on more than 1 GPU");
+            } else if (gObj != null){
+                copiedFromGPU = gObj.acquireHostRead();
+                if( _data == null )
+                    getCache();
+            }
+        }
 				
 		boolean pWrite = false; // !fName.equals(_hdfsFileName); //persistent write flag
 		if ( fName.equals(_hdfsFileName) ) {

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/controlprogram/context/ExecutionContext.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/controlprogram/context/ExecutionContext.java b/src/main/java/org/apache/sysml/runtime/controlprogram/context/ExecutionContext.java
index b6e1830..35b4cd1 100644
--- a/src/main/java/org/apache/sysml/runtime/controlprogram/context/ExecutionContext.java
+++ b/src/main/java/org/apache/sysml/runtime/controlprogram/context/ExecutionContext.java
@@ -22,6 +22,8 @@ package org.apache.sysml.runtime.controlprogram.context;
 import java.util.ArrayList;
 import java.util.HashMap;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.sysml.api.DMLScript;
 import org.apache.sysml.debug.DMLFrame;
 import org.apache.sysml.debug.DMLProgramCounter;
@@ -52,9 +54,9 @@ import org.apache.sysml.runtime.matrix.data.Pair;
 import org.apache.sysml.runtime.util.MapReduceTool;
 
 
-public class ExecutionContext 
-{
-	
+public class ExecutionContext {
+	protected static final Log LOG = LogFactory.getLog(ExecutionContext.class.getName());
+
 	//program reference (e.g., function repository)
 	protected Program _prog = null;
 	
@@ -64,6 +66,8 @@ public class ExecutionContext
 	//debugging (optional)
 	protected DebugState _dbState = null;
 
+    protected GPUContext _gpuContext = null;
+
 	protected ExecutionContext()
 	{
 		//protected constructor to force use of ExecutionContextFactory
@@ -94,13 +98,15 @@ public class ExecutionContext
 	public void setVariables(LocalVariableMap vars) {
 		_variables = vars;
 	}
-	
-	public void destroyGPUContext() throws DMLRuntimeException {
-		if(GPUContext.isGPUContextCreated)
-			GPUContext.getGPUContext().destroy();
-	}
-	
-	
+
+    public GPUContext getGPUContext() {
+        return _gpuContext;
+    }
+
+    public void setGPUContext(GPUContext _gpuContext) {
+        this._gpuContext = _gpuContext;
+    }
+
 	/* -------------------------------------------------------
 	 * Methods to handle variables and associated data objects
 	 * -------------------------------------------------------
@@ -232,7 +238,7 @@ public class ExecutionContext
 		throws DMLRuntimeException 
 	{	
 		MatrixObject mo = allocateGPUMatrixObject(varName);
-		boolean allocated = mo.getGPUObject().acquireDeviceModifyDense();
+		boolean allocated = mo.getGPUObject(getGPUContext()).acquireDeviceModifyDense();
 		mo.getMatrixCharacteristics().setNonZeros(-1);
 		return new Pair<MatrixObject, Boolean>(mo, allocated);
 	}
@@ -251,7 +257,7 @@ public class ExecutionContext
     {
         MatrixObject mo = allocateGPUMatrixObject(varName);
         mo.getMatrixCharacteristics().setNonZeros(nnz);
-				boolean allocated = mo.getGPUObject().acquireDeviceModifySparse();
+				boolean allocated = mo.getGPUObject(getGPUContext()).acquireDeviceModifySparse();
         return new Pair<MatrixObject, Boolean>(mo, allocated);
     } 
 
@@ -263,8 +269,9 @@ public class ExecutionContext
 	 */
 	public MatrixObject allocateGPUMatrixObject(String varName) throws DMLRuntimeException {
 		MatrixObject mo = getMatrixObject(varName);
-		if( mo.getGPUObject() == null ) {
-			mo.setGPUObject(GPUContext.createGPUObject(mo));
+		if( mo.getGPUObject(getGPUContext()) == null ) {
+			GPUObject newGObj = getGPUContext().createGPUObject(mo);
+			mo.setGPUObject(getGPUContext(), newGObj);
 		}
 		return mo;
 	}
@@ -272,20 +279,23 @@ public class ExecutionContext
 	public Pair<MatrixObject, Boolean> getMatrixInputForGPUInstruction(String varName)
 			throws DMLRuntimeException 
 	{
+		GPUContext gCtx = getGPUContext();
 		boolean copied = false;
 		MatrixObject mo = getMatrixObject(varName);
 		if(mo == null) {
 			throw new DMLRuntimeException("No matrix object available for variable:" + varName);
 		}
-		if( mo.getGPUObject() == null ) {
-			mo.setGPUObject(GPUContext.createGPUObject(mo));
-		}
+
 		boolean acquired = false;
-		if( !mo.getGPUObject().isAllocated() ) {
+		if( mo.getGPUObject(gCtx) == null ) {
+			GPUObject newGObj = gCtx.createGPUObject(mo);
+			mo.setGPUObject(gCtx, newGObj);
+		} else if( !mo.getGPUObject(gCtx).isInputAllocated() ) {
 			mo.acquireRead();
 			acquired = true;
 		}
-		copied = mo.getGPUObject().acquireDeviceRead();
+
+		copied = mo.getGPUObject(gCtx).acquireDeviceRead();
 		if(acquired) {
 			mo.release();
 		}
@@ -309,7 +319,7 @@ public class ExecutionContext
 		throws DMLRuntimeException 
 	{
 		MatrixObject mo = getMatrixObject(varName);
-		mo.getGPUObject().releaseInput();
+		mo.getGPUObject(getGPUContext()).releaseInput();
 	}
 	
 	/**
@@ -361,10 +371,10 @@ public class ExecutionContext
 	
 	public void releaseMatrixOutputForGPUInstruction(String varName) throws DMLRuntimeException {
 		MatrixObject mo = getMatrixObject(varName);
-		if(mo.getGPUObject() == null || !mo.getGPUObject().isAllocated()) {
+		if(mo.getGPUObject(getGPUContext()) == null || !mo.getGPUObject(getGPUContext()).isAllocated()) {
 			throw new DMLRuntimeException("No output is allocated on GPU");
 		}
-		mo.getGPUObject().releaseOutput();
+		mo.getGPUObject(getGPUContext()).releaseOutput();
 	}
 
 	public void setMatrixOutput(String varName, MatrixBlock outputData) 
@@ -486,7 +496,7 @@ public class ExecutionContext
 		return varlist;
 	}
 
-	public void cleanupMatrixObject(MatrixObject mo) 
+	public void cleanupMatrixObject(MatrixObject mo)
 		throws DMLRuntimeException 
 	{
 		try

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/LocalParWorker.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/LocalParWorker.java b/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/LocalParWorker.java
index e146821..c4684ec 100644
--- a/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/LocalParWorker.java
+++ b/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/LocalParWorker.java
@@ -21,6 +21,7 @@ package org.apache.sysml.runtime.controlprogram.parfor;
 
 import java.util.Collection;
 
+import org.apache.sysml.api.DMLScript;
 import org.apache.sysml.conf.CompilerConfig;
 import org.apache.sysml.conf.ConfigurationManager;
 import org.apache.sysml.hops.OptimizerUtils;
@@ -79,6 +80,10 @@ public class LocalParWorker extends ParWorker implements Runnable
 			SparkExecutionContext sec = (SparkExecutionContext)_ec;
 			sec.setThreadLocalSchedulerPool("parforPool"+_workerID);
 		}
+
+		// Initialize this GPUContext to this thread
+		if (DMLScript.USE_ACCELERATOR)
+			_ec.getGPUContext().initializeThread();
 		
 		//setup compiler config for worker thread
 		ConfigurationManager.setLocalConfig(_cconf);

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/ParWorker.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/ParWorker.java b/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/ParWorker.java
index e0b30f4..05872b1 100644
--- a/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/ParWorker.java
+++ b/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/ParWorker.java
@@ -49,6 +49,11 @@ public abstract class ParWorker
 	protected long                      _workerID    = -1;
 	
 	protected ArrayList<ProgramBlock>   _childBlocks = null;
+
+	public ExecutionContext getExecutionContext() {
+		return _ec;
+	}
+
 	protected ExecutionContext          _ec          = null;
 	protected ArrayList<String>         _resultVars  = null;
 

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/opt/OptTreeConverter.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/opt/OptTreeConverter.java b/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/opt/OptTreeConverter.java
index 5c693d5..1d5a195 100644
--- a/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/opt/OptTreeConverter.java
+++ b/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/opt/OptTreeConverter.java
@@ -516,7 +516,7 @@ public class OptTreeConverter
 			LopProperties.ExecType et = (hop.getExecType()!=null) ? 
 					   hop.getExecType() : LopProperties.ExecType.CP;
 			switch( et ) {
-				case CP:
+				case CP:case GPU:
 					node.setExecType(ExecType.CP); break;
 				case SPARK:
 					node.setExecType(ExecType.SPARK); break;

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/cp/FunctionCallCPInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/FunctionCallCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/FunctionCallCPInstruction.java
index c65553b..0958aeb 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/cp/FunctionCallCPInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/cp/FunctionCallCPInstruction.java
@@ -172,8 +172,12 @@ public class FunctionCallCPInstruction extends CPInstruction
 		// Create a symbol table under a new execution context for the function invocation,
 		// and copy the function arguments into the created table. 
 		ExecutionContext fn_ec = ExecutionContextFactory.createContext(false, ec.getProgram());
+		if (DMLScript.USE_ACCELERATOR) {
+			fn_ec.setGPUContext(ec.getGPUContext());
+			ec.setGPUContext(null);
+			fn_ec.getGPUContext().initializeThread();
+		}
 		fn_ec.setVariables(functionVariables);
-		
 		// execute the function block
 		try {
 			fpb._functionName = this._functionName;
@@ -187,7 +191,6 @@ public class FunctionCallCPInstruction extends CPInstruction
 			String fname = DMLProgram.constructFunctionKey(_namespace, _functionName);
 			throw new DMLRuntimeException("error executing function " + fname, e);
 		}
-		
 		LocalVariableMap retVars = fn_ec.getVariables();  
 		
 		// cleanup all returned variables w/o binding 
@@ -206,6 +209,12 @@ public class FunctionCallCPInstruction extends CPInstruction
 		
 		// Unpin the pinned variables
 		ec.unpinVariables(_boundInputParamNames, pinStatus);
+
+		if (DMLScript.USE_ACCELERATOR) {
+			ec.setGPUContext(fn_ec.getGPUContext());
+			fn_ec.setGPUContext(null);
+			ec.getGPUContext().initializeThread();
+		}
 		
 		// add the updated binding for each return variable to the variables in original symbol table
 		for (int i=0; i< fpb.getOutputParams().size(); i++){

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/AggregateBinaryGPUInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/AggregateBinaryGPUInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/AggregateBinaryGPUInstruction.java
index 55c8f8d..2531c17 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/AggregateBinaryGPUInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/AggregateBinaryGPUInstruction.java
@@ -96,7 +96,7 @@ public class AggregateBinaryGPUInstruction extends GPUInstruction
 		int clen = (int) (_isRightTransposed ? m2.getNumRows() : m2.getNumColumns());
 
 		ec.setMetaData(_output.getName(), rlen, clen);
-		LibMatrixCUDA.matmult(ec, getExtendedOpcode(), m1, m2, _output.getName(), _isLeftTransposed, _isRightTransposed);
+		LibMatrixCUDA.matmult(ec, ec.getGPUContext(), getExtendedOpcode(), m1, m2, _output.getName(), _isLeftTransposed, _isRightTransposed);
         
 		//release inputs/outputs
 		ec.releaseMatrixInputForGPUInstruction(_input1.getName());
@@ -113,6 +113,6 @@ public class AggregateBinaryGPUInstruction extends GPUInstruction
 	@SuppressWarnings("unused")
 	private boolean isSparse(ExecutionContext ec, String var) throws DMLRuntimeException {
 		MatrixObject mo = ec.getMatrixObject(var);
-		return LibMatrixCUDA.isInSparseFormat(mo);
+		return LibMatrixCUDA.isInSparseFormat(ec.getGPUContext(), mo);
 	}
 }

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/AggregateUnaryGPUInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/AggregateUnaryGPUInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/AggregateUnaryGPUInstruction.java
index 45db44c..bfe30f9 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/AggregateUnaryGPUInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/AggregateUnaryGPUInstruction.java
@@ -27,6 +27,7 @@ import org.apache.sysml.runtime.functionobjects.ReduceCol;
 import org.apache.sysml.runtime.functionobjects.ReduceRow;
 import org.apache.sysml.runtime.instructions.InstructionUtils;
 import org.apache.sysml.runtime.instructions.cp.CPOperand;
+import org.apache.sysml.runtime.instructions.gpu.context.GPUContext;
 import org.apache.sysml.runtime.matrix.data.LibMatrixCUDA;
 import org.apache.sysml.runtime.matrix.operators.AggregateUnaryOperator;
 import org.apache.sysml.runtime.matrix.operators.Operator;
@@ -93,7 +94,7 @@ public class AggregateUnaryGPUInstruction extends GPUInstruction {
       ec.setMetaData(_output.getName(), rlen, 1);
     }
 
-    LibMatrixCUDA.unaryAggregate(ec, getExtendedOpcode(), in1, _output.getName(), (AggregateUnaryOperator)_optr);
+    LibMatrixCUDA.unaryAggregate(ec, ec.getGPUContext(), getExtendedOpcode(), in1, _output.getName(), (AggregateUnaryOperator)_optr);
 
     //release inputs/outputs
     ec.releaseMatrixInputForGPUInstruction(_input1.getName());

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/ConvolutionGPUInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/ConvolutionGPUInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/ConvolutionGPUInstruction.java
index a02115d..e806f3b 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/ConvolutionGPUInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/ConvolutionGPUInstruction.java
@@ -18,19 +18,20 @@
  */
 package org.apache.sysml.runtime.instructions.gpu;
 
+import java.util.ArrayList;
+
 import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.controlprogram.caching.MatrixObject;
 import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
 import org.apache.sysml.runtime.functionobjects.SwapIndex;
 import org.apache.sysml.runtime.instructions.InstructionUtils;
 import org.apache.sysml.runtime.instructions.cp.CPOperand;
+import org.apache.sysml.runtime.instructions.gpu.context.GPUContext;
 import org.apache.sysml.runtime.matrix.data.LibMatrixCUDA;
 import org.apache.sysml.runtime.matrix.operators.ReorgOperator;
 import org.apache.sysml.runtime.util.ConvolutionUtils;
 import org.apache.sysml.utils.GPUStatistics;
 
-import java.util.ArrayList;
-
 public class ConvolutionGPUInstruction extends GPUInstruction 
 {
 	private CPOperand _input1; 
@@ -186,9 +187,9 @@ public class ConvolutionGPUInstruction extends GPUInstruction
 		ec.setMetaData(_output.getName(), input.getNumRows(), input.getNumColumns());
 		MatrixObject out = getDenseMatrixOutputForGPUInstruction(ec, _output.getName());
 		if(instOpcode.equalsIgnoreCase("bias_add"))
-			LibMatrixCUDA.biasAdd(getExtendedOpcode(), input, bias, out);
+			LibMatrixCUDA.biasAdd(ec.getGPUContext(), getExtendedOpcode(), input, bias, out);
 		else if(instOpcode.equalsIgnoreCase("bias_multiply"))
-			LibMatrixCUDA.biasMultiply(getExtendedOpcode(), input, bias, out);
+			LibMatrixCUDA.biasMultiply(ec.getGPUContext(), getExtendedOpcode(), input, bias, out);
 		// release inputs/outputs
 		ec.releaseMatrixInputForGPUInstruction(_input1.getName());
 		ec.releaseMatrixInputForGPUInstruction(_input2.getName());
@@ -202,7 +203,7 @@ public class ConvolutionGPUInstruction extends GPUInstruction
 		
 		MatrixObject out = getDenseMatrixOutputForGPUInstruction(ec, _output.getName());
 		ec.setMetaData(_output.getName(), input.getNumRows(), input.getNumColumns());
-		LibMatrixCUDA.reluBackward(getExtendedOpcode(), input, dout, out);
+		LibMatrixCUDA.reluBackward(ec.getGPUContext(), getExtendedOpcode(), input, dout, out);
 		// release inputs/outputs
 		ec.releaseMatrixInputForGPUInstruction(_input1.getName());
 		ec.releaseMatrixInputForGPUInstruction(_input2.getName());
@@ -253,7 +254,7 @@ public class ConvolutionGPUInstruction extends GPUInstruction
 			
 			ec.setMetaData(_output.getName(), N, K * P * Q);
 			MatrixObject out = getDenseMatrixOutputForGPUInstruction(ec, _output.getName());
-			LibMatrixCUDA.conv2d(getExtendedOpcode(), image, filter, out, N, C, H, W,
+			LibMatrixCUDA.conv2d(ec.getGPUContext(), getExtendedOpcode(), image, filter, out, N, C, H, W,
 					K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
 		}
 		else if (instOpcode.equalsIgnoreCase("conv2d_bias_add")) {
@@ -268,7 +269,7 @@ public class ConvolutionGPUInstruction extends GPUInstruction
 			
 			ec.setMetaData(_output.getName(), N, K * P * Q);
 			MatrixObject out = getDenseMatrixOutputForGPUInstruction(ec, _output.getName());
-			LibMatrixCUDA.conv2dBiasAdd(getExtendedOpcode(), image, bias, filter, out, N, C, H, W,
+			LibMatrixCUDA.conv2dBiasAdd(ec.getGPUContext(), getExtendedOpcode(), image, bias, filter, out, N, C, H, W,
 						K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
 		}
 		else if (instOpcode.equalsIgnoreCase("conv2d_backward_filter")) {
@@ -283,7 +284,7 @@ public class ConvolutionGPUInstruction extends GPUInstruction
 			
 			ec.setMetaData(_output.getName(), K, C * R * S);
 			MatrixObject out = getDenseMatrixOutputForGPUInstruction(ec, _output.getName());
-			LibMatrixCUDA.conv2dBackwardFilter(getExtendedOpcode(), image, dout, out, N, C, H, W,
+			LibMatrixCUDA.conv2dBackwardFilter(ec.getGPUContext(), getExtendedOpcode(), image, dout, out, N, C, H, W,
 					K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
 			// TODO: For now always copy the device data to host
 			// ec.gpuCtx.copyDeviceToHost(outputBlock);
@@ -300,7 +301,7 @@ public class ConvolutionGPUInstruction extends GPUInstruction
 			
 			ec.setMetaData(_output.getName(), N, C * H * W);
 			MatrixObject out = getDenseMatrixOutputForGPUInstruction(ec, _output.getName());
-			LibMatrixCUDA.conv2dBackwardData(getExtendedOpcode(), filter, dout, out, N, C, H, W,
+			LibMatrixCUDA.conv2dBackwardData(ec.getGPUContext(), getExtendedOpcode(), filter, dout, out, N, C, H, W,
 					K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
 		}
 		else if (instOpcode.equalsIgnoreCase("maxpooling") || instOpcode.equalsIgnoreCase("relu_maxpooling")) {
@@ -313,10 +314,10 @@ public class ConvolutionGPUInstruction extends GPUInstruction
 			ec.setMetaData(_output.getName(), N, C * P * Q);
 			MatrixObject out = getDenseMatrixOutputForGPUInstruction(ec, _output.getName());
 			if(instOpcode.equalsIgnoreCase("maxpooling"))
-				LibMatrixCUDA.maxpooling(getExtendedOpcode(), image, out, N, C, H, W,
+				LibMatrixCUDA.maxpooling(ec.getGPUContext(), getExtendedOpcode(), image, out, N, C, H, W,
 					K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
 			else
-				LibMatrixCUDA.reluMaxpooling(getExtendedOpcode(), image, out, N, C, H, W,
+				LibMatrixCUDA.reluMaxpooling(ec.getGPUContext(), getExtendedOpcode(), image, out, N, C, H, W,
 						K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
 		}
 		else if (instOpcode.equalsIgnoreCase("maxpooling_backward")) {
@@ -331,7 +332,7 @@ public class ConvolutionGPUInstruction extends GPUInstruction
 			
 			ec.setMetaData(_output.getName(), N, C * H * W);
 			MatrixObject out = getDenseMatrixOutputForGPUInstruction(ec, _output.getName());
-			LibMatrixCUDA.maxpoolingBackward(getExtendedOpcode(), image, dout, out, N, C, H, W,
+			LibMatrixCUDA.maxpoolingBackward(ec.getGPUContext(), getExtendedOpcode(), image, dout, out, N, C, H, W,
 					K, R, S, pad_h, pad_w, stride_h, stride_w, P, Q);
 		}
 		else {

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/GPUInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/GPUInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/GPUInstruction.java
index ab275e7..0b69b5e 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/GPUInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/GPUInstruction.java
@@ -19,7 +19,6 @@
 
 package org.apache.sysml.runtime.instructions.gpu;
 
-import jcuda.runtime.JCuda;
 import org.apache.sysml.lops.runtime.RunMRJobs;
 import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.controlprogram.caching.MatrixObject;
@@ -149,7 +148,7 @@ public abstract class GPUInstruction extends Instruction
 	public void postprocessInstruction(ExecutionContext ec)
 					throws DMLRuntimeException
 	{
-		JCuda.cudaDeviceSynchronize();
+		//JCuda.cudaDeviceSynchronize();
 	}
 
 	/**

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/MMTSJGPUInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/MMTSJGPUInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/MMTSJGPUInstruction.java
index 0f2542a..af97141 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/MMTSJGPUInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/MMTSJGPUInstruction.java
@@ -32,6 +32,7 @@ import org.apache.sysml.runtime.controlprogram.caching.MatrixObject;
 import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
 import org.apache.sysml.runtime.instructions.InstructionUtils;
 import org.apache.sysml.runtime.instructions.cp.CPOperand;
+import org.apache.sysml.runtime.instructions.gpu.context.GPUContext;
 import org.apache.sysml.runtime.matrix.data.LibMatrixCUDA;
 import org.apache.sysml.runtime.matrix.operators.Operator;
 import org.apache.sysml.utils.GPUStatistics;
@@ -107,7 +108,7 @@ public class MMTSJGPUInstruction extends GPUInstruction
 
                 //execute operations 
                 ec.setMetaData(_output.getName(), rlen, clen);
-                LibMatrixCUDA.matmultTSMM(ec, getExtendedOpcode(), mat, _output.getName(), isLeftTransposed);
+                LibMatrixCUDA.matmultTSMM(ec, ec.getGPUContext(), getExtendedOpcode(), mat, _output.getName(), isLeftTransposed);
                 
                 ec.releaseMatrixInputForGPUInstruction(_input.getName());
                 ec.releaseMatrixOutputForGPUInstruction(_output.getName());

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixBuiltinGPUInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixBuiltinGPUInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixBuiltinGPUInstruction.java
index 2766909..3fc8a98 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixBuiltinGPUInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixBuiltinGPUInstruction.java
@@ -23,6 +23,7 @@ import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.controlprogram.caching.MatrixObject;
 import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
 import org.apache.sysml.runtime.instructions.cp.CPOperand;
+import org.apache.sysml.runtime.instructions.gpu.context.GPUContext;
 import org.apache.sysml.runtime.matrix.data.LibMatrixCUDA;
 import org.apache.sysml.runtime.matrix.operators.Operator;
 import org.apache.sysml.utils.GPUStatistics;
@@ -43,10 +44,10 @@ public class MatrixBuiltinGPUInstruction extends BuiltinUnaryGPUInstruction {
 		ec.setMetaData(_output.getName(), mat.getNumRows(), mat.getNumColumns());
 
 		if(opcode.equals("sel+")) {
-			LibMatrixCUDA.relu(ec, getExtendedOpcode(), mat, _output.getName());
+			LibMatrixCUDA.relu(ec, ec.getGPUContext(), getExtendedOpcode(), mat, _output.getName());
 
 		} else if (opcode.equals("exp")) {
-			LibMatrixCUDA.exp(ec, getExtendedOpcode(), mat, _output.getName());
+			LibMatrixCUDA.exp(ec, ec.getGPUContext(), getExtendedOpcode(), mat, _output.getName());
 		}
 		else {
 			throw new DMLRuntimeException("Unsupported GPU operator:" + opcode);

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixMatrixArithmeticGPUInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixMatrixArithmeticGPUInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixMatrixArithmeticGPUInstruction.java
index 2da1aa6..a1520a9 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixMatrixArithmeticGPUInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixMatrixArithmeticGPUInstruction.java
@@ -23,6 +23,7 @@ import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.controlprogram.caching.MatrixObject;
 import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
 import org.apache.sysml.runtime.instructions.cp.CPOperand;
+import org.apache.sysml.runtime.instructions.gpu.context.GPUContext;
 import org.apache.sysml.runtime.matrix.data.LibMatrixCUDA;
 import org.apache.sysml.runtime.matrix.operators.BinaryOperator;
 import org.apache.sysml.runtime.matrix.operators.Operator;
@@ -71,10 +72,10 @@ public class MatrixMatrixArithmeticGPUInstruction extends ArithmeticBinaryGPUIns
 		ec.setMetaData(_output.getName(), (int)rlen, (int)clen);
 		
 		BinaryOperator bop = (BinaryOperator) _optr;
-		LibMatrixCUDA.matrixScalarArithmetic(ec, getExtendedOpcode(), in1, in2, _output.getName(), isLeftTransposed, isRightTransposed, bop);
+		LibMatrixCUDA.matrixScalarArithmetic(ec, ec.getGPUContext(), getExtendedOpcode(), in1, in2, _output.getName(), isLeftTransposed, isRightTransposed, bop);
 		
 		ec.releaseMatrixInputForGPUInstruction(_input1.getName());
 		ec.releaseMatrixInputForGPUInstruction(_input2.getName());
-        ec.releaseMatrixOutputForGPUInstruction(_output.getName());
+		ec.releaseMatrixOutputForGPUInstruction(_output.getName());
 	}
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixMatrixAxpyGPUInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixMatrixAxpyGPUInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixMatrixAxpyGPUInstruction.java
index 2c833e4..28254c6 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixMatrixAxpyGPUInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/MatrixMatrixAxpyGPUInstruction.java
@@ -26,6 +26,7 @@ import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
 import org.apache.sysml.runtime.instructions.InstructionUtils;
 import org.apache.sysml.runtime.instructions.cp.CPOperand;
 import org.apache.sysml.runtime.instructions.cp.ScalarObject;
+import org.apache.sysml.runtime.instructions.gpu.context.GPUContext;
 import org.apache.sysml.runtime.matrix.data.LibMatrixCUDA;
 import org.apache.sysml.runtime.matrix.operators.Operator;
 import org.apache.sysml.utils.GPUStatistics;
@@ -103,11 +104,11 @@ public class MatrixMatrixAxpyGPUInstruction extends ArithmeticBinaryGPUInstructi
 					" and input2:" + rlen2 + " X " + clen2);
 		}
 		
-		LibMatrixCUDA.axpy(ec, getExtendedOpcode(), in1, in2, _output.getName(), multiplier*scalar.getDoubleValue());
+		LibMatrixCUDA.axpy(ec, ec.getGPUContext(), getExtendedOpcode(), in1, in2, _output.getName(), multiplier*scalar.getDoubleValue());
 		
 		ec.releaseMatrixInputForGPUInstruction(_input1.getName());
 		ec.releaseMatrixInputForGPUInstruction(_input2.getName());
-        ec.releaseMatrixOutputForGPUInstruction(_output.getName());
+		ec.releaseMatrixOutputForGPUInstruction(_output.getName());
 	}
 	
 	private boolean isValidMMOperation(long rlen1, long rlen2, long clen1, long clen2) {

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/ReorgGPUInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/ReorgGPUInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/ReorgGPUInstruction.java
index 54ba32e..424a2c5 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/ReorgGPUInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/ReorgGPUInstruction.java
@@ -25,6 +25,7 @@ import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
 import org.apache.sysml.runtime.functionobjects.SwapIndex;
 import org.apache.sysml.runtime.instructions.InstructionUtils;
 import org.apache.sysml.runtime.instructions.cp.CPOperand;
+import org.apache.sysml.runtime.instructions.gpu.context.GPUContext;
 import org.apache.sysml.runtime.matrix.data.LibMatrixCUDA;
 import org.apache.sysml.runtime.matrix.operators.Operator;
 import org.apache.sysml.runtime.matrix.operators.ReorgOperator;
@@ -81,10 +82,10 @@ public class ReorgGPUInstruction extends GPUInstruction
 		
 		//execute operation
 		ec.setMetaData(_output.getName(), rlen, clen);
-		LibMatrixCUDA.transpose(ec, getExtendedOpcode(), mat, _output.getName());
+		LibMatrixCUDA.transpose(ec, ec.getGPUContext(), getExtendedOpcode(), mat, _output.getName());
 		
 		//release inputs/outputs
 		ec.releaseMatrixInputForGPUInstruction(_input.getName());
-        ec.releaseMatrixOutputForGPUInstruction(_output.getName());
+		ec.releaseMatrixOutputForGPUInstruction(_output.getName());
 	}
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/ScalarMatrixArithmeticGPUInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/ScalarMatrixArithmeticGPUInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/ScalarMatrixArithmeticGPUInstruction.java
index 44cc6e2..64cb6c4 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/gpu/ScalarMatrixArithmeticGPUInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/ScalarMatrixArithmeticGPUInstruction.java
@@ -60,9 +60,9 @@ public class ScalarMatrixArithmeticGPUInstruction extends ArithmeticBinaryGPUIns
 		ScalarOperator sc_op = (ScalarOperator) _optr;
 		sc_op.setConstant(constant.getDoubleValue());
 		
-		LibMatrixCUDA.matrixScalarArithmetic(ec, getExtendedOpcode(), in1, _output.getName(), isTransposed, sc_op);
+		LibMatrixCUDA.matrixScalarArithmetic(ec, ec.getGPUContext(), getExtendedOpcode(), in1, _output.getName(), isTransposed, sc_op);
 		
 		ec.releaseMatrixInputForGPUInstruction(mat.getName());
-        ec.releaseMatrixOutputForGPUInstruction(_output.getName());
+		ec.releaseMatrixOutputForGPUInstruction(_output.getName());
 	}
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/129f0f6b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/CSRPointer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/CSRPointer.java b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/CSRPointer.java
new file mode 100644
index 0000000..5e202a9
--- /dev/null
+++ b/src/main/java/org/apache/sysml/runtime/instructions/gpu/context/CSRPointer.java
@@ -0,0 +1,457 @@
+package org.apache.sysml.runtime.instructions.gpu.context;
+
+import static jcuda.jcusparse.JCusparse.cusparseCreateMatDescr;
+import static jcuda.jcusparse.JCusparse.cusparseDcsr2dense;
+import static jcuda.jcusparse.JCusparse.cusparseSetMatIndexBase;
+import static jcuda.jcusparse.JCusparse.cusparseSetMatType;
+import static jcuda.jcusparse.JCusparse.cusparseSetPointerMode;
+import static jcuda.jcusparse.JCusparse.cusparseXcsrgeamNnz;
+import static jcuda.jcusparse.JCusparse.cusparseXcsrgemmNnz;
+import static jcuda.jcusparse.cusparseIndexBase.CUSPARSE_INDEX_BASE_ZERO;
+import static jcuda.jcusparse.cusparseMatrixType.CUSPARSE_MATRIX_TYPE_GENERAL;
+import static jcuda.runtime.JCuda.cudaMemcpy;
+import static jcuda.runtime.cudaMemcpyKind.cudaMemcpyDeviceToHost;
+import static jcuda.runtime.cudaMemcpyKind.cudaMemcpyHostToDevice;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.sysml.api.DMLScript;
+import org.apache.sysml.runtime.DMLRuntimeException;
+import org.apache.sysml.utils.GPUStatistics;
+
+import jcuda.Pointer;
+import jcuda.jcublas.cublasHandle;
+import jcuda.jcusparse.cusparseHandle;
+import jcuda.jcusparse.cusparseMatDescr;
+import jcuda.jcusparse.cusparsePointerMode;
+
+/**
+ * Compressed Sparse Row (CSR) format for CUDA
+ * Generalized matrix multiply is implemented for CSR format in the cuSparse library among other operations
+ */
+public class CSRPointer {
+
+  private static final Log LOG = LogFactory.getLog(CSRPointer.class.getName());
+
+  /** {@link GPUContext} instance to track the GPU to do work on */
+  private final GPUContext gpuContext;
+
+  private static final double ULTRA_SPARSITY_TURN_POINT = 0.0004;
+
+  public static cusparseMatDescr matrixDescriptor;
+
+  /** Number of non zeroes */
+  public long nnz;
+
+  /** double array of non zero values */
+  public Pointer val;
+
+  /** integer array of start of all rows and end of last row + 1 */
+  public Pointer rowPtr;
+
+  /** integer array of nnz values' column indices */
+  public Pointer colInd;
+
+  /** descriptor of matrix, only CUSPARSE_MATRIX_TYPE_GENERAL supported */
+  public cusparseMatDescr descr;
+
+  /**
+   * Default constructor to help with Factory method {@link #allocateEmpty(GPUContext, long, long)}
+   * @param gCtx   a valid {@link GPUContext}
+   */
+  private CSRPointer(GPUContext gCtx) {
+    gpuContext = gCtx;
+    val = new Pointer();
+    rowPtr = new Pointer();
+    colInd = new Pointer();
+    allocateMatDescrPointer();
+  }
+
+  private Pointer allocate(String instName, long size) throws DMLRuntimeException {
+    return getGPUContext().allocate(instName, size);
+  }
+
+  private Pointer allocate(long size) throws DMLRuntimeException {
+    return getGPUContext().allocate(size);
+  }
+
+  private void cudaFreeHelper(Pointer toFree) throws DMLRuntimeException {
+    getGPUContext().cudaFreeHelper(toFree);
+  }
+
+  private void cudaFreeHelper(Pointer toFree, boolean eager) throws DMLRuntimeException {
+    getGPUContext().cudaFreeHelper(toFree, eager);
+  }
+
+  private void cudaFreeHelper(String instName, Pointer toFree, boolean eager) throws DMLRuntimeException {
+    getGPUContext().cudaFreeHelper(instName, toFree, eager);
+  }
+
+  private static long getDoubleSizeOf(long numElems) {
+    return numElems * ((long)jcuda.Sizeof.DOUBLE);
+  }
+
+  private static long getIntSizeOf(long numElems) {
+    return numElems * ((long)jcuda.Sizeof.INT);
+  }
+
+  private GPUContext getGPUContext() throws DMLRuntimeException {
+    return gpuContext;
+  }
+
+  public static int toIntExact(long l) throws DMLRuntimeException {
+    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
+      throw new DMLRuntimeException("Cannot be cast to int:" + l);
+    }
+    return (int) l;
+  }
+
+  /**
+   * @return Singleton default matrix descriptor object
+   * (set with CUSPARSE_MATRIX_TYPE_GENERAL, CUSPARSE_INDEX_BASE_ZERO)
+   */
+  public static cusparseMatDescr getDefaultCuSparseMatrixDescriptor() {
+    if (matrixDescriptor == null) {
+      // Code from JCuda Samples - http://www.jcuda.org/samples/JCusparseSample.java
+      matrixDescriptor = new cusparseMatDescr();
+      cusparseCreateMatDescr(matrixDescriptor);
+      cusparseSetMatType(matrixDescriptor, CUSPARSE_MATRIX_TYPE_GENERAL);
+      cusparseSetMatIndexBase(matrixDescriptor, CUSPARSE_INDEX_BASE_ZERO);
+    }
+    return matrixDescriptor;
+  }
+
+  /**
+   * Estimate the size of a CSR matrix in GPU memory
+   * Size of pointers is not needed and is not added in
+   *
+   * @param nnz2 number of non zeroes
+   * @param rows number of rows
+   * @return size estimate
+   */
+  public static long estimateSize(long nnz2, long rows) {
+    long sizeofValArray = getDoubleSizeOf(nnz2);
+    long sizeofRowPtrArray = getIntSizeOf(rows + 1);
+    long sizeofColIndArray = getIntSizeOf(nnz2);
+    long sizeofDescr = getIntSizeOf(4);
+    // From the CUSPARSE documentation, the cusparseMatDescr in native code is represented as:
+    // typedef struct {
+    // 	cusparseMatrixType_t MatrixType;
+    //	cusparseFillMode_t FillMode;
+    //	cusparseDiagType_t DiagType;
+    // 	cusparseIndexBase_t IndexBase;
+    // } cusparseMatDescr_t;
+    long tot = sizeofValArray + sizeofRowPtrArray + sizeofColIndArray + sizeofDescr;
+    return tot;
+  }
+
+  /**
+   * Static method to copy a CSR sparse matrix from Host to Device
+   *
+   * @param dest   [input] destination location (on GPU)
+   * @param rows   number of rows
+   * @param nnz    number of non-zeroes
+   * @param rowPtr integer array of row pointers
+   * @param colInd integer array of column indices
+   * @param values double array of non zero values
+   */
+  public static void copyToDevice(CSRPointer dest, int rows, long nnz, int[] rowPtr, int[] colInd, double[] values) {
+    CSRPointer r = dest;
+    long t0 = 0;
+    if (DMLScript.STATISTICS) t0 = System.nanoTime();
+    r.nnz = nnz;
+    cudaMemcpy(r.rowPtr, Pointer.to(rowPtr), getIntSizeOf(rows + 1), cudaMemcpyHostToDevice);
+    cudaMemcpy(r.colInd, Pointer.to(colInd), getIntSizeOf(nnz), cudaMemcpyHostToDevice);
+    cudaMemcpy(r.val, Pointer.to(values), getDoubleSizeOf(nnz), cudaMemcpyHostToDevice);
+    if (DMLScript.STATISTICS) GPUStatistics.cudaToDevTime.addAndGet(System.nanoTime() - t0);
+    if (DMLScript.STATISTICS) GPUStatistics.cudaToDevCount.addAndGet(3);
+  }
+
+  /**
+   * Static method to copy a CSR sparse matrix from Device to host
+   *
+   * @param src    [input] source location (on GPU)
+   * @param rows   [input] number of rows
+   * @param nnz    [input] number of non-zeroes
+   * @param rowPtr [output] pre-allocated integer array of row pointers of size (rows+1)
+   * @param colInd [output] pre-allocated integer array of column indices of size nnz
+   * @param values [output] pre-allocated double array of values of size nnz
+   */
+  public static void copyToHost(CSRPointer src, int rows, long nnz, int[] rowPtr, int[] colInd, double[] values) {
+    CSRPointer r = src;
+    long t0 = 0;
+    if (DMLScript.STATISTICS) t0 = System.nanoTime();
+    cudaMemcpy(Pointer.to(rowPtr), r.rowPtr, getIntSizeOf(rows + 1), cudaMemcpyDeviceToHost);
+    cudaMemcpy(Pointer.to(colInd), r.colInd, getIntSizeOf(nnz), cudaMemcpyDeviceToHost);
+    cudaMemcpy(Pointer.to(values), r.val, getDoubleSizeOf(nnz), cudaMemcpyDeviceToHost);
+    if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevTime.addAndGet(System.nanoTime() - t0);
+    if (DMLScript.STATISTICS) GPUStatistics.cudaFromDevCount.addAndGet(3);
+  }
+
+  /**
+   * Estimates the number of non zero elements from the results of a sparse cusparseDgeam operation
+   * C = a op(A) + b op(B)
+   * @param gCtx   a valid {@link GPUContext}
+   * @param handle a valid {@link cusparseHandle}
+   * @param A      Sparse Matrix A on GPU
+   * @param B      Sparse Matrix B on GPU
+   * @param m      Rows in A
+   * @param n      Columns in Bs
+   * @return CSR (compressed sparse row) pointer
+   * @throws DMLRuntimeException if DMLRuntimeException occurs
+   */
+  public static CSRPointer allocateForDgeam(GPUContext gCtx, cusparseHandle handle, CSRPointer A, CSRPointer B, int m, int n)
+          throws DMLRuntimeException {
+    if (A.nnz >= Integer.MAX_VALUE || B.nnz >= Integer.MAX_VALUE) {
+      throw new DMLRuntimeException("Number of non zeroes is larger than supported by cuSparse");
+    }
+    CSRPointer C = new CSRPointer(gCtx);
+    step1AllocateRowPointers(gCtx, handle, C, m);
+    step2GatherNNZGeam(gCtx, handle, A, B, C, m, n);
+    step3AllocateValNInd(gCtx, handle, C);
+    return C;
+  }
+
+  /**
+   * Estimates the number of non-zero elements from the result of a sparse matrix multiplication C = A * B
+   * and returns the {@link CSRPointer} to C with the appropriate GPU memory.
+   *
+   * @param handle a valid {@link cusparseHandle}
+   * @param A      Sparse Matrix A on GPU
+   * @param transA 'T' if A is to be transposed, 'N' otherwise
+   * @param B      Sparse Matrix B on GPU
+   * @param transB 'T' if B is to be transposed, 'N' otherwise
+   * @param m      Rows in A
+   * @param n      Columns in B
+   * @param k      Columns in A / Rows in B
+   * @return a {@link CSRPointer} instance that encapsulates the CSR matrix on GPU
+   * @throws DMLRuntimeException if DMLRuntimeException occurs
+   */
+  public static CSRPointer allocateForMatrixMultiply(GPUContext gCtx, cusparseHandle handle, CSRPointer A, int transA, CSRPointer B, int transB, int m, int n, int k)
+          throws DMLRuntimeException {
+    // Following the code example at http://docs.nvidia.com/cuda/cusparse/#cusparse-lt-t-gt-csrgemm and at
+    // https://github.com/jcuda/jcuda-matrix-utils/blob/master/JCudaMatrixUtils/src/test/java/org/jcuda/matrix/samples/JCusparseSampleDgemm.java
+    CSRPointer C = new CSRPointer(gCtx);
+    step1AllocateRowPointers(gCtx, handle, C, m);
+    step2GatherNNZGemm(gCtx, handle, A, transA, B, transB, C, m, n, k);
+    step3AllocateValNInd(gCtx, handle, C);
+    return C;
+  }
+
+  /**
+   * Check for ultra sparsity
+   *
+   * @param rows number of rows
+   * @param cols number of columns
+   * @return true if ultra sparse
+   */
+  public boolean isUltraSparse(int rows, int cols) {
+    double sp = ((double) nnz / rows / cols);
+    return sp < ULTRA_SPARSITY_TURN_POINT;
+  }
+
+// ==============================================================================================
+
+// The following methods estimate the memory needed for sparse matrices that are
+// results of operations on other sparse matrices using the cuSparse Library.
+// The operation is C = op(A) binaryOperation op(B), C is the output and A & B are the inputs
+// op = whether to transpose or not
+// binaryOperation = For cuSparse, +, - are *(matmul) are supported
+
+// From CuSparse Manual,
+// Since A and B have different sparsity patterns, cuSPARSE adopts a two-step approach
+// to complete sparse matrix C. In the first step, the user allocates csrRowPtrC of m+1
+// elements and uses function cusparseXcsrgeamNnz() to determine csrRowPtrC
+// and the total number of nonzero elements. In the second step, the user gathers nnzC
+//(number of nonzero elements of matrix C) from either (nnzC=*nnzTotalDevHostPtr)
+// or (nnzC=csrRowPtrC(m)-csrRowPtrC(0)) and allocates csrValC, csrColIndC of
+// nnzC elements respectively, then finally calls function cusparse[S|D|C|Z]csrgeam()
+// to complete matrix C.
+
+  /**
+   * Initializes {@link #descr} to CUSPARSE_MATRIX_TYPE_GENERAL,
+   * the default that works for DGEMM.
+   */
+  private void allocateMatDescrPointer() {
+    this.descr = getDefaultCuSparseMatrixDescriptor();
+  }
+
+  /**
+   * Factory method to allocate an empty CSR Sparse matrix on the GPU
+   *
+   * @param nnz2 number of non-zeroes
+   * @param rows number of rows
+   * @return a {@link CSRPointer} instance that encapsulates the CSR matrix on GPU
+   * @throws DMLRuntimeException if DMLRuntimeException occurs
+   */
+  public static CSRPointer allocateEmpty(GPUContext gCtx, long nnz2, long rows) throws DMLRuntimeException {
+    LOG.trace("GPU : allocateEmpty from CSRPointer with nnz=" + nnz2 + " and rows=" + rows + ", GPUContext=" + gCtx);
+    assert nnz2 > -1 : "Incorrect usage of internal API, number of non zeroes is less than 0 when trying to allocate sparse data on GPU";
+    CSRPointer r = new CSRPointer(gCtx);
+    r.nnz = nnz2;
+    if (nnz2 == 0) {
+      // The convention for an empty sparse matrix is to just have an instance of the CSRPointer object
+      // with no memory allocated on the GPU.
+      return r;
+    }
+    gCtx.ensureFreeSpace(getDoubleSizeOf(nnz2) + getIntSizeOf(rows + 1) + getIntSizeOf(nnz2));
+    // increment the cudaCount by 1 for the allocation of all 3 arrays
+    r.val = gCtx.allocate(null, getDoubleSizeOf(nnz2));
+    r.rowPtr = gCtx.allocate(null, getIntSizeOf(rows + 1));
+    r.colInd = gCtx.allocate(null, getIntSizeOf(nnz2));
+    return r;
+  }
+
+  /**
+   * Allocate row pointers of m+1 elements
+   * @param gCtx   a valid {@link GPUContext}
+   * @param handle a valid {@link cusparseHandle}
+   * @param C      Output matrix
+   * @param rowsC  number of rows in C
+   * @throws DMLRuntimeException ?
+   */
+  private static void step1AllocateRowPointers(GPUContext gCtx, cusparseHandle handle, CSRPointer C, int rowsC) throws DMLRuntimeException {
+    LOG.trace("GPU : step1AllocateRowPointers" + ", GPUContext=" + gCtx);
+    cusparseSetPointerMode(handle, cusparsePointerMode.CUSPARSE_POINTER_MODE_HOST);
+    //cudaDeviceSynchronize;
+    // Do not increment the cudaCount of allocations on GPU
+    C.rowPtr = gCtx.allocate(getIntSizeOf((long) rowsC + 1));
+  }
+
+  /**
+   * Determine total number of nonzero element for the cusparseDgeam  operation.
+   * This is done from either (nnzC=*nnzTotalDevHostPtr) or (nnzC=csrRowPtrC(m)-csrRowPtrC(0))
+   * @param gCtx   a valid {@link GPUContext}
+   * @param handle a valid {@link cusparseHandle}
+   * @param A      Sparse Matrix A on GPU
+   * @param B      Sparse Matrix B on GPU
+   * @param C      Output Sparse Matrix C on GPU
+   * @param m      Rows in C
+   * @param n      Columns in C
+   * @throws DMLRuntimeException ?
+   */
+  private static void step2GatherNNZGeam(GPUContext gCtx, cusparseHandle handle, CSRPointer A, CSRPointer B, CSRPointer C, int m, int n) throws DMLRuntimeException {
+    LOG.trace("GPU : step2GatherNNZGeam for DGEAM" + ", GPUContext=" + gCtx);
+    int[] CnnzArray = {-1};
+    cusparseXcsrgeamNnz(handle, m, n,
+            A.descr, toIntExact(A.nnz), A.rowPtr, A.colInd,
+            B.descr, toIntExact(B.nnz), B.rowPtr, B.colInd,
+            C.descr, C.rowPtr, Pointer.to(CnnzArray));
+    //cudaDeviceSynchronize;
+    if (CnnzArray[0] != -1) {
+      C.nnz = CnnzArray[0];
+    } else {
+      int baseArray[] = {0};
+      cudaMemcpy(Pointer.to(CnnzArray), C.rowPtr.withByteOffset(getIntSizeOf(m)), getIntSizeOf(1), cudaMemcpyDeviceToHost);
+      cudaMemcpy(Pointer.to(baseArray), C.rowPtr, getIntSizeOf(1), cudaMemcpyDeviceToHost);
+      C.nnz = CnnzArray[0] - baseArray[0];
+    }
+  }
+
+// ==============================================================================================
+
+  /**
+   * Determine total number of nonzero element for the cusparseDgemm operation.
+   * @param gCtx   a valid {@link GPUContext}
+   * @param handle a valid {@link cusparseHandle}
+   * @param A      Sparse Matrix A on GPU
+   * @param transA op - whether A is transposed
+   * @param B      Sparse Matrix B on GPU
+   * @param transB op - whether B is transposed
+   * @param C      Output Sparse Matrix C on GPU
+   * @param m      Number of rows of sparse matrix op ( A ) and C
+   * @param n      Number of columns of sparse matrix op ( B ) and C
+   * @param k      Number of columns/rows of sparse matrix op ( A ) / op ( B )
+   * @throws DMLRuntimeException ?
+   */
+  private static void step2GatherNNZGemm(GPUContext gCtx, cusparseHandle handle, CSRPointer A, int transA, CSRPointer B, int transB, CSRPointer C, int m, int n, int k) throws DMLRuntimeException {
+    LOG.trace("GPU : step2GatherNNZGemm for DGEMM" + ", GPUContext=" + gCtx);
+    int[] CnnzArray = {-1};
+    if (A.nnz >= Integer.MAX_VALUE || B.nnz >= Integer.MAX_VALUE) {
+      throw new DMLRuntimeException("Number of non zeroes is larger than supported by cuSparse");
+    }
+    cusparseXcsrgemmNnz(handle, transA, transB, m, n, k,
+            A.descr, toIntExact(A.nnz), A.rowPtr, A.colInd,
+            B.descr, toIntExact(B.nnz), B.rowPtr, B.colInd,
+            C.descr, C.rowPtr, Pointer.to(CnnzArray));
+    //cudaDeviceSynchronize;
+    if (CnnzArray[0] != -1) {
+      C.nnz = CnnzArray[0];
+    } else {
+      int baseArray[] = {0};
+      cudaMemcpy(Pointer.to(CnnzArray), C.rowPtr.withByteOffset(getIntSizeOf(m)), getIntSizeOf(1), cudaMemcpyDeviceToHost);
+      cudaMemcpy(Pointer.to(baseArray), C.rowPtr, getIntSizeOf(1), cudaMemcpyDeviceToHost);
+      C.nnz = CnnzArray[0] - baseArray[0];
+    }
+  }
+
+  /**
+   * Allocate val and index pointers.
+   * @param gCtx   a valid {@link GPUContext}
+   * @param handle a valid {@link cusparseHandle}
+   * @param C      Output sparse matrix on GPU
+   * @throws DMLRuntimeException ?
+   */
+  private static void step3AllocateValNInd(GPUContext gCtx, cusparseHandle handle, CSRPointer C) throws DMLRuntimeException {
+    LOG.trace("GPU : step3AllocateValNInd" + ", GPUContext=" + gCtx);
+    // Increment cudaCount by one when all three arrays of CSR sparse array are allocated
+    C.val = gCtx.allocate(null, getDoubleSizeOf(C.nnz));
+    C.colInd = gCtx.allocate(null, getIntSizeOf(C.nnz));
+  }
+
+  /**
+   * Copies this CSR matrix on the GPU to a dense column-major matrix
+   * on the GPU. This is a temporary matrix for operations such as
+   * cusparseDcsrmv.
+   * Since the allocated matrix is temporary, bookkeeping is not updated.
+   * The caller is responsible for calling "free" on the returned Pointer object
+   *
+   * @param cusparseHandle a valid {@link cusparseHandle}
+   * @param cublasHandle   a valid {@link cublasHandle}
+   * @param rows           number of rows in this CSR matrix
+   * @param cols           number of columns in this CSR matrix
+   * @throws DMLRuntimeException if DMLRuntimeException occurs
+   * @return A {@link Pointer} to the allocated dense matrix (in column-major format)
+   */
+  public Pointer toColumnMajorDenseMatrix(cusparseHandle cusparseHandle, cublasHandle cublasHandle, int rows, int cols) throws DMLRuntimeException {
+    LOG.trace("GPU : sparse -> column major dense (inside CSRPointer) on " + this + ", GPUContext=" + getGPUContext());
+    long size = ((long) rows) * getDoubleSizeOf((long) cols);
+    Pointer A = allocate(size);
+    // If this sparse block is empty, the allocated dense matrix, initialized to zeroes, will be returned.
+    if (val != null && rowPtr != null && colInd != null && nnz > 0) {
+      // Note: cusparseDcsr2dense method cannot handle empty blocks
+      cusparseDcsr2dense(cusparseHandle, rows, cols, descr, val, rowPtr, colInd, A, rows);
+      //cudaDeviceSynchronize;
+    } else {
+      LOG.warn("in CSRPointer, the values array, row pointers array or column indices array was null");
+    }
+    return A;
+  }
+
+  /**
+   * Calls cudaFree lazily on the allocated {@link Pointer} instances
+   */
+  public void deallocate() throws DMLRuntimeException {
+    deallocate(false);
+  }
+
+  /**
+   * Calls cudaFree lazily or eagerly on the allocated {@link Pointer} instances
+   *
+   * @param eager whether to do eager or lazy cudaFrees
+   */
+  public void deallocate(boolean eager) throws DMLRuntimeException {
+    if (nnz > 0) {
+      cudaFreeHelper(val, eager);
+      cudaFreeHelper(rowPtr, eager);
+      cudaFreeHelper(colInd, eager);
+    }
+  }
+
+  @Override
+  public String toString() {
+    return "CSRPointer{" +
+            "nnz=" + nnz +
+            '}';
+  }
+}