Using parfor in matlab for nested loops

后端 未结 4 1437
囚心锁ツ
囚心锁ツ 2021-01-19 07:28

I want to parallelize block2 for each block1 and parallerlize outer loop too.

previous code:

    for i=rangei
        
           


        
4条回答
  •  生来不讨喜
    2021-01-19 07:55

    The following code uses a single parfor loop to implicitly manage two nested loops. The loop1_index and loop2_index are the ranges, and the loop1_counter and loop2_counter are the actual loop iterators. Also, the iterators are put in reverse order in order to have a better load balance, because usually the load of higher range values is bigger than those of smaller values.

    loop1_index = [1:5]
    loop2_index = [1:4]
    
    parfor temp_label_parfor = 1 : numel(loop1_index) * numel(loop2_index)
        [loop1_counter, loop2_counter] = ind2sub([numel(loop1_index), numel(loop2_index)], temp_label_parfor)
        loop1_counter = numel(loop1_index) - loop1_counter + 1;
        loop2_counter = numel(loop2_index) - loop2_counter + 1;
    end
    

提交回复
热议问题