Skip to content

Instantly share code, notes, and snippets.

View lebedov's full-sized avatar

Lev E. Givon lebedov

View GitHub Profile
@lebedov
lebedov / p2p_pycuda_ipc_demo.py
Created October 8, 2014 15:31
Allocate a GPUArray on one GPU in one process and copy it to some other GPU in another process using IPC handles.
#!/usr/bin/env python
"""
Allocate a GPUArray on one GPU in one process and copy it to
some other GPU in another process using IPC handles.
Notes
-----
Requires that the two GPUs support peer-to-peer data transfers.
"""
@lebedov
lebedov / sync_pub_sub.py
Last active August 29, 2015 14:09
ZeroMQ asynchronous socket pair synchronizer.
#!/usr/bin/env python
"""
Synchronize PUB/SUB sockets.
"""
import multiprocessing as mp
import time
import zmq
@lebedov
lebedov / mpi4py_msgpack_demo.py
Created December 25, 2014 04:04
How to use mpi4py with msgpack serialization
#!/usr/bin/env python
"""
How to use mpi4py with msgpack serialization
"""
from mpi4py import MPI
import msgpack
# mpi4py assumes that the serializer function takes more than one parameter:
@lebedov
lebedov / profile_decorator.py
Created January 2, 2015 15:05
Decorator for profiling functions.
import inspect
import cProfile as profile
import wrapt
def prof(*dec_args, **dec_kwargs):
pr = profile.Profile()
if not dec_args or not inspect.isfunction(dec_args[0]):
@wrapt.decorator
def wrapper(wrapped, instance, args, kwargs):
pr.enable()
@lebedov
lebedov / ipython_config.py
Created January 29, 2015 18:19
Show virtualenv or conda environment in IPython prompt
# Put this in your IPython profile configuration, e.g., ~/.ipython/profile_default/ipython_config.py
import os
extra = ''
if os.environ.has_key('VIRTUAL_ENV'):
v = os.path.basename(os.environ['VIRTUAL_ENV'])
extra += '<%s> ' % v
if os.environ.has_key('CONDA_DEFAULT_ENV'):
extra += '[%s] ' % os.environ['CONDA_DEFAULT_ENV']
@lebedov
lebedov / indexer_attrib_demo.py
Created March 4, 2015 05:28
How to create a class with an attribute that provides its own __getitem__() method.
#!/usr/bin/env python
"""
How to create a class with an attribute that provides its own __getitem__()
method (similar to pandas.DataFrame.ix).
"""
class Indexer(object):
def __init__(self, data):
if not hasattr(data, '__getitem__') or not hasattr(data, '__setitem__'):
@lebedov
lebedov / allglobalvars.py
Last active August 29, 2015 14:16
How to find globals accessed by a Python object.
#!/usr/bin/env python
"""
How to find globals accessed by a Python object.
"""
import inspect
import numpy as np
@lebedov
lebedov / mpi4py_spawn_twiggy.py
Created March 4, 2015 15:32
How to use twiggy for logging in both parent and MPI-spawned processes.
#!/usr/bin/bin python
"""
How to use twiggy for logging in both parent and MPI-spawned processes.
"""
import sys
# Need to use dill to serialize the twiggy emitters because pickle can't handle
# them:
@lebedov
lebedov / queue_group.py
Created March 11, 2015 19:50
Class for managing a group of queues with per-queue push and simultaneous pop
#!/usr/bin/env python
"""
Group of queues; data can be pushed/popped into each queue separately, but one
can also pop off values from all of the queues simultaneously.
+-------+
queue a: in -> | |A|B|
+-------+ out -> (B,C)
queue b: in -> | |C|
@lebedov
lebedov / ml_syn.py
Created March 3, 2015 21:59
Morris-Lecar neurons connected by a conductance-based synapse.
#!/usr/bin/env python
"""
Morris-Lecar neurons connected by a conductance-based synapse.
"""
import numpy as np
import matplotlib
matplotlib.use('agg')