Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VolatileViews with Invalidate #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package bdv.util.volatiles;

import java.util.function.Predicate;

import net.imglib2.AbstractWrappedInterval;
import net.imglib2.Interval;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.Volatile;
import net.imglib2.cache.Invalidate;

public class VolatileRandomAccessibleIntervalView< T, V extends Volatile< T > >
extends AbstractWrappedInterval< RandomAccessibleInterval< V > >
implements VolatileView< T, V >, RandomAccessibleInterval< V >
extends AbstractWrappedInterval< RandomAccessibleInterval< V > >
implements VolatileView< T, V >, RandomAccessibleInterval< V >, Invalidate< Long >
{
private final VolatileViewData< T, V > viewData;

Expand Down Expand Up @@ -36,4 +39,22 @@ public RandomAccess< V > randomAccess( final Interval interval )
{
return sourceInterval.randomAccess( interval );
}

@Override
public void invalidate( Long key )
{
this.viewData.invalidate( key );
}

@Override
public void invalidateIf( long parallelismThreshold, Predicate< Long > condition )
{
this.viewData.invalidateIf( parallelismThreshold, condition );
}

@Override
public void invalidateAll( long parallelismThreshold )
{
this.viewData.invalidateAll( parallelismThreshold );
}
}
34 changes: 32 additions & 2 deletions src/main/java/bdv/util/volatiles/VolatileViewData.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package bdv.util.volatiles;

import java.util.function.Predicate;

import bdv.cache.CacheControl;
import net.imglib2.RandomAccessible;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.Volatile;
import net.imglib2.cache.Invalidate;
import net.imglib2.cache.img.CachedCellImg;

/**
Expand All @@ -23,7 +26,7 @@
*
* @author Tobias Pietzsch
*/
public class VolatileViewData< T, V extends Volatile< T > >
public class VolatileViewData< T, V extends Volatile< T > > implements Invalidate< Long >
{
private final RandomAccessible< V > img;

Expand All @@ -33,16 +36,20 @@ public class VolatileViewData< T, V extends Volatile< T > >

private final V volatileType;

private final Invalidate< Long > invalidate;

public VolatileViewData(
final RandomAccessible< V > img,
final CacheControl cacheControl,
final T type,
final V volatileType )
final V volatileType,
final Invalidate< Long > invalidate )
{
this.img = img;
this.cacheControl = cacheControl;
this.type = type;
this.volatileType = volatileType;
this.invalidate = invalidate;
}

/**
Expand Down Expand Up @@ -86,4 +93,27 @@ public V getVolatileType()
{
return volatileType;
}

public Invalidate< Long > getInvalidate()
{
return this.invalidate;
}

@Override
public void invalidate( Long key )
{
this.invalidate.invalidate( key );
}

@Override
public void invalidateIf( long parallelismThreshold, Predicate< Long > condition )
{
this.invalidate.invalidateIf( parallelismThreshold, condition );
}

@Override
public void invalidateAll( long parallelismThreshold )
{
this.invalidate.invalidateAll( parallelismThreshold );
}
}
21 changes: 11 additions & 10 deletions src/main/java/bdv/util/volatiles/VolatileViews.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ else if ( rai instanceof IntervalView )
new IntervalView<>( sourceData.getImg(), view ),
sourceData.getCacheControl(),
sourceData.getType(),
sourceData.getVolatileType() );
sourceData.getVolatileType(),
sourceData.getInvalidate() );
}
else if ( rai instanceof MixedTransformView )
{
Expand All @@ -120,7 +121,8 @@ else if ( rai instanceof MixedTransformView )
new MixedTransformView<>( sourceData.getImg(), view.getTransformToSource() ),
sourceData.getCacheControl(),
sourceData.getType(),
sourceData.getVolatileType() );
sourceData.getVolatileType(),
sourceData.getInvalidate() );
}
else if ( rai instanceof WrappedImg )
{
Expand Down Expand Up @@ -151,23 +153,22 @@ private static < T extends NativeType< T >, V extends Volatile< T > & NativeType
if ( hints == null )
hints = new CacheHints( LoadingStrategy.VOLATILE, 0, false );
@SuppressWarnings( "rawtypes" )
final VolatileCachedCellImg< V, ? > img = createVolatileCachedCellImg( grid, vtype, dirty, ( Cache ) cache, queue, hints );
final VolatileCache< Long, Cell< ? extends VolatileArrayDataAccess< ? > > > volatileCache = createVolatileCache( grid, vtype, dirty, ( Cache ) cache, queue );

return new VolatileViewData<>( img, queue, type, vtype );
final VolatileCachedCellImg< V, ? extends VolatileArrayDataAccess< ? > > volatileImg = new VolatileCachedCellImg<>( grid, vtype, hints, volatileCache.unchecked()::get );

return new VolatileViewData<>( volatileImg, queue, type, vtype, volatileCache );
}

private static < T extends NativeType< T >, A extends VolatileArrayDataAccess< A > > VolatileCachedCellImg< T, A > createVolatileCachedCellImg(
private static < T extends NativeType< T >, A extends VolatileArrayDataAccess< A > > VolatileCache< Long, Cell< A > > createVolatileCache(
final CellGrid grid,
final T type,
final boolean dirty,
final Cache< Long, Cell< A > > cache,
final SharedQueue queue,
final CacheHints hints )
final SharedQueue queue )
{
final CreateInvalid< Long, Cell< A > > createInvalid = CreateInvalidVolatileCell.get( grid, type, dirty );
final VolatileCache< Long, Cell< A > > volatileCache = new WeakRefVolatileCache<>( cache, queue, createInvalid );
final VolatileCachedCellImg< T, A > volatileImg = new VolatileCachedCellImg<>( grid, type, hints, volatileCache.unchecked()::get );
return volatileImg;
return new WeakRefVolatileCache<>( cache, queue, createInvalid );
}
}