Main Content

numpartitions

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

默认分区数

语法

n = numpartitions(ds)
n = numpartitions(ds,pool)

说明

n = numpartitions(ds) 返回数据存储 ds 的默认分区数。

n = numpartitions(ds,pool) 返回一个合理的分区数,以便对 pool 指定的并行池进行并行数据存储访问。要对数据存储访问进行并行处理,必须安装 Parallel Computing Toolbox™。

输入参数

全部展开

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

并行池,指定为并行池对象。

示例: gcp

属性

Sealedtrue
Abstractfalse

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

示例

全部展开

构建支持并行处理的数据存储,并使用它将您的自定义或专有数据导入 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

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

提示

  • 对于自定义数据存储实现,我们建议您不要实现 numpartitions 方法。

算法

在默认实现中,选择的分区数 n 是下面两个值中较小的那个值:

  • 对数据存储对象调用 maxpartitions 方法所返回的值。

  • 可用工作进程数的三倍。

版本历史记录

在 R2017b 中推出