Azzera filtri
Azzera filtri

Parallelize cumulative average algorithm

1 visualizzazione (ultimi 30 giorni)
Hi,
is there a way how to parallelize algorithm computing average of data accessible only as a stream using parfor?
dat = 1:9;
avg = 0;
for i=1:numel(dat)
avg = (dat(i)+(i-1)*avg)/i;
end
avg
In reality the code looks more like the following, where the input data is too big to be stored.
avg = 0;
for i=1:numFrames
frm = getNewFrame(); % returns new data
avg = (frm+(i-1)*avg)/i;
end
I somehow understand why this code can not be parallelized as is, but maybe Matlab offers some trick.

Risposta accettata

Edric Ellis
Edric Ellis il 6 Mar 2024
You can use a parfor "reduction variable" to compute this, with a slight modification which may or may not be significant:
dat = rand(1,9);
avg = 0;
N = numel(dat);
parfor i = 1:N
avg = avg + dat(i)/N;
end
Starting parallel pool (parpool) using the 'Processes' profile ... Parallel pool using the 'Processes' profile is shutting down.
avg, mean(dat)
avg = 0.6135
ans = 0.6135

Più risposte (0)

Categorie

Scopri di più su Parallel for-Loops (parfor) in Help Center e File Exchange

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by