【问题标题】:Cython Memoryview Seg FaultCython Memoryview 段错误
【发布时间】:2019-09-30 05:45:32
【问题描述】:

我在尝试使用 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) 时,我会遇到分段错误。有谁知道是什么导致了这个问题以及如何解决它?

谢谢, 鲁宁

【问题讨论】:

    标签: python c cython


    【解决方案1】:

    这看起来像是 32 位整数溢出 - 即 213*213*213*213 它大于最大 32 位整数。您应该使用 64 位整数作为索引(long 或更明确的 int64_t)。

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

    【讨论】:

      猜你喜欢
      • 2016-09-07
      • 1970-01-01
      • 2019-05-18
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 2019-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多