83 lines
2.2 KiB
Matlab
83 lines
2.2 KiB
Matlab
function wh = submit_simulations(wh,options)
|
|
|
|
arguments
|
|
wh
|
|
options.parallel = 1;
|
|
options.simulation_mode = 1;
|
|
end
|
|
|
|
%%% 2) SUBMIT SIMULATION
|
|
% Initialize job results
|
|
if options.parallel
|
|
curpool = gcp('nocreate');
|
|
if isempty(curpool)
|
|
parpool;
|
|
else
|
|
% stop all forgotten or unfetched jobs from queue
|
|
if ~isempty(curpool.FevalQueue.QueuedFutures) || ~isempty(curpool.FevalQueue.RunningFutures)
|
|
oldq = length(curpool.FevalQueue.QueuedFutures) +length(curpool.FevalQueue.RunningFutures);
|
|
curpool.FevalQueue.cancelAll
|
|
fprintf('Canceled %d unfetched jobs from old queue.',oldq);
|
|
end
|
|
|
|
end
|
|
results = parallel.FevalFuture.empty();
|
|
else
|
|
results = [];
|
|
end
|
|
|
|
fprintf('Requested %d loops',wh.getLastLinIndice);
|
|
|
|
lin_idx = 1;
|
|
for lin_idx = 1:wh.getLastLinIndice
|
|
optionalVars = struct();
|
|
if ~isempty(wh.getDimension)
|
|
% Build the optionalVars struct
|
|
[parametervalues,parameternames]=wh.getPhysIndicesByLinIndex(lin_idx);
|
|
for pidx = 1:numel(parameternames)
|
|
optionalVars.(parameternames{pidx}) = parametervalues{pidx};
|
|
end
|
|
end
|
|
|
|
%%% SIMULATION HERE
|
|
if options.parallel
|
|
|
|
numOutputs = 1;
|
|
results(lin_idx) = parfeval(@imdd_model, numOutputs, options.simulation_mode, optionalVars);
|
|
|
|
else
|
|
|
|
finalresults{lin_idx} = imdd_model(options.simulation_mode,optionalVars);
|
|
wh.addValueToStorageByLinIdx(finalresults{lin_idx}, 'ber', lin_idx);
|
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
if options.parallel
|
|
|
|
%%% 4) Setup waitbar
|
|
h = waitbar(0, 'Processing Simulations...');
|
|
% Helper function to compute progress
|
|
updateWaitbar = @(~) waitbar(mean(arrayfun(@(f) strcmp(f.State, 'finished'), results)), h);
|
|
|
|
fprintf('Fetching results... \n');
|
|
|
|
% Update the waitbar after each simulation
|
|
updateWaitbarFutures = afterEach(results, updateWaitbar, 0);
|
|
|
|
% Close the waitbar after all simulations complete
|
|
afterAll(updateWaitbarFutures, @(~) delete(h), 0);
|
|
|
|
%%% 7) Fetch final results after all computations
|
|
fetchOutputs(results);
|
|
|
|
for ridx = 1:length(results)
|
|
wh.addValueToStorageByLinIdx(results(ridx).OutputArguments{1}, 'ber', ridx);
|
|
end
|
|
|
|
end
|
|
|
|
|
|
end |