Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Sebastian Kehl
pybind11-hello-world
Commits
85a14938
Commit
85a14938
authored
Dec 02, 2021
by
Klaus Reuter
Browse files
add minimal numpy example for illustration purposes
parent
1fcfcbd8
Changes
5
Hide whitespace changes
Inline
Side-by-side
CMakeLists.txt
View file @
85a14938
cmake_minimum_required
(
VERSION 3.18
)
project
(
hello-world VERSION
"1.0"
)
project
(
pybind11-hello-world VERSION
"1.0"
)
find_package
(
pybind11
)
pybind11_add_module
(
_hello MODULE src/hello/hello.cpp
)
install
(
TARGETS _hello DESTINATION .
)
pybind11_add_module
(
_cumsum MODULE src/cumsum/cumsum.cpp
)
install
(
TARGETS _cumsum DESTINATION .
)
\ No newline at end of file
setup.py
View file @
85a14938
import
sys
from
skbuild
import
setup
setup
(
name
=
"
hello-world
"
,
name
=
"
pybind11-numpy-like-cumsum-example
"
,
version
=
"1.0"
,
description
=
"
hello world
example"
,
description
=
"
pybind11-numpy
example"
,
author
=
'MPCDF'
,
license
=
"MIT"
,
packages
=
[
'
hello
'
],
packages
=
[
'
cumsum
'
],
package_dir
=
{
''
:
'src'
},
cmake_install_dir
=
'src/
hello
'
cmake_install_dir
=
'src/
cumsum
'
)
src/cumsum/__init__.py
0 → 100644
View file @
85a14938
from
._cumsum
import
cumsum
\ No newline at end of file
src/cumsum/cumsum.cpp
0 → 100644
View file @
85a14938
// pybind11 example module 'cumsum'
#include
<numeric>
#include
<functional>
#include
<pybind11/pybind11.h>
#include
<pybind11/numpy.h>
namespace
py
=
pybind11
;
// numpy-like cumulative sum, taking a NumPy array as input and returning a NumPy array
py
::
array_t
<
double
>
cumsum
(
py
::
array_t
<
double
>
a
)
{
// obtain information about the nd input array
auto
shape
=
a
.
request
().
shape
;
size_t
count
=
std
::
accumulate
(
shape
.
begin
(),
shape
.
end
(),
1
,
std
::
multiplies
<
size_t
>
());
// create output NumPy array
py
::
array_t
<
double
>
b
(
count
);
// obtain raw pointers
double
*
a_p
=
(
double
*
)
a
.
request
().
ptr
;
double
*
b_p
=
(
double
*
)
b
.
request
().
ptr
;
// compute cumulative sum into b
double
cs
=
0.0
;
for
(
size_t
i
=
0
;
i
<
count
;
++
i
)
{
cs
+=
a_p
[
i
];
b_p
[
i
]
=
cs
;
}
return
b
;
}
// Python binary module _cumsum, expose cumsum C++ function as cumsum to Python
PYBIND11_MODULE
(
_cumsum
,
m
)
{
m
.
doc
()
=
"pybind11 cumulative sum example"
;
// module docstring
m
.
def
(
"cumsum"
,
&
cumsum
,
// third parameter is the function docstring
"return the cumulative sum of a double-precision numpy array"
);
}
test/test_cumsum.py
0 → 100755
View file @
85a14938
#!/usr/bin/env python3
import
numpy
as
np
import
cumsum
a
=
np
.
random
.
rand
(
80
)
ap
=
a
.
reshape
(
20
,
4
)
b
=
cumsum
.
cumsum
(
ap
)
c
=
np
.
cumsum
(
ap
)
assert
(
np
.
allclose
(
b
,
c
))
print
(
"OK!"
)
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment