0

I am a beginner in MATLAB software and I use MATLAB online .The problem is in initializing a variable called 'estModel' in a parallel loop . I want to use parallel processing because the calculations take a long time to complete but this problem occurs when I use 'threads' in parpool instead of 'local' . In 'local' mode this problem does not exist and the code is executed correctly and the results are correct . But in the case of 'threads', the variable 'estModel' is not initialize in the parfor loop , and give this error : Unrecognized function or variable 'estModel' . In 'local' mode the number of workers are 2 but in 'threads' mode the number of workers are 8 . So I use 'threads' instead of 'local' . I do not know about parpool structure completely . Is there a problem in this part ( parpool : local or threads or ... and chooses 'threads' based on the number of workers ) ? Finally the three variables 'kh', 'model' and 'DATA' are completely initializing before parallel loop and the variable 'model' uses the arima function . Can anyone help me to solve this problem?

code :

clear all;
close all;
clc;
DATA=xlsread('test');  % The data for modeling and forecasting is read from an excel file called test, which is a column vector.
size_of_DATA=length(DATA);
p=20;  % nonseasonal autoregressive polynomial degree range in arima model.
q=10;  % nonseasonal moving average polynomial degree range in arima model.
D=2;   % degree of nonseasonal integration range in arima model.
h=0;   % counter
for k=1:D
    for j=1:q
        for i=1:p
            if i+j+k-2<=size_of_DATA
                h=h+1;
                model(h)=arima(i-1,k-1,j-1);
            end
        end
    end
end
poolobj=parpool('threads'); 
parfor i=1:h
    estModel(i)=estimate(model(i),DATA);
end
delete(poolobj);

The excel file called 'test' is an arbitrary time series that is a column vector with 500 datas (of course, its length can vary). you can use file in this link for test : ln5.sync.com/dl/9e3913140/ctiyajcx-mgmnis6z-8mgyaigq-p2a9hpxu

AEP
  • 1
  • 2
  • 3
    Pre-allocate the variable before calling parfor – Ander Biguri Mar 28 '22 at 10:00
  • Hi thanks for reply but again, there is the problem of initialization although I initialized the variable 'estModel' before the parfor loop, no new value is assigned to the variable 'estModel' after the parfor loop is executed, and the same initial values that were in the variable 'estModel' before the loop remain unchanged. – AEP Mar 28 '22 at 17:28
  • Then the problem is your code I supose, code we can not test... – Ander Biguri Mar 28 '22 at 18:02
  • I dont think it is wrong elsewhere because as I said in the initial description, when I use 'local' instead of 'thread' in parpool, the code executes perfectly correctly and the results are completely correct, and in the parfor loop, the variable 'estModel' is completely set. This problem only occurs when I use 'thread'. – AEP Mar 28 '22 at 19:14
  • I wrote the complete code , please take a look at it and tell me if there is a problem . Thanks – AEP Mar 28 '22 at 20:16
  • The code in your code is not executable as is. 'Please provide a [minimal, complete and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) – Sardar Usama Mar 28 '22 at 20:28
  • What does `estimate` do? The problem might be in that function. As Sardar said, please prepare a MRE, which involves removing parts of your code that don't contribute to the problem. The process of creating the MRE will likely point you to the issue, but if it doesn't, it will allow us to help you find the problem. As is, we cannot tell why `estModel` is not updated. It looks like it should be. – Cris Luengo Mar 28 '22 at 20:33
  • As I said Im a beginner and I may not do much in coding. There wasnt much to change this code, but I tried to follow the minimal, complete and verifiable example code as little as possible with good naming and commenting right. This code now only needs an excel file called test that contains a time series in the form of a column vector to be executed (no specific data is intended. Any data that is a time series can be used at any length). Also, please let the person who executes this code use local instead of threads in parpool once, which will notice that the program runs without any errors. – AEP Mar 29 '22 at 08:44
  • estimate do it : https://www.mathworks.com/help/econ/arima.estimate.html – AEP Mar 29 '22 at 08:46
  • If someone wants to run the code, you can download the 'test' file as data from this link : https://ln5.sync.com/dl/9e3913140/ctiyajcx-mgmnis6z-8mgyaigq-p2a9hpxu – AEP Mar 29 '22 at 09:56

0 Answers0