Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [x, pasi] = hybrid_jacobi_gauss_seidel (A, b, err)
- p = gcp('nocreate');
- if isempty(p)
- num_workers = 0;
- else
- num_workers = p.NumWorkers;
- end
- n = length(A);
- bucket_size = floor(sqrt(n));
- which_bucket = zeros(1, n);
- ends_of_buckets = [];
- for i = 1:n
- if i == 1
- remaining = bucket_size;
- which_bucket(i) = 1;
- elseif remaining > 0
- which_bucket(i) = which_bucket(i-1);
- else
- remaining = bucket_size;
- which_bucket(i) = which_bucket(i-1) + 1;
- ends_of_buckets(end+1) = i-1;
- end
- remaining = remaining - 1;
- end
- ends_of_buckets(end+1) = n;
- x = zeros(n, 1);
- pasi = 0;
- while pasi == 0 || max(abs(x - x_last)) > err
- x_last = x;
- %the outer for is serial
- for current_bucket = 1:length(ends_of_buckets)
- buck_bound = 0;
- if current_bucket > 1
- buck_bound = ends_of_buckets(current_bucket-1);
- end
- %worker_length = UPPER BOUND for how many indexes need to be calculated by one worker
- worker_length = floor((ends_of_buckets(current_bucket) - buck_bound + num_workers - 1) / num_workers);
- future_results(1:num_workers) = parallel.FevalFuture;
- for i = 1:num_workers
- start_itv = buck_bound+1 + (i - 1) * worker_length;
- end_itv = min(buck_bound+1 + i * worker_length - 1, ends_of_buckets(current_bucket));
- future_results(i) = parfeval(p, @hybrid_parallel_function, 3, A, b, x, x_last, buck_bound, n, start_itv, end_itv);
- end
- for i = 1:num_workers
- [~, arr, start_itv, end_itv] = fetchNext(future_results(i));
- x(start_itv:end_itv) = arr;
- end
- end
- pasi = pasi + 1;
- end
- end
- function [x_par, start_itv, end_itv] = hybrid_parallel_function (A, b, x, x_last, buck_bound, n, start_itv, end_itv)
- x_par = zeros(end_itv - start_itv + 1, 1);
- for i = start_itv:end_itv
- x_par(i-start_itv+1) = b(i);
- x_par(i-start_itv+1) = x_par(i-start_itv+1) - A(i, 1:buck_bound) * x(1:buck_bound);
- 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);
- x_par(i-start_itv+1) = x_par(i-start_itv+1) - A(i, i+1:n) * x_last(i+1:n);
- x_par(i-start_itv+1) = x_par(i-start_itv+1) / A(i, i);
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement