Borrar filtros
Borrar filtros

How to use composite objects using spmd

25 visualizaciones (últimos 30 días)
Hala Nagi
Hala Nagi el 17 de Oct. de 2022
Comentada: Hala Nagi el 18 de Oct. de 2022
I am trying to implement video-on-demand using the spmd parallel computing function. I have a saved video encoded at 3 different qualities and segmented into 3second segments, and I am trying to save video frames to the video buffer (modelled as a structure) while simultaneously playing from the buffer at a constant rate of 24 frames per second. The quality of each segment must be selected based on the buffer level. However, I am getting the error "Error detected on worker 1. Subscripted assignment between dissimilar structures" from running the following code:
mov = struct; % video buffer
bufsize = length(mov); %buffer level
switch labindex
case 1
segnum = 0; %segment number
br = 20; %assumed channel bandwidth in Mbps
%Check last segment number and get segment names from file
res = ["low" "mid" "sample"]; %available resolutions
resnum = 1;
filename = sprintf('%sdash.m3u8',res(resnum));
s = importdata(filename);
lastseg = s{end-1,1};
[lastsegnum,n,err] = sscanf(lastseg,(res(resnum)+'dash%d.ts'));
%Get frame size for highest quality
vidobj = VideoReader("sampledash1.ts");
v = read(vidobj,1);
framesize = size(v);
%Get next segment with appropriate quality
while segnum<=lastsegnum
if bufsize<72 %24fps*3sec = 72 frames in one segment
resnum = 1;
elseif bufsize>=72 && br<216
resnum = 2;
elseif bufsize>=216
resnum = 3;
segment = sprintf('%sdash%d.ts',res(resnum),segnum);
vidobj = VideoReader(segment);
f = dir(segment);
fsz = f.bytes; %size of segment
x = rand;
delay = ((fsz*8)/(br*1e6)) + x; %network delay
while hasFrame(vidobj)
mov(c) = im2frame(readFrame(vidobj));
mov(c).cdata = imresize(mov(c).cdata,[framesize(1) framesize(2)]);
bufsize = bufsize+1;
c = c+1;
case 2
depvid = vision.DeployableVideoPlayer('Size','Full-screen');
while ~isempty(mov) %start playing the video only after the first two segments have been downloaded
frame = labReceive(labindex-1,11);
segnum = labReceive(labindex-1,12);
vid(i).cdata = frame;
if segnum>1
vid(1) = [];
i = i-1;
I am a bit confused about why I am getting this error. I think it may be due to the Composite objects created by the function, but I'm not sure how.

Respuesta aceptada

Raymond Norris
Raymond Norris el 17 de Oct. de 2022
It's not related to your parallel code, the issue is the initialization of mov. This can be reproduced by the following
function nagi
mov = struct();
%mov.cdata = [];
%mov.colormap = [];
M.cdata = ones(240,320,3,'uint8');
M.colormap = [];
mov(1) = M;
So it's getting tripped up here
mov(c) = im2frame(readFrame(vidobj));
If you initialize mov as I've commented it out, you'll be fine.
Side note: I'm assuming at some point, labindex 1 won't have anything else to send labindex 2, correct? If so, you're going to run into the issue that labindex 2 is continuing to wait to receive from labindex 1. Keep in mind, for labindex 2, mov is never empty. For all intents and purposes, it only sees
mov = struct;
(Or however you end up initializing it). Therefore, at somepoint, MATLAB will throw
A communication mismatch error was encountered:
The other lab became idle during labReceive.
"The other lab" in this case is labindex 1.
  3 comentarios
Raymond Norris
Raymond Norris el 18 de Oct. de 2022
You mentioned in a comment
%start playing the video ...
but I don't see where that was actually happening, is that the call to step? Keep in mind that technically the graphics can display (and you could even print the figures to a file), but you can't see the graphics in a spmd or parfor block. Case in point, try
parfor idx = 1:2, plot(rand(10)); end
spmd, plot(rand(10)); end
The reason is because the workers are "headless" -- they don't show their graphics. What you can use is a parallel.pool.DataQueue. Here's a simple toy example
Q = parallel.pool.DataQueue;
for idx = 1:50
if labindex==2
% Worker 2 is the workhorse, responsible for sending data back
% to the client, which in turn plots the result
% "Calculate" some data
r = round(rand*100)+5;
% Send it to the client
function updatePlot(r)
% Plot the results
axis tight, shg
Hala Nagi
Hala Nagi el 18 de Oct. de 2022
That worked! Thank you so so much!!

Iniciar sesión para comentar.

Más respuestas (0)


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

Community Treasure Hunt

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

Start Hunting!

Translated by