Skip to content

Commit

Permalink
example_mod => example_cpython, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Feb 19, 2024
1 parent 84bee0d commit f1cf9ee
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ External Projects
~~~~~~~~~~~~~~~~~

For integration into Python, C++, CUDA, CMake, and general SWIG projects, see `the external project documentation <https://amypad.github.io/CuVec/#external-projects>`_.
Full and explicit example modules using the `CPython API <https://github.com/AMYPAD/CuVec/tree/main/cuvec/src/example_mod>`_ and `SWIG <https://github.com/AMYPAD/CuVec/tree/main/cuvec/src/example_swig>`_ are also provided.
Full and explicit example modules using the `CPython API <https://github.com/AMYPAD/CuVec/tree/main/cuvec/src/example_cpython>`_ and `SWIG <https://github.com/AMYPAD/CuVec/tree/main/cuvec/src/example_swig>`_ are also provided.

Contributing
~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion cuvec/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ endif()

# example projects

add_subdirectory(src/example_mod)
add_subdirectory(src/example_cpython)
if(pybind11_FOUND AND SKBUILD)
add_subdirectory(src/example_pybind11)
endif()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(example_mod)
project(example_cpython)
file(GLOB SRC LIST_DIRECTORIES false "*.cu")

include_directories(${Python_INCLUDE_DIRS})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ static PyMethodDef example_methods[] = {
};

/** module */
static struct PyModuleDef example_mod = {PyModuleDef_HEAD_INIT,
"example_mod", // module
"Example external module.",
-1, // module keeps state in global variables
example_methods};
PyMODINIT_FUNC PyInit_example_mod(void) {
static struct PyModuleDef example_cpython = {PyModuleDef_HEAD_INIT,
"example_cpython", // module
"Example external module.",
-1, // module keeps state in global variables
example_methods};
PyMODINIT_FUNC PyInit_example_cpython(void) {
Py_Initialize();
return PyModule_Create(&example_mod);
return PyModule_Create(&example_cpython);
}
13 changes: 10 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,11 @@ CUDA:
}
```

For a full reference, see `cuvec.example_mod`'s source code: [example_mod.cu](https://github.com/AMYPAD/CuVec/blob/main/cuvec/src/example_mod/example_mod.cu) or the alternative `cuvec.example_swig` sources [example_swig.i](https://github.com/AMYPAD/CuVec/blob/main/cuvec/src/example_swig/example_swig.i) & [example_swig.cu](https://github.com/AMYPAD/CuVec/blob/main/cuvec/src/example_swig/example_swig.cu).
For a full reference, see:

- `cuvec.example_cpython` source: [example_cpython.cu](https://github.com/AMYPAD/CuVec/blob/main/cuvec/src/example_cpython/example_cpython.cu)
- `cuvec.example_pybind11` source: [example_pybind11.cu](https://github.com/AMYPAD/CuVec/blob/main/cuvec/src/example_pybind11/example_pybind11.cu)
- `cuvec.example_swig` sources: [example_swig.i](https://github.com/AMYPAD/CuVec/blob/main/cuvec/src/example_swig/example_swig.i) & [example_swig.cu](https://github.com/AMYPAD/CuVec/blob/main/cuvec/src/example_swig/example_swig.cu)

See also [NumCu](https://github.com/AMYPAD/NumCu), a minimal stand-alone Python package built using CuVec.

Expand All @@ -509,7 +513,10 @@ See also [NumCu](https://github.com/AMYPAD/NumCu), a minimal stand-alone Python
python -c "import cuvec; print(cuvec.include_path)"
```

For reference, see `cuvec.example_mod`'s source code: [example_mod.cu](https://github.com/AMYPAD/CuVec/blob/main/cuvec/src/example_mod/example_mod.cu).
For reference, see:

- `cuvec.example_cpython` source: [example_cpython.cu](https://github.com/AMYPAD/CuVec/blob/main/cuvec/src/example_cpython/example_cpython.cu)
- `cuvec.example_pybind11` source: [example_pybind11.cu](https://github.com/AMYPAD/CuVec/blob/main/cuvec/src/example_pybind11/example_pybind11.cu)

=== "SWIG"
`cuvec` is a header-only library so simply `%include "cuvec.i"` in a SWIG interface file. You can find the location of the headers using:
Expand All @@ -518,7 +525,7 @@ See also [NumCu](https://github.com/AMYPAD/NumCu), a minimal stand-alone Python
python -c "import cuvec; print(cuvec.include_path)"
```

For reference, see `cuvec.example_swig`'s source code: [example_swig.i](https://github.com/AMYPAD/CuVec/blob/main/cuvec/src/example_swig/example_swig.i) and [example_swig.cu](https://github.com/AMYPAD/CuVec/blob/main/cuvec/src/example_swig/example_swig.cu).
For reference, see `cuvec.example_swig`'s sources: [example_swig.i](https://github.com/AMYPAD/CuVec/blob/main/cuvec/src/example_swig/example_swig.i) and [example_swig.cu](https://github.com/AMYPAD/CuVec/blob/main/cuvec/src/example_swig/example_swig.cu).

=== "CMake"
This is likely unnecessary (see the "C++ & CUDA" tab above for simpler `#include` instructions).
Expand Down
8 changes: 4 additions & 4 deletions tests/test_cpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def test_asarray():


def test_increment():
# `example_mod` is defined in ../cuvec/src/example_mod/
from cuvec.example_mod import increment2d_f
# `example_cpython` is defined in ../cuvec/src/example_cpython/
from cuvec.example_cpython import increment2d_f
a = cu.zeros((1337, 42), 'f')
assert (a == 0).all()
res = cu.asarray(increment2d_f(a.cuvec, a.cuvec))
Expand All @@ -70,7 +70,7 @@ def test_increment():


def test_increment_return():
from cuvec.example_mod import increment2d_f
from cuvec.example_cpython import increment2d_f
a = cu.zeros((1337, 42), 'f')
assert (a == 0).all()
res = cu.asarray(increment2d_f(a, a))
Expand All @@ -80,7 +80,7 @@ def test_increment_return():


def test_np_types():
from cuvec.example_mod import increment2d_f
from cuvec.example_cpython import increment2d_f
f = cu.zeros((1337, 42), 'f')
d = cu.zeros((1337, 42), 'd')
cu.asarray(increment2d_f(f))
Expand Down
8 changes: 4 additions & 4 deletions tests/test_perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import cuvec.cpython as cu

# `example_mod` is defined in ../cuvec/src/example_mod/
from cuvec import example_mod # type: ignore # yapf: disable
# `example_cpython` is defined in ../cuvec/src/example_cpython/
from cuvec import example_cpython # type: ignore # yapf: disable

try:
# `cuvec.swig` alternative to `cuvec.cpython`
Expand Down Expand Up @@ -61,7 +61,7 @@ def test_inner(*args, **kwargs):
return wrapper


@mark.parametrize("cu,ex", [(cu, example_mod), (py, example_pybind11), (sw, example_swig)])
@mark.parametrize("cu,ex", [(cu, example_cpython), (py, example_pybind11), (sw, example_swig)])
@retry_on_except()
def test_perf(cu, ex, shape=(1337, 42), quiet=False, return_time=False):
if cu is None:
Expand Down Expand Up @@ -105,7 +105,7 @@ def test_perf(cu, ex, shape=(1337, 42), quiet=False, return_time=False):
trange = range
nruns = 500

for args in [(cu, example_mod), (py, example_pybind11), (sw, example_swig)]:
for args in [(cu, example_cpython), (py, example_pybind11), (sw, example_swig)]:
print(f"# One run ({args[1].__name__}):")
test_perf(*args, shape=(1000, 1000))

Expand Down

0 comments on commit f1cf9ee

Please sign in to comment.