Skip to content

Commit b8122cc

Browse files
committed
calculate number of workers based on file size
1 parent e2fbf40 commit b8122cc

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

startParPool.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function startParPool(mem_per_worker)
2+
3+
if nargin < 1
4+
% Estimate memory usage per worker (in bytes, e.g., 1 GB)
5+
mem_per_worker = 1 * 1024^3;
6+
end
7+
8+
% Get system memory info
9+
if ispc % For Windows
10+
[~, system_info] = memory;
11+
totalPhysicalMemory = system_info.PhysicalTotal;
12+
else % For UNIX/Mac, example using system command
13+
[~, cmdout] = system('sysctl hw.memsize');
14+
totalPhysicalMemory = str2double(regexp(cmdout, '\d+', 'match'));
15+
end
16+
17+
% Calculate max number of workers
18+
max_workers = floor(totalPhysicalMemory *.8 / mem_per_worker);
19+
20+
% Get current pool information
21+
pool = gcp('nocreate');
22+
23+
% Adjust the pool size if necessary
24+
fprintf('start pool with %d workers', max_workers);
25+
if isempty(pool)
26+
parpool(max_workers);
27+
elseif pool.NumWorkers ~= max_workers
28+
delete(pool);
29+
parpool(max_workers);
30+
end
31+
end

unpackData.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function unpackData(inFileNames, outFileNames, outFilePath, verbose, skipExist)
3434
suffix_int = cellfun(@(x) int8(str2double(x)), suffix);
3535
[~, computeTS] = findFirstOccurrence(suffix_int);
3636

37+
fileInfo = dir(inFileNames{1});
38+
fileSize = fileInfo.bytes / 1024^3;
39+
startParPool(fileSize * 10)
40+
3741
% unpack ncs files:
3842
parfor i = 1:length(inFileNames)
3943
inFileName = inFileNames{i};

0 commit comments

Comments
 (0)