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月17日

tmux resolution

在tmux執行程式,經常會遇到解析度的問題,比如說在A電腦的解析度是1366*768,而在B電腦的解析度是1920*1080,如果在A電腦登入遠端主機使用tmux後,再使用B電腦登入遠端主機使用tmux attach時,會發現tmux邊緣會有一大片空白。

解決方法是B電腦使用tmux時,使用tmux attach -d,(detach the client),然後tmux就會依B電腦的解析度正確設定。

2015年6月15日

optimization solver

今天在測試J. Duchi, S. Shalev-Shwartz, Y. Singer, and T. Chandra, "Efficient projections onto the l 1-ball for learning in high dimensions," in  Proceedings of the 25th international conference on Machine learning, 2008, pp. 272-279.
這篇論文中的projection in simplex演算法,將該算法的結果,與直接用pyomo求解比較,該問題是quadratic programming(因為objective function is to minimize Euclidean distance between original vector and projected vector, 而constraint為convex set)。

使用pyomo求解時,solver使用了cplex, gurobi, and ipopt三種,cplex, gurobi與algorithm的結果一致,而使用ipopt時,答案居然有很大的誤差,看來商用軟體還是比較穩定。 

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(), 檢查版本是否正確,正確後即解決此問題。