Skip to content

Commit

Permalink
Merge pull request openucx#31 from TomAugspurger/default-address
Browse files Browse the repository at this point in the history
REF/ENH: Add function for getting the address of an interface
  • Loading branch information
TomAugspurger authored Feb 18, 2019
2 parents d64ac17 + 735e561 commit 3ee5b82
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 20 deletions.
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
*.*~
pybind/build
pybind/ucp_py.c
pybind/ucp_py.*.so
pybind/*.a
pybind/*.o
build
ucp_py/_libs/ucp_py.c
*.so
ucp_py/_libs/*.a
ucp_py/_libs/*.o

dask-worker-space
__pytestcache__
Expand Down
13 changes: 6 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ UCX_PY_CUDA_PATH ?= /usr/local/cuda
CPPFLAGS := -I$(UCX_PY_CUDA_PATH)/include -I$(UCX_PY_UCX_PATH)/include
LDFLAGS := -L$(UCX_PY_CUDA_PATH)/lib64 -lcuda -L$(UCX_PY_UCX_PATH)/lib -lucp -luct -lucm -lucs

pybind/libucp_py_ucp_fxns.a: pybind/ucp_py_ucp_fxns.o pybind/buffer_ops.o
ucp_py/_libs/libucp_py_ucp_fxns.a: ucp_py/_libs/ucp_py_ucp_fxns.o ucp_py/_libs/buffer_ops.o
ar rcs $@ $^

pybind/%.o: pybind/%.c
ucp_py/_libs/%.o: ucp_py/_libs/%.c
$(CC) -shared -fPIC $(CPPFLAGS) -c $^ -o $@ $(LDFLAGS)

install: pybind/libucp_py_ucp_fxns.a
cd pybind && \
install: ucp_py/_libs/libucp_py_ucp_fxns.a
python3 setup.py build_ext && \
python3 -m pip install -e .

clean:
rm pybind/*.o pybind/*.a
rm -rf pybind/build pybind/ucx_py.egg-info
rm pybind/ucp_py.c
rm ucp_py/_libs/*.o ucp_py/_libs/*.a
rm -rf ucp_py/_libs/build ucp_py/_libs/ucx_py.egg-info
rm ucp_py/_libs/ucp_py.c
8 changes: 8 additions & 0 deletions pybind/code-organization.md → doc/code-organization.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,11 @@ character. `check_*_buffer` methods return the number of mismatches in
the buffers with the character provided. `set_*_buffer` methods have
not be tested when python objects are assoicated with
**buffer_region's** *data_buf* structure.

## Tests

To run the automated tests

```
$ python3 -m pytest tests
```
19 changes: 11 additions & 8 deletions pybind/setup.py → setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,25 @@
sys.exit(1)


if not os.path.exists("libucp_py_ucp_fxns.a"):
assert os.system("gcc -shared -fPIC -c ucp_py_ucp_fxns.c -o ucp_py_ucp_fxns.o") == 0
assert os.system("gcc -shared -fPIC -c buffer_ops.c -o buffer_ops.o") == 0
assert os.system("ar rcs libucp_py_ucp_fxns.a ucp_py_ucp_fxns.o buffer_ops.o") == 0
prefix = "ucp_py/_libs"

if not os.path.exists(f"{prefix}/libucp_py_ucp_fxns.a"):
assert os.system(f"gcc -shared -fPIC -c {prefix}/ucp_py_ucp_fxns.c -o {prefix}/ucp_py_ucp_fxns.o") == 0
assert os.system(f"gcc -shared -fPIC -c {prefix}/buffer_ops.c -o {prefix}/buffer_ops.o") == 0
assert os.system(f"ar rcs {prefix}/libucp_py_ucp_fxns.a {prefix}/ucp_py_ucp_fxns.o {prefix}/buffer_ops.o") == 0


ext_modules = cythonize([
Extension("ucp_py",
sources=["ucp_py.pyx"],
Extension("ucp_py._libs.ucp_py",
sources=["ucp_py/_libs/ucp_py.pyx"],
include_dirs=[os.getcwd(), UCX_DIR + '/include', CUDA_DIR + '/include'],
library_dirs=[os.getcwd(), UCX_DIR + '/lib', CUDA_DIR + '/lib64'],
runtime_library_dirs=[os.getcwd(), UCX_DIR + '/lib', CUDA_DIR + '/lib64'],
libraries=['ucp_py_ucp_fxns', 'ucp', 'uct', 'ucm', 'ucs', 'cuda', 'cudart']),
libraries=[f'{prefix}/ucp_py_ucp_fxns', 'ucp', 'uct', 'ucm', 'ucs', 'cuda', 'cudart']),
])

setup(
name='ucx_py',
ext_modules=ext_modules
packages=['ucp_py'],
ext_modules=ext_modules,
)
7 changes: 7 additions & 0 deletions tests/test_default_address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ucp_py as ucp


def test_get_address():
result = ucp.get_address()
assert isinstance(result, str)
assert '.' in result
2 changes: 2 additions & 0 deletions ucp_py/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from ._utils import get_address, sizeof
from ._libs.ucp_py import *
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
52 changes: 52 additions & 0 deletions ucp_py/_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import fcntl
import socket
import struct
import sys


def get_address(ifname='ib0'):
"""
Get the address associated with a network interface.
Parameters
----------
ifname : str, default ib0
The network interface name to find the address for.
By default, 'ib0' is used. An OSError is raised for
invalid interfaces.
Returns
-------
address : str
The inet addr associated with an interface.
Examples
--------
>>> get_address()
'10.33.225.160'
>>> get_address(ifname='lo')
'127.0.0.1'
"""
# https://stackoverflow.com/a/24196955/1889400
ifname = ifname.encode()

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])


def sizeof(obj):
"""
Get the size of an object, defined as
1. it's length
2. it's Python overhead
So this is appropriate for sequences where each element
is a byte (bytestrings or memoryviews).
"""
return len(obj) + sys.getsizeof(obj[:0])

0 comments on commit 3ee5b82

Please sign in to comment.