Python Forum
I really need help!!! about argtypes and restype I think..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I really need help!!! about argtypes and restype I think..
#1
Recently Ive tried using Fortran subroutine with ctypeps in Python

the question is how can I send array to fortran?
here are my Fortran code and python code.
subroutine add_array(a,b,N)
    implicit none
    real(8),dimension(0:1024),intent(in) :: a
    real(8),dimension(0:1024),intent(inout):: b
    integer(8),intent(in) :: N  
    
    if (N.LT.1024) then
        write(*,*) "Maxsize is too small."
    end if
    b(0:N-1) = b(0:N-1) + a(0:N-1)
end subroutine
stop
end

#!/usr/bin/env python
#vim:fileencoding=utf8
from ctypes import *
import numpy as np

add_np = cdll.LoadLibrary("C:\\Users\\Owner\\Desktop\\Forpython\\add_np\\add_np\\add_np\\Debug\\add_np.dll")

add_np.add_array_.argtypes = [
               np.ctypeslib.ndpointer(dtype=np.float64),#a
               np.ctypeslib.ndpointer(dtype=np.float64),#b
               POINTER(c_int64),]                       #N

add_np.add_array_.restype = c_void_p   #b

a = np.arange(0.,10.,dtype=np.float64) # 0,1,2,3,4,5,..,9 
b = a*2                                # 0,2,4,6,8,...,18
N = byref(c_int64(b.N)) 
add_np.add_array_(byref(a),byref(b),byref(N)) 
print b # [ 0 3 6 9 12 .. 27 ]
but Python message is 
Error:
Traceback (most recent call last):   File "C:\Users\Owner\Desktop\Forpython\add_np\add_np.py", line 11, in <module>     POINTER(c_int64),]                       #N   File "C:\Python27\lib\ctypes\__init__.py", line 375, in __getattr__     func = self.__getitem__(name)   File "C:\Python27\lib\ctypes\__init__.py", line 380, in __getitem__     func = self._FuncPtr((name_or_ordinal, self)) AttributeError: function 'add_array_' not found
and are these codes right?
could you help me please!!
thanks..
Reply
#2
please use code tags and repost
Thank you
Reply
#3
from ctypes import *

import numpy as np

add_np = cdll.LoadLibrary("C:\\Users\\Owner\\Desktop\\Forpython\\add_np\\add_np\\add_np\\Debug\\add_np.dll")

add_np.add_array.argtypes = [
               np.ctypeslib.ndpointer(dtype=np.float64),#a
               np.ctypeslib.ndpointer(dtype=np.float64),#b
               POINTER(c_int64),]                       #N

add_np.add_array_.restype = c_void_p   #b

a = np.arange(0.,10.,dtype=np.float64) # 0,1,2,3,4,5,..,9 
b = a*2                                # 0,2,4,6,8,...,18
N = byref(c_int64(b.N)) 
add_np.add_array_(byref(a),byref(b),byref(N)) 
print b # [ 0 3 6 9 12 .. 27 ] 
Error:
Traceback (most recent call last):   File "C:\Users\Owner\Desktop\Forpython\add_np\add_np.py", line 11, in <module>     POINTER(c_int64),]                       #N   File "C:\Python27\lib\ctypes\__init__.py", line 375, in __getattr__     func = self.__getitem__(name)   File "C:\Python27\lib\ctypes\__init__.py", line 380, in __getitem__     func = self._FuncPtr((name_or_ordinal, self)) AttributeError: function 'add_array' not found
Sorry and thanks!!!
Reply
#4
Every reference I can find for this (the actual error) is in chinese.

The acuual error:
Error:
File "C:\Python27\lib\ctypes\__init__.py", line 380, in __getitem__     func = self._FuncPtr((name_or_ordinal, self)) AttributeError: function 'add_array' not found
Appears to be an error in line 380 of the dll that you are loading.
What is it's purpose, and can you find another one?

If you can read chinese, google  "add_np.add_array.argtypes = ["
I tried translating (google translate) but the translation appears poor and could only figure out that you
are seeing a known error.

I'm going to post the code docs (which I assume you already have, but just in case you don't) https://docs.scipy.org/doc/numpy/referen...eslib.html
Reply
#5
Sorry for late and Sorry I can't understand what you mean that "Appears to be an error in line 380 of the dll that you are loading.
What is it's purpose, and can you find another one?"

whats more, I got new question... How can I install numpy in Ironpython2.7?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020