Parallelize cumulative average algorithm

3 visualizaciones (últimos 30 días)
Radek
Radek el 6 de Mzo. de 2024
Respondida: Edric Ellis el 6 de Mzo. de 2024
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.

Respuesta aceptada

Edric Ellis
Edric Ellis el 6 de Mzo. de 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

Más respuestas (0)

Categorías

Más información sobre Parallel for-Loops (parfor) en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by