partition
类: matlab.io.datastore.Partitionable
包: matlab.io.datastore
划分数据存储
说明
输入参数
ds
— 输入数据存储
matlab.io.Datastore
对象
输入数据存储,指定为 matlab.io.Datastore
对象。要创建 Datastore
对象,请参阅 matlab.io.Datastore
。
n
— 分区数
正整数
分区数量,指定为正整数。要获得合理的 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
index
— 索引
正整数
索引,指定为正整数。
示例: 1
数据类型: double
示例
构建支持并行处理的数据存储
构建支持并行处理的数据存储,并使用它将您的自定义或专有数据导入 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
您的自定义数据存储现在已准备就绪。使用您的自定义数据存储来读取并处理并行池中的数据。
详细信息
空数据存储
空数据存储是不包含任何记录的数据存储对象。对于空数据存储,您的自定义数据存储方法必须满足以下条件:
hasdata
必须返回false
。read
必须返回错误。numpartitions
和maxpartitions
必须返回0
。partition
必须返回空数据存储。preview
和readall
必须返回保留非 tall 维度的空数据。例如,如果非空数据存储的read
方法返回大小为5
×15
×25
的数据,则preview
和readall
方法必须返回大小为0
×15
×25
的空数据。
非 tall 维度
数组中除第一个维度之外的维度。对于大小为 5
×15
×25
的数组,tall 维度为 5
,非 tall 维度为 15
和 25
。
提示
在您的
partition
方法实现中,必须包括以下步骤。在创建分区的数据存储
subds
之前,为原始数据存储ds
创建一个深拷贝。在
partition
方法的末尾,重置分区的数据存储subds
。
有关
partition
方法的示例实现,请参阅Add Support for Parallel Processing。当数据存储的分区不包含可读记录时,
read
方法必须返回空数据。这个空数据的非 tall 维度必须与read
方法在具有可读记录的分区中的输出结果的非 tall 维度一致。此要求是为了确保readall
方法的行为与gather
函数的行为一致。
版本历史记录
在 R2017b 中推出
另请参阅
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)