|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Basic Multi GPU Computation in TensorFlow\n", |
| 8 | + "\n", |
| 9 | + "Credits: Forked from [TensorFlow-Examples](https://github.com/aymericdamien/TensorFlow-Examples) by Aymeric Damien\n", |
| 10 | + "\n", |
| 11 | + "## Setup\n", |
| 12 | + "\n", |
| 13 | + "Refer to the [setup instructions](http://nbviewer.ipython.org/github/donnemartin/data-science-ipython-notebooks/blob/master/deep-learning/tensor-flow-examples/Setup_TensorFlow.md)" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "markdown", |
| 18 | + "metadata": { |
| 19 | + "collapsed": true |
| 20 | + }, |
| 21 | + "source": [ |
| 22 | + "This tutorial requires your machine to have 2 GPUs\n", |
| 23 | + "* \"/cpu:0\": The CPU of your machine.\n", |
| 24 | + "* \"/gpu:0\": The first GPU of your machine\n", |
| 25 | + "* \"/gpu:1\": The second GPU of your machine\n", |
| 26 | + "* For this example, we are using 2 GTX-980" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": 2, |
| 32 | + "metadata": { |
| 33 | + "collapsed": true |
| 34 | + }, |
| 35 | + "outputs": [], |
| 36 | + "source": [ |
| 37 | + "import numpy as np\n", |
| 38 | + "import tensorflow as tf\n", |
| 39 | + "import datetime" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": 3, |
| 45 | + "metadata": { |
| 46 | + "collapsed": true |
| 47 | + }, |
| 48 | + "outputs": [], |
| 49 | + "source": [ |
| 50 | + "#Processing Units logs\n", |
| 51 | + "log_device_placement = True\n", |
| 52 | + "\n", |
| 53 | + "#num of multiplications to perform\n", |
| 54 | + "n = 10" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": null, |
| 60 | + "metadata": { |
| 61 | + "collapsed": false |
| 62 | + }, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "# Example: compute A^n + B^n on 2 GPUs\n", |
| 66 | + "\n", |
| 67 | + "# Create random large matrix\n", |
| 68 | + "A = np.random.rand(1e4, 1e4).astype('float32')\n", |
| 69 | + "B = np.random.rand(1e4, 1e4).astype('float32')\n", |
| 70 | + "\n", |
| 71 | + "# Creates a graph to store results\n", |
| 72 | + "c1 = []\n", |
| 73 | + "c2 = []\n", |
| 74 | + "\n", |
| 75 | + "# Define matrix power\n", |
| 76 | + "def matpow(M, n):\n", |
| 77 | + " if n < 1: #Abstract cases where n < 1\n", |
| 78 | + " return M\n", |
| 79 | + " else:\n", |
| 80 | + " return tf.matmul(M, matpow(M, n-1))" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": 6, |
| 86 | + "metadata": { |
| 87 | + "collapsed": true |
| 88 | + }, |
| 89 | + "outputs": [], |
| 90 | + "source": [ |
| 91 | + "# Single GPU computing\n", |
| 92 | + "\n", |
| 93 | + "with tf.device('/gpu:0'):\n", |
| 94 | + " a = tf.constant(A)\n", |
| 95 | + " b = tf.constant(B)\n", |
| 96 | + " #compute A^n and B^n and store results in c1\n", |
| 97 | + " c1.append(matpow(a, n))\n", |
| 98 | + " c1.append(matpow(b, n))\n", |
| 99 | + "\n", |
| 100 | + "with tf.device('/cpu:0'):\n", |
| 101 | + " sum = tf.add_n(c1) #Addition of all elements in c1, i.e. A^n + B^n\n", |
| 102 | + "\n", |
| 103 | + "t1_1 = datetime.datetime.now()\n", |
| 104 | + "with tf.Session(config=tf.ConfigProto(log_device_placement=log_device_placement)) as sess:\n", |
| 105 | + " # Runs the op.\n", |
| 106 | + " sess.run(sum)\n", |
| 107 | + "t2_1 = datetime.datetime.now()" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": 7, |
| 113 | + "metadata": { |
| 114 | + "collapsed": true |
| 115 | + }, |
| 116 | + "outputs": [], |
| 117 | + "source": [ |
| 118 | + "# Multi GPU computing\n", |
| 119 | + "# GPU:0 computes A^n\n", |
| 120 | + "with tf.device('/gpu:0'):\n", |
| 121 | + " #compute A^n and store result in c2\n", |
| 122 | + " a = tf.constant(A)\n", |
| 123 | + " c2.append(matpow(a, n))\n", |
| 124 | + "\n", |
| 125 | + "#GPU:1 computes B^n\n", |
| 126 | + "with tf.device('/gpu:1'):\n", |
| 127 | + " #compute B^n and store result in c2\n", |
| 128 | + " b = tf.constant(B)\n", |
| 129 | + " c2.append(matpow(b, n))\n", |
| 130 | + "\n", |
| 131 | + "with tf.device('/cpu:0'):\n", |
| 132 | + " sum = tf.add_n(c2) #Addition of all elements in c2, i.e. A^n + B^n\n", |
| 133 | + "\n", |
| 134 | + "t1_2 = datetime.datetime.now()\n", |
| 135 | + "with tf.Session(config=tf.ConfigProto(log_device_placement=log_device_placement)) as sess:\n", |
| 136 | + " # Runs the op.\n", |
| 137 | + " sess.run(sum)\n", |
| 138 | + "t2_2 = datetime.datetime.now()" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": 8, |
| 144 | + "metadata": { |
| 145 | + "collapsed": false |
| 146 | + }, |
| 147 | + "outputs": [ |
| 148 | + { |
| 149 | + "name": "stdout", |
| 150 | + "output_type": "stream", |
| 151 | + "text": [ |
| 152 | + "Single GPU computation time: 0:00:11.833497\n", |
| 153 | + "Multi GPU computation time: 0:00:07.085913\n" |
| 154 | + ] |
| 155 | + } |
| 156 | + ], |
| 157 | + "source": [ |
| 158 | + "print \"Single GPU computation time: \" + str(t2_1-t1_1)\n", |
| 159 | + "print \"Multi GPU computation time: \" + str(t2_2-t1_2)" |
| 160 | + ] |
| 161 | + } |
| 162 | + ], |
| 163 | + "metadata": { |
| 164 | + "kernelspec": { |
| 165 | + "display_name": "Python 3", |
| 166 | + "language": "python", |
| 167 | + "name": "python3" |
| 168 | + }, |
| 169 | + "language_info": { |
| 170 | + "codemirror_mode": { |
| 171 | + "name": "ipython", |
| 172 | + "version": 3 |
| 173 | + }, |
| 174 | + "file_extension": ".py", |
| 175 | + "mimetype": "text/x-python", |
| 176 | + "name": "python", |
| 177 | + "nbconvert_exporter": "python", |
| 178 | + "pygments_lexer": "ipython3", |
| 179 | + "version": "3.4.3" |
| 180 | + } |
| 181 | + }, |
| 182 | + "nbformat": 4, |
| 183 | + "nbformat_minor": 0 |
| 184 | +} |
0 commit comments