public synchronized MatrixBlock readMatrixPartition(IndexRange pred) throws CacheException {
  if (LOG.isTraceEnabled())   LOG.trace("Acquire partition " + getVarName() + " "+ pred);
  long t0=DMLScript.STATISTICS ? System.nanoTime() : 0;
  if (!_partitioned)   throw new CacheException("MatrixObject not available to indexed read.");
  if (_partitionInMemory != null)   return _partitionInMemory;
  MatrixBlock mb=null;
  try {
    boolean blockwise=(_partitionFormat == PDataPartitionFormat.ROW_BLOCK_WISE || _partitionFormat == PDataPartitionFormat.COLUMN_BLOCK_WISE);
    MatrixFormatMetaData iimd=(MatrixFormatMetaData)_metaData;
    MatrixCharacteristics mc=iimd.getMatrixCharacteristics();
    int brlen=mc.getRowsPerBlock();
    int bclen=mc.getColsPerBlock();
    String fname=getPartitionFileName(pred,brlen,bclen);
    if (blockwise && _partitionCacheName != null && _partitionCacheName.equals(fname)) {
      mb=_cache.get();
    }
    if (mb == null) {
      long rows=-1;
      long cols=-1;
switch (_partitionFormat) {
case ROW_WISE:
        rows=1;
      cols=mc.getCols();
    break;
case ROW_BLOCK_WISE:
  rows=brlen;
cols=mc.getCols();
break;
case COLUMN_WISE:
rows=mc.getRows();
cols=1;
break;
case COLUMN_BLOCK_WISE:
rows=mc.getRows();
cols=bclen;
break;
default :
throw new CacheException("Unsupported partition format: " + _partitionFormat);
}
if (MapReduceTool.existsFileOnHDFS(fname)) mb=readBlobFromHDFS(fname,rows,cols);
 else {
mb=new MatrixBlock((int)rows,(int)cols,true);
LOG.warn("Reading empty matrix partition " + fname);
}
}
if (blockwise) {
_partitionCacheName=fname;
_cache=new SoftReference<MatrixBlock>(mb);
if (_partitionFormat == PDataPartitionFormat.ROW_BLOCK_WISE) {
int rix=(int)((pred.rowStart - 1) % brlen);
mb=mb.sliceOperations(rix,rix,(int)(pred.colStart - 1),(int)(pred.colEnd - 1),new MatrixBlock());
}
if (_partitionFormat == PDataPartitionFormat.COLUMN_BLOCK_WISE) {
int cix=(int)((pred.colStart - 1) % bclen);
mb=mb.sliceOperations((int)(pred.rowStart - 1),(int)(pred.rowEnd - 1),cix,cix,new MatrixBlock());
}
}
}
 catch (Exception ex) {
throw new CacheException(ex);
}
if (DMLScript.STATISTICS) {
long t1=System.nanoTime();
CacheStatistics.incrementAcquireRTime(t1 - t0);
}
return mb;
}

public synchronized MatrixBlock readMatrixPartition ( IndexRange pred ) throws CacheException { if ( LOG . isTraceEnabled ( ) ) LOG . trace ( "Acquire partition " + getVarName ( ) + " " + pred ) ; long t0 = DMLScript . STATISTICS ? System . nanoTime ( ) : 0 ; if ( ! _partitioned ) throw new CacheException ( "MatrixObject not available to indexed read." ) ; if ( _partitionInMemory != null ) return _partitionInMemory ; MatrixBlock mb = null ; try { boolean blockwise = ( _partitionFormat == PDataPartitionFormat . ROW_BLOCK_WISE || _partitionFormat == PDataPartitionFormat . COLUMN_BLOCK_WISE ) ; MatrixFormatMetaData iimd = ( MatrixFormatMetaData ) _metaData ; MatrixCharacteristics mc = iimd . getMatrixCharacteristics ( ) ; int brlen = mc . getRowsPerBlock ( ) ; int bclen = mc . getColsPerBlock ( ) ; String fname = getPartitionFileName ( pred , brlen , bclen ) ; if ( blockwise && _partitionCacheName != null && _partitionCacheName . equals ( fname ) ) { mb = _cache . get ( ) ; } if ( mb == null ) { long rows = - 1 ; long cols = - 1 ; switch ( _partitionFormat ) { case ROW_WISE : rows = 1 ; cols = mc . getCols ( ) ; break ; case ROW_BLOCK_WISE : rows = brlen ; cols = mc . getCols ( ) ; break ; case COLUMN_WISE : rows = mc . getRows ( ) ; cols = 1 ; break ; case COLUMN_BLOCK_WISE : rows = mc . getRows ( ) ; cols = bclen ; break ; default : throw new CacheException ( "Unsupported partition format: " + _partitionFormat ) ; } if ( MapReduceTool . existsFileOnHDFS ( fname ) ) mb = readBlobFromHDFS ( fname , rows , cols ) ; else { mb = new MatrixBlock ( ( int ) rows , ( int ) cols , true ) ; LOG . warn ( "Reading empty matrix partition " + fname ) ; } } if ( blockwise ) { _partitionCacheName = fname ; _cache = new SoftReference < MatrixBlock > ( mb ) ; if ( _partitionFormat == PDataPartitionFormat . ROW_BLOCK_WISE ) { int rix = ( int ) ( ( pred . rowStart - 1 ) % brlen ) ; mb = mb . sliceOperations ( rix , rix , ( int ) ( pred . colStart - 1 ) , ( int ) ( pred . colEnd - 1 ) , new MatrixBlock ( ) ) ; } if ( _partitionFormat == PDataPartitionFormat . COLUMN_BLOCK_WISE ) { int cix = ( int ) ( ( pred . colStart - 1 ) % bclen ) ; mb = mb . sliceOperations ( ( int ) ( pred . rowStart - 1 ) , ( int ) ( pred . rowEnd - 1 ) , cix , cix , new MatrixBlock ( ) ) ; } } } catch ( Exception ex ) { throw new CacheException ( ex ) ; } if ( DMLScript . STATISTICS ) { long t1 = System . nanoTime ( ) ; CacheStatistics . incrementAcquireRTime ( t1 - t0 ) ; } return mb ; }
note : for reading matrix partitions , we could cache ( in its real sense ) the read block with soft references ( no need for eviction , as partitioning only applied for read - only matrices ) . however , since we currently only support row - and column - wise partitioning caching is not applied yet . this could be changed once we also support column - block - wise and row - block - wise . furthermore , as we reject to partition vectors and support only full row or column indexing , no metadata ( apart from the partition flag ) is required .
