Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize parallel Operation in MATLAB

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how MATLAB implements parallel computing. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Yesterday

Originally, I recorded the whole process, but after it was converted to GIF, it was more than 2 megabytes, so I couldn't upload it, so I recorded some of it.

When recording, I forgot to set the EdgeAlpha in the patch object to zero, so the edge of patch obscured the previous line (I put line in front of the program). If you set the EdgeAlpha in patch to 0, the connection is red, or you don't need line, or you can just set the value of EdgeColor to'r'. I think this should look better.

The code is basically the same as yesterday. I just set the FaceAlpha value of the patch object out of plotSTL to 0 and patch each small block in the loop.

Let's release the source code here.

Clc

Clear

Close all

VertexData= stl2matlab ('pig_stl.txt')

HPatch = plotSTL (VertexData, [1 10])

HPatch.FaceAlpha = 0

View (3)

Set (gca,'NextPlot','add')

Set (gca,'Clipping','off')

Axis off

Set (gca,'Units','nor')

Set (gca,'Position', [0 0 1 1])

Set (gca,'Color',get (gcf,'Color'))

X = VertexData {1}

Y = VertexData {2}

Z = VertexData {3}

For I = 1: size (XMagol 2)

Line (X (:, I), Y (:, I), Z (:, I), 'Color',' r', 'LineWidth', 0.5)

Scatter3 (X (:, I), Y (:, I), Z (:, I), 'SizeData', 20,' MarkerFaceColor','y')

Patch (X (:, I), Y (:, I), Z (:, I), [1 128 FaceAlpha' 255 128], 'EdgeAlpha', 0)

Drawnow

I

End

Do something useful.

As for parallel computing, I only know a parfor. Like the for loop, parfor is paired with an end.

. There is a big difference between parfor and for loops.

You cannot nest a parfor in parfor.

Parfor only supports looping integers

Parfor it is not necessarily in order (I don't know how to describe it, see the following example)

Each loop must be independent, that is, the next loop cannot depend on the previous loop.

Well, this request is the most annoying.

When we use parfor, MATLAB opens several workspace, and then divides the loop into several parts and allocates them to those workspace to calculate at the same time. It's several times faster.

. However, if the for loop can solve the problem in a few seconds, using parfor may be slower than for.

It is said above that parfor cannot be nested under parfor, but what if we have double loops? The solution is outer parfor, inner for, or outer for, inner parfor. But the latter is slow. Another way is to change the two-layer loop single-layer loop, such as:

A = magic (2000)

M = size (A < 1)

N = size (A ~ (2))

% A = reshape (A, m * n, 1)

Tic

Parfor k = 1:numel (A)

I = mod (k-1, m) + 1

J = floor ((k-1) / m) + 1

A (k) = A (k) + I * j matlab% A (k) = A (k) + I * j matlab% you can only use vectors in a single matlab.

End

A = reshape (A _ mam _ m _ n)

Method4 = toc

Thank you for reading! This is the end of this article on "how to achieve parallel computing in MATLAB". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report