Monday, February 10, 2014

MATLAB Distributed Computing

1. Installing and Configuring Parallel Computing Toolbox and MATLAB Distributed Computing Server

1.1 System Requirements

Required:
SSE2

1.2 Parallel Computing with MATLAB on a Cluster
Interaction between the Distributed Computing productsParallel 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.
1.2.1 Install Products and Configure Your Cluster (latest release)
1.2.1.1 install Licence Manager in Server

使用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$/etc/win64/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

1.2.1.1 install Matlab Distrubuted Computing Engine (分布式计算引擎MDCE的安装)

  • Windows Platform
1) 用管理员启动cmd.exe, cd %MATLABROOT%/toolbox/distcomp/bin
2) > mdce install
3) > mdce start
4) WIN+R,输入services.msc,查看MDCS是否启动
5) 运行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的映射条目。

1.2.1.2 调度程序(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来管理集群中的作业调度。 Jm.jpg

Notes:

if you can't run matlabpool, try this in command line:>> distcomp.feature( 'LocalUseMpiexec', false )
or
>> matlabpool close

Reference:

第一周主题:数组 之 Single Number II in LeetCode (in java)


要求:
Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
(给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数。)
解答:
这是一道很有趣的位运算题。百思不得其解,最后看了[1]介绍的思路。

如果用二进制来表达数组中每一个数字的话,如果我们遍历一遍数组中的所有的数,即将数组中所有数的二进制表达的每位上的“1”都数一遍,那么除了需要找的那个只出现一次的整数外,其他所有的数字二进制表示中每一位“1”都出现了3k次(k为非负整数)。
如果能有办法将所有的二进制表达中这些3k个“1”清零,那么剩下的二进制表达就表示了需要找到的数。

(0)初始设定ones=0; twos=0;

(1)用twos记录到当前为止,二进制”1“出现3k+2次的位数;
twos = twos | (ones & A[i]);

(2)用ones记录到当前为止,二进制“1”出现3k+1次的位数;
ones = ones ^ A[i];

(3)当ones和twos的某一位同时为”1“时,即该位出现二进制”1“ 3k+3次(也就是3k次),清零该位。
xthrees = ~(ones & twos); // xthrees 是二进制“1”没有出现3k次的位数
ones = ones & xthrees;
twos = twos & xthrees;

(4)遍历完数组中所有的数之后,ones记录的就是最终结果。

扩展:

同样的道理,可以解决{给定一个包含n个整数的数组,除了一个数出现两次外所有的整数均出现三次,找出这个只出现两次的整数。},return twos就行了。

更强的扩展: {给定一个包含n个整数的数组,有一个整数x出现b次,一个整数y出现c次,其他所有的数均出现a次,其中b和c均不是a的倍数,找出x和y。}可以参见[1],思路是一样的。

Reference:

Tuesday, December 24, 2013

open salaries

http://open.bufferapp.com/

It is a crazy idea to make a company like this. I damn like it.