我在尝试使用Cython的memoryview时遇到了分段错误。这是我的代码:
def fock_build_init_with_inputs(tei_ints):
# set the number of orbitals
norb = tei_ints.shape[0]
# get memory view of TEIs
cdef double[:,:,:,::1] tei_memview = tei_ints
# get index pairs
prep_ipss_serial(norb, &tei_memview[0,0,0,0])
void prep_ipss_serial(const int n, const double * const tei) {
int p, q, r, s, np;
double maxval;
const double thresh = 1.0e-9;
// first we count the number of index pairs with above-threshold integrals
np = 0;
for (q = 0; q < n; q++)
for (p = q; p < n; p++) {
maxval = 0.0;
for (s = 0; s < n; s++)
for (r = s; r < n; r++) {
maxval = fmax( maxval, fabs( tei[ r + n*s + n*n*p + n*n*n*q ] ) );
}
if ( maxval > thresh )
np++;
}
ipss_np = np;
当我通过调用输入numpy.zeros([n,n,n,n])的第一个函数来运行代码时,当n超过某个数字(212)时,我会遇到分段错误。有人知道是什么导致了这个问题以及如何解决它吗
谢谢, 鲁宁
这看起来像32位整数溢出-即
213*213*213*213
它大于最大32位整数。您应该使用64位整数作为索引(long
或更明确地说int64_t
)但为什么要将memoryview转换为指针?你不会获得太多的速度,也会丢失关于形状的任何信息(例如,你假设所有维度都是相同的),你可以让Cython为你处理多维索引。最好将
tei
参数设置为memoryview,而不是指针相关问题 更多 >
编程相关推荐