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

org.apache.sysml.runtime.matrix.data.MatrixPackedCell 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.matrix.data;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.sysml.lops.PartialAggregate.CorrectionLocationType;
import org.apache.sysml.runtime.DMLRuntimeException;
import org.apache.sysml.runtime.DMLUnsupportedOperationException;
import org.apache.sysml.runtime.instructions.cp.KahanObject;
import org.apache.sysml.runtime.matrix.operators.AggregateOperator;


public class MatrixPackedCell extends MatrixCell
{

	private static final long serialVersionUID = -3633665169444817750L;

	private double[] extras=null;
	private int extra_size=0;
	
	public MatrixPackedCell(double v, int n)
	{
		value=v;
		checkAndAllocateSpace(n);
	}
	
	public MatrixPackedCell(MatrixPackedCell that)
	{
		this.value=that.value;
		checkAndAllocateSpace(that.extra_size);
		for(int i=0; i=extra_size)
			return 0;
		else
			return extras[i];
	}

	//with corrections
	@Override
	public void incrementalAggregate(AggregateOperator aggOp, MatrixValue correction, 
			MatrixValue newWithCorrection)throws DMLUnsupportedOperationException, DMLRuntimeException {
		incrementalAggregate(aggOp, newWithCorrection);
	}
	
	//with corrections
	@Override
	public void incrementalAggregate(AggregateOperator aggOp,
			MatrixValue newWithCorrection)throws DMLUnsupportedOperationException, DMLRuntimeException {

		
		MatrixPackedCell newWithCor=checkType(newWithCorrection);
		if(aggOp.correctionLocation==CorrectionLocationType.NONE || aggOp.correctionLocation==CorrectionLocationType.LASTROW || aggOp.correctionLocation==CorrectionLocationType.LASTCOLUMN)
		{
			checkAndAllocateSpace(1);
			KahanObject buffer=new KahanObject(value, extras[0]);
			buffer=(KahanObject) aggOp.increOp.fn.execute(buffer, newWithCor.value, newWithCor.getExtraByPostition(0));
			value=buffer._sum;
			extras[0]=buffer._correction;
		//	System.out.println("--- "+buffer);
		}else if(aggOp.correctionLocation==CorrectionLocationType.LASTROW || aggOp.correctionLocation==CorrectionLocationType.LASTTWOCOLUMNS)
		{
			checkAndAllocateSpace(2);
			KahanObject buffer=new KahanObject(value, extras[0]);
			buffer._sum=value;
			double n=extras[0];
			buffer._correction=extras[1];
			double mu2=newWithCor.value;
			double n2=newWithCor.getExtraByPostition(0);
			n=n+n2;
			double toadd=(mu2-buffer._sum)*n2/n;
			buffer=(KahanObject) aggOp.increOp.fn.execute(buffer, toadd);
			value=buffer._sum;
			extras[0]=n;
			extras[1]=buffer._correction;
		}
		else
			throw new DMLRuntimeException("unrecognized correctionLocation: "+aggOp.correctionLocation);
		
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		value=in.readDouble();
		int n=in.readInt();
		if(extras==null || extras.length




© 2015 - 2024 Weber Informatics LLC | Privacy Policy