Skip to content

Instantly share code, notes, and snippets.

@lebedov
Created April 1, 2011 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lebedov/898288 to your computer and use it in GitHub Desktop.
Save lebedov/898288 to your computer and use it in GitHub Desktop.
IPython profile to automatically initialize PyCUDA on a specified device
#!/usr/bin/env python
"""
IPython 0.10 profile to automatically initialize PyCUDA on a specified
device.
"""
import sys
import atexit
import IPython.ipapi
ip = IPython.ipapi.get()
ip.ex("import numpy as np")
ip.ex("import atexit")
ip.ex("import pycuda.driver as drv")
ip.ex("drv.init()")
dev_num_str = sys.argv[-1] if sys.argv[-1].isdigit() else '0'
ip.ex("dev = drv.Device(%s)" % dev_num_str)
ip.ex("atexit.register(dev.make_context().pop)")
ip.ex("import pycuda.gpuarray as gpuarray")
ip.ex("print ")
ip.ex("print '*** pycuda initialized on ' + dev.name() + ' [%s] ***'" % dev_num_str)
ip.ex("print ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment