您的位置:首页 > 编程语言 > MATLAB

Matlab集群搭建及使用指南

2020-01-15 08:35 4539 查看

http://www.mathworks.cn/support/product/DM/installation/ver_current/?s_cid=pi_dc_pi_R2012b

installing and Configuring Parallel Computing Toolbox and MATLAB Distributed Computing Server (R2012b)

Parallel Computing with MATLAB

Parallel computing with MATLAB on a cluster requires two products:

  • Parallel Computing Toolbox (formerly Distributed Computing Toolbox) should be installed on the computer where you write your applications.
  • MATLAB Distributed Computing Server (formerly MATLAB Distributed Computing Engine) should be installed on each computer of the cluster that performs the computation.

The installation and setup instructions for MATLAB Distributed Computing Server are available in two formats:

  • HTML
  • PDF (see the “Product Installation” chapter)

Read the system requirements for these products before proceeding.


 

Single-Machine Setup

For setup on a single machine without a separate cluster, you can use the local cluster included with Parallel Computing Toolbox to run as many as 12 workers on a single MATLAB client machine. This local cluster does not require a separate job scheduler or MATLAB Distributed Computing Server, so these instructions are not necessary.

If you want to run more than 12 workers on a single machine, install the MATLAB Distributed Computing Server as outlined in the instructions, installing and running everything on the same machine.

Installing Other Versions

To install other versions of these products, see instructions on Installing and Configuring Other Versions.

Licensing

To install and configure these products, you must have an activated License File and a File Installation Key, which you download from the License Center at the appropriate time in the installation procedure. Please contact Installation Support if you require assistance.

Getting Help

If you have questions at any point in the installation and configuration, contact the MathWorks install support team at 1-508-647-7000, or visit the support Web site.

 

 

http://bcc.hitsz.edu.cn/wiki/index.php?title=Matlab%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA%E5%8F%8A%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97&oldid=406

目录

[隐藏]
  • 2 集群搭建
  • 3 编写并行应用程序
  • 4 存在问题及扩展
  • 5 Matlab资源
  • 6 外部链接
  • 分布式计算环境介绍

    并行计算工具箱(Parallel Computing Toolbox)

    并行计算工具箱(Parallel Computing Toolbox)可以在多处理器计算环境中使用 MATLAB 和 Simulink 解决计算、数据密集型问题。使用工具箱可以解决通过装配多个处理器包含几个单独工作单位或单个大型计算的问题。这些处理器可以驻留在一个多处理器计算机上,当工具箱配合MATLAB 分布式计算引擎(MATLAB Distributed Computing Engine(Server),简称MDCE)时,实现分布式并行计算。

    利用并行计算工具箱(Parallel Computing Toolbox),可在多核和多处理器计算机上使用 MATLAB 和 Simulink 来解决计算问题和数据密集型问题。并行处理结构包括并行 for 循环和代码块、分布式数组、并行数值算法,以及消息传递函数等,并以较高的级别在 MATLAB 中执行任务及数据并行算法,而无需为特定的硬件和网络架构编写程序。这样,将串行 MATLAB 应用程序转换为 并行 MATLAB 应用程序,便几乎不需要修改代码,且不需要使用低级语言编写程序。

    用户可以使用工具箱在单个多核或多处理器桌面上执行应用程序。无需更改代码,即可在计算机集群上运行同一个应用程序(使用 MATLAB Distributed Computing Server)。并行的 MATLAB 应用程序可以作为可执行程序或共享库(用 MATLAB Compiler 构建)分发,这些可执行程序或共享库可以访问 MATLAB Distributed Computing Server。

    分布式计算引擎(服务器)(MDCE)

    MATLAB Distributed Computing Engine可以使并行计算工具箱应用程序得到扩展,从而可以使用运行在任意数量计算机上的任意数量的worker。MDCE是以服务的形式运行在所有worker node节点上,接受来自调度程序的作业,在节点上建立进程,解释执行Matlab代码,将结果返回给调度程序。

    • 调度程序(Job Scheduler)

    MATLAB Distributed Computing Engine支持多个调度程序:MathWorks 作业管理器(Job Manager)(随产品提供)或任何其他第三方调度程序,例如 Platform LSF、Microsoft Windows Compute Cluster Server(CCS)、Altair PBS Pro,以及 TORQUE。

    • 许可证策略(License Policy)

    MATLAB Distributed Computing Engine会在应用程序运行时在基于用户配置文件的集群上动态启用所需的许可证。这样,管理员便只需在集群上管理一个服务器许可证,而无需针对每位集群用户在集群上管理单独的工具箱和模块集许可证。

    • Matlab作业任务会话定义

    作业(Job)是在MATLAB中大量的操作运算。一个作业可以分解不同的部分称为任务(Task),客户可以决定如何更好的划分任务,各任务可以相同也可以不同。MALAB中定义并建立作业及其任务的会话(Session)被称为客户端会话,通常这是在你用来编写程序那台机器上进行的。客户端用并行计算工具箱来定义和建立作业及其任务,MDCE通过计算各个任务来执行作业并负责把结果返回客户端。作业管理器(Job Manager)是MDCE的一个组成部分,用来协调各个作业及其任务在各个woker上的执行。

    集群架构

    分布计算,指的是一项工作(Job)中的若干任务(Task)是独立的,任务与任务之间不存在数据交换,因此多个相似的任务(例如指纹匹配)可以同时运行在不同的数据集上。

    该系统中有三种角色,分别为提交Job(并将Job分割为多个Tasks)的Client,管理Job运行的Job Manager(JM)和进行具体运算的Worker。

    Client就是编写程序的机器,可以是任何一台局域网内的机器,Client将写好的程序作为Job提交到JM,然后JM(也可以是任何一台局域网内的机器)按接收到Job的顺序执行这些Job。每个Worker从JM那里接收Task并执行,完成之后将结果返回给JM,接下来接收下一个Task。当前正在运行的Job分配完成之后,JM则开始下一Job的分配。

    在一个分布计算系统中通常包括多个Wroker,以便同时执行多个Task,以提高速度。对Client来说,具体哪一个Task由哪一个Worker执行是透明的。也就是说,使用者只需关心如何将Job提交到JM和从JM取回运算结果,而无需管理具体的任务调度。

    集群搭建

    License Manager安装

    使用MDCE引擎建立集群,需要获得Mathworks公司的授权。Licence Manager可以通过Network方式下安装,在standalone方式下安装是没有的。在一个集群中只要一个Node安装License Manager,其它节点就可以获得授权。

    • 安装参考

    选择network安装

    1) choose "install manually without using the internet"
    2) enter the "file installation key"   xxxxx-xxxxx-xxxxx-xxxxx-xxxxx
    3) check out "license manager" option
    4) use "license_server.dat" when asked for license file
    • 启动参考

    Windows Platform

    1) 以管理员模式运行$MATLABROOT$/flexlm/lmtools.exe
    2) 选择Start/Stop Reread标签,单击Start Server

    Linux&Unix Platform

    1) 在$MATLAB$/etc/license.dat中有SERVER一栏,后面填入your hostname
    2) 将license server的端口设置成27000
    3) 设置DAEMON MLM为DAEMON MLM $MATLAB$/etc/lm_matlab
    4) 设置OPTIONS=$MATLAB$/etc/lmopts.sh
    5) 用普通用户运行./lmstart,开启License Manager

    分布式计算引擎(MDCE)安装

    安装启动MDCE

    • Windows Platform
    1) 用管理员启动cmd.exe, cd %MATLABROOT%/toolbox/distcomp/bin
    2) 修改mdce_def.bat, 设置MDCEUSER=./%username%, %username%是你的登入帐号
    3) 运行mdce.bat install, 输入帐号密码
    4) 运行mdce.bat start, 启动Matalb Distributed Computing Server
    5) WIN+R,输入services.msc,查看MDCS是否启动
    6) 运行addMatlabToWindowsFirewall.bat, 配置防火墙,开放MDCE服务

    说明windows服务默认Local System帐号启动,用Local System启动的服务是没有权限访问UNC(Universal Naming Convention)命名约定的网络资源,UNC格式为\\servername[server IP]\sharename\directory\filename。MDCE中传输和共享文件都是使用UNC命名约定,因此要使MDCE支持文件共享,启动它的用户需要有权限访问UNC网络资源。为了配置方便,用登入帐号启动MDCE服务,就可以访问共享资源。

    • Linux&Unix Platform
    1) $cd $MATLABROOT$/toolbox/distcomp/bin
    2) #./mdce install
    3) #./mdce start
    4) config iptables, make input chain ports tcp(135,139,445) and udp(137,138) open

    说明在UNIX LIKE的系统中mdce是以ROOT身份启动的,可以轻松访问UNC格式的网络资源。

    • DNS设置

    MDCE引擎使用机器名(hostname)在各个节点之间通信。为了能够正确解析集群中所有的node,需要在各个node的hosts文件中添加集群中所有机器的ip和hostname的映射条目。

    调度程序(Job Manager)和work node启动配置

    • 启动Job Manager

    在集群中所有的提交的Job都是通过Job Manager分配和管理的,所有的work node都是注册在Job Manager这台机器上,需要固定这台Job Manager主机。client端编写完任务就可以向这台Job Manager这台主机提交。MDCE支持当前所有主流的作业调度程序如LSF, CCS, PBS Pro等。作为实验性的环境,第三方调度程序没有配置,而是采用了随MDCE一起发布的Job Manager来管理集群中的作业调度。

    • 启动Worker Node
    startworker -jobmanagerhost jmip -jobmanager jmname -name worker_name -remotehost localip

    • 查看集群节点信息
    nodestatus -remotehost hostname[ip] -infolevel n
    Flag Operation
    -remotehost <hostname> Displays the status of the mdce service and the processes it maintains on the specified host. The default value is the local host.
    -infolevel <level> Specifies how much status information to report, using a level of 1-3. 1 means only the basic information, 3 means all information available. The default value is 1.
    -v Verbose mode displays the progress of the command execution.

    文件共享设置

    一个集群内网络中主要有三种流量,代码,数据,运行时通信。在Matlab集群中,运行时通信对于Client是透明的,用户需要设置的是如何共享代码和数据。代码可以通过Job Manager传输到各个节点上,代码的大小相对来说比较小,网络传输开销小,而对于数据来说,不可能把数据在每一个Worker node节点上都复制一份拷贝,这样开销太多了

    1. 通过Job Manager传输代码

    set(job, 'FileDependencies', {'fun1.m','fun2.m','main.m'})

    2. 设置文件关联,让Worker node访问网络文件和本地文件

    set(job, 'PathDependencies', {'\\ip\sharedir\','/mnt/'})

    3. 使用Configurations Manager设置

    Configurations Manager是Matlab中提供的图形化配置管理软件,使用此功能可以方便地配置常用的Job Manager,而不用每次运行都在代码中加入set函数。

    在matlab的菜单栏上选择Parallel->Manage Configurations..., 启动Configurations Manager,设置jobmanager的属性(Properties...)

    在我们的集群中大多数是基于Windows的系统,采用windows的共享文件机制就可以在各个windows系统间实现文件的共享。对于linux节点,就需要安装samba,然后将网络文件mount到local路径。

    • Windows共享文件设置
    • Linux&Unix共享文件设置

    安装参考: [Samba wiki]

    挂载Windows共享文件

    #mount -t smbfs //ip/share /mnt/

    提供本机文件共享,samba设置

    [global]
    ...
    security=share
    [sharedir]
    ...
    public = yes

    编写并行应用程序

    任务级并行

    • 流程

    1) 搜索JM,创建对象: findResource()

    % MyJobManager 表示JM的名字
    % MyJMhost 表示运行该JM的主机名或IP地址
    jm  =  findResource('scheduler','type','jobmanager','Name','MyJobManager','LookupURL','MyJMhost')

    使用findResource函数时,省略所有property和value,可搜索所有局域网内可用的JM

    all_managers = findResource('scheduler','type','jobmanager')

    2) 创建Job: createJob()

    虽然这个函数在client会话中执行,但实际上是在名叫jm的Job Manager上创建了一个名叫job1的Job

    job  =  createJob(jm)

    3) 分配Task: createTask()

    在创建Job之后,使用createTask函数为该Job创建Task。Task定义了Worker所要执行的函数。在本例中,每个Task将会生成3*3的随机数矩阵。

    createTask(job, @rand, 1, {3,3});
    createTask(job, @rand, 1, {3,3});
    createTask(job, @rand, 1, {3,3});
    createTask(job, @rand, 1, {3,3});
    createTask(job, @rand, 1, {3,3});

    也可用以下方法生成5个Task:

    T = createTask(job, @rand, 1, {{3,3} {3,3} {3,3} {3,3} {3,3}});

    4) 提交工作: submit()

    使用submit函数提交Job到Job Manager,Job Manager将Task分配到已注册的Worker上并开始执行。

    submit(job);

    5) 等待: waitForState()

    waitForState(job, 'finished');

    6) 取回计算结果: getAllOutputArguments()

    计算结果存储在每个任务对象的OutputArguments属性中,使用getAllOutputArguments取回所有的结果,运算结果以cell数组的形式返回。

    results = getAllOutputArguments(job);
    • 完整示例
    clear all;
    % 寻找资源。
    jm = findResource('scheduler', 'type', 'jobmanager', 'name', 'myJM', 'LookupURL', '192.168.0.100');
    % 使用刚才找到的资源建立一个工作
    job = createJob(jm);
    % 设置该工作的文件关联,让所有workers都可以找到原程序文件,需要在Client上设置共享文件夹。
    set(job, 'PathDependencies', {'\\192.168.0.101\matlab_code\', '/mnt/'})
    % 另一种方法,把用到的原程序文件传给所有workers。
    % set(job, 'FileDependencies', {'hm.m'});
    N = 5;
    M = 4;
    % 建立4个任务,每任务都是算hp(M, N)。
    createTask(job, @hp, 1, {{M, N}, {M, N}, {M, N}, {M, N}});
    % 提交工作给jobmanager。
    submit(job)
    % 等待所有workers都把任务做完。
    waitForState(job, 'finished')
    % 取出计算结果。
    results = getAllOutputArguments(job);
    % 销毁Job,释放资源
    destroy(job);

    代码级并行

    存在问题及扩展

    • 异构集群

    由于集群中存在多种系统平台,32位,64位,xp, windows 7, ubuntu等,在用Matlab的PCT工具时(e.g., parfor,分布式矩阵,spmd),有部分worker node无法参与工作,根据Matlab官方关于异构集群的Requirement:

    "Homogeneous cluster configurations are recommended. Parallel processing constructs that work on the infrastructure enabled by matlabpool—parfor, spmd, distributed arrays, and message passing functions—cannot be used on a heterogeneous cluster configuration. The underlying MPI infrastructure requires that all cluster computers have matching word sizes and processor endianness. A limited set of functions in Parallel Computing Toolbox can work in heterogeneous cluster configurations."

    使用PCT工具箱需要有相同的机器字长和字节序。

    • MDCE版本

    我们采用了MDCE 4.3(Matlab R2010a)来搭建集群,MDCE需要相同的版本才能够互相通信,不同版本的Matalb是无法加入到集群中的。

    • 扩展及设想

    目前我们用了5台机器,每台机器建立两个Worker node,共计10个worker node,每个worker node分配512兆内存的实验集群。我们的设想是搭建一个拥有20个CPU,40个worker node每个worker node拥有1024兆的集群,利用晚上实验室电脑闲置的时间来跑程序。这样的话,如果有人想跑大型应用时,就可以经过一个晚上的时间将它跑完。

    • 加入我们

    如果你对此感兴趣,或者你有实际需要请加入我们,联系方式: ltryee, steven at c203。

     

    转载于:https://my.oschina.net/alphajay/blog/102208

    • 点赞
    • 收藏
    • 分享
    • 文章举报
    chenqiechun3408 发布了0 篇原创文章 · 获赞 0 · 访问量 563 私信 关注
    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: