企业建设网站公司简介,wordpress 商城系统,绍兴市网站建设,国外购物平台排行榜一. 内容简介
matlab多线程#xff0c;parfor循环进度#xff0c;matlab互斥锁
二. 软件环境
2.1 matlab 2022b
2.2代码链接
https://gitee.com/JJW_1601897441/csdn
三.主要流程
3.1 matlab多线程
有好几种#xff0c;最简单的#xff0c;最好理解的就是parforparfor循环进度matlab互斥锁
二. 软件环境
2.1 matlab 2022b
2.2代码链接
https://gitee.com/JJW_1601897441/csdn
三.主要流程
3.1 matlab多线程
有好几种最简单的最好理解的就是parfor就拿那个为例子了,使用情况就是每一个for循环之前不能依赖关系每轮计算都是独立的这种才可以用parfor不同版本不太一样老版限制好像更多一些
parfor i 1:1000disp(i);
end3.2 parfor循环进度
用parfor并行去算一些东西的时候有些需要循环很多次我们是不清楚程序到底执行了多少轮如果可以看到进度的话时间太长了我们或许就不会算了
3.2.1 进度条窗口方式
会用就行可以直接拷贝走这个是官方文档里面找的
w waitbar(0,Please wait ...);% Create DataQueue and listener
D parallel.pool.DataQueue;
afterEach(D,parforWaitbar);N 10000;
parforWaitbar(w,N)parfor i 1:N% pause替换乘自己的就可以了pause(rand)send(D,[]);
end
delete(w);function parforWaitbar(waitbarHandle,iterations)persistent count h Nif nargin 2% Initializecount 0;h waitbarHandle;N iterations;else% Update the waitbar% Check whether the handle is a reference to a deleted objectif isvalid(h)count count 1;waitbar(count / N,h);endend
end3.2.2 底部进度条方式
会用就行也是官方网站里面找的这个还需要一个文件放到同一级目录就可以了代码我放上边链接了
% 设置循环次数
N 10000;
% 创建一个迭代计数器对象
parfor_progress(N);
parfor i 1:N% pause替换乘自己的就可以了pause(rand)parfor_progress;
end
parfor_progress(0);3.3 matlab互斥锁
在parfor循环中读写数据是很麻烦的并行计算就是要计算结果的但是parfor中的计算结果很难接收出来量少的时候还可以通过数组接收(2016版不可以)量多的时候数组接收就不太现实每轮循环把数据写入文件中呢parfor会报错即使可以的话由于matlab没有互斥锁写文件即使可以写进去也会是乱的没办法用
还是那句话会用就行替换成自己的就可以了注释写里面了
% 创建一个 DataQueue 对象
dq parallel.pool.DataQueue;
file fopen(ccc.txt,w);% 多重匿名函数
fun_with_params (data) saveData(data, file);
% 在 DataQueue 上设置 afterEach 方法
% 这个是给数据绑定一个处理方法就是在信号发送以后执行那个处理方法
% 这个处理方法不是并行的同一时刻只有一个线程在处理数据其他的线程都要排队
% 和互斥锁的思想很像
afterEach(dq, fun_with_params);
% 在工作线程中定义处理函数并发送数据到 DataQueue
parfor i 1:100% 替换成自己的a rand();% 在这里通过 send 函数将数据发送到 DataQueue% 可以发送单个也可也发送数组send(dq, a);
endfclose(file);function saveData(data, file)% 在此处编写后处理代码fprintf(file,%.10f , data); fprintf(file,\n);
end 3.4 parfor_progress.m
function percent parfor_progress(N)
%PARFOR_PROGRESS Progress monitor (progress bar) that works with parfor.
% PARFOR_PROGRESS works by creating a file called parfor_progress.txt in
% your working directory, and then keeping track of the parfor loops
% progress within that file. This workaround is necessary because parfor
% workers cannot communicate with one another so there is no simple way
% to know which iterations have finished and which havent.
%
% PARFOR_PROGRESS(N) initializes the progress monitor for a set of N
% upcoming calculations.
%
% PARFOR_PROGRESS updates the progress inside your parfor loop and
% displays an updated progress bar.
%
% PARFOR_PROGRESS(0) deletes parfor_progress.txt and finalizes progress
% bar.
%
% To suppress output from any of these functions, just ask for a return
% variable from the function calls, like PERCENT PARFOR_PROGRESS which
% returns the percentage of completion.
%
% Example:
%
% N 100;
% parfor_progress(N);
% parfor i1:N
% pause(rand); % Replace with real code
% parfor_progress;
% end
% parfor_progress(0);
%
% See also PARFOR.% By Jeremy Scheff - jdscheffgmail.com - http://www.jeremyscheff.com/error(nargchk(0, 1, nargin, struct));if nargin 1N -1;
endpercent 0;
w 50; % Width of progress barif N 0f fopen(parfor_progress.txt, w);if f0error(Do you have write permissions for %s?, pwd);endfprintf(f, %d\n, N); % Save N at the top of progress.txtfclose(f);if nargout 0disp([ 0%[, repmat( , 1, w), ]]);end
elseif N 0delete(parfor_progress.txt);percent 100;if nargout 0disp([repmat(char(8), 1, (w9)), char(10), 100%[, repmat(, 1, w1), ]]);end
elseif ~exist(parfor_progress.txt, file)error(parfor_progress.txt not found. Run PARFOR_PROGRESS(N) before PARFOR_PROGRESS to initialize parfor_progress.txt.);endf fopen(parfor_progress.txt, a);fprintf(f, 1\n);fclose(f);f fopen(parfor_progress.txt, r);progress fscanf(f, %d);fclose(f);percent (length(progress)-1)/progress(1)*100;if nargout 0perc sprintf(%3.0f%%, percent); % 4 characters wide, percentagedisp([repmat(char(8), 1, (w9)), char(10), perc, [, repmat(, 1, round(percent*w/100)), , repmat( , 1, w - round(percent*w/100)), ]]);end
end
3.5 补充
按下面代码执行的afterEach只能保证传完数据以后执行对应函数并不代表一个循环里面的都一起执行的
% 创建一个 DataQueue 对象
dq1 parallel.pool.DataQueue;
dq2 parallel.pool.DataQueue;
file1 fopen(ccc1.txt,w);
file2 fopen(ccc2.txt,w);
% 多重匿名函数
fun_with_params1 (data) saveData(data, file1);
fun_with_params2 (data) saveData(data, file2);
% 在 DataQueue 上设置 afterEach 方法
afterEach(dq1, fun_with_params1);
afterEach(dq2, fun_with_params2);
% 在工作线程中定义处理函数并发送数据到 DataQueue
parfor i 1:1000% 替换成自己的a rand();% 在这里通过 send 函数将数据发送到 DataQueue% 可以发送单个也可也发送数组send(dq1, a);send(dq2, a);
end% 等待所有数据接收完成% 显示接收到的数据
% 辅助函数用于保存接收到的数据到数组
function saveData(data, file)% 在此处编写后处理代码fprintf(file,%.10f , data); fprintf(file,\n);
end
改进
% 创建一个 DataQueue 对象
dq1 parallel.pool.DataQueue;
file1 fopen(ccc11.txt,w);
file2 fopen(ccc21.txt,w);
file3 fopen(ccc31.txt,w);
% 多重匿名函数
fun_with_params1 (data1) saveData(data1, file1, file2,file3);
% 在 DataQueue 上设置 afterEach 方法
afterEach(dq1, fun_with_params1);
% 在工作线程中定义处理函数并发送数据到 DataQueue
parfor i 1:100% 替换成自己的a rand();data {i,i,i}% 在这里通过 send 函数将数据发送到 DataQueue% 可以发送单个也可也发送数组send(dq1, data);
end% 等待所有数据接收完成fclose(file1);
fclose(file2);
fclose(file3);
% 显示接收到的数据
% 辅助函数用于保存接收到的数据到数组
function saveData(data, file1,file2,file3)% 在此处编写后处理代码data1 data{1};data2 data{2};data3 data{1};fprintf(file1,%.10f , data1); fprintf(file1,\n); fprintf(file2,%.10f , data2);fprintf(file2,\n); fprintf(file3,%.10f , data3);fprintf(file3,\n);
end