Skip to content

Commit f90499e

Browse files
committed
[SYSTEMDS-3465] Typed return on CacheBlock Interface Slice
This task it to change the CacheBlock interface to return typed types on slice. This makes the individual CacheBlock types independent and essentially make FrameBlock not able to slice to MatrixBlock and vice versa. Also changed in this commit is the memory estimates in the FrameBlock that now use our memory estimates in utils/MemoryEstimates.java
1 parent e4491b3 commit f90499e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+379
-264
lines changed

src/main/java/org/apache/sysds/hops/recompile/Recompiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,8 +1610,8 @@ public static void executeInMemoryReblock(ExecutionContext ec, String varin, Str
16101610

16111611
@SuppressWarnings("unchecked")
16121612
public static void executeInMemoryReblock(ExecutionContext ec, String varin, String varout, LineageItem litem) {
1613-
CacheableData<CacheBlock> in = (CacheableData<CacheBlock>) ec.getCacheableData(varin);
1614-
CacheableData<CacheBlock> out = (CacheableData<CacheBlock>) ec.getCacheableData(varout);
1613+
CacheableData<CacheBlock<?>> in = (CacheableData<CacheBlock<?>>) ec.getCacheableData(varin);
1614+
CacheableData<CacheBlock<?>> out = (CacheableData<CacheBlock<?>>) ec.getCacheableData(varout);
16151615

16161616
if( in.isFederated() ) {
16171617
out.setMetaData(in.getMetaData());

src/main/java/org/apache/sysds/runtime/compress/CompressedMatrixBlock.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import org.apache.sysds.runtime.compress.lib.CLALibTSMM;
6060
import org.apache.sysds.runtime.compress.lib.CLALibUnary;
6161
import org.apache.sysds.runtime.compress.lib.CLALibUtils;
62-
import org.apache.sysds.runtime.controlprogram.caching.CacheBlock;
6362
import org.apache.sysds.runtime.controlprogram.caching.MatrixObject.UpdateType;
6463
import org.apache.sysds.runtime.controlprogram.parfor.stat.InfrastructureAnalyzer;
6564
import org.apache.sysds.runtime.data.DenseBlock;
@@ -591,7 +590,7 @@ public void setOverlapping(boolean overlapping) {
591590
}
592591

593592
@Override
594-
public MatrixBlock slice(int rl, int ru, int cl, int cu, boolean deep, CacheBlock ret) {
593+
public MatrixBlock slice(int rl, int ru, int cl, int cu, boolean deep, MatrixBlock ret) {
595594
validateSliceArgument(rl, ru, cl, cu);
596595
return CLALibSlice.slice(this, rl, ru, cl, cu, deep);
597596
}

src/main/java/org/apache/sysds/runtime/controlprogram/caching/ByteBuffer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public class ByteBuffer
4343
private final long _size;
4444

4545
protected byte[] _bdata = null; //sparse matrix
46-
protected CacheBlock _cdata = null; //dense matrix/frame
46+
protected CacheBlock<?> _cdata = null; //dense matrix/frame
4747

4848
public ByteBuffer( long size ) {
4949
_size = size;
5050
_serialized = false;
5151
}
5252

53-
public void serializeBlock( CacheBlock cb )
53+
public void serializeBlock( CacheBlock<?> cb )
5454
throws IOException
5555
{
5656
_shallow = cb.isShallowSerialize(true);
@@ -85,10 +85,10 @@ public void serializeBlock( CacheBlock cb )
8585
_serialized = true;
8686
}
8787

88-
public CacheBlock deserializeBlock()
88+
public CacheBlock<?> deserializeBlock()
8989
throws IOException
9090
{
91-
CacheBlock ret = null;
91+
CacheBlock<?> ret = null;
9292

9393
if( !_shallow ) { //sparse matrix / string frame
9494
DataInput din = _matrix ? new CacheDataInput(_bdata) :
@@ -163,7 +163,7 @@ public void checkSerialized()
163163
* @param cb cache block
164164
* @return true if valid capacity
165165
*/
166-
public static boolean isValidCapacity( long size, CacheBlock cb )
166+
public static boolean isValidCapacity( long size, CacheBlock<?> cb )
167167
{
168168
if( !cb.isShallowSerialize(true) ) { //SPARSE matrix blocks
169169
// since cache blocks are serialized into a byte representation

src/main/java/org/apache/sysds/runtime/controlprogram/caching/CacheBlock.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* allows us to keep the buffer pool independent of matrix and frame blocks.
2929
*
3030
*/
31-
public interface CacheBlock extends Writable
31+
public interface CacheBlock <T> extends Writable
3232
{
3333

3434
public int getNumRows();
@@ -96,7 +96,21 @@ public interface CacheBlock extends Writable
9696
* @param block cache block
9797
* @return sub-block of cache block
9898
*/
99-
public CacheBlock slice(int rl, int ru, int cl, int cu, CacheBlock block);
99+
public CacheBlock<T> slice(int rl, int ru, int cl, int cu);
100+
101+
102+
/**
103+
* Slice a sub block out of the current block and write into the given output block.
104+
* This method returns the passed instance if not null.
105+
*
106+
* @param rl row lower
107+
* @param ru row upper
108+
* @param cl column lower
109+
* @param cu column upper
110+
* @param block cache block
111+
* @return sub-block of cache block
112+
*/
113+
public CacheBlock<T> slice(int rl, int ru, int cl, int cu, T block);
100114

101115
/**
102116
* Slice a sub block out of the current block and write into the given output block.
@@ -110,7 +124,7 @@ public interface CacheBlock extends Writable
110124
* @param block cache block
111125
* @return sub-block of cache block
112126
*/
113-
public CacheBlock slice(int rl, int ru, int cl, int cu, boolean deep, CacheBlock block);
127+
public CacheBlock<T> slice(int rl, int ru, int cl, int cu, boolean deep, T block);
114128

115129

116130
/**
@@ -120,7 +134,7 @@ public interface CacheBlock extends Writable
120134
* @param that cache block
121135
* @param appendOnly ?
122136
*/
123-
public void merge(CacheBlock that, boolean appendOnly);
137+
public void merge(T that, boolean appendOnly);
124138

125139
/**
126140
* Returns the double value at the passed row and column.
@@ -129,7 +143,7 @@ public interface CacheBlock extends Writable
129143
* @param c column of the value
130144
* @return double value at the passed row and column
131145
*/
132-
double getDouble(int r, int c);
146+
public double getDouble(int r, int c);
133147

134148
/**
135149
* Returns the double value at the passed row and column.
@@ -138,7 +152,7 @@ public interface CacheBlock extends Writable
138152
* @param c column of the value
139153
* @return double value at the passed row and column
140154
*/
141-
double getDoubleNaN(int r, int c);
155+
public double getDoubleNaN(int r, int c);
142156

143157
/**
144158
* Returns the string of the value at the passed row and column.
@@ -147,5 +161,5 @@ public interface CacheBlock extends Writable
147161
* @param c column of the value
148162
* @return string of the value at the passed row and column
149163
*/
150-
String getString(int r, int c);
164+
public String getString(int r, int c);
151165
}

src/main/java/org/apache/sysds/runtime/controlprogram/caching/CacheBlockFactory.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*/
3636
public class CacheBlockFactory
3737
{
38-
public static CacheBlock newInstance(int code) {
38+
public static CacheBlock<?> newInstance(int code) {
3939
switch( code ) {
4040
case 0: return new MatrixBlock();
4141
case 1: return new FrameBlock();
@@ -44,7 +44,17 @@ public static CacheBlock newInstance(int code) {
4444
throw new RuntimeException("Unsupported cache block type: "+code);
4545
}
4646

47-
public static int getCode(CacheBlock block) {
47+
public static CacheBlock<?> newInstance(CacheBlock<?> block) {
48+
if(block instanceof MatrixBlock)
49+
return new MatrixBlock();
50+
else if(block instanceof FrameBlock)
51+
return new FrameBlock();
52+
else if(block instanceof TensorBlock)
53+
return new TensorBlock();
54+
throw new RuntimeException("Unsupported cache block type: " + block.getClass().getName());
55+
}
56+
57+
public static int getCode(CacheBlock<?> block) {
4858
if (block instanceof MatrixBlock)
4959
return 0;
5060
else if (block instanceof FrameBlock)
@@ -54,7 +64,7 @@ else if (block instanceof TensorBlock)
5464
throw new RuntimeException("Unsupported cache block type: " + block.getClass().getName());
5565
}
5666

57-
public static ArrayList<?> getPairList(CacheBlock block) {
67+
public static ArrayList<?> getPairList(CacheBlock<?> block) {
5868
int code = getCode(block);
5969
switch (code) {
6070
case 0: return new ArrayList<Pair<MatrixIndexes, MatrixBlock>>();

src/main/java/org/apache/sysds/runtime/controlprogram/caching/CacheMaintenanceService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void deleteFile(String fname) {
4444
LocalFileUtils.deleteFileIfExists(fname, true);
4545
}
4646

47-
public void serializeData(ByteBuffer bbuff, CacheBlock cb) {
47+
public void serializeData(ByteBuffer bbuff, CacheBlock<?> cb) {
4848
//sync or async file delete
4949
if( CacheableData.CACHING_ASYNC_SERIALIZE )
5050
_pool.submit(new CacheMaintenanceService.DataSerializerTask(bbuff, cb));
@@ -85,9 +85,9 @@ public void run() {
8585

8686
private static class DataSerializerTask implements Runnable {
8787
private ByteBuffer _bbuff = null;
88-
private CacheBlock _cb = null;
88+
private CacheBlock<?> _cb = null;
8989

90-
public DataSerializerTask(ByteBuffer bbuff, CacheBlock cb) {
90+
public DataSerializerTask(ByteBuffer bbuff, CacheBlock<?> cb) {
9191
_bbuff = bbuff;
9292
_cb = cb;
9393
}

src/main/java/org/apache/sysds/runtime/controlprogram/caching/CacheableData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
* to allow Java garbage collection. If other parts of the system continue
7474
* keep references to the cache block, its eviction will not release any memory.
7575
*/
76-
public abstract class CacheableData<T extends CacheBlock> extends Data
76+
public abstract class CacheableData<T extends CacheBlock<?>> extends Data
7777
{
7878
private static final long serialVersionUID = -413810592207212835L;
7979

@@ -1047,7 +1047,7 @@ protected boolean isBelowCachingThreshold() {
10471047
return (_data.getInMemorySize() <= CACHING_THRESHOLD);
10481048
}
10491049

1050-
public static boolean isBelowCachingThreshold(CacheBlock data) {
1050+
public static boolean isBelowCachingThreshold(CacheBlock<?> data) {
10511051
boolean ret;
10521052
if (OptimizerUtils.isUMMEnabled())
10531053
ret = UnifiedMemoryManager.getCacheBlockSize(data) <= CACHING_THRESHOLD;

src/main/java/org/apache/sysds/runtime/controlprogram/caching/LazyWriteBuffer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public enum RPolicy {
4747
//maintenance service for synchronous or asynchronous delete of evicted files
4848
private static CacheMaintenanceService _fClean;
4949

50-
public static int writeBlock(String fname, CacheBlock cb)
50+
public static int writeBlock(String fname, CacheBlock<?> cb)
5151
throws IOException
5252
{
5353
//obtain basic meta data of cache block
@@ -131,10 +131,10 @@ public static void deleteBlock(String fname)
131131
_fClean.deleteFile(fname);
132132
}
133133

134-
public static CacheBlock readBlock(String fname, boolean matrix)
134+
public static CacheBlock<?> readBlock(String fname, boolean matrix)
135135
throws IOException
136136
{
137-
CacheBlock cb = null;
137+
CacheBlock<?> cb = null;
138138
ByteBuffer ldata = null;
139139

140140
//probe write buffer
@@ -211,7 +211,7 @@ public static int getQueueSize() {
211211
return _mQueue.size();
212212
}
213213

214-
public static long getCacheBlockSize(CacheBlock cb) {
214+
public static long getCacheBlockSize(CacheBlock<?> cb) {
215215
return cb.isShallowSerialize() ?
216216
cb.getInMemorySize() : cb.getExactSerializedSize();
217217
}

src/main/java/org/apache/sysds/runtime/controlprogram/caching/UnifiedMemoryManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,10 @@ public static long getUMMFree() {
253253
}
254254

255255
// Reads a cached object. This is called from cacheabledata implementations
256-
public static CacheBlock readBlock(String fname, boolean matrix)
256+
public static CacheBlock<?> readBlock(String fname, boolean matrix)
257257
throws IOException
258258
{
259-
CacheBlock cb = null;
259+
CacheBlock<?> cb = null;
260260
ByteBuffer ldata = null;
261261

262262
//probe write buffer
@@ -336,7 +336,7 @@ public static int makeSpace(long reqSpace) {
336336
}
337337

338338
// Write an object to the cache
339-
public static int writeBlock(String fname, CacheBlock cb)
339+
public static int writeBlock(String fname, CacheBlock<?> cb)
340340
throws IOException
341341
{
342342
//obtain basic metadata of the cache block
@@ -380,7 +380,7 @@ public static int writeBlock(String fname, CacheBlock cb)
380380
return numEvicted;
381381
}
382382

383-
public static long getCacheBlockSize(CacheBlock cb) {
383+
public static long getCacheBlockSize(CacheBlock<?> cb) {
384384
return cb.isShallowSerialize() ?
385385
cb.getInMemorySize() : cb.getExactSerializedSize();
386386
}

src/main/java/org/apache/sysds/runtime/controlprogram/context/ExecutionContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ public void setFrameOutput(String varName, FrameBlock outputData) {
628628
setVariable(varName, fo);
629629
}
630630

631-
public static CacheableData<?> createCacheableData(CacheBlock cb) {
631+
public static CacheableData<?> createCacheableData(CacheBlock<?> cb) {
632632
if( cb instanceof MatrixBlock )
633633
return createMatrixObject((MatrixBlock) cb);
634634
else if( cb instanceof FrameBlock )

0 commit comments

Comments
 (0)