How to store and retrieve multiple data sent from worker nodes to client node in MATLAB?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
q = parallel.pool.DataQueue;
parfor i = 1:20
data = ['test - ', num2str(i)];
send(q,data);
end
This is the code I used to send data from the worker nodes to the client node. But I don't know if it helps me store the data I send from the workers to the client. The Queue length happens to be 20 which is correct but if it contains the data, I don't know how to retrieve it. I tried using a PollableDataQueue and poll() but it retrieves only one value which I guess is the last value.
So my questions are :
- Is the method I'm using to store data sent from the workers correct?
- If yes, how do I retrieve the stored data?
- If no, how do I store data sent from the workers in the client?
- Is the method I'm using to send data from the workers to the client correct?
Accepted Answer
Walter Roberson
on 28 Oct 2017
11 Comments
Viswanath Hariharan
on 28 Oct 2017
Edited: Viswanath Hariharan
on 28 Oct 2017
I did try using aftereach() without success. The queue length shows to be 20. How do I retrieve these items from the queue? How do I access the data in data queue? Could you please guide me to use aftereach() to store data in let's say a stack of cells with an example code or by adding a line of code to mine?
Thank you so much.
aftereach() automatically calls the given function on each queue entry as it comes in.
If you use a pollable data queue then you can look at QueueLength to see if anything is there, and you can use poll() to retrieve a value.
There is no provision to access the queue members out of order.
function testQueue
q = parallel.pool.DataQueue;
stack_of_cells = {};
afterEach(q, @store_on_stack)
parfor i = 1:20
data = ['test - ', num2str(i)];
send(q,data);
end
disp('done')
celldisp(stack_of_cells)
function store_on_stack(data)
stack_of_cells{end+1} = data;
end
end
Thank you so much. That was really helpful. One last question, aftereach() calls the given function on each queue entry as it comes in. Is there any command/method to retrieve, in this case all 20 string data from the queue at once as opposed to retrieving data after each entry?
If you
p_struct = struct(p);
Qimpl = p_struct.Queue;
then Qimpl will be a java.util.concurrent.LinkedBlockingQueue that you could theoretically apply the drainTo() method on, after having constructed an appropriate java Collection to store into.
I tried executing the 2 lines of code above. I got a warning -
"Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided. Use DISP or DISPLAY to see the visible public details of an object."
Should I be worried about this warning?
I tried the code you suggested, the one using aftereach(). I got an error :
Warning: Undefined function or variable 'stack_of_cells'.
I got this in all 20 iterations. I don't think the function store_on_stack() gets the cell variable. Am I doing something wrong?
In R2017b, the code works exactly as-is for me.
The warning about calling struct is to be expected. You can avoid having it printed out using code such as
old_warning_state = warning('off', 'MATLAB:structOnObject');
values = struct(values);
warning(old_warning_state);
It works now. It was my mistake I guess. I didn't define the code in a function i.e. I didn't type in function testqueue. After I did that, everything worked. Thank you.
Walter Roberson
on 30 Oct 2017
Ah, yes. The testQueue I posted relies upon shared variables and nested functions; I did not mention that.
Is there a way I can do this without relying on shared variables and nested functions? Because when I use this for my application, it produces a logical error that I am not able to clear.
Walter Roberson
on 30 Oct 2017
http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
You should be able to push your parfor call into a function that you pass all appropriate variables to: indeed, isolating the parfor is recommended, as it can make it easier for the parser to classify the variables, by reducing the scope of what needs to be analyzed as possible inputs and outputs.
More Answers (0)
Categories
Find more on COM Component Integration in Help Center and File Exchange
Products
See Also
on 28 Oct 2017
on 30 Oct 2017
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)