Skip to content

Commit

Permalink
bump version, merge pull request #21 from AMYPAD/simplify-build
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl authored Jan 6, 2022
2 parents 022a17f + 7c0ee23 commit 18279f5
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python: [3.6, 3.9]
python: [3.6, '3.10']
steps:
- uses: actions/checkout@v2
with:
Expand All @@ -69,7 +69,7 @@ jobs:
runs-on: [self-hosted, cuda, python]
strategy:
matrix:
python: [3.6, 3.9]
python: [3.6, '3.10']
steps:
- uses: actions/checkout@v2
with:
Expand Down
1 change: 1 addition & 0 deletions cuvec/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")

option(CUVEC_DEBUG "Print out CUDA malloc & free operations" OFF)
if(CUVEC_DEBUG)
add_compile_definitions(CUVEC_DEBUG)
Expand Down
4 changes: 2 additions & 2 deletions cuvec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# classes
'CuVec',
# functions
'dev_sync', 'cu_copy', 'cu_zeros',
'dev_set', 'dev_sync', 'cu_copy', 'cu_zeros',
'copy', 'asarray',
'zeros', 'ones', 'zeros_like', 'ones_like',
# data
Expand All @@ -33,7 +33,7 @@
from importlib import resources

try:
from .cuvec import dev_sync
from .cuvec import dev_set, dev_sync
except ImportError as err: # pragma: no cover
from warnings import warn
warn(str(err), UserWarning)
Expand Down
26 changes: 24 additions & 2 deletions cuvec/include/pycuvec.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,27 @@ _PYCUVEC_TPCHR(_CUVEC_HALF, "e", "e");
#endif
_PYCUVEC_TPCHR(float, "f", "f");
_PYCUVEC_TPCHR(double, "d", "d");
#ifndef CUVEC_DISABLE_CUDA
/// returns `0` iff last CUDA operation was a success; `-1` and sets exception otherwise
static int PyHandleError(cudaError_t err, const char *file, int line) {
if (err != cudaSuccess) {
std::stringstream ss;
ss << file << ':' << line << ": " << cudaGetErrorString(err);
std::string s = ss.str();
PyErr_SetString(PyExc_ValueError, s.c_str());
return -1;
}
return 0;
}
#endif // CUVEC_DISABLE_CUDA
} // namespace cuvec
#ifndef CUDA_PyErr
#ifdef CUVEC_DISABLE_CUDA
#define CUDA_PyErr() (0)
#else // CUVEC_DISABLE_CUDA
#define CUDA_PyErr() (cuvec::PyHandleError(cudaGetLastError(), __FILE__, __LINE__))
#endif // CUVEC_DISABLE_CUDA
#endif // CUDA_PyErr

/** classes */
/// class PyCuVec<T>
Expand Down Expand Up @@ -86,7 +106,7 @@ template <class T> int PyCuVec_init(PyCuVec<T> *self, PyObject *args, PyObject *
self->strides[ndim - 1] = (Py_ssize_t)sizeof(T);
for (int i = ndim - 2; i >= 0; i--) self->strides[i] = self->shape[i + 1] * self->strides[i + 1];
self->vec.resize(self->shape[0] * (self->strides[0] / sizeof(T)));
return 0;
return CUDA_PyErr();
}
/// __del__
template <class T> void PyCuVec_dealloc(PyCuVec<T> *self) {
Expand Down Expand Up @@ -213,12 +233,13 @@ template <class T> PyCuVec<T> *PyCuVec_zeros(std::vector<Py_ssize_t> shape) {
self->strides[ndim - 1] = (Py_ssize_t)sizeof(T);
for (int i = ndim - 2; i >= 0; i--) self->strides[i] = self->shape[i + 1] * self->strides[i + 1];
self->vec.resize(self->shape[0] * (self->strides[0] / sizeof(T)));
return self;
return CUDA_PyErr() ? NULL : self;
}
template <class T> PyCuVec<T> *PyCuVec_zeros_like(PyCuVec<T> *other) {
PyCuVec<T> *self = PyCuVec_new<T>();
if (!self) return NULL;
self->vec.resize(other->vec.size());
if (CUDA_PyErr()) return NULL;
self->shape = other->shape;
self->strides = other->strides;
return self;
Expand All @@ -227,6 +248,7 @@ template <class T> PyCuVec<T> *PyCuVec_deepcopy(PyCuVec<T> *other) {
PyCuVec<T> *self = PyCuVec_new<T>();
if (!self) return NULL;
self->vec = other->vec;
if (CUDA_PyErr()) return NULL;
self->shape = other->shape;
self->strides = other->strides;
return self;
Expand Down
14 changes: 13 additions & 1 deletion cuvec/src/cuvec.cu
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@
static PyObject *dev_sync(PyObject *self, PyObject *args) {
#ifndef CUVEC_DISABLE_CUDA
cudaDeviceSynchronize();
if (CUDA_PyErr()) return NULL;
#endif
Py_INCREF(Py_None);
return Py_None;
}
/// select CUDA device
static PyObject *dev_set(PyObject *self, PyObject *args) {
#ifndef CUVEC_DISABLE_CUDA
int DEVID = 0;
if (!PyArg_ParseTuple(args, "i", &DEVID)) return NULL;
cudaSetDevice(DEVID);
if (CUDA_PyErr()) return NULL;
#endif

Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef cuvec_methods[] = {
{"dev_sync", dev_sync, METH_NOARGS, "Required before accessing cuvec on host."},
{"dev_set", dev_set, METH_VARARGS, "Select CUDA device."},
{NULL, NULL, 0, NULL} // Sentinel
};

Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ author=Casper da Costa-Luis
author_email[email protected]
keywords=Python, C, C++, buffer, vector, array, CUDA, CPython, SWIG, extensions, API
classifiers=
Development Status :: 4 - Beta
Development Status :: 5 - Production/Stable
Intended Audience :: Developers
Environment :: GPU
Environment :: GPU :: NVIDIA CUDA
Expand All @@ -29,6 +29,7 @@ classifiers=
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3 :: Only
Topic :: Software Development :: Libraries
Topic :: Software Development :: Libraries :: Python Modules
Expand Down
24 changes: 9 additions & 15 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,28 @@
import sys
from pathlib import Path

from setuptools import find_packages, setup
from setuptools import find_packages
from setuptools_scm import get_version
from skbuild import setup

__version__ = get_version(root=".", relative_to=__file__)

logging.basicConfig(level=logging.INFO)
log = logging.getLogger("cuvec.setup")

build_ver = ".".join(__version__.split('.')[:3]).split(".dev")[0]
setup_kwargs = {"use_scm_version": True, "packages": find_packages(exclude=["docs", "tests"])}
cmake_args = [f"-DCUVEC_BUILD_VERSION={build_ver}", f"-DPython3_ROOT_DIR={sys.prefix}"]

try:
from miutil import cuinfo
nvcc_arch_raw = map(cuinfo.compute_capability, range(cuinfo.num_devices()))
nvcc_arches = {"%d%d" % i for i in nvcc_arch_raw if i >= (3, 5)}
if nvcc_arches:
cmake_args.append("-DCMAKE_CUDA_ARCHITECTURES=" + ";".join(sorted(nvcc_arches)))
except Exception as exc:
log.warning("Import or CUDA device detection error:\n%s", exc)

try:
from skbuild import setup as sksetup
except ImportError:
log.warning("`skbuild.setup` not found: Using `setuptools.setup`")
setup(**setup_kwargs) # type: ignore # yapf: disable
else:
for i in (Path(__file__).resolve().parent / "_skbuild").rglob("CMakeCache.txt"):
i.write_text(re.sub("^//.*$\n^[^#].*pip-build-env.*$", "", i.read_text(), flags=re.M))
sksetup(cmake_source_dir="cuvec", cmake_minimum_required_version="3.18", cmake_args=cmake_args,
**setup_kwargs)
if "sdist" not in sys.argv or any(i in sys.argv for i in ["build", "bdist", "wheel"]):
log.warning("Import or CUDA device detection error:\n%s", exc)
for i in (Path(__file__).resolve().parent / "_skbuild").rglob("CMakeCache.txt"):
i.write_text(re.sub("^//.*$\n^[^#].*pip-build-env.*$", "", i.read_text(), flags=re.M))
setup(use_scm_version=True, packages=find_packages(exclude=["docs", "tests"]),
cmake_source_dir="cuvec", cmake_languages=("C", "CXX"),
cmake_minimum_required_version="3.18", cmake_args=cmake_args)

0 comments on commit 18279f5

Please sign in to comment.