Main Content

partition

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

데이터저장소 파티셔닝

설명

예제

subds = partition(ds,n,index)는 데이터저장소 dsn으로 지정된 파트 개수로 파티셔닝하고 인덱스 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단계: 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

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

세부 정보

모두 확장

  • partition 메서드를 구현할 때는 다음 단계를 거쳐야 합니다.

    • 분할된 데이터저장소 subds를 생성하기 전에 원본 데이터저장소 ds의 깊은 복사본을 생성합니다.

    • partition 메서드의 끝에서, 분할된 데이터저장소 subds를 재설정합니다.

    partition 메서드 구현의 실례를 보려면 Add Support for Parallel Processing 항목을 참조하십시오.

  • 데이터저장소의 파티션에 읽기 가능한 레코드가 없는 경우 read 메서드는 빈 데이터를 반환해야 합니다. 이 빈 데이터의 tall형이 아닌 차원은 읽기 가능한 레코드를 갖는 파티션에 대한 read 메서드 출력값의 tall형이 아닌 차원과 일치해야 합니다. 이 요구 사항은 readall 메서드의 동작이 gather 함수의 동작과 일치하도록 해 줍니다.

버전 내역

R2017b에 개발됨