{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "CPU_GPU_tensorflow.ipynb", "provenance": [], "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "accelerator": "GPU" }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "code", "metadata": { "id": "0_vRX29fPmEM", "colab_type": "code", "colab": {} }, "source": [ "from __future__ import absolute_import, division, print_function, unicode_literals\n", "import tensorflow as tf" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "d_j-EXTFQyqN", "colab_type": "code", "outputId": "33bc5997-c07f-44bc-8a21-4141add9cc75", "colab": { "base_uri": "https://localhost:8080/", "height": 87 } }, "source": [ "tf.config.experimental.list_physical_devices()" ], "execution_count": 10, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),\n", " PhysicalDevice(name='/physical_device:XLA_CPU:0', device_type='XLA_CPU'),\n", " PhysicalDevice(name='/physical_device:XLA_GPU:0', device_type='XLA_GPU'),\n", " PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]" ] }, "metadata": { "tags": [] }, "execution_count": 10 } ] }, { "cell_type": "code", "metadata": { "id": "uQ3DeAzfRZba", "colab_type": "code", "outputId": "277de6e5-e5dd-4642-8fe6-41f44cecc68a", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "len(tf.config.experimental.list_physical_devices('GPU'))" ], "execution_count": 11, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1" ] }, "metadata": { "tags": [] }, "execution_count": 11 } ] }, { "cell_type": "code", "metadata": { "id": "oTTpI27rSUPN", "colab_type": "code", "outputId": "02ece91e-897c-4e9a-ce3e-499ccd8701d0", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "tf.debugging.set_log_device_placement(True)\n", "\n", "# Place tensors on the CPU\n", "with tf.device('/CPU:0'):\n", " a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])\n", " b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])\n", "\n", "c = tf.matmul(a, b)\n", "print(c)" ], "execution_count": 12, "outputs": [ { "output_type": "stream", "text": [ "Tensor(\"MatMul_1:0\", shape=(2, 2), dtype=float32)\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "qiCeKR4HR1OM", "colab_type": "code", "outputId": "47011f4d-6e59-4be4-9652-f42da1cc8c43", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "gpus = tf.config.experimental.list_physical_devices('GPU')\n", "if gpus:\n", " # Create 2 virtual GPUs with 512MB memory each\n", " try:\n", " tf.config.experimental.set_virtual_device_configuration(\n", " gpus[0],\n", " [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=512),\n", " tf.config.experimental.VirtualDeviceConfiguration(memory_limit=512)])\n", " logical_gpus = tf.config.experimental.list_logical_devices('GPU')\n", " print(len(gpus), \"Physical GPU,\", len(logical_gpus), \"Logical GPUs\")\n", " except RuntimeError as e:\n", " # Virtual devices must be set before GPUs have been initialized\n", " print(e)" ], "execution_count": 13, "outputs": [ { "output_type": "stream", "text": [ "1 Physical GPU, 2 Logical GPUs\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "JjNGiv2STVjO", "colab_type": "code", "outputId": "2858de66-d5b1-432b-fc27-41c4ce8e3de1", "colab": { "base_uri": "https://localhost:8080/", "height": 87 } }, "source": [ "import time\n", "\n", "cpu_slot = 0\n", "gpu_slot = 0\n", "\n", "# Using CPU at slot 0\n", "with tf.device('/CPU:' + str(cpu_slot)):\n", " # Starting a timer\n", " start = time.time()\n", "\n", " # Doing operations on CPU\n", " A = tf.constant([[3, 2], [5, 2]])\n", " print(tf.eye(2,2))\n", "\n", " # Printing how long it took with CPU\n", " end = time.time() - start\n", " print(end)\n", "\n", "# Using the GPU at slot 0\n", "with tf.device('/GPU:' + str(gpu_slot)):\n", " # Starting a timer\n", " start = time.time()\n", "\n", " # Doing operations on GPU\n", " A = tf.constant([[3, 2], [5, 2]])\n", " print(tf.eye(2,2))\n", "\n", " # Printing how long it took with GPU\n", " end = time.time() - start\n", " print(end)" ], "execution_count": 14, "outputs": [ { "output_type": "stream", "text": [ "Tensor(\"eye_2/diag:0\", shape=(2, 2), dtype=float32, device=/device:CPU:0)\n", "0.002489328384399414\n", "Tensor(\"eye_3/diag:0\", shape=(2, 2), dtype=float32, device=/device:GPU:0)\n", "0.0018601417541503906\n" ], "name": "stdout" } ] } ] }