Advertisement
Guest User

parfeval overhead

a guest
Apr 11th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.57 KB | None | 0 0
  1. function [x, pasi] = hybrid_jacobi_gauss_seidel (A, b, err)
  2.     p = gcp('nocreate');
  3.     if isempty(p)
  4.         num_workers = 0;
  5.     else
  6.         num_workers = p.NumWorkers;
  7.     end
  8.    
  9.     n = length(A);
  10.  
  11.     bucket_size = floor(sqrt(n));
  12.     which_bucket = zeros(1, n);
  13.     ends_of_buckets = [];
  14.     for i = 1:n
  15.         if i == 1
  16.             remaining = bucket_size;
  17.             which_bucket(i) = 1;
  18.         elseif remaining > 0
  19.             which_bucket(i) = which_bucket(i-1);
  20.         else
  21.             remaining = bucket_size;
  22.             which_bucket(i) = which_bucket(i-1) + 1;
  23.             ends_of_buckets(end+1) = i-1;
  24.         end
  25.         remaining = remaining - 1;
  26.     end
  27.     ends_of_buckets(end+1) = n;
  28.  
  29.     x = zeros(n, 1);
  30.     pasi = 0;
  31.    
  32.     while pasi == 0 || max(abs(x - x_last)) > err
  33.         x_last = x;
  34.         %the outer for is serial
  35.         for current_bucket = 1:length(ends_of_buckets)
  36.             buck_bound = 0;
  37.             if current_bucket > 1
  38.                 buck_bound = ends_of_buckets(current_bucket-1);
  39.             end
  40.            
  41.             %worker_length = UPPER BOUND for how many indexes need to be calculated by one worker
  42.             worker_length = floor((ends_of_buckets(current_bucket) - buck_bound + num_workers - 1) / num_workers);
  43.            
  44.             future_results(1:num_workers) = parallel.FevalFuture;
  45.             for i = 1:num_workers
  46.                 start_itv = buck_bound+1 + (i - 1) * worker_length;
  47.                 end_itv = min(buck_bound+1 + i * worker_length - 1, ends_of_buckets(current_bucket));                
  48.                 future_results(i) = parfeval(p, @hybrid_parallel_function, 3, A, b, x, x_last, buck_bound, n, start_itv, end_itv);
  49.             end
  50.            
  51.             for i = 1:num_workers
  52.                 [~, arr, start_itv, end_itv] = fetchNext(future_results(i));              
  53.                 x(start_itv:end_itv) = arr;
  54.             end
  55.         end
  56.  
  57.         pasi = pasi + 1;
  58.     end
  59. end
  60.  
  61. function [x_par, start_itv, end_itv] = hybrid_parallel_function (A, b, x, x_last, buck_bound, n, start_itv, end_itv)
  62.     x_par = zeros(end_itv - start_itv + 1, 1);
  63.     for i = start_itv:end_itv
  64.         x_par(i-start_itv+1) = b(i);
  65.         x_par(i-start_itv+1) = x_par(i-start_itv+1) - A(i, 1:buck_bound) * x(1:buck_bound);
  66.         x_par(i-start_itv+1) = x_par(i-start_itv+1) - A(i, buck_bound+1:i-1) * x_last(buck_bound+1:i-1);
  67.         x_par(i-start_itv+1) = x_par(i-start_itv+1) - A(i, i+1:n) * x_last(i+1:n);
  68.         x_par(i-start_itv+1) = x_par(i-start_itv+1) / A(i, i);
  69.     end
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement