Skip to content

Commit cd98e67

Browse files
authored
Create call_fortran.py
1 parent a64d2ec commit cd98e67

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
Author: wrry
3+
email: wangrry@hotmail.com
4+
Date: 2023-06-16 01:01:32
5+
LastEditors: wrry wangrry@hotmail.com
6+
LastEditTime: 2023-06-16 01:16:29
7+
FilePath: /nonsense/pycallfortran/call_fortran.py
8+
Description:
9+
'''
10+
import ctypes
11+
import numpy as np
12+
13+
# 加载.so文件
14+
lib = ctypes.CDLL('./libadd_arrays.so')
15+
16+
# 定义函数原型
17+
add_arrays = lib.add_arrays_
18+
# add_arrays.restype = None
19+
add_arrays.argtypes = [
20+
np.ctypeslib.ndpointer(dtype=np.float32, flags='C_CONTIGUOUS'), # array1
21+
np.ctypeslib.ndpointer(dtype=np.float32, flags='C_CONTIGUOUS'), # array2
22+
np.ctypeslib.ndpointer(dtype=np.float32, flags='C_CONTIGUOUS'), # result
23+
ctypes.POINTER(ctypes.c_int) # size
24+
]
25+
26+
# 创建输入数组
27+
array1 = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32)
28+
array2 = np.array([5.0, 6.0, 7.0, 8.0], dtype=np.float32)
29+
size = len(array1)
30+
31+
# 创建输出数组
32+
result = np.zeros_like(array1)
33+
# 调用函数
34+
add_arrays(array1, array2, result, ctypes.pointer(ctypes.c_int(size)))
35+
36+
# 输出结果
37+
print("Result:", result)

0 commit comments

Comments
 (0)