阿里云搭建基于MatlabMPI的集群(九):一个基本的MatlabMPI程序

来源:互联网 发布:js 数组 split 编辑:程序博客网 时间:2024/06/04 18:52

一个最基本的MatlabMPI程序应当有以下结构:

% 初始化MPIMPI_Init;% 创建通信域comm = MPI_COMM_WORLD;%获取线程个数和自己的线程号comm_size = MPI_Comm_size(comm);my_rank = MPI_Comm_rank(comm);%线程号是从0开始的整数rank1=0;rank2=1;%定义消息传递标志tag=1;%线程1运行部分代码if(my_rank==rank1)    定义要发送的消息    SendMsg=1:10;    向rank2发送消息    MPI_Send(rank2,tag,com,SendMsg);end%rank2运行部分代码if(my_rank==rank2)    接收rank1发来的消息    RevMsg=MPI_Rev(rank1,tag,com);end% 结束MatlabMPIMPI_Finalize;

再附一个官网的基本MatlabMPI程序

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Basic Matlab MPI script that% sends a matrix to another processor.%% To run, start Matlab and type:%%   eval( MPI_Run('basic',2,{}) );%% Or, to run a different machine type:%%   eval( MPI_Run('basic',2,{'machine1' 'machine2'}) );%% Output will be piped into two files:%%   MatMPI/basic.0.out%   MatMPI/basic.1.out%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MatlabMPI% Dr. Jeremy Kepner% MIT Lincoln Laboratory% kepner@ll.mit.edu%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Initialize MPI.MPI_Init;% Create communicator.comm = MPI_COMM_WORLD;% Modify common directory from default for better performance.% comm = MatMPI_Comm_dir(comm,'/tmp');% Uncomment if you want to save the messages that were sent.% comm = MatMPI_Save_messages(comm,1);% Get size and rank.comm_size = MPI_Comm_size(comm);my_rank = MPI_Comm_rank(comm);% Print rank.disp(['my_rank: ',num2str(my_rank)]);% Set who is source and who is destination.source = 1;dest = 0;% Create a unique tag id for this message (very important in Matlab MPI!).tag = 1;% Check to make sure that we have exactly two processors.if(comm_size == 2)  % Source.  if (my_rank == source)    % Create data.    data = 1:10;    % Send it.    MPI_Send( dest, tag, comm, data, data );  end  % Destination.  if (my_rank == dest)    % Receive data.    [data data1] = MPI_Recv( source, tag, comm );    % Check data.    if(any((data  - (1:10)) ~= 0))      disp('ERROR: incorrect data sent.');      exit;    end  endend% Finalize Matlab MPI.MPI_Finalize;disp('SUCCESS');if (my_rank ~= MatMPI_Host_rank(comm))  exit;end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Copyright 2002 Massachusetts Institute of Technology% % Permission is herby granted, without payment, to copy, modify, display% and distribute this software and its documentation, if any, for any% purpose, provided that the above copyright notices and the following% three paragraphs appear in all copies of this software.  Use of this% software constitutes acceptance of these terms and conditions.%% IN NO EVENT SHALL MIT BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,% SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF% THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF MIT HAS BEEN ADVISED OF THE% POSSIBILITY OF SUCH DAMAGE.% % MIT SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTIES INCLUDING,% BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS% FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.%% THIS SOFTWARE IS PROVIDED "AS IS," MIT HAS NO OBLIGATION TO PROVIDE% MAINTENANCE, SUPPORT, UPDATE, ENHANCEMENTS, OR MODIFICATIONS.
阅读全文
0 0
原创粉丝点击