All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.sysml.runtime.util.LocalFileUtils Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
/*
 * 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.util;

import java.io.BufferedInputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;

import org.apache.hadoop.io.Writable;
import org.apache.sysml.api.DMLScript;
import org.apache.sysml.conf.ConfigurationManager;
import org.apache.sysml.conf.DMLConfig;
import org.apache.sysml.lops.Lop;
import org.apache.sysml.runtime.DMLRuntimeException;
import org.apache.sysml.runtime.controlprogram.caching.CacheBlock;
import org.apache.sysml.runtime.controlprogram.parfor.stat.InfrastructureAnalyzer;
import org.apache.sysml.runtime.controlprogram.parfor.util.IDSequence;
import org.apache.sysml.runtime.io.IOUtilFunctions;
import org.apache.sysml.runtime.matrix.data.FrameBlock;
import org.apache.sysml.runtime.matrix.data.MatrixBlock;
import org.apache.sysml.runtime.matrix.data.MatrixIndexes;
import org.apache.sysml.runtime.matrix.data.MatrixValue;
import org.apache.sysml.runtime.matrix.data.Pair;

public class LocalFileUtils 
{
	public static final int BUFFER_SIZE = 8192;
	
	//unique IDs per JVM for tmp files
	private static IDSequence _seq = null;
	private static String _workingDir = null;
	
	//categories of temp files under process-specific working dir
	public static final String CATEGORY_CACHE        = "cache";
	public static final String CATEGORY_PARTITIONING = "partitioning";
	public static final String CATEGORY_RESULTMERGE  = "resultmerge";
	public static final String CATEGORY_WORK         = "work";
	public static final String CATEGORY_CODEGEN      = "codegen";
	
	static {
		_seq = new IDSequence();
	}
	
	/** Reads a matrix block from local file system.
	 * 
	 * @param filePathAndName file to read
	 * @return matrix block
	 * @throws IOException if IOException occurs
	 */
	public static MatrixBlock readMatrixBlockFromLocal(String filePathAndName) throws IOException {
		return (MatrixBlock) readWritableFromLocal(filePathAndName, new MatrixBlock());
	}
	
	/** Reads a matrix block from local file system.
	 * 
	 * @param filePathAndName file to read
	 * @param reuse matrix block to reuse
	 * @return matrix block
	 * @throws IOException if IOException occurs
	 */
	public static MatrixBlock readMatrixBlockFromLocal(String filePathAndName, MatrixBlock reuse) throws IOException {
		return (MatrixBlock) readWritableFromLocal(filePathAndName, reuse);
	}

	/** Reads a matrix/frame block from local file system.
	 * 
	 * @param filePathAndName file to read
	 * @param matrix if true, read matrix. if false, read frame.
	 * @return cache block (common interface to MatrixBlock and FrameBlock)
	 * @throws IOException if IOException occurs
	 */
	public static CacheBlock readCacheBlockFromLocal(String filePathAndName, boolean matrix) throws IOException {
		return (CacheBlock) readWritableFromLocal(filePathAndName, matrix?new MatrixBlock():new FrameBlock());
	}
	
	/**
	 * Reads an arbitrary writable from local file system, using a fused buffered reader
	 * with special support for matrix blocks.
	 * 
	 * @param filePathAndName file to read
	 * @param ret hadoop writable
	 * @return hadoop writable
	 * @throws IOException if IOException occurs
	 */
	public static Writable readWritableFromLocal(String filePathAndName, Writable ret)
		throws IOException
	{
		FileInputStream fis = new FileInputStream( filePathAndName );
		DataInput in  = !(ret instanceof MatrixBlock) ? 
				new DataInputStream(new BufferedInputStream(fis, BUFFER_SIZE)) :
				new FastBufferedDataInputStream(fis, BUFFER_SIZE);		
		try {
			ret.readFields(in);
		}
		finally {
			IOUtilFunctions.closeSilently((InputStream)in);
			IOUtilFunctions.closeSilently(fis);
		}
			
		return ret;
	}
	
	/** Writes a matrix block to local file system.
	 * 
	 * @param filePathAndName file to write
	 * @param mb matrix block
	 * @throws IOException if IOException occurs
	 */
	public static void writeMatrixBlockToLocal(String filePathAndName, MatrixBlock mb) throws IOException {
		writeWritableToLocal(filePathAndName, mb);
	}

	/** Writes a matrix/frame block to local file system.
	 * 
	 * @param filePathAndName file to write
	 * @param cb cache block (common interface to matrix block and frame block)
	 * @throws IOException if IOException occurs
	 */
	public static void writeCacheBlockToLocal(String filePathAndName, CacheBlock cb) throws IOException {
		writeWritableToLocal(filePathAndName, cb);
	}
	
	/**
	 * Writes an arbitrary writable to local file system, using a fused buffered writer
	 * with special support for matrix blocks.
	 * 
	 * @param filePathAndName file to write
	 * @param mb Hadoop writable
	 * @throws IOException if IOException occurs
	 */
	public static void writeWritableToLocal(String filePathAndName, Writable mb)
		throws IOException
	{	
		FileOutputStream fos = new FileOutputStream( filePathAndName );
		FastBufferedDataOutputStream out = new FastBufferedDataOutputStream(fos, BUFFER_SIZE);
		
		try {
			mb.write(out);
		}
		finally {
			IOUtilFunctions.closeSilently(out);
			IOUtilFunctions.closeSilently(fos);
		}	
	}

	public static void writeByteArrayToLocal( String filePathAndName, byte[] data )
		throws IOException
	{	
		//byte array write via java.nio file channel ~10-15% faster than java.io
		FileChannel channel = null;
		try {
			Path path = Paths.get(filePathAndName);
			channel = FileChannel.open(path, StandardOpenOption.CREATE, 
				StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
			channel.write(ByteBuffer.wrap(data));
		}
		finally {
			IOUtilFunctions.closeSilently(channel);
		}
	}

	public static int readBlockSequenceFromLocal( String filePathAndName, Pair[] outValues, HashMap outMap) 
		throws IOException
	{
		FileInputStream fis = new FileInputStream( filePathAndName );
		FastBufferedDataInputStream in = new FastBufferedDataInputStream( fis, BUFFER_SIZE );
		int bufferSize = 0;
		
		try
		{
			int len = in.readInt();
			for( int i=0; i[] inValues, int len ) 
		throws IOException
	{
		if( len > inValues.length )
			throw new IOException("Invalid length of block sequence: len="+len+" vs data="+inValues.length);
		
		FileOutputStream fos = new FileOutputStream( filePathAndName );
		FastBufferedDataOutputStream out = new FastBufferedDataOutputStream(fos, BUFFER_SIZE);
		
		try 
		{
			out.writeInt(len);
			for( int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy