-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimOfflow.m
More file actions
125 lines (88 loc) · 3.16 KB
/
Copy pathsimOfflow.m
File metadata and controls
125 lines (88 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
clc
clear
close all
set(0,"defaultfigurecolor","w")
% Initialize offarmLab MATLAB paths
if ~exist('core/offarmLab', 'dir')
error('core/offarmLab directory not found');
elseif ~exist('initOffarmLab', 'file')
error('initOffarmLab function not found');
else
addpath("core/offarmLab");
initOffarmLab(nargout=0);
end
%% Run single case
addpath("usr0")
file_inp = 'usr0/INPUT_manifest.json';
resultsPre = struct();
resultsPre.nCase = 2;
resultsPre.batchCasesID = 1;
resultsPre.caseMatrix = [5, 11, 0.5, 0, 0];
resultsPre.infoList = "Hs=5;Tp=11;Uc1=0.5;waveDir=0;currentDir=0";
resultsPre.savingDir = 'results';
diary_filename = [ 'diary_' '.txt'];
diary(diary_filename);
simOffarm(file_inp, resultsPre);
diary off
%% Set up multiple cases
resultsPre = struct();
resultsPre.nCase = 2;
resultsPre.batchCasesID = 2;
resultsPre.caseMatrix = [5, 10, 0.5, 0, 0;
5, 10, 0.5, 0, 90];
resultsPre.infoList = {"Hs=5;Tp=11;Uc1=0.5;waveDir=0;currentDir=0";...
"Hs=5;Tp=11;Uc1=0.5;waveDir=0;currentDir=90"};
resultsPre.savingDir = 'results';
diary_filename = [ 'diary_' '.txt'];
diary(diary_filename);
simOffarm(file_inp, resultsPre);
diary off
%% SETTING parallel
% ---- Create a cluster object -----------------------------------------
cluster = parcluster();
% Get the number of workers available in the cluster
numWorkers = cluster.NumWorkers;
% Display the number of workers
disp(['Number of workers available: ', num2str(numWorkers)]);
% ---- Arribute clusters and threads ------------------------------------
% Define the number of parallel workers
numWorkers = 4;
% Create a cluster object
cluster = parcluster();
% Set the number of workers for the cluster
cluster.NumWorkers = numWorkers;
% ---- Arribute Jobs and Groups of batch --------------------------------
% Determine jobs
numCases = resultsPre.nCase;
casePerJob = 100;
numJobs = ceil(numCases/casePerJob);
IdxCase2Run = cell(numJobs,1);
for iJob = 1: numJobs
IdxCase2Run{iJob,1} = (iJob-1)*casePerJob +1:(min(iJob*casePerJob,numCases));
end
% Determine groups
groupSize = cluster.NumWorkers;
numGroups = ceil(numJobs/groupSize);
%% RUNNING parallel
for iGroup = 1:numGroups
startIdx = (iGroup-1) * groupSize + 1;
endIdx = min(iGroup * groupSize, numJobs);
% Submit the current group of jobs
jobs = cell(1, groupSize);
for j = startIdx:endIdx
resultsPre.batchCasesID = IdxCase2Run{j,1};
jobIdx = j - startIdx + 1;
jobs{jobIdx} = batch(cluster, @simOffarm, 0, {file_inp, resultsPre, jobIdx});
disp([num2str(jobIdx) ' job is distributed; ' num2str(numel(IdxCase2Run{j,1})) ' cases are under iteration'])
pause(3.)
end
disp(['The ' num2str(iGroup) '/' num2str(numGroups) 'th Group is created'])
% Wait for the current group of jobs to complete
for j = startIdx:endIdx
jobIdx = j - startIdx + 1;
wait(jobs{jobIdx});
end
% Optionally, clear completed jobs to free up memory
clear jobs;
disp(['The ' num2str(iGroup) '/' num2str(numGroups) 'th Group is accomplished'])
end