partition
클래스: matlab.io.datastore.Partitionable
네임스페이스: matlab.io.datastore
데이터저장소 파티셔닝
설명
입력 인수
입력 데이터저장소로, 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
예제
병렬 처리 지원을 포함한 데이터저장소를 빌드하고 사용하여 사용자 지정 데이터나 사유 데이터를 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
이제 사용자 지정 데이터저장소를 사용할 준비가 되었습니다. 사용자 지정 데이터저장소를 사용하여 병렬 풀에서 데이터를 읽고 처리할 수 있습니다.
세부 정보
빈 데이터저장소는 레코드가 없는 datastore 객체입니다. 빈 데이터저장소의 사용자 지정 데이터저장소 메서드는 다음 조건을 충족해야 합니다.
hasdata는false를 반환해야 합니다.read는 오류를 반환해야 합니다.numpartitions와maxpartitions는0을 반환해야 합니다.partition은 빈 데이터저장소를 반환해야 합니다.preview와readall은 tall형이 아닌 차원이 그대로 유지된 빈 데이터를 반환해야 합니다. 예를 들어, 비어 있지 않은 데이터저장소의read메서드가 크기가5×15×25인 데이터를 반환한다면preview메서드와readall메서드는 크기가0×15×25인 빈 데이터를 반환해야 합니다.
배열의 첫 번째 차원이 아닌 차원을 의미합니다. 크기가 5×15×25인 배열의 경우, 5는 tall형 차원이고 15와 25는 tall형이 아닌 차원입니다.
팁
partition메서드를 구현할 때는 다음 단계를 거쳐야 합니다.분할된 데이터저장소
subds를 생성하기 전에 원본 데이터저장소ds의 깊은 복사본을 생성합니다.partition메서드의 끝에서, 분할된 데이터저장소subds를 재설정합니다.
partition메서드 구현의 실례를 보려면 Add Support for Parallel Processing 항목을 참조하십시오.데이터저장소의 파티션에 읽기 가능한 레코드가 없는 경우
read메서드는 빈 데이터를 반환해야 합니다. 이 빈 데이터의 tall형이 아닌 차원은 읽기 가능한 레코드를 갖는 파티션에 대한read메서드 출력값의 tall형이 아닌 차원과 일치해야 합니다. 이 요구 사항은readall메서드의 동작이gather함수의 동작과 일치하도록 해 줍니다.
버전 내역
R2017b에 개발됨
참고 항목
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)