python - Cython Memoryview 段错误

标签 python c cython

我在尝试使用 Cython 的内存 View 时遇到段错误。这是我的代码:

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)。

为什么要将内存 View 转换为指针?您不会获得太多速度,并且会丢失有关形状的任何信息(例如,您假设所有维度都相同),您可以让 Cython 为您处理多维索引。将 tei 参数设置为内存 View 而不是指针会好得多。

关于python - Cython Memoryview 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58162070/

相关文章:

python - 按A列分组并按列聚合时获取列对应的值

c - 设置简单 UDP 服务器时出现的问题

python - 使用 Cython 进行扩展,名称与源文件不同

python - 如何改进 Python 中 try 语句的重复使用?

python - 如何告诉 PyCharm async fixture 返回了一些东西

python - Pandas 中的 Vlookup 功能完全相同

c - 重新分配内存时出现段错误

c - 获取 unsigned char 中最后 2 位的值

python - 如何静态加载 python 模块(如 scipy)?

python - 'easy_install -U cython' 提示 vcvarsall.bat 和 -mno-cygwin 失败