Skip to content

Instantly share code, notes, and snippets.

@matthewcarbone
Created June 28, 2018 15:14
Show Gist options
  • Save matthewcarbone/4b1b3c948aee503f2124d26e30a8d37c to your computer and use it in GitHub Desktop.
Save matthewcarbone/4b1b3c948aee503f2124d26e30a8d37c to your computer and use it in GitHub Desktop.
Basic usage of GPU code on a cluster

Step 1: Generate a virtual environment using Anaconda. conda create -n tensorflow pip python=3.3

Step 2: Within that virtual environment on the login node, install the tensorflow-gpu package. My VI is called 'tensorflow'. conda install -n tensorflow anaconda tensorflow-gpu

Step 3: Make tf_gpu_test.py executable. chmod +x tf_gpu_test.py

Step 4: Run the SLURM script. sbatch submit.sbatch

#!/bin/bash
#SBATCH -p debug
#SBATCH --output=j.out
#SBATCH --error=j.err
#SBATCH -t 00:01:00
#SBATCH -A your_acct_number_here
#SBATCH -N 1
#SBATCH -n 1 # not sure if this matters for pure gpu runs
#SBATCH -C tesla # or 'pascal' or whatever your gpu is called
#SBATCH --gres=gpu:4 # or '--gres=gpu:2' if pascal above
#SBATCH --qos=normal
module load openmpi/1.10.2 # not really necessary for this run
module load gcc/5.3.0
srun ./tf_gpu_test.py
#!/usr/bin/env python3
"""
from the tensorflow documentation
https://www.tensorflow.org/programmers_guide/using_gpu
make executable with
chmod +x tf_gpu_test.py
"""
import tensorflow as tf
# Creates a graph.
with tf.device('/gpu:0'):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with allow_soft_placement and log_device_placement set
# to True.
sess = tf.Session(config=tf.ConfigProto(
allow_soft_placement=False, log_device_placement=True))
# Runs the op.
print(sess.run(c))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment