- All Superinterfaces:
NSCoding, NSSecureCoding
public interface MPSImageAllocator
extends NSSecureCoding
A class that allocates new MPSImage or MPSTemporaryImage
Sometimes it is prohibitively costly for MPS to figure out how
big an image should be in advance. In addition, you may want to
have some say over whether the image is a temporary image or not.
In such circumstances, the MPSImageAllocator is used to
provide the developer with an opportunity for just in time feedback
about how the image should be allocated.
Two standard MPSImageAllocators are provided: MPSImageDefaultAllocator
and MPSTemporaryImageDefaultAllocator. You may of course provide
your own allocator instead.
Example:
[@code]
// Note: MPSImageDefaultAllocator is already provided
// by the framework under that name. It is provided here
// as sample code for writing your own variant.
-(MPSImage * __nonnull) imageForCommandBuffer: (__nonnull id ) cmdBuf
imageDescriptor: (MPSImageDescriptor * __nonnull) descriptor
kernel: (MPSKernel * __nonnull) kernel
{
MPSImage * result = [[MPSImage alloc] initWithDevice: cmdBuf.device
imageDescriptor: descriptor ];
// make sure the object sticks around at least as lomg as the command buffer
[result retain];
[cmdBuf addCompletedHandler: ^(id c){[result release];}];
// return autoreleased result
return [result autorelease];
};
-(BOOL) supportsSecureCoding{ return YES; }
-(void)encodeWithCoder:(NSCoder * __nonnull)aCoder
{
[super encodeWithCoder: aCoder];
// encode any data owned by the class at this level
}
-(nullable instancetype) initWithCoder: (NSCoder*__nonnull) aDecoder
{
self = [super initWithCoder: aDecoder ];
if( nil == self )
return self;
// use coder to load any extra data kept by this object here
return self;
}
[@endcode]
Please see [MPSImage defaultAllocator] and [MPSTemporaryImage defaultAllocator]
for implentations of the protocol already provided by MPS.
When considering whether to write your own MPSImageAllocator, you should know
the existing MPSImage and MPSTemporaryImage default allocators are optimized
to make image batch allocation much faster than one MPSImage at a time in a loop.
When possible, it can be better to use the MPS provided allocators and override
the behavior in a padding policy instead, if the changes can be contained in
the MPSImageDescriptor. This will help reduce CPU encode time. However, custom
padding policies can inhibit optimizations in the MPSNNGraph, particularly node
fusion, resulting in more work for the GPU. In cases where the custom padding method
does not change filter properties but only adjusts the result image (e.g. adjust result
feature channel format) then MPSNNPaddingMethodCustomWhitelistForNodeFusion may be
used to signal that node fusion is acceptable.