Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* build python3 packages of pycuda and pynvml
* add dependency - also for python3 server
* python3 string fixes, None vs int
* python3 dict keys aren't lists
* python3 needs to use temporary numpy array to upload bytearray

git-svn-id: https://xpra.org/svn/Xpra/trunk@16399 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Jul 18, 2017
1 parent cdf823e commit d873432
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 25 deletions.
73 changes: 69 additions & 4 deletions rpmbuild/python-pycuda.spec
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
%{!?__python2: %global __python2 /usr/bin/python2}
# This file is part of Xpra.
# Copyright (C) 2014-2017 Antoine Martin <[email protected]>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

%{!?__python2: %global __python2 python2}
%{!?__python3: %define __python3 python3}
%{!?python2_sitearch: %global python2_sitearch %(%{__python2} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")}
%{!?python3_sitearch: %global python3_sitearch %(%{__python3} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")}

#we don't want to depend on libcuda via RPM dependencies
#so that we can install NVidia drivers without using RPM packages:
%define __requires_exclude ^libcuda.*$

Name: python2-pycuda
Version: 2017.1
Release: 1
Release: 2
URL: http://mathema.tician.de/software/pycuda
Summary: Python wrapper CUDA
License: MIT
Expand All @@ -29,16 +36,42 @@ BuildRequires: python-setuptools
%else
BuildRequires: python-distribute
%endif
BuildRequires: boost-devel
BuildRequires: numpy
BuildRequires: boost-devel
BuildRequires: cuda


%description
PyCUDA lets you access Nvidia‘s CUDA parallel computation API from Python.


%if 0%{?fedora}
%package -n python3-pycuda
Summary: Python3 wrapper CUDA
License: MIT
Group: Development/Libraries/Python

Requires: python3-decorator
Requires: python3-numpy
Requires: python3-pytools

BuildRequires: gcc-c++
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-numpy
BuildRequires: boost-devel
BuildRequires: cuda

%description -n python3-pycuda
Python3 version.
%endif

%prep
%setup -q -n pycuda-%{version}
%if 0%{?fedora}
rm -fr %{py3dir}
cp -a . %{py3dir}
%endif


%build
%{__python2} ./configure.py \
Expand All @@ -52,10 +85,32 @@ PyCUDA lets you access Nvidia‘s CUDA parallel computation API from Python.
# --boost-python-libname=boost_python-mt \
# --boost-thread-libname=boost_thread
%{__python2} setup.py build
%if 0%{?fedora}
pushd %{py3dir}
%{__python3} ./setup.py clean
rm -f siteconf.py
%{__python3} ./configure.py \
--cuda-enable-gl \
--cuda-root=/usr/local/cuda \
--cudadrv-lib-dir=/usr/local/lib64 \
--cudadrv-lib-dir=%{_libdir} \
--boost-inc-dir=%{_includedir} \
--boost-lib-dir=%{_libdir} \
--no-cuda-enable-curand
# --boost-python-libname=boost_python-mt \
# --boost-thread-libname=boost_thread
%{__python3} setup.py build
popd
%endif
make

%install
%{__python2} setup.py install --prefix=%{_prefix} --root=%{buildroot}
%if 0%{?fedora}
pushd %{py3dir}
%{__python3} setup.py install --prefix=%{_prefix} --root=%{buildroot}
popd
%endif

%clean
rm -rf %{buildroot}
Expand All @@ -65,7 +120,17 @@ rm -rf %{buildroot}
%doc examples/ test/
%{python2_sitearch}/pycuda*

%if 0%{?fedora}
%files -n python3-pycuda
%defattr(-,root,root)
%doc examples/ test/
%{python3_sitearch}/pycuda*
%endif

%changelog
* Tue Jul 18 2017 Antoine Martin <[email protected]> - 2017.1-2
- build python3 variant too

* Thu Jun 01 2017 Antoine Martin <[email protected]> - 2017.1-1
- new upstream release

Expand Down
47 changes: 43 additions & 4 deletions rpmbuild/python-pynvml.spec
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
%{!?__python2: %global __python2 /usr/bin/python2}
%{!?python2_sitelib: %global python2_sitelib %(%{__python2} -c "from distutils.sysconfig import get_python_lib; print get_python_lib()")}
# This file is part of Xpra.
# Copyright (C) 2015-2017 Antoine Martin <[email protected]>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

%{!?__python2: %global __python2 python2}
%{!?__python3: %define __python3 python3}
%{!?python2_sitearch: %global python2_sitearch %(%{__python2} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")}
%{!?python3_sitearch: %global python3_sitearch %(%{__python3} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")}

#this is a pure python package so debug is meaningless here:
%define debug_package %{nil}

Name: python2-pynvml
Version: 7.352.0
Release: 1
Release: 2
URL: http://pythonhosted.org/nvidia-ml-py/
Summary: Python wrapper for NVML
License: BSD
Expand All @@ -15,18 +22,37 @@ Source: https://pypi.python.org/packages/72/31/378ca145e919ca415641a0f17
BuildRoot: %{_tmppath}/%{name}-%{version}-build
Provides: python-pynvml


%description
Python Bindings for the NVIDIA Management Library

%if 0%{?fedora}
%package -n python3-pynvml
Summary: Python3 wrapper for NVML
License: BSD
Group: Development/Libraries/Python

%description -n python3-pynvml
Python Bindings for the NVIDIA Management Library
%endif

%prep
%setup -q -n nvidia-ml-py-%{version}

%build
%{__python2} ./setup.py build
%if 0%{?fedora}
rm -fr %{py3dir}
cp -r . %{py3dir}
find %{py3dir} -name "*.py" -exec 2to3 -w {} \;
%endif

%install
%{__python2} ./setup.py install --prefix=%{_prefix} --root=%{buildroot}
%if 0%{?fedora}
pushd %{py3dir}
%{__python3} ./setup.py install --prefix=%{_prefix} --root=%{buildroot}
popd
%endif

%clean
rm -rf %{buildroot}
Expand All @@ -37,7 +63,20 @@ rm -rf %{buildroot}
%{python2_sitelib}/nvidia_smi.py*
%{python2_sitelib}/nvidia_ml_py-%{version}-py*.egg-info

%if 0%{?fedora}
%files -n python3-pynvml
%defattr(-,root,root)
%{python3_sitelib}/__pycache__/nvidia*
%{python3_sitelib}/__pycache__/pynvml*
%{python3_sitelib}/pynvml.py*
%{python3_sitelib}/nvidia_smi.py*
%{python3_sitelib}/nvidia_ml_py-%{version}-py*.egg-info
%endif

%changelog
* Tue Jul 18 2017 Antoine Martin <[email protected]> - 7.352.0-2
- build python3 variant too

* Mon Aug 29 2016 Antoine Martin <[email protected]> - 7.352.0-1
- build newer version

Expand Down
11 changes: 10 additions & 1 deletion rpmbuild/xpra.spec
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file is part of Xpra.
# Copyright (C) 2010-2016 Antoine Martin <[email protected]>
# Copyright (C) 2010-2017 Antoine Martin <[email protected]>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

Expand Down Expand Up @@ -317,7 +317,10 @@ BuildRequires: pam-devel
BuildRequires: gcc
BuildRequires: %{requires_cython}
%ifarch x86_64
%if 0%{?fedora}
%{Recommends}: python2-pynvml
%{Recommends}: python2-pycuda
%endif
%endif
%description -n python2-xpra-server
This package contains the python2 xpra server.
Expand Down Expand Up @@ -413,6 +416,12 @@ Recommends: gtk3-immodule-xim
Recommends: python3-setproctitle
BuildRequires: gcc, gcc-c++
BuildRequires: python3-Cython
%ifarch x86_64
%if 0%{?fedora}
%{Recommends}: python3-pynvml
%{Recommends}: python3-pycuda
%endif
%endif
%description -n python3-xpra-server
This package contains the python3 xpra server.
%endif
Expand Down
2 changes: 1 addition & 1 deletion src/xpra/codecs/cuda_common/cuda_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def select_device(preferred_device_id=-1, preferred_device_name=None, min_comput
preferred_device_name = get_pref("device-name")
if preferred_device_id<0:
device_id = get_pref("device-id")
if device_id>=0:
if device_id is not None and device_id>=0:
preferred_device_id = device_id
devices = init_all_devices()
global DEVICE_STATE
Expand Down
8 changes: 4 additions & 4 deletions src/xpra/codecs/nv_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_nvml_driver_version():
nvmlInit()
v = nvmlSystemGetDriverVersion()
log("nvmlSystemGetDriverVersion=%s", v)
return v.split(".")
return v.split(b".")
except Exception as e:
log("get_nvml_driver_version() pynvml error", exc_info=True)
log.warn("Warning: failed to query the NVidia kernel module version via NVML:")
Expand All @@ -41,13 +41,13 @@ def get_proc_driver_version():
log.warn("Warning: NVidia kernel module not installed?")
log.warn(" cannot open '%s'", proc_file)
return ""
KSTR = "Kernel Module"
KSTR = b"Kernel Module"
p = v.find(KSTR)
if not p:
log.warn("unknown NVidia kernel module version")
return ""
v = v[p+len(KSTR):].strip().split(" ")[0]
v = v.split(".")
v = v[p+len(KSTR):].strip().split(b" ")[0]
v = v.split(b".")
return v


Expand Down
33 changes: 22 additions & 11 deletions src/xpra/codecs/nvenc/encoder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ from collections import deque

from pycuda import driver

from xpra.os_util import WIN32, OSX, LINUX
from xpra.os_util import WIN32, OSX, LINUX, PYTHON3, bytestostr
from xpra.util import AtomicInteger, engs, csv, pver, envint, envbool
from xpra.codecs.cuda_common.cuda_context import init_all_devices, get_devices, select_device, \
get_cuda_info, get_pycuda_info, device_info, reset_state, \
Expand Down Expand Up @@ -1044,11 +1044,11 @@ cdef guidstr(GUID guid):
b = array.array('B', [0 for _ in range(s)])
for j in range(s):
b[s-j-1] = v % 256
v = v / 256
v = v // 256
parts.append(b.tostring())
parts.append(array.array('B', (guid.get("Data4")[:2])).tostring())
parts.append(array.array('B', (guid.get("Data4")[2:8])).tostring())
s = "-".join([binascii.hexlify(b).upper() for b in parts])
s = b"-".join(binascii.hexlify(b).upper() for b in parts)
#log.info("guidstr(%s)=%s", guid, s)
return s

Expand All @@ -1058,22 +1058,29 @@ cdef GUID c_parseguid(src) except *:
sample_guid = "CE788D20-AAA9-4318-92BB-AC7E858C8D36"
if len(src)!=len(sample_guid):
raise Exception("invalid GUID format: expected %s characters but got %s" % (len(sample_guid), len(src)))
cdef int i, s
src = bytestostr(src)
for i in range(len(sample_guid)):
if sample_guid[i]=="-":
#dash must be in the same place:
if src[i]!="-":
raise Exception("invalid GUID format: character at position %s is not '-'" % i)
raise Exception("invalid GUID format: character at position %s is not '-': %s" % (i, src[i]))
else:
#must be an hex number:
if src.upper()[i] not in ("0123456789ABCDEF"):
raise Exception("invalid GUID format: character at position %s is not in hex" % i)
c = src.upper()[i]
if c not in (u"0123456789ABCDEF"):
raise Exception("invalid GUID format: character at position %s is not in hex: %s" % (i, c))
parts = src.split("-") #ie: ["CE788D20", "AAA9", ...]
nparts = []
for i, s in (0, 4), (1, 2), (2, 2), (3, 2), (4, 6):
b = array.array('B', (binascii.unhexlify(parts[i]))).tostring()
v = 0
for j in range(s):
v += ord(b[j])<<((s-j-1)*8)
if PYTHON3:
c = b[j]
else:
c = ord(c)
v += c<<((s-j-1)*8)
nparts.append(v)
cdef GUID guid
guid.Data1 = nparts[0]
Expand All @@ -1088,7 +1095,7 @@ def parseguid(s):
return c_parseguid(s)

def test_parse():
sample_guid = "CE788D20-AAA9-4318-92BB-AC7E858C8D36"
sample_guid = b"CE788D20-AAA9-4318-92BB-AC7E858C8D36"
x = c_parseguid(sample_guid)
v = guidstr(x)
assert v==sample_guid, "expected %s but got %s" % (sample_guid, v)
Expand Down Expand Up @@ -1223,7 +1230,7 @@ def get_COLORSPACES(encoding):
return COLORSPACES

def get_input_colorspaces(encoding):
return get_COLORSPACES(encoding).keys()
return list(get_COLORSPACES(encoding).keys())

def get_output_colorspaces(encoding, input_colorspace):
cs = get_COLORSPACES(encoding)
Expand Down Expand Up @@ -2184,7 +2191,11 @@ cdef class Encoder:
stride = image_stride
#assert pix_len<=input_size, "too many pixels (expected %s max, got %s) image: %sx%s stride=%s, input buffer: stride=%s, height=%s" % (input_size, pix_len, w, h, stride, self.inputPitch, self.input_height)
log("copying %s bytes from %s into %s (len=%i), in one shot", pix_len, type(pixels), type(target_buffer), len(target_buffer))
buf[:pix_len] = pixels
if PYTHON3 and isinstance(pixels, bytearray):
tmp = numpy.frombuffer(pixels, numpy.int8)
buf[:pix_len] = tmp
else:
buf[:pix_len] = pixels
else:
#ouch, we need to copy the source pixels into the smaller buffer
#before uploading to the device... this is probably costly!
Expand Down Expand Up @@ -2429,7 +2440,7 @@ cdef class Encoder:
else:
presets[preset_name] = guidstr(preset_GUID)
if len(unknowns)>0:
log.warn("nvenc: found some unknown presets: %s", ", ".join(unknowns))
log.warn("nvenc: found some unknown presets: %s", b", ".join(unknowns))
finally:
free(preset_GUIDs)
if DEBUG_API:
Expand Down

0 comments on commit d873432

Please sign in to comment.