简体   繁体   English

CULA-在CULA中使用Python Solve()

[英]CULA - using Python solve() within CULA

How can I incorporate Python's solve() in a Python CULA program? 如何将Python的Solve()合并到Python CULA程序中? I am using 我在用

LA = libculaC.solve() 

Result: 结果:

Traceback (most recent call last):
      File "culaTest.py", line 96, in <module>
        LA = libculaC.solve(0)
      File "/usr/lib/python2.7/ctypes/__init__.py", line 378, in __getattr__
        func = self.__getitem__(name)
      File "/usr/lib/python2.7/ctypes/__init__.py", line 383, in __getitem__
        func = self._FuncPtr((name_or_ordinal, self))
    AttributeError: /usr/local/cula/lib64/libcula_lapack.so: undefined symbol:     
    solve

Any combination of liculaC and ctypes give me similar errors. liculaC和ctypes的任何组合都会给我类似的错误。 How can I bring this function in? 如何引入此功能? Do I need to use a C function (scanf) or something. 我是否需要使用C函数(scanf)或其他工具? Thanks. 谢谢。

This took awhile, but this is what I have so far. 这花了一段时间,但这是到目前为止。 Must use ctypes and must convert to column major vice the standard row major. 必须使用ctypes并且必须将标准行专业转换为列专业。 Use matrices not arrays and keep your data types consistent throughout. 使用矩阵而不是数组,并始终保持数据类型的一致性。

import ctypes
from scipy import *
from scipy.linalg import *
import numpy as np
import sys
import csv
print     "___________________________________________________________________________________________________"

libculaC=ctypes.CDLL('libcula_lapack.so',mode=ctypes.RTLD_GLOBAL)
libculaC.culaGetStatusString.restype=ctypes.c_char_p

info=libculaC.culaInitialize()

#Row major-normal form, but must be converted-('4 1;2 5')
Anp = np.matrix('4.0 2.0;1.0 5.0') #Column major
print "This is Anp: "
print Anp
print '___________END Anp______________'

#use ctypes to convert from Py to C
#2x2 matrix
Anp = Anp.astype(numpy.float32)  #astype is array type for ctype
c_float_p = ctypes.POINTER(ctypes.c_float)
A1_p = Anp.ctypes.data_as(c_float_p)
# 2x1 matrix
B1 = np.matrix('5.0 ;7.0')
print "This is B1"
print B1
print '__________________B1 END______________________'
B1 = B1.astype(numpy.float32)
B1_p = B1.ctypes.data_as(c_float_p)

X=np.empty([2])
X=X.astype(numpy.float32)
X_p =X.ctypes.data_as(c_float_p)
print "This is X"
print X
print '__________________X END______________________'


info = 0
libculaC.culaSgesv(2,1,A1_p,2,X_p,B1_p,2)  #libculaC.culaSgesv

a = np.fromiter(B1_p, dtype=np.float32, count=2)
a = np.reshape(a,(-1,2))
print "The solution returning from Sgesv: "
print a
print "-----------------------Program End----------------------------"

libculaC.culaShutdown()

OUTPUT: This is Anp: 输出:这是Anp:

[[ 4. 2.] [ 1. 5.]] [[4. 2.] [1. 5.]]

___________END Anp______________ ___________END Anp______________

This is B1 这是B1

[[ 5.] [ 7.]] [[5.] [7.]]

__________________B1 END______________________ __________________B1 END______________________

This is X 这是X

[ 5. 7.] [5. 7.]

__________________X END______________________ __________________X END______________________

The solution returning from Sgesv: 从Sgesv返回的解决方案:

[[ 1. 1.]] [[1. 1.]]

-----------------------Program End---------------------------- -----------------------程序结束------------------------- ---

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM