Main Content

maxpartitions

클래스: matlab.io.datastore.Partitionable
네임스페이스: matlab.io.datastore

가능한 최대 파티션 개수

구문

n = maxpartitions(ds)

설명

n = maxpartitions(ds)는 데이터저장소 ds에 대한 최대 파티션 개수를 반환합니다.

입력 인수

모두 확장

입력 데이터저장소로, matlab.io.Datastore 객체로 지정됩니다. Datastore 객체를 만들려면 matlab.io.Datastore를 참조하십시오.

특성

Abstracttrue
Accessprotected

메서드의 특성에 대해 자세히 알아보려면 메서드 특성을 참조하십시오.

예제

모두 확장

병렬 처리 지원을 포함한 데이터저장소를 빌드하고 사용하여 사용자 지정 데이터나 사유 데이터를 MATLAB®으로 가져옵니다. 그런 다음 해당 데이터를 병렬 풀에서 처리합니다.

사용자 지정 데이터저장소를 구현하는 코드를 포함하는 .m 클래스 정의 파일을 만듭니다. 이 파일은 작업 폴더나 MATLAB® 경로에 있는 폴더에 저장해야 합니다. .m 파일의 이름은 객체 생성자 함수의 이름과 동일해야 합니다. 예를 들어, 생성자 함수가 MyDatastorePar 이름을 가지려고 하면 .m 파일의 이름이 MyDatastorePar.m이어야 합니다. .m 클래스 정의 파일은 다음 단계를 포함해야 합니다.

  • 1단계: datastore 클래스에서 상속합니다.

  • 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

이제 사용자 지정 데이터저장소를 사용할 준비가 되었습니다. 사용자 지정 데이터저장소를 사용하여 병렬 풀에서 데이터를 읽고 처리할 수 있습니다.

버전 내역

R2017b에 개발됨