Copying content of C buffer to numpy array
I have a function in C like this:
source file
// foo.cpp
int foo(int input1, double *output1, int size1, int* output2, int size2)
{
// does stuff and allocate space for output1 and output2
return 0;
}
header file
// foo.h
int foo(int input1, double *output1, int size1, int* output2, int size2);
In Cython I need to convert output1
and output2
to a numpy array, so in my foo.pyx
I do the following steps:
cdef extern from "foo.h":
cdef int foo(int input1, double* output1, int size1, double* output2, int size2)
cdef double* ptnOutput1
cdef int* ptnOutput2
foo(1, ptnOutput1, 10, ptnOutput2, 10)
but now I'm not able to get the output in form of two np.array
. How should I align the pointers ptnOutput1
and ptnOutput2
to two numpy arrays?
I've tried np.frombuffer
as well as np.PyArray_SimpleNewFromData
but with no luck. I always get segmentation fault.
Any idea how to do it correctly?
python c++ c data-binding cython
add a comment |
I have a function in C like this:
source file
// foo.cpp
int foo(int input1, double *output1, int size1, int* output2, int size2)
{
// does stuff and allocate space for output1 and output2
return 0;
}
header file
// foo.h
int foo(int input1, double *output1, int size1, int* output2, int size2);
In Cython I need to convert output1
and output2
to a numpy array, so in my foo.pyx
I do the following steps:
cdef extern from "foo.h":
cdef int foo(int input1, double* output1, int size1, double* output2, int size2)
cdef double* ptnOutput1
cdef int* ptnOutput2
foo(1, ptnOutput1, 10, ptnOutput2, 10)
but now I'm not able to get the output in form of two np.array
. How should I align the pointers ptnOutput1
and ptnOutput2
to two numpy arrays?
I've tried np.frombuffer
as well as np.PyArray_SimpleNewFromData
but with no luck. I always get segmentation fault.
Any idea how to do it correctly?
python c++ c data-binding cython
2
Ignoring the Cython aspect of it for the moment, how would you call this in C? I'd have thoughtptnOutput1
andptnOutput2
need to already be allocated when you callfoo
(which then fills in their contents)?
– DavidW
Nov 4 '16 at 16:32
cython.readthedocs.io/en/latest/src/userguide/memoryviews.html is a good description of how to work with arrays, cython native memoryviews and C arrays.
– hpaulj
Nov 4 '16 at 17:06
add a comment |
I have a function in C like this:
source file
// foo.cpp
int foo(int input1, double *output1, int size1, int* output2, int size2)
{
// does stuff and allocate space for output1 and output2
return 0;
}
header file
// foo.h
int foo(int input1, double *output1, int size1, int* output2, int size2);
In Cython I need to convert output1
and output2
to a numpy array, so in my foo.pyx
I do the following steps:
cdef extern from "foo.h":
cdef int foo(int input1, double* output1, int size1, double* output2, int size2)
cdef double* ptnOutput1
cdef int* ptnOutput2
foo(1, ptnOutput1, 10, ptnOutput2, 10)
but now I'm not able to get the output in form of two np.array
. How should I align the pointers ptnOutput1
and ptnOutput2
to two numpy arrays?
I've tried np.frombuffer
as well as np.PyArray_SimpleNewFromData
but with no luck. I always get segmentation fault.
Any idea how to do it correctly?
python c++ c data-binding cython
I have a function in C like this:
source file
// foo.cpp
int foo(int input1, double *output1, int size1, int* output2, int size2)
{
// does stuff and allocate space for output1 and output2
return 0;
}
header file
// foo.h
int foo(int input1, double *output1, int size1, int* output2, int size2);
In Cython I need to convert output1
and output2
to a numpy array, so in my foo.pyx
I do the following steps:
cdef extern from "foo.h":
cdef int foo(int input1, double* output1, int size1, double* output2, int size2)
cdef double* ptnOutput1
cdef int* ptnOutput2
foo(1, ptnOutput1, 10, ptnOutput2, 10)
but now I'm not able to get the output in form of two np.array
. How should I align the pointers ptnOutput1
and ptnOutput2
to two numpy arrays?
I've tried np.frombuffer
as well as np.PyArray_SimpleNewFromData
but with no luck. I always get segmentation fault.
Any idea how to do it correctly?
python c++ c data-binding cython
python c++ c data-binding cython
asked Nov 4 '16 at 16:08
linello
3,47774982
3,47774982
2
Ignoring the Cython aspect of it for the moment, how would you call this in C? I'd have thoughtptnOutput1
andptnOutput2
need to already be allocated when you callfoo
(which then fills in their contents)?
– DavidW
Nov 4 '16 at 16:32
cython.readthedocs.io/en/latest/src/userguide/memoryviews.html is a good description of how to work with arrays, cython native memoryviews and C arrays.
– hpaulj
Nov 4 '16 at 17:06
add a comment |
2
Ignoring the Cython aspect of it for the moment, how would you call this in C? I'd have thoughtptnOutput1
andptnOutput2
need to already be allocated when you callfoo
(which then fills in their contents)?
– DavidW
Nov 4 '16 at 16:32
cython.readthedocs.io/en/latest/src/userguide/memoryviews.html is a good description of how to work with arrays, cython native memoryviews and C arrays.
– hpaulj
Nov 4 '16 at 17:06
2
2
Ignoring the Cython aspect of it for the moment, how would you call this in C? I'd have thought
ptnOutput1
and ptnOutput2
need to already be allocated when you call foo
(which then fills in their contents)?– DavidW
Nov 4 '16 at 16:32
Ignoring the Cython aspect of it for the moment, how would you call this in C? I'd have thought
ptnOutput1
and ptnOutput2
need to already be allocated when you call foo
(which then fills in their contents)?– DavidW
Nov 4 '16 at 16:32
cython.readthedocs.io/en/latest/src/userguide/memoryviews.html is a good description of how to work with arrays, cython native memoryviews and C arrays.
– hpaulj
Nov 4 '16 at 17:06
cython.readthedocs.io/en/latest/src/userguide/memoryviews.html is a good description of how to work with arrays, cython native memoryviews and C arrays.
– hpaulj
Nov 4 '16 at 17:06
add a comment |
1 Answer
1
active
oldest
votes
The numpy documentation mentions that
In order to make use of the C-API from another extension module, the
import_array
function must be called
It seems you got the segmentation fault error because you didn't include import_array in your code.
The internal machinery of import_array
is more complex than what you may expect because it's actually a very lengthy macro, not a plain function. Refer to this for more information
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40427178%2fcopying-content-of-c-buffer-to-numpy-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The numpy documentation mentions that
In order to make use of the C-API from another extension module, the
import_array
function must be called
It seems you got the segmentation fault error because you didn't include import_array in your code.
The internal machinery of import_array
is more complex than what you may expect because it's actually a very lengthy macro, not a plain function. Refer to this for more information
add a comment |
The numpy documentation mentions that
In order to make use of the C-API from another extension module, the
import_array
function must be called
It seems you got the segmentation fault error because you didn't include import_array in your code.
The internal machinery of import_array
is more complex than what you may expect because it's actually a very lengthy macro, not a plain function. Refer to this for more information
add a comment |
The numpy documentation mentions that
In order to make use of the C-API from another extension module, the
import_array
function must be called
It seems you got the segmentation fault error because you didn't include import_array in your code.
The internal machinery of import_array
is more complex than what you may expect because it's actually a very lengthy macro, not a plain function. Refer to this for more information
The numpy documentation mentions that
In order to make use of the C-API from another extension module, the
import_array
function must be called
It seems you got the segmentation fault error because you didn't include import_array in your code.
The internal machinery of import_array
is more complex than what you may expect because it's actually a very lengthy macro, not a plain function. Refer to this for more information
edited Nov 21 at 2:03
answered Sep 4 at 5:17
bombs
90110
90110
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40427178%2fcopying-content-of-c-buffer-to-numpy-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Ignoring the Cython aspect of it for the moment, how would you call this in C? I'd have thought
ptnOutput1
andptnOutput2
need to already be allocated when you callfoo
(which then fills in their contents)?– DavidW
Nov 4 '16 at 16:32
cython.readthedocs.io/en/latest/src/userguide/memoryviews.html is a good description of how to work with arrays, cython native memoryviews and C arrays.
– hpaulj
Nov 4 '16 at 17:06