How to run UQLab Bayesian module with parallel processing

Hi

I have a scenario in which I’m carrying out Bayesian inversion multiple times. I’d like to parallelize the process and run multiple Bayesian inversions independently. I’m using the MH algorithm for inversion.

Currently my code looks like follows:

function myFunction(arguments)
    % do some preliminary calcultions
    % set up data structure
    parfor i = 1:nsim % number of simulations
        % set up data.y
        % set up BayesOpts
        % uq_createAnalysis
        % uq_postProcessInversion
        % generate result structure called "struct_result"
        total_results(i).result = struct_result
    end
end

I’m getting an error from MATLAB which says:

Error using uq_initialize_uq_inversion (line 71)
Dot indexing is not supported for variables of this type.

Error in RS_uq_updating (line 43)
    parfor i = 1:nsim

The line 71 being referred to here is:
Internal.nModelParams = length(Internal.Prior.Marginals);

Any thoughts on what could be causing this error? It works perfectly fine with a simple for loop. Any alternate strategies for more efficient computation are also welcome.

Hi Shihab

I reproduced your problem and the error you see probably occurs because you did not explicitly assign the Forward Model and the Prior to the BayesOpts structure. This is required when running the uq_createAnalysis command in parallel. You can then, actually put everything except the uq_createAnalysis command outside the parfor loop.

So your code should look something like this:

% do some preliminary calculations
% set up data structure
% set up data.y
....
% set up BayesOpts like
BayesOpts.Prior = myPriorDist;
BayesOpts.ForwardModel.Model = myForwardModel;

parfor i = 1:nsim % number of simulations
   % uq_createAnalysis
   myAnalysisContainer{i} = uq_createAnalysis(BayesOpts,'-private');
end

Don’t forget the '-private' flag in the uq_createAnalysis command. Let me know if it works!

Hi Paul

You were spot on. Making these two changes worked.

Did I miss something from the documentation or does it not talk about parallel processing?

May I also ask what does the flag -private do?

Hi Shihab

No, you did not miss anything - but parallel processing with parfor is not officially supported at the moment. When initializing UQLab (i.e. the command uqlab) a UQLab session is started. Every UQLab session stores all uq_analysis, uq_model and uq_input objects created in that session - you can access them by typing

uq_listModels
uq_listAnalyses
uq_listInputs 

Inside the initialization script of the inversion (and most other) modules, this list of uq objects is used to automatically find the forward model and prior distribution and use them in the subsequent analysis. When running the uq_createAnalysis command on parallel workers, it seems like the UQlab session does not get replicated properly. Assigning the forward model and prior distribution directly to the BayesOpts structure circumvents this problem.

Similarly, the '-private' flag is necessary to not store the analysis object in the current UQLab session.

1 Like

Thanks a lot Paul. I understand now.