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단계: 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
인 빈 데이터를 반환해야 합니다.
tall형이 아닌 차원
배열의 첫 번째 차원이 아닌 차원을 의미합니다. 크기가 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 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 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)