質問編集履歴

1

文章を修正。

2023/03/31 08:05

投稿

taka3391
taka3391

スコア5

test CHANGED
@@ -1 +1 @@
1
- pybind11でfloatのポインタを関数の引数として与えたい
1
+ pybind11で、クラス内関数にfloatのポインタを与える方法
test CHANGED
@@ -1,100 +1,52 @@
1
- ### 実現こと
1
+ 以下のようなfloatのポインタを引数とて受け取るクラス内関数があるとき、どのようにpython側から呼べば良のでしょうか?
2
2
 
3
+ ```
3
- pybind11を使って、C++のコードをPythonで呼び出そうとしています。
4
+ #include <pybind11/numpy.h>
4
- その際に、c++側の関数の引数にfloatのポインタを与えたいです。
5
+ #include <pybind11/pybind11.h>
5
6
 
7
+ namespace py = pybind11;
6
8
 
7
- ### 発生している問題・エラーメッセージ
8
-
9
- 下記のtest2.pyを実行すると、以下のメッセージが出ています。
10
- ```
11
- Traceback (most recent call last):
12
- File "test2.py", line 10, in <module>
13
- out = p.Process(1.0, coef)
14
- TypeError: Process(): incompatible function arguments. The following argument types are supported:
15
- 1. (self: example2.Convolution, arg0: float, arg1: float) -> float
16
-
17
- Invoked with: <example2.Convolution object at 0x7fbc4f415d70>, 1.0, [1.0, 0.0, 0.0, 0.0]
18
- ```
19
-
20
- ### 該当のソースコード
21
-
22
- ```c++:Convolution.h
23
- #pragma once
24
-
25
- class Convolution {
9
+ class MyClass {
26
10
  public:
27
- Convolution(int tap_num);
28
- ~Convolution();
29
-
30
- float Process(const float input, const float* coef);
11
+ void myFunc(float *arr, int size) {
31
-
32
- private:
12
+ for (int i = 0; i < size; i++) {
33
- float* m_pDataBuf;
13
+ arr[i] += 1.0f;
34
- const int m_nTapNum;
14
+ }
35
-
15
+ }
36
16
  };
37
17
 
38
- ```
39
-
40
- ```c++:Convolution.cpp
18
+ PYBIND11_MODULE(example, m) {
41
- #include "Convolution.h"
19
+ py::class_<MyClass>(m, "MyClass")
42
-
43
- Convolution::Convolution(int tap_num) : m_nTapNum(tap_num)
44
- {
45
- m_pDataBuf = new float[m_nTapNum]();
46
- }
47
-
48
- Convolution::~Convolution()
49
- {
50
- delete[] m_pDataBuf; m_pDataBuf = nullptr;
51
- }
52
-
53
- float Convolution::Process(const float input, const float* coef)
54
- {
55
- for(int i=m_nTapNum-1; i>0; i--)
56
- {
57
- m_pDataBuf[i] = m_pDataBuf[i-1];
58
- }
59
- m_pDataBuf[0] = input;
60
-
61
- const float* x = m_pDataBuf;
62
- const float* y = coef;
63
- const int nx = m_nTapNum;
64
- float sum = 0.f;
65
-
66
- for (int i = 0; i < nx; i++)
20
+ .def(py::init<>())
67
- {
68
- sum += x[i] * y[i];
21
+ .def("my_func", &MyClass::myFunc);
69
- }
70
-
71
- return sum;
72
22
  }
73
23
  ```
74
24
 
75
-
76
- ```python:test2.py
25
+ 以下のようにpython側で呼んでいるのですが、エラーが出ていて困っている状況です。
26
+ ```
77
27
  import numpy as np
78
28
  import sys
79
29
  sys.path.append( "./build" )
80
- import example2
30
+ import example
81
31
 
32
+ # Create an instance of MyClass
82
- coef = [1.0, 0.0, 0.0, 0.0]
33
+ my_obj = example.MyClass()
83
34
 
84
- p = example2.Convolution(128)
35
+ # Create a NumPy array
85
- out = p.Process(1.0, coef)
36
+ arr = np.array([1.0, 2.0, 3.0], dtype=np.float32)
86
- print(out)
87
37
 
38
+ # Call myFunc with the NumPy array
39
+ my_obj.my_func(arr, arr.size)
40
+
41
+ # Print the updated array
42
+ print(arr)
88
43
  ```
89
44
 
90
- ### 試したこと
45
+ 以下がエラーです。
46
+ > Traceback (most recent call last):
47
+ > File "test2.py", line 13, in <module>
48
+ > my_obj.my_func(arr, arr.size)
49
+ > TypeError: my_func(): incompatible function arguments. The following argument types are supported:
50
+ > 1. (self: example.MyClass, arg0: float, arg1: int) -> None
51
+ > Invoked with: <example.MyClass object at 0x7f987db50ef0>, array([1., 2., 3.], dtype=float32), 3
91
52
 
92
- `test2.py`のcoefをnumpyに変えてみましたが、同じエラーでした。
93
-
94
-
95
- ### 補足情報(FW/ツールのバージョンなど)
96
-
97
- ubuntu:20.04
98
- python:3.8.10
99
- pybind11:2.4.3
100
-