在+ 我试图用AVX优化一段python代码。我使用cType访问C++函数。有时函数会出错,有时则不会。我想这可能和路线有关? 也许有人能帮我,我有点困在这里了。在
Python代码:
from ctypes import *
import numpy as np
#path_cnt
path_cnt = 16
c_path_cnt = c_int(path_cnt)
#ndarray1
ndarray1 = np.ones(path_cnt,dtype=np.float32,order='C')
ndarray1.setflags(align=1,write=1)
c_ndarray1 = stock.ctypes.data_as(POINTER(c_float))
#ndarray2
ndarray2 = np.ones(path_cnt,dtype=np.float32,order='C');
ndarray2.setflags(align=1,write=1)
c_ndarray2 = max_vola.ctypes.data_as(POINTER(c_float))
#call function
finance = cdll.LoadLibrary(".../libfin.so")
finance.foobar.argtypes = [c_void_p, c_void_p,c_int]
finance.foobar(c_ndarray1,c_ndarray2,c_path_cnt)
x=0
while x < path_cnt:
print c_stock[x]
x+=1
C++代码
^{pr2}$现在奇怪的输出行为,在终端上两次调用会得到不同的结果!在
tobias@tobias-Lenovo-U310:~/workspace/finance$ python finance.py
Segmentation fault (core dumped)
tobias@tobias-Lenovo-U310:~/workspace/finance$ python finance.py
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
提前谢谢!在
有对齐和未对齐的加载指令。如果你违反了对齐规则,对齐的会出错,但是它们更快。未对齐的接收任何地址,并在内部进行加载/移位以获取所需的数据。您使用的是对齐版本
_mm256_load_ps
,并且可以切换到未对齐的版本_mm256_loadu_ps
,而无需任何中间分配。在一个好的矢量化编译器将包括一个导入循环以到达一个对齐的地址,然后是一个处理对齐数据的主体,然后是一个最后一个循环来清除任何偏离。在
好吧,我想我找到了一个苏丹,它不是很优雅,但它至少有效! 这应该是个更好的方法,有什么建议吗?在
相关问题 更多 >
编程相关推荐