Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

matlab.io.datastore.Partitionable class

Package: matlab.io.datastore

Add parallelization support to datastore

Description

matlab.io.datastore.Partitionable is an abstract mixin class that adds parallelization support to your custom datastore for use with Parallel Computing Toolbox™ and MATLAB® Distributed Computing Server™.

To use this mixin class, you must subclass from matlab.io.datastore.Partitionable class in addition to subclassing from the matlab.io.Datastore base class. Type the following syntax as the first line of your class definition file:

classdef MyDatastore < matlab.io.Datastore & ...
                                           matlab.io.datastore.Partitionable
    ...
end

To add support for parallel processing to your custom datastore, you must:

For more details and steps to create your custom datastore with parallel processing support, see Develop Custom Datastore.

Methods

Attributes

Sealedfalse

To learn about attributes of classes, see Class Attributes.

Examples

expand all

Build a datastore with parallel processing support, use it to bring your custom or proprietary data into MATLAB®, and process the data in a parallel pool.

This example uses a simple data set to illustrate a workflow that you can use to build a custom datastore for your own data. The data set is a collection of 15 binary (.bin) files where each file contains a column (1 variable) and 10000 rows (records) of unsigned integers.

dir('*.bin')
binary_data01.bin  binary_data05.bin  binary_data09.bin  binary_data13.bin  
binary_data02.bin  binary_data06.bin  binary_data10.bin  binary_data14.bin  
binary_data03.bin  binary_data07.bin  binary_data11.bin  binary_data15.bin  
binary_data04.bin  binary_data08.bin  binary_data12.bin  

Implement your custom datastore in your working folder or in a folder that is on the MATLAB® path. Then create a new script, MyDatastorePar.m that contains the code implementing your custom datastore. The name of the script file must be the same as the name of your object constructor function. For example, if you want your constructor function to have the name MyDatastorePar, then the name of the script file must be MyDatastorePar.m. The script must contain the following steps:

  • Step 1: Inherit from the datastore classes.

  • Step 2: Define the constructor and the required methods.

  • Step 3: Define your custom file reading function.

%% STEP 1: INHERIT FROM DATASTORE CLASSES
classdef MyDatastorePar < matlab.io.Datastore & ...
        matlab.io.datastore.Partitionable
   
    % properties(Access = private)
    properties
        CurrentFileIndex double
        FileSet matlab.io.datastore.DsFileSet
    end
    
    
%% STEP 2: DEFINE THE CONSTRUCTOR AND THE REQUIRED METHODS
    methods
        % Define your datastore constructor
        function myds = MyDatastorePar(location)
            myds.FileSet = matlab.io.datastore.DsFileSet(location,...
                'FileExtensions','.bin', ...
                'FileSplitSize',8*1024);
            myds.CurrentFileIndex = 1;
            reset(myds);
        end
        
        % Define the hasdata method
        function tf = hasdata(myds)
            % Return true if more data is available
            tf = hasfile(myds.FileSet);
        end
        
        % Define the read method
        function [data,info] = read(myds)
            % Read data and information about the extracted data
            % See also: MyFileReader()
            if ~hasdata(myds)
                error('No more data');
            end
            
            fileInfoTbl = nextfile(myds.FileSet);
            data = MyFileReader(fileInfoTbl);
            info.Size = size(data);
            info.FileName = fileInfoTbl.FileName;
            info.Offset = fileInfoTbl.Offset;
            
            % Update CurrentFileIndex for tracking progress
            if fileInfoTbl.Offset + fileInfoTbl.SplitSize >= ...
                    fileInfoTbl.FileSize
                myds.CurrentFileIndex = myds.CurrentFileIndex + 1 ;
            end
        end
        
        % Define the reset method
        function reset(myds)
            % Reset to the start of the data
            reset(myds.FileSet);
            myds.CurrentFileIndex = 1;
        end
        
        % Define the progress method
        function frac = progress(myds)
            % Determine percentage of data that you have read
            % from a datastore
            frac = (myds.CurrentFileIndex-1)/myds.FileSet.NumFiles;
        end
        
        % Define the partition method
        function subds = partition(myds,n,ii)
            subds = copy(myds);
            subds.FileSet = partition(myds.FileSet,n,ii);
            reset(subds);
        end
    end
    
    methods(Access = protected)
        % If you use the  FileSet property in the datastore,
        % then you must define the COPYELEMENT method. The
        % copyelement method allows methods such as readall
        % and preview to remain stateless 
        function dscopy = copyElement(ds)
            dscopy = copyElement@matlab.mixin.Copyable(ds);
            dscopy.FileSet = copy(ds.FileSet);
        end
        
        % Define the maxpartitions method
        function n = maxpartitions(myds)
            n = maxpartitions(myds.FileSet);
        end
    end
end

%% STEP 3: IMPLEMENT YOUR CUSTOM FILE READING FUNCTION
function data = MyFileReader(fileInfoTbl)
% create a reader object using FileName
reader = matlab.io.datastore.DsFileReader(fileInfoTbl.FileName);

% seek to the offset
seek(reader,fileInfoTbl.Offset,'Origin','start-of-file');

% read fileInfoTbl.SplitSize amount of data
data = read(reader,fileInfoTbl.SplitSize);

end

Use your custom datastore to read data from folder and return the datastore object.

folder = fullfile('*.bin');
ds = MyDatastorePar(folder) ;

Preview the data from the datastore.

preview(ds)
ans =

  8x1 uint8 column vector

   113
   180
   251
    91
    29
    66
   254
   214

Identify the number of partitions for your datastore.

n = numpartitions(ds);

Partition the datastore into n parts and n workers in a parallel pool.

parfor ii = 1:n
    subds = partition(ds,n,ii);
      while hasdata(subds)
        data = read(subds);
        % do something
      end
end

Introduced in R2017b

Was this topic helpful?