顯示具有 平行處理 標籤的文章。 顯示所有文章
顯示具有 平行處理 標籤的文章。 顯示所有文章

2015年6月18日

ipython parallel callback

使用apply_async()函式後,會回傳AsyncResult_object,因此原本我設想可以如同multiprocessing模組中的apply_async函式使用callback function來處理執行完成物件的訊息,
但是在ipython parallel的apply_async似乎不支援callback的用法,所以後來我在stackoverflow找到了一個簡單的處理事件完成的解法如下:
c = Client()
dview = c[:]
asyncs = [dview.map_async(f, [arg]) for arg in args]
while asyncs:
    for async in asyncs[:]:
        if async.ready():
            asyncs.remove(async)
            print async.result[0]

2015年6月15日

ipython parallel import

如果平行處理時需要import package時,可使用以下方式:
rc = Client(profile='ssh')
rc.block = True
dview = rc.direct_view()
dview.use_dill()

with dview.sync_imports():
   import numpy as np

# update dict
dview['np'] = np
dview['MyDerive'] = MyDerive

如果有自已定義的package(e.g. ipro)時,必須在每台機器設定PYTHONPATH,以保證可以直接在python當中import pro。

ipython parallel serialization

平行處理時,不論使用direct view或是loadbalance view, 物件序列化都是一個很麻煩的問題,後來找解法時,發現可以使用dill處理。

因為只有direct view支援use_dill() function, 如果loadbalace view也要使用dill時,可使用以下方法 (stackoverflow)。
rc = Client()
rc.direct_view().use_dill()
lv = rc.load_balanced_view()
如果要使用自定義的class時,還要考慮Data movement via DirectView, 否則無法呼叫class instance method,
class MyObject(object):

    def __init__(self, val):
        import os
        import platform
        self.name = platform.node()
        self.pid = os.getpid()
        self.val = val

    def run(self):
        return "{}, myobject {}_{}".format(self.val, self.name, self.pid)


def MyObjectrun(val):
    obj = MyObject(val)
    return obj.run()


def test_parallel():
    rc = Client(profile='ssh')
    rc.block = True
    print "nodes: {}".format(len(rc.ids))
    rc.direct_view().use_dill()

    rc.direct_view()['MyObject'] = MyObject
    lview = rc.load_balanced_view()
    res = lview.apply_sync(MyObjectrun, 10)
    print res

ipython parallel

  • automatic setting: ipcluster
  • manual setting: ipcontroller and ipengine

因為我們使用多台機器計算,所以使用ipcontroller and ssh方式設定。

設定profile

  • $ipython profile create --parallel --profile=ssh
  • 會在/home/chenhh/.ipython/profile_ssh/中建立許多設定檔

執行ipcontroller

  • $ipcontroller --profile=ssh --reuse
    • 必須指定profile=ssh才會讀取profile_ssh中的設定,否則預設讀取profile_default中的設定。
    • --reuse指定重複使用json檔,否則每次執行ipcontroller時會生成新的json檔。
  • 會在/home/chenhh/.ipython/profile_default/security/資料夾下生成ipcontroller-client.json與ipcontroller-engine.json兩個設定檔。 
  • 將engine的設定檔以scp方式傳給engine
    • scp /home/chenhh/.ipython/profile_ssh/security/ipcontroller-engine.json ./
  • 而engine使用以下命令執行
    • ipengine --file=./ipcontroller-engine.json

ipcontroller_config.py設定 

HubFactory

  • c.HubFactory.ip = u'*' 
    • #listen to all interface,此設定對應到ipcontroller-engine.json中的location內容。

IPControllerApp

  • c.IPControllerApp.work_dir = u'/home/chenhh/.ipython/profile_ssh'
  • c.IPControllerApp.profile = u'ssh'
  • c.IPControllerApp.reuse_files = True

engine

  • $ipython profile create --parallel --profile=ssh
  • 使用scp chenhh@192.168.1.1:/home/chenhh/.ipython/profile_ssh/security/ipcontroller-engine.json /tmp 將檔案複制到/tmp資料夾下。
  • $ipengine --file=/tmp/ipcontroller-engine.json --ssh=chenhh@192.168.1.1  --profile=ssh
    • 注意一個ipengine只會對應到一顆cpu, 如果要使用多個cpu時,上述指令多執行幾次即可。

ipython

  • from IPython.parallel import Client
  • rc = Client(profile="ssh")
  • 指定profile後,即可使用

debug

我的controller主機使用的zmq版本是4.0.4,而engine中,有一台的zmq版本是2.0.2,所以該台主機在連線時,一直出現heartbeat timeout。
解決方法:
  • 首先將engine的zmq-dev版本更新為libzmq3-dev:amd64的版本。
  • pip uninstall pyzmq
  • 然後抓取pyzmq的source code, python setup.py install後,使用ipython。
    • import zmq
    • zmq.zmq_version_info(), 檢查版本是否正確,正確後即解決此問題。

2014年6月4日

MPICH3.1安裝

  • 已成功安裝在Ubuntu 14.04, Gentoo與Mint 16上,大部份機器使用icc/icpc與iort,部份機器使用gcc/gfortran。
  • 所有軟體均安裝在/opt/mpich資料夾下。
安裝步驟:
  1. 下載mpich3.tar.gz至/tmp目錄下並解壓縮
  2. cd /tmp/mpich-3.1/
    ./configure --prefix=/opt/mpich 
  3. make -j4 2
  4. sudo make install (要注意sudo的environment會重設的問題)

設定PATH: 
  • sudo vim /etc/bash.bashrc
  • sudo export PATH="$PATH:/opt/mpich/bin"
  • 確認PATH設定成功:mpicc

設定LDCONFIG:
  • sudo touch /etc/ld.so.conf.d/mpich.conf
  • sudo echo "/opt/mpichbin" >> /etc/ld.so.conf.d/mpich.conf
  • sudo ldconfig
  • 確認ldconfig設定成功:ldconfig -p|grep mpich