Wrapping a LAPACKE function using Cython

心不动则不痛 提交于 2019-12-04 07:11:38

OK, I figured it out eventually - it seems I've misunderstood what row- and column-major refer to in this case.

Since C-contiguous arrays follow row-major order, I assumed that I ought to specify LAPACK_ROW_MAJOR as the first argument to LAPACKE_dgtsv.

In fact, if I change

info = LAPACKE_dgtsv(LAPACK_ROW_MAJOR, ...)

to

info = LAPACKE_dgtsv(LAPACK_COL_MAJOR, ...)

then my function works:

test_trisolve2.test_trisolve()
0
||x - x_hat|| =  6.67064747632e-12

This seems pretty counter-intuitive to me - can anyone explain why this is the case?

Although rather old the question seems still to be relevant. The observed behavior is the result of a misinterpretation of parameter LDB:

  • Fortran arrays are col major and the leading dimension of the array B corresponds to N. Therefore LDB >= max(1,N).
  • With row major LDB corresponds to NRHS and therefore the condition LDB >= max(1,NRHS) must be met.

Comment # b is (LDB, NRHS) is not correct since b has the dimension (LDB,N) and LDB should be 1 in this case.

Switching from LAPACK_ROW_MAJOR to LAPACK_COL_MAJOR fixes the issue as long as NRHS is equal to 1. The memory layout of a col major (N,1) is the same as row major (1,N). It will fail, however, if NRHS is greater than 1.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!