Skip to content

Instantly share code, notes, and snippets.

@alimanfoo
Last active July 14, 2021 11:09
Show Gist options
  • Save alimanfoo/3effdc51b1cbd20e3581582036854cd9 to your computer and use it in GitHub Desktop.
Save alimanfoo/3effdc51b1cbd20e3581582036854cd9 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "3a633470-3da0-477a-b83d-5fed6c797657",
"metadata": {},
"source": [
"# Launching Dask clusters\n",
"\n",
"This notebook illustrates how to launch a Dask cluster on Abafar."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "9e9ef947-c9b1-4706-8d9e-f3226e9a583f",
"metadata": {},
"outputs": [],
"source": [
"# gain access to the gateway\n",
"from dask_gateway import Gateway\n",
"gateway = Gateway()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a4f6cdad-1926-4bfc-9920-a18bf23a702e",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "579bf0d8253544c9a73fd2f33a5cc87a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(HTML(value='<h2>Cluster Options</h2>'), GridBox(children=(HTML(value=\"<p style='font-weight: bo…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# list available options\n",
"options = gateway.cluster_options()\n",
"options"
]
},
{
"cell_type": "markdown",
"id": "cf1b377b-93d6-449c-9731-444520c6b155",
"metadata": {},
"source": [
"**N.B., when selecting the environment, make sure it matches the environment you're using to run this notebook!**"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0fca85b8-d196-48ee-84ef-77442d3df4e5",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1ae10961e40449e5827342ee41cca47c",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(HTML(value='<h2>GatewayCluster</h2>'), HBox(children=(HTML(value='\\n<div>\\n<style scoped>\\n …"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# create a cluster with the options selected via the widgets above\n",
"# ... may take a few minutes to provision a VM for the scheduler\n",
"cluster = gateway.new_cluster(options)\n",
"cluster"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f991632c-560d-49a5-a6d5-396159f61809",
"metadata": {},
"outputs": [],
"source": [
"# alternatively, create a cluster by specifying environment and profile as arguments\n",
"# cluster = gateway.new_cluster(environment=\"binder-v3.0.0\", profile=\"High Memory Worker\")\n",
"# cluster"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "697a5cd3-34ec-41bc-828c-00aad0894f37",
"metadata": {},
"outputs": [],
"source": [
"# you can scale the cluster using the widget above, or via a manual API call\n",
"# ... may take a few minutes to provision VMs for the workers\n",
"cluster.scale(8)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a4e63eaf-ffbd-4c69-b32e-b179b7fdf95d",
"metadata": {},
"outputs": [],
"source": [
"# get a client - this tells Dask to use the cluster you've just created\n",
"client = cluster.get_client()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "b513edaa-3fef-48e0-8333-74732e51f1b4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1.00095105, 0.99624698, 1.00024171, ..., 1.00190085, 1.00039146,\n",
" 0.99803349])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# now we can use Dask!\n",
"import dask.array as da\n",
"x = da.random.random((50000, 50000), chunks=(1000, 1000))\n",
"y = x + x.T\n",
"z = y[::2, 5000:].mean(axis=1)\n",
"z.compute()"
]
},
{
"cell_type": "markdown",
"id": "e23c294f-b6b7-4dff-95a0-c83a23bc02d3",
"metadata": {},
"source": [
"**When you've finished using the cluster, scale it back down to zero.**"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "87ef4049-2c96-4256-bbb2-494dc84fa3d0",
"metadata": {},
"outputs": [],
"source": [
"cluster.scale(0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de4c410b-dcc8-47ad-8b6b-2c05b58f22a7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:abafar-20210701a]",
"language": "python",
"name": "conda-env-abafar-20210701a-py"
},
"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.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment