Skip to content

Instantly share code, notes, and snippets.

@tkamishima
Created December 12, 2017 19:47
Show Gist options
  • Save tkamishima/8a8675f0f3eb687316513788379af92a to your computer and use it in GitHub Desktop.
Save tkamishima/8a8675f0f3eb687316513788379af92a to your computer and use it in GitHub Desktop.
ipyparallel のデモ @ Pythonオープンサイエンスシンポジウム in つくば,2017-12-12
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"パッケージのインポート"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import ipyparallel as ipp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Client の取得,クライアント番号リストの表示"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2, 3, 4]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rc = ipp.Client(profile='mypara')\n",
"rc.ids"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## DirectView"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Directview を取得,blockモード,viewに変数を設定して,全エンジンに同じ関数を適用"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[42, 42, 42, 42, 42]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dview = rc[:] # use all engines\n",
"dview.block = True\n",
"dview['a'] = 5\n",
"dview['b'] = 10\n",
"dview.apply(lambda x: a + b + x, 27)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ただ sleep して待つ関数,エンジン上で実行するとき外のimportは効かないので import が関数内にある"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def wait(t):\n",
" import time\n",
" tic = time.time()\n",
" time.sleep(t)\n",
" return time.time() - tic"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"DirectViewで非同期(非ブロックモード)で関数を適用,結果を待って,結果を得る"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2.000717878341675,\n",
" 2.000119686126709,\n",
" 1.999979019165039,\n",
" 2.0045907497406006,\n",
" 2.0042550563812256]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar = dview.apply_async(wait, 2)\n",
"ar.wait()\n",
"ar.get()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"非同期モードで,10秒と15秒ずつ待つようなプロセスを起動"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"ar = dview.map_async(wait, [10, 15])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"同期の終了状態を見る,上記セルから15秒以内なら False になる"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar.ready()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"終了前に結果を得ようとするとエラーになる"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"ename": "TimeoutError",
"evalue": "Result not ready.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTimeoutError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-8-b34be1c10183>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mar\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/anaconda/envs/py3k/lib/python3.6/site-packages/ipyparallel/client/asyncresult.py\u001b[0m in \u001b[0;36mget\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 167\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexception\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 169\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0merror\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTimeoutError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Result not ready.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 170\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 171\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_check_ready\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTimeoutError\u001b[0m: Result not ready."
]
}
],
"source": [
"ar.get(timeout=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"終わるまでの結果を対話的に表示"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 2/2 tasks finished after 15 s\n",
"done\n"
]
}
],
"source": [
"ar.wait_interactive()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## LoadBalancedView"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"LoadBalancedView を取得"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"lbview = rc.load_balanced_view()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ブロックモード,ジョブを一つキューに投入する(全部のエンジンで関数を処理するわけではない,どのエンジンが処理するかも計算機まかせ)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1: 2.004533052444458}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar.block = True\n",
"ar = lbview.apply(wait, 2)\n",
"ar.get_dict()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ジョブを順次投入する,非同期モードなので対話環境もブロックされない"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"ar = lbview.map_async(wait, range(20))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"同期の終了状態を見る,上記セルから20秒以内なら False になる"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar.ready()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"終了前に結果を得ようとするとエラーになる"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"ename": "TimeoutError",
"evalue": "Result not ready.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTimeoutError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-14-b34be1c10183>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mar\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/anaconda/envs/py3k/lib/python3.6/site-packages/ipyparallel/client/asyncresult.py\u001b[0m in \u001b[0;36mget\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 167\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexception\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 169\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0merror\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTimeoutError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Result not ready.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 170\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 171\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_check_ready\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTimeoutError\u001b[0m: Result not ready."
]
}
],
"source": [
"ar.get(timeout=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"終わるまでの結果を対話的に表示"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 20/20 tasks finished after 46 s\n",
"done\n"
]
}
],
"source": [
"ar.wait_interactive()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"結果を表示"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[9.059906005859375e-06,\n",
" 1.0038368701934814,\n",
" 2.00134015083313,\n",
" 3.0034308433532715,\n",
" 4.002649784088135,\n",
" 4.999927043914795,\n",
" 6.004699945449829,\n",
" 7.00310492515564,\n",
" 8.003487825393677,\n",
" 9.004545211791992,\n",
" 10.001446962356567,\n",
" 11.000537157058716,\n",
" 12.002309799194336,\n",
" 13.004585027694702,\n",
" 14.000120162963867,\n",
" 15.002197027206421,\n",
" 15.999535083770752,\n",
" 17.001800060272217,\n",
" 18.003535270690918,\n",
" 19.0007266998291]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ar.get()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment