- Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
- Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
- Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
parfor problems (structure, reduction assignment, etc)
    조회 수: 4 (최근 30일)
  
       이전 댓글 표시
    
I am attempting to utilize a 'parfor loop' in the code to reduce the time required for the estimation of the "sign restriction vector autoregressive model." The complexity of my sign restriction (=SIGN) results in a lengthy process (more than 24 hours on my laptop) to obtain a sufficient number (ndraws=10000) of successful draws (i.e., those that meet the sign restrictions). 
However, I am encountering some difficulties in using 'parfor'. Specifically, the following section of the code, which I would like to apply 'parfor loop', is intended to store the successful draws that satisfy a particular condition (my sign restrictions).
jj = 0; % accepted draws
c1 = 1; % just constant (for counting)
parfor tt = 1:sr_draw    
    if tt > ndraws
        continue
    end
    % Set up VAR_draw.(draw{j}) for rotations: Only identification uncertainty
    label  = {['draw' num2str(jj)]};
    VAR_draw.(label{1}) = VAR;
    % Compute rotated B matrix
    B = SignRestrictions(SIGN,VAR_draw.(label{1}),VARopt); 
    if ~isempty(B)
        jj = jj+c1;
        % Store B        
        Ball(:,:,jj) = B;
        % Update VAR_draw.(draw{j}) with the rotated B matrix for IR, VD, and HD
        VAR_draw.(label{1}).B = B; 
        % Compute and store IR, VD, HD
        [aux_irf, VAR_draw.(label{1})] = VARir(VAR_draw.(label{1}),VARopt); 
        IRall(:,:,:,jj)  = aux_irf;
        aux_fevd = VARvd(VAR_draw.(label{1}),VARopt);
        VDall(:,:,:,jj)  = aux_fevd;
        aux_hd = VARhd(VAR_draw.(label{1}),VARopt);        
        shock(:,:,:,jj) = aux_hd.shock;
    end
end
    if jj<ndraws
        disp('Draws which meet SIGN are not sufficiently stored. Increase sr_draw')
    end
댓글 수: 1
  Steven Lord
    
      
 2024년 6월 21일
				However, I am encountering some difficulties in using 'parfor'.
What specific difficulties are you experiencing?
채택된 답변
  Edric Ellis
    
      
 2024년 6월 25일
        The problem here is that you're trying to extend the arrays like Ball by assigning off the end of the current size of the array. You cannot do that in parfor. What you can do is use concatenation, a bit like this:
Ball = zeros(7, 7, 0); % Or whatever size is appropriate
cat3 = @(x, y) cat(3, x, y); % Function to use as reduction operation
parfor tt = 1:sr_draw
     B = SignRestrictions(SIGN,VAR_draw.(label{1}),VARopt);
     if ~isempty(B)
         Ball = cat3(Ball, B)
     end
end
Note that I needed to make a simple function to ensure that I could write a parfor reduction operation.
You should be able to apply the same pattern to the other problematic assignments.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


