3 Replies Latest reply on Jul 21, 2011 8:29 AM by fenser

    Segmentation fault using Python

    fenser
      dgetri_ segfault

      I'm trying to use acml library in my python code (using ctypes) for matrix inversion. However, dgetri_() gives segmentation fault, while dgetrf_() works wonderful. When I implement pure LAPACK, everything is OK.

      Details: acml 4.4.0, Mandriva linux 2010.2, Python 2.6.5, gcc 4.4.3

        • Segmentation fault using Python
          chipf

          You may need to provide an example.   I tried this very simple program and did not see a segfault.

          Note that this example uses the C style interfaces.  Nothing wrong with using dgetri_, but that is a Fortran entry point, not the C wrapper.

          -----dgetri_example.c

          //gcc dgetri_example.c -I/opt/acml4.4.0/gfortran64/include -lgfortran /opt/acml4.4.0/gfortran64/lib/libacml.a

          #include <stdio.h>
          #include "acml.h"


          double x[] = {
          3, 1, 3,
          1, 5, 9,
          2, 6, 5
          };

          int main()
          {
          int i, j, lda, info;
          int ipiv[10];

          lda =3;

          dgetri(3, x, lda, ipiv, &info);

          for (i=0; i<3; ++i)
          printf("%5.1f\n", x);

          return 0;
          }

            • Segmentation fault using Python
              fenser

              It's a very simle example in Python. you need numpy to test it

              import numpy
              from ctypes import *
              acml=CDLL("/usr/local/acml4.4.0/gfortran32/lib/libacml.so")

              def ACML_inverse(matr):
                  n_order=matr.shape[0]
                  lwork=80*n_order
                  ipiv=(c_int*n_order)()
                  size=c_int(n_order)
                  ones=c_int(1)
                  l_work=c_int(lwork)
                  work=(c_double*lwork)()
                  ok=c_int(0)
                  acml.dgetrf_(byref(size),byref(size),matr.ctypes.data_as(c_void_p),byref(size),ipiv,byref(ok))
                  print "dgetrf_() OK \n-----------\n",matr,"\n-----------\n"
                  acml.dgetri_(byref(size),matr.ctypes.data_as(c_void_p),byref(size),ipiv,work,byref(l_work),byref(ok))
                  print "dgetri_() OK"

              N=3
              M=numpy.zeros((N,N))
              for z in xrange(N):
                  M[z][z]=1.0

              M[0][0]=5.0
              M[1][1]=2.0
              M[2][2]=3.0
              M[0][1]=1.0
              M[1][2]=4.0
              M[2][0]=2.0

              print M, "\n-----------"
              ACML_inverse(M)
              print M, "\n"

              The output of this simple program is

              [[ 5.  1.  0.]
               [ 0.  2.  4.]
               [ 2.  0.  3.]]
              -----------
              dgetrf_() OK
              -----------
              [[ 5.   0.   0.2]
               [ 0.   4.   0.5]
               [ 2.   3.  -1.9]]
              -----------

              Segmentation fault

              I've checked this example on different machines under mandriva 2010.2 and OpenSuse 11.4. The result is the same.

               

                • Segmentation fault using Python
                  fenser

                  I've just tried this example on out server under Fedora 14 64 bit, Python 2.7, numpy-1.4.1. the result is positive!? SO, THERE IS A TERRIBLE PROBLEM with 32 BIT LIBRARY, since 64bit one works.

                  code:
                  import numpy
                  from ctypes import *
                  acml=CDLL("/usr/local/acml4.4.0/gfortran64/lib/libacml.so")

                  def ACML_inverse(matr):
                      n_order=matr.shape[0]
                      lwork=80*n_order
                      ipiv=(c_int*n_order)()
                      size=c_int(n_order)
                      ones=c_int(1)
                      l_work=c_int(lwork)
                      work=(c_double*lwork)()
                      ok=c_int(0)
                      acml.dgetrf_(byref(size),byref(size),matr.ctypes.data_as(c_void_p),byref(size),ipiv,byref(ok))
                      print "dgetrf_() OK \n-----------\n",matr,"\n-----------\n"
                      acml.dgetri_(byref(size),matr.ctypes.data_as(c_void_p),byref(size),ipiv,work,byref(l_work),byref(ok))
                      print "dgetri_() OK"

                  N=3
                  M=numpy.zeros((N,N))
                  for z in xrange(N):
                      M[z][z]=1.0

                  M[0][0]=5.0
                  M[1][1]=2.0
                  M[2][2]=3.0
                  M[0][1]=1.0
                  M[1][2]=4.0
                  M[2][0]=2.0
                  copyM=M.copy()
                  print M, "\n-----------"
                  ACML_inverse(M)
                  print M, "\n"
                  print "=========="
                  print numpy.dot(M,copyM)

                  output:
                  [[ 5.  1.  0.]
                   [ 0.  2.  4.]
                   [ 2.  0.  3.]]
                  -----------
                  dgetrf_() OK
                  -----------
                  [[ 5.   0.   0.2]
                   [ 0.   4.   0.5]
                   [ 2.   3.  -1.9]]
                  -----------

                  dgetri_() OK
                  [[ 0.15789474 -0.07894737  0.10526316]
                   [ 0.21052632  0.39473684 -0.52631579]
                   [-0.10526316  0.05263158  0.26315789]]

                  ==========
                  [[  1.00000000e+00   0.00000000e+00  -5.55111512e-17]
                   [  0.00000000e+00   1.00000000e+00   0.00000000e+00]
                   [  0.00000000e+00   0.00000000e+00   1.00000000e+00]]