Main Content

partition

类: matlab.io.datastore.Partitionable
命名空间: matlab.io.datastore

划分数据存储

说明

示例

subds = partition(ds,n,index) 将数据存储 ds 划分为由 n 指定的数量的部分,并返回与索引 index 对应的分区。分区的数据存储 subds 的类型与输入数据存储 ds 的类型相同。

输入参数

全部展开

输入数据存储,指定为 matlab.io.Datastore 对象。要创建 Datastore 对象,请参阅 matlab.io.Datastore

分区数量,指定为正整数。要获得合理的 n 值,请使用 numpartitions 函数。

如果您指定的 n 值不在数据存储的可用分区范围内,partition 方法将返回一个空数据存储。有关详细信息,请参阅空数据存储。例如,如果数据存储可以容纳 10 个分区,则 partition 方法的输出取决于 n 的值。

  • 如果为 n 指定的值小于或等于 10,则 partition 方法返回由 index 指定的分区。例如,partition(ds,10,1) 返回原始数据存储 ds 的第一个分区的副本。

  • 如果为 n 指定的值大于 10,则 partition 方法返回一个空数据存储。例如,partition(ds,100,11) 返回一个空数据存储。

示例: 3

数据类型: double

索引,指定为正整数。

示例: 1

数据类型: double

属性

Abstracttrue

要了解方法的属性,请参阅方法属性

示例

全部展开

构建支持并行处理的数据存储,并使用它将您的自定义或专有数据导入 MATLAB®。然后,在并行池中处理数据。

创建一个 .m 类定义文件,其中包含实现自定义数据存储的代码。您必须将此文件保存在工作文件夹或 MATLAB® 路径上的文件夹中。.m 文件的名称必须与对象构造函数的名称相同。例如,如果您希望构造函数的名称为 MyDatastorePar,则 .m 文件的名称必须为 MyDatastorePar.m.m 类定义文件必须包含以下步骤:

  • 步骤 1:从数据存储类继承。

  • 步骤 2:定义构造函数和必需的方法。

  • 步骤 3:定义您的自定义文件读取函数。

除这些步骤之外,还需要定义处理和分析数据所需的所有其他属性或方法。

%% STEP 1: INHERIT FROM DATASTORE CLASSES
classdef MyDatastorePar < matlab.io.Datastore & ...
        matlab.io.datastore.Partitionable
   
    properties(Access = private)
        CurrentFileIndex double
        FileSet matlab.io.datastore.DsFileSet
    end
    
    % Property to support saving, loading, and processing of
    % datastore on different file system machines or clusters.
    % In addition, define the methods get.AlternateFileSystemRoots()
    % and set.AlternateFileSystemRoots() in the methods section. 
    properties(Dependent)
        AlternateFileSystemRoots
    end
    
%% STEP 2: DEFINE THE CONSTRUCTOR AND THE REQUIRED METHODS
    methods
        % Define your datastore constructor
        function myds = MyDatastorePar(location,altRoots)
            myds.FileSet = matlab.io.datastore.DsFileSet(location,...
                'FileExtensions','.bin', ...
                'FileSplitSize',8*1024);
            myds.CurrentFileIndex = 1;
             
            if nargin == 2
                 myds.AlternateFileSystemRoots = altRoots;
            end
            
            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)
                msgII = ['Use the reset method to reset the datastore ',... 
                         'to the start of the data.']; 
                msgIII = ['Before calling the read method, ',...
                          'check if data is available to read ',...
                          'by using the hasdata method.'];
                error('No more data to read.\n%s\n%s',msgII,msgIII);
            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 partition method
        function subds = partition(myds,n,ii)
            subds = copy(myds);
            subds.FileSet = partition(myds.FileSet,n,ii);
            reset(subds);
        end
        
        % Getter for AlternateFileSystemRoots property
        function altRoots = get.AlternateFileSystemRoots(myds)
            altRoots = myds.FileSet.AlternateFileSystemRoots;
        end

        % Setter for AlternateFileSystemRoots property
        function set.AlternateFileSystemRoots(myds,altRoots)
            try
              % The DsFileSet object manages AlternateFileSystemRoots
              % for your datastore
              myds.FileSet.AlternateFileSystemRoots = altRoots;

              % Reset the datastore
              reset(myds);  
            catch ME
              throw(ME);
            end
        end
      
    end
    
    methods (Hidden = true)          
        % Define the progress method
        function frac = progress(myds)
            % Determine percentage of data read from datastore
            if hasdata(myds) 
               frac = (myds.CurrentFileIndex-1)/...
                             myds.FileSet.NumFiles; 
            else 
               frac = 1;  
            end 
        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

您的自定义数据存储现在已准备就绪。使用您的自定义数据存储来读取并处理并行池中的数据。

详细信息

全部展开

提示

  • 在您的 partition 方法实现中,必须包括以下步骤。

    • 在创建分区的数据存储 subds 之前,为原始数据存储 ds 创建一个深拷贝。

    • partition 方法的末尾,重置分区的数据存储 subds

    有关 partition 方法的示例实现,请参阅Add Support for Parallel Processing

  • 当数据存储的分区不包含可读记录时,read 方法必须返回空数据。这个空数据的非 tall 维度必须与 read 方法在具有可读记录的分区中的输出结果的非 tall 维度一致。此要求是为了确保 readall 方法的行为与 gather 函数的行为一致。

版本历史记录

在 R2017b 中推出