Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
usadelndsoc
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
JYU Condensed Matter Theory
usadelndsoc
Commits
ca7e3898
Commit
ca7e3898
authored
2 years ago
by
patavirt
Browse files
Options
Downloads
Patches
Plain Diff
pythonutil: support returning stored arrays (move semantics)
parent
df5075f0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/core.cpp
+7
-3
7 additions, 3 deletions
src/core.cpp
src/pythonutil.hpp
+58
-13
58 additions, 13 deletions
src/pythonutil.hpp
with
65 additions
and
16 deletions
src/core.cpp
+
7
−
3
View file @
ca7e3898
...
...
@@ -11,14 +11,18 @@ using namespace array::python;
typedef
pybind11
::
detail
::
type_caster
<
array
::
ArrayView
<
double
,
array
::
Shape
<
4
>
>
>
xcaster
;
double
add
(
const
array
::
python
::
PythonArray
<
double
,
Shape
<
4
>
>
array
,
PythonArray
<
double
,
Shape
<
1
>
>
out
)
array
::
StoredArray
<
double
,
Shape
<
1
>
>
add
(
array
::
python
::
PyArrayView
<
double
,
Shape
<
4
>
>
array
)
{
StoredArray
<
double
,
Shape
<
1
>
>
out
;
double
sum
=
0
;
for
(
size_t
i
=
0
;
i
<
4
;
++
i
)
sum
+=
array
(
i
);
out
(
0u
)
=
-
sum
;
return
sum
;
out
(
0u
)
=
sum
;
return
out
;
}
PYBIND11_MODULE
(
_core
,
m
)
{
...
...
This diff is collapsed.
Click to expand it.
src/pythonutil.hpp
+
58
−
13
View file @
ca7e3898
...
...
@@ -22,7 +22,7 @@ namespace array { namespace PYTHON_NAMESPACE {
};
template
<
typename
Scalar
,
typename
Shape
,
size_t
Alignment
=
0
,
bool
BoundsCheck
=
true
>
class
Py
thon
Array
:
public
ArrayView
<
Scalar
,
Shape
,
Alignment
,
BoundsCheck
>
class
PyArray
View
:
public
ArrayView
<
Scalar
,
Shape
,
Alignment
,
BoundsCheck
>
{
private:
typedef
ArrayView
<
Scalar
,
Shape
,
Alignment
,
BoundsCheck
>
BaseType
;
...
...
@@ -48,31 +48,31 @@ namespace array { namespace PYTHON_NAMESPACE {
}
public
:
Py
thon
Array
()
PyArray
View
()
:
BaseType
(
nullptr
,
Shape
::
size
,
Shape
::
shape
())
{}
/* Only move constructor / assignment: resolve writeback only once */
Py
thon
Array
(
Python
Array
&&
other
)
PyArray
View
(
Py
Array
View
&&
other
)
:
BaseType
(
other
.
data
(),
other
.
size
(),
other
.
shape
()),
storage_
(
pybind11
::
reinterpret_steal
<
pybind11
::
object
>
(
other
.
storage_
.
release
()))
{}
Py
thon
Array
&
operator
=
(
Py
thon
Array
&&
other
)
PyArray
View
&
operator
=
(
PyArray
View
&&
other
)
{
BaseType
::
operator
=
(
other
);
storage_
=
pybind11
::
reinterpret_steal
<
pybind11
::
object
>
(
other
.
storage_
.
release
());
return
*
this
;
}
~
Py
thon
Array
()
~
PyArray
View
()
{
if
(
storage_
)
PyArray_ResolveWritebackIfCopy
(
reinterpret_cast
<
PyArrayObject
*>
(
storage_
.
ptr
()));
}
Py
thon
Array
(
pybind11
::
object
obj
)
PyArray
View
(
pybind11
::
object
obj
)
:
BaseType
(
reinterpret_cast
<
Scalar
*>
(
PyArray_DATA
(
reinterpret_cast
<
PyArrayObject
*>
(
obj
.
ptr
()))),
PyArray_SIZE
(
reinterpret_cast
<
PyArrayObject
*>
(
obj
.
ptr
())),
get_shape
(
reinterpret_cast
<
PyArrayObject
*>
(
obj
.
ptr
()))),
...
...
@@ -80,8 +80,6 @@ namespace array { namespace PYTHON_NAMESPACE {
{
/* steals reference */
}
static
constexpr
auto
name
=
pybind11
::
detail
::
const_name
<
BaseType
>
();
};
namespace
detail
{
...
...
@@ -93,12 +91,13 @@ namespace array { namespace PYTHON_NAMESPACE {
template
<
>
struct
NumpyTypeMap
<
short
>
{
const
static
int
numpy_type
=
NPY_SHORT
;
};
template
<
typename
Scalar
,
typename
Shape
,
size_t
Alignment
=
0
,
bool
BoundsCheck
=
true
>
struct
array_
cast
er
{
struct
array_
load
er
{
private:
typedef
PythonArray
<
Scalar
,
Shape
,
Alignment
,
BoundsCheck
>
ArrayType
;
typedef
PyArrayView
<
Scalar
,
Shape
,
Alignment
,
BoundsCheck
>
ArrayType
;
static
constexpr
auto
array_type_name
=
pybind11
::
detail
::
const_name
<
ArrayType
>
();
public:
PYBIND11_TYPE_CASTER
(
ArrayType
,
A
rray
T
ype
::
name
);
PYBIND11_TYPE_CASTER
(
ArrayType
,
a
rray
_t
ype
_
name
);
bool
load
(
pybind11
::
handle
src
,
bool
)
{
...
...
@@ -125,14 +124,60 @@ namespace array { namespace PYTHON_NAMESPACE {
}
};
template
<
typename
Scalar
,
typename
Shape
,
size_t
Alignment
=
0
,
bool
BoundsCheck
=
true
,
typename
StorageType
=
Scalar
>
struct
array_caster
{
private:
typedef
StoredArray
<
Scalar
,
Shape
,
Alignment
,
BoundsCheck
>
ArrayType
;
static
constexpr
auto
array_type_name
=
pybind11
::
detail
::
const_name
<
ArrayType
>
();
public:
PYBIND11_TYPE_CASTER
(
ArrayType
,
array_type_name
);
static
pybind11
::
handle
cast
(
ArrayType
&&
src
,
pybind11
::
return_value_policy
/* policy */
,
pybind11
::
handle
/* parent */
)
{
typedef
std
::
vector
<
StorageType
>
Storage
;
ArrayType
moved
=
std
::
move
(
src
);
Storage
*
store
=
new
Storage
(
std
::
move
(
moved
.
storage
()));
npy_intp
shape
[
Shape
::
ndim
];
for
(
size_t
i
=
0
;
i
<
Shape
::
ndim
;
++
i
)
shape
[
i
]
=
moved
.
dim
(
i
);
pybind11
::
capsule
base
(
store
,
[](
void
*
o
)
{
delete
static_cast
<
Storage
*>
(
o
);
});
PyObject
*
obj
=
PyArray_New
(
&
PyArray_Type
,
Shape
::
ndim
,
shape
,
NumpyTypeMap
<
Scalar
>::
numpy_type
,
NULL
,
store
->
data
(),
0
,
0
,
NULL
);
if
(
!
obj
)
throw
pybind11
::
error_already_set
();
PyArray_SetBaseObject
(
reinterpret_cast
<
PyArrayObject
*>
(
obj
),
base
.
inc_ref
().
ptr
());
return
obj
;
}
};
template
<
typename
T
>
using
is_py_array
=
std
::
is_base_of
<
PythonArray
<
typename
T
::
Scalar
,
typename
T
::
Shape
,
T
::
Alignment
,
T
::
BoundsCheck
>
,
T
>
;
using
is_py_array_view
=
std
::
is_base_of
<
PyArrayView
<
typename
T
::
Scalar
,
typename
T
::
Shape
,
T
::
Alignment
,
T
::
BoundsCheck
>
,
T
>
;
template
<
typename
T
>
using
is_stored_array
=
std
::
is_base_of
<
StoredArray
<
typename
T
::
Scalar
,
typename
T
::
Shape
,
T
::
Alignment
,
T
::
BoundsCheck
>
,
T
>
;
}
}
}
// namespace array::python
namespace
pybind11
{
namespace
detail
{
template
<
typename
Type
>
struct
type_caster
<
Type
,
enable_if_t
<
array
::
python
::
detail
::
is_py_array
<
Type
>::
value
>
>
struct
type_caster
<
Type
,
enable_if_t
<
array
::
python
::
detail
::
is_py_array_view
<
Type
>::
value
>
>
:
public
array
::
python
::
detail
::
array_loader
<
typename
Type
::
Scalar
,
typename
Type
::
Shape
,
Type
::
Alignment
,
Type
::
BoundsCheck
>
{
};
template
<
typename
Type
>
struct
type_caster
<
Type
,
enable_if_t
<
array
::
python
::
detail
::
is_stored_array
<
Type
>::
value
>
>
:
public
array
::
python
::
detail
::
array_caster
<
typename
Type
::
Scalar
,
typename
Type
::
Shape
,
Type
::
Alignment
,
Type
::
BoundsCheck
>
{
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment