# Parallel computation applications of UQLab in MATLAB

**URL:** <https://uqworld.org/t/parallel-computation-applications-of-uqlab-in-matlab/856>\
**Category:** Community Q&A and How To\
**Tags:** pck, uqlab, metamodeling, kriging\
**Created:** [September 23, 2020, 6:38am UTC](https://uqworld.org/t/parallel-computation-applications-of-uqlab-in-matlab/856 "2020-09-23T06:38:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![GPLai](https://uqworld.org/user_avatar/uqworld.org/gplai/32/403_2.png) [@GPLai](https://uqworld.org/u/GPLai)\
**Post date:** [September 23, 2020, 6:38am UTC](https://uqworld.org/t/parallel-computation-applications-of-uqlab-in-matlab/856/1 "2020-09-23T06:38:09Z")

</div>

Hi UQworld,

I’m trying to sequentially update my metamodel, using UQLab, for instance the Kriging metamodel. And I want to use the parallel comptating capability of MATLAB, to accelerate the process. However, I found that the code in UQLab seems not supported the parallel computing well. The code can be used in series computing sometimes fails in parall computing.

Could anyone give me some suggetsion? Or could the UQLab developers could solve this issue, on the parallel computing capability? It would be much more efficient for the modeling if UQLab can support this capability.

---

<div class="post-metadata">

**Author:** ![damarginal](https://uqworld.org/user_avatar/uqworld.org/damarginal/32/73_2.png) [@damarginal](https://uqworld.org/u/damarginal)\
**Post date:** [September 23, 2020, 9:51am UTC](https://uqworld.org/t/parallel-computation-applications-of-uqlab-in-matlab/856/2 "2020-09-23T09:51:29Z")

</div>

Hi @GPLai,

Perhaps you could be more concrete about what you’re trying to do? Which part of UQLab you’re trying to parallelize? And what did you do so far that didn’t work? An example would be helpful.

Thanks!

---

<div class="post-metadata">

**Author:** ![GPLai](https://uqworld.org/user_avatar/uqworld.org/gplai/32/403_2.png) [@GPLai](https://uqworld.org/u/GPLai)\
**Post date:** [September 26, 2020, 12:05pm UTC](https://uqworld.org/t/parallel-computation-applications-of-uqlab-in-matlab/856/3 "2020-09-26T12:05:49Z")

</div>

Dear damarginal,

Thanks for your reply. I’m trying to parallelize the meta-modeling process, that is, the regression process.

I attach four matlab code file herein. And I test the parallel process of two meta-model methods——Kriging and PCKriging. I can succeed the parallel computing of Kriging meta-modeling, but fail for the PCKriging meta-modeling.

Thanks

[Regression\_Kriging.m](https://uqworld.org/uploads/short-url/c9TVrB5LEvEMOyQTfGc8lmon3bf.m) (786 Bytes) [Regression\_PCK.m](https://uqworld.org/uploads/short-url/nCOeUgXPEI9emHkbBwkI64cIF4K.m) (888 Bytes) [Test\_Kriging.m](https://uqworld.org/uploads/short-url/s7kTJd8CUvdFk6vJX3csY82EbIV.m) (985 Bytes) [Test\_PCK.m](https://uqworld.org/uploads/short-url/awr9ShzUySmd7vyPb0BjLSmYOKO.m) (978 Bytes)

---

<div class="post-metadata">

**Author:** ![GPLai](https://uqworld.org/user_avatar/uqworld.org/gplai/32/403_2.png) [@GPLai](https://uqworld.org/u/GPLai)\
**Post date:** [September 26, 2020, 12:07pm UTC](https://uqworld.org/t/parallel-computation-applications-of-uqlab-in-matlab/856/4 "2020-09-26T12:07:55Z")

</div>

In addition, when selecting the optimization method for Kriging regression, I used the ‘GA’ method. And I noticed tha in matlab the GA method can be also parallelized, but I didn’t found how to configure the setup in uqlab to realize it.

---

<div class="post-metadata">

**Author:** ![damarginal](https://uqworld.org/user_avatar/uqworld.org/damarginal/32/73_2.png) [@damarginal](https://uqworld.org/u/damarginal)\
**Post date:** [September 28, 2020, 2:07pm UTC](https://uqworld.org/t/parallel-computation-applications-of-uqlab-in-matlab/856/5 "2020-09-28T14:07:52Z")

</div>

Hi @GPLai,

thanks for the more complete description of your problem.

I’ve had a look at your PCK script (`Regression_PCK.m`). To use it under a `parfor` loop you need to explicitly specify the INPUT object you created to the configuration option of the PCK metamodel. So:

```matlab
function MyPCKriging_Gaussian= Regression_PCK(x,y)
...
myInput = uq_createInput(IOpts,'-private');
...
MetaOpts.Input = myInput;
...
MyPCKriging_Gaussian = uq_createModel(MetaOpts, '-private');

end

```

Note that you need to create the INPUT object inside the function as a private object to avoid UQLab trying to attach the object to the current UQLab session. In the `parfor` setting, this seems to create a problem (not sure why; maybe a race condition?).

In the serial setting, creating the INPUT objects and adding it to the UQLab session happen one after the other so there’s no chance of conflict. At the end of the `for` loop, a total of 8 additional INPUT objects have been created (`uq_listInputs` to check). You don’t even need to specify this INPUT object to the configuration option in the case of serial `for` loop because UQLab automatically getting the current INPUT object (the lastly created one by default) in the UQLab session and assign it to the option (this only works if you don’t use the `'-private'` flag). In your case of Kriging, there’s no need to deal with an INPUT object, so there’s no problem there.

Alternatively, you can create an isolated UQLab session for each of the MATLAB parallel workers by first calling `uqlab('-nosplash')` in `Regression_PCK.m`. This way you can use your old script as-is (i.e., no explicit INPUT specification on the configuration option but remove the `'private'` flag on the INPUT object).

Now about the parallel GA of MATLAB. Currently, the option to activate the parallel option is not open to user. So you cannot access this option via the usual configuration option of UQLab. There’s a backdoor solution to this if you wish to use parallel GA; you can edit the function `uq_Kriging_initialize_optimizer.m` and look for the relevant line for setting up the GA (around line 74) and add a new line: `'UseParallel', true` to the list of options. I don’t know what’s the performance gain of using the parallel GA to optimize the hyperparameters of Kriging model, however. I’d happy to know what you find out!

I hope this helps!

---

<div class="post-metadata">

**Author:** ![GPLai](https://uqworld.org/user_avatar/uqworld.org/gplai/32/403_2.png) [@GPLai](https://uqworld.org/u/GPLai)\
**Post date:** [September 28, 2020, 7:13pm UTC](https://uqworld.org/t/parallel-computation-applications-of-uqlab-in-matlab/856/6 "2020-09-28T19:13:55Z")

</div>

Thanks very much. Your method is great! 😀 it does work for my problem. I can parrallelize the PCKriging metamodeling process now. And I’ll try the parall GA method latter, and give the feedback for its performance herein.

---

<div class="post-metadata">

**Author:** ![damarginal](https://uqworld.org/user_avatar/uqworld.org/damarginal/32/73_2.png) [@damarginal](https://uqworld.org/u/damarginal)\
**Post date:** [October 20, 2020, 8:20am UTC](https://uqworld.org/t/parallel-computation-applications-of-uqlab-in-matlab/856/7 "2020-10-20T08:20:36Z")

</div>


