Skip to content

Commit

Permalink
test the NVCC compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakob committed Sep 6, 2024
1 parent a251000 commit 94488a5
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 25 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,28 @@ jobs:
run: >
cd build;
python -m pytest
nvcc:
runs-on: ubuntu-latest
container: nvidia/cuda:12.2.0-devel-ubuntu22.04
name: "Python 3 / NVCC (CUDA 12.2)"

steps:
- name: Install dependencies
run: apt-get update && DEBIAN_FRONTEND="noninteractive" apt-get install -y cmake git python3-dev python3-pytest python3-numpy python3-pip libeigen3-dev && python3 -m pip install typing_extensions

- uses: actions/checkout@v4
with:
submodules: true

- name: Configure
run: >
cmake -S . -B build -DNB_TEST_CUDA=ON
- name: Build C++
run: VERBOSE=1 cmake --build build -j 2

- name: Run tests
run: >
cd build;
python -m pytest
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ option(NB_USE_SUBMODULE_DEPS "Use the nanobind dependencies shipped as a git s
option(NB_TEST "Compile nanobind tests?" ${NB_MASTER_PROJECT})
option(NB_TEST_STABLE_ABI "Test the stable ABI interface?" OFF)
option(NB_TEST_SHARED_BUILD "Build a shared nanobind library for the test suite?" OFF)
option(NB_TEST_CUDA "Force the use of the CUDA/NVCC compiler for testing purposes" OFF)

if (NOT MSVC)
option(NB_TEST_SANITZE "Build tests with address and undefined behavior sanitizers?" OFF)
Expand Down
48 changes: 33 additions & 15 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,45 @@ if (UNIX AND (CMAKE_SIZEOF_VOID_P EQUAL 4) AND (CMAKE_SYSTEM_PROCESSOR STREQUAL
add_compile_options(-mfpmath=sse -msse2)
endif()

# Enforce the use of the CUDA NVCC compiler if requested
if (NB_TEST_CUDA)
enable_language(CUDA)
set(CMAKE_CUDA_STANDARD 17)
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
endif()

if (NB_TEST_SANITIZE)
add_compile_options(-fsanitize=address,undefined)
add_link_options(-fsanitize=address,undefined)
endif()

set(TEST_NAMES
functions
classes
holders
stl
stl_bind_map
stl_bind_vector
chrono
enum
eval
ndarray
exception
make_iterator
typing
issue
intrusive
)

foreach (NAME ${TEST_NAMES})
nanobind_add_module(test_${NAME}_ext test_${NAME}.cpp ${NB_EXTRA_ARGS})

if (NB_TEST_CUDA)
set_property(SOURCE test_${NAME}.cpp PROPERTY LANGUAGE CUDA)
endif()
endforeach()

nanobind_add_module(test_functions_ext test_functions.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_classes_ext test_classes.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_holders_ext test_holders.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_stl_ext test_stl.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_bind_map_ext test_stl_bind_map.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_bind_vector_ext test_stl_bind_vector.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_chrono_ext test_chrono.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_enum_ext test_enum.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_eval_ext test_eval.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_ndarray_ext test_ndarray.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_intrusive_ext test_intrusive.cpp test_intrusive_impl.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_exception_ext test_exception.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_make_iterator_ext test_make_iterator.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_typing_ext test_typing.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_issue_ext test_issue.cpp ${NB_EXTRA_ARGS})
target_sources(test_intrusive_ext PRIVATE test_intrusive_impl.cpp)

foreach (NAME functions classes ndarray stl enum typing make_iterator)
if (NAME STREQUAL typing)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_stl_bind_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ NestMap *times_hundred(int n) {
return m;
}

NB_MODULE(test_bind_map_ext, m) {
NB_MODULE(test_stl_bind_map_ext, m) {
// test_map_string_double
nb::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
nb::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
Expand Down
10 changes: 5 additions & 5 deletions tests/test_stl_bind_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import platform

import test_bind_map_ext as t
import test_stl_bind_map_ext as t


def test_map_string_double(capfd):
Expand Down Expand Up @@ -35,12 +35,12 @@ def test_map_string_double(capfd):
assert len(mm2) == 1
mm2.clear()
assert len(mm2) == 0
assert repr(mm) == "test_bind_map_ext.MapStringDouble({'a': 1.0, 'b': 2.5})"
assert repr(mm) == "test_stl_bind_map_ext.MapStringDouble({'a': 1.0, 'b': 2.5})"

with pytest.raises(TypeError):
mm2.update({"a" : "b"})
captured = capfd.readouterr().err.strip()
ref = "nanobind: implicit conversion from type 'dict' to type 'test_bind_map_ext.MapStringDouble' failed!"
ref = "nanobind: implicit conversion from type 'dict' to type 'test_stl_bind_map_ext.MapStringDouble' failed!"

# Work around Pytest-related flakiness (https://github.com/pytest-dev/pytest/issues/10843)
if platform.system() == 'Windows':
Expand Down Expand Up @@ -102,7 +102,7 @@ def test_map_string_double(capfd):

assert t.MapStringDouble.__init__.__doc__ == \
"""__init__(self) -> None
__init__(self, arg: test_bind_map_ext.MapStringDouble) -> None
__init__(self, arg: test_stl_bind_map_ext.MapStringDouble) -> None
__init__(self, arg: %s[str, float], /) -> None
Overloaded function.
Expand All @@ -111,7 +111,7 @@ def test_map_string_double(capfd):
Default constructor
2. ``__init__(self, arg: test_bind_map_ext.MapStringDouble) -> None``
2. ``__init__(self, arg: test_stl_bind_map_ext.MapStringDouble) -> None``
Copy constructor
Expand Down
2 changes: 1 addition & 1 deletion tests/test_stl_bind_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace nb = nanobind;

NB_MODULE(test_bind_vector_ext, m) {
NB_MODULE(test_stl_bind_vector_ext, m) {
nb::bind_vector<std::vector<unsigned int>>(m, "VectorInt");
nb::bind_vector<std::vector<bool>>(m, "VectorBool");

Expand Down
6 changes: 3 additions & 3 deletions tests/test_stl_bind_vector.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
import platform

import test_bind_vector_ext as t
import test_stl_bind_vector_ext as t

def test01_vector_int(capfd):
v_int = t.VectorInt([0, 0])
Expand All @@ -11,7 +11,7 @@ def test01_vector_int(capfd):
# test construction from a generator
v_int1 = t.VectorInt(x for x in range(5))
assert t.VectorInt(v_int1) == t.VectorInt([0, 1, 2, 3, 4])
assert repr(v_int1) == "test_bind_vector_ext.VectorInt([0, 1, 2, 3, 4])"
assert repr(v_int1) == "test_stl_bind_vector_ext.VectorInt([0, 1, 2, 3, 4])"

v_int2 = t.VectorInt([0, 0])
assert v_int == v_int2
Expand Down Expand Up @@ -47,7 +47,7 @@ def test01_vector_int(capfd):
v_int2.extend([8, "a"])

captured = capfd.readouterr().err.strip()
ref = "nanobind: implicit conversion from type 'list' to type 'test_bind_vector_ext.VectorInt' failed!"
ref = "nanobind: implicit conversion from type 'list' to type 'test_stl_bind_vector_ext.VectorInt' failed!"

# Work around Pytest-related flakiness (https://github.com/pytest-dev/pytest/issues/10843)
if platform.system() == 'Windows':
Expand Down

0 comments on commit 94488a5

Please sign in to comment.