Commit 68da62ce authored by Mozul Rémy's avatar Mozul Rémy

managing to reference the whole derived type instead of copy of non-pointer data

parent ef980a04
...@@ -21,5 +21,7 @@ typedef struct ...@@ -21,5 +21,7 @@ typedef struct
} C_datatype; } C_datatype;
void getOne(int id, C_datatype * d); void getOne(int id, C_datatype * d);
C_datatype * addOne(char * nn, double rv, int iv);
void display(int id); void display(int id);
void change(int id, char * nn, double rv, int iv); void change(int id, char * nn, double rv, int iv);
...@@ -15,5 +15,7 @@ cdef extern from 'dtype.h': ...@@ -15,5 +15,7 @@ cdef extern from 'dtype.h':
C_param * weird C_param * weird
void getOne(int id, C_datatype * d) void getOne(int id, C_datatype * d)
C_datatype * addOne(char * nn, double rv, int iv)
void display(int id) void display(int id)
void change(int id, char * nn, double rv, int iv) void change(int id, char * nn, double rv, int iv)
...@@ -12,19 +12,23 @@ cdef class P_datatype(object): ...@@ -12,19 +12,23 @@ cdef class P_datatype(object):
cdef int id cdef int id
cdef C_datatype f_image cdef C_datatype f_image
cdef C_datatype * f_image_ptr
cdef public np.ndarray rtab cdef public np.ndarray rtab
@ct.locals(i=int) @ct.locals(i=int)
def __init__(self, i): def __init__(self, i,nn,rv,iv):
self.id = i self.id = i
getOne(i,&self.f_image) getOne(i,&self.f_image)
self.rtab = np.PyArray_SimpleNewFromData(1, [self.f_image.itab[0]], np.NPY_DOUBLE, self.f_image.rtab) self.rtab = np.PyArray_SimpleNewFromData(1, [self.f_image.itab[0]], np.NPY_DOUBLE, self.f_image.rtab)
self.f_image_ptr = addOne(nn, rv, iv)
def cythonChange(self,nn,rv,iv): def cythonChange(self,nn,rv,iv):
#&self.f_image.nickname[0] = nn #&self.f_image.nickname[0] = nn
self.f_image.rsingle = rv self.f_image.rsingle = rv
self.f_image.isingle = iv self.f_image.isingle = iv
self.f_image_ptr.rsingle = rv
self.f_image_ptr.isingle = iv
def fortranChange(self,nn,rv,iv): def fortranChange(self,nn,rv,iv):
change(self.id, nn, rv, iv) change(self.id, nn, rv, iv)
...@@ -39,6 +43,7 @@ cdef class P_datatype(object): ...@@ -39,6 +43,7 @@ cdef class P_datatype(object):
display(self.id) display(self.id)
def cythonContent(self): def cythonContent(self):
print 'f_image :'
cdef int i cdef int i
print self.f_image.nickname print self.f_image.nickname
print self.f_image.rsingle print self.f_image.rsingle
...@@ -48,4 +53,12 @@ cdef class P_datatype(object): ...@@ -48,4 +53,12 @@ cdef class P_datatype(object):
print self.rtab print self.rtab
for i in range(self.f_image.itab[1]): for i in range(self.f_image.itab[1]):
print self.f_image.weird[i] print self.f_image.weird[i]
print 'f_image_ptr :'
print self.f_image_ptr.nickname
print self.f_image_ptr.rsingle
print self.f_image_ptr.isingle
for i in range(self.f_image_ptr.itab[0]):
print self.f_image_ptr.rtab[i]
for i in range(self.f_image_ptr.itab[1]):
print self.f_image_ptr.weird[i]
...@@ -41,6 +41,13 @@ module data_type ...@@ -41,6 +41,13 @@ module data_type
type(T_datatype), dimension(:), allocatable :: mydatas type(T_datatype), dimension(:), allocatable :: mydatas
type T_dt_link
type(t_datatype), pointer :: d => null()
type(T_dt_link) , pointer :: n => null()
end type
type(T_dt_link), pointer :: root => null()
type(T_dt_link), pointer :: curr => null()
!---------------------------------------------------------------------- !----------------------------------------------------------------------
integer :: nb_data = 0 integer :: nb_data = 0
...@@ -96,6 +103,59 @@ contains ...@@ -96,6 +103,59 @@ contains
end subroutine end subroutine
function add_one(nn,rv,iv) bind(c, name='addOne')
implicit none
real(kind=c_double), intent(in), value :: rv
integer(kind=c_int), intent(in), value :: iv
type(c_ptr), value :: nn
type(c_ptr) :: add_one
!
real(kind=8) , dimension(:), pointer :: rtab
type(T_param), dimension(:), pointer :: weird
character(kind=c_char,len=30), pointer :: nname
integer(kind=4) :: i
type(T_dt_link), pointer :: new
allocate(new)
allocate(new%d)
call c_f_pointer(cptr=nn,fptr=nname)
i = 0
do while( i <30 .and. nname(i+1:i+1) /= c_null_char )
i = i + 1
end do
new%d%nickname(1:i) = nname(1:i)
new%d%nickname(i+1:) = ''
new%d%rsingle = rv
new%d%isingle = iv
new%d%itab = (/2,3/)
allocate(rtab(2))
rtab = (/0.d0,0.d0/)
new%d%rtab = c_loc(rtab(1))
allocate(weird(3))
do i = 1, 3
weird(i)%itype = i
weird(i)%val = 1.
write(weird(i)%field,'(A,I0)') 'field of ',i
end do
new%d%weird = c_loc(weird(1))
if( .not. associated(root) ) then
root => new
else
curr%n => new
curr => new
end if
add_one = c_loc(new%d)
end function
subroutine display(id) bind(c, name='display') subroutine display(id) bind(c, name='display')
implicit none implicit none
integer(kind=c_int), intent(in), value :: id integer(kind=c_int), intent(in), value :: id
...@@ -103,6 +163,7 @@ contains ...@@ -103,6 +163,7 @@ contains
integer(kind=4) :: i integer(kind=4) :: i
real(kind=8) , dimension(:), pointer :: rtab real(kind=8) , dimension(:), pointer :: rtab
type(T_param), dimension(:), pointer :: weird type(T_param), dimension(:), pointer :: weird
type(T_dt_link) , pointer :: dl
print *,'T_DATA of id ', id print *,'T_DATA of id ', id
print *,' nickname : ', mydatas(id)%nickname print *,' nickname : ', mydatas(id)%nickname
...@@ -121,6 +182,34 @@ contains ...@@ -121,6 +182,34 @@ contains
print *,' --field : ', weird(i)%field print *,' --field : ', weird(i)%field
end do end do
print *,'T_DATA of in linked list of rank ', id
dl => Root
do i = 1, id-1
if( associated(dl%n) ) then
dl => dl%n
else
print *,'rank given higher than number of elements... get last'
exit
end if
end do
print *,' nickname : ', dl%d%nickname
print *,' rsingle : ', dl%d%rsingle
print *,' isingle : ', dl%d%isingle
print *,' itab : ', dl%d%itab
call c_f_pointer(dl%d%rtab,rtab,(/dl%d%itab(1)/))
call c_f_pointer(dl%d%weird,weird,(/dl%d%itab(2)/))
print *,' rtab : ', rtab
do i = 1, dl%d%itab(2)
print *,' weird : ', i
print *,' --itype : ', weird(i)%itype
print *,' --val : ', weird(i)%val
print *,' --field : ', weird(i)%field
end do
end subroutine end subroutine
subroutine change(id, nn, rv, iv) bind(c, name='change') subroutine change(id, nn, rv, iv) bind(c, name='change')
...@@ -132,6 +221,7 @@ contains ...@@ -132,6 +221,7 @@ contains
! !
character(kind=c_char,len=30), pointer :: nname character(kind=c_char,len=30), pointer :: nname
integer(kind=4) :: i integer(kind=4) :: i
type(T_dt_link) , pointer :: dl
call c_f_pointer(cptr=nn,fptr=nname) call c_f_pointer(cptr=nn,fptr=nname)
i = 0 i = 0
...@@ -144,6 +234,21 @@ contains ...@@ -144,6 +234,21 @@ contains
mydatas(id)%rsingle = rv mydatas(id)%rsingle = rv
mydatas(id)%isingle = iv mydatas(id)%isingle = iv
dl => Root
do i = 1, id-1
if( associated(dl%n) ) then
dl => dl%n
else
print *,'rank given higher than number of elements... no modif'
return
end if
end do
dl%d%nickname(1:i) = nname(1:i)
dl%d%nickname(i+1:) = ''
dl%d%rsingle = rv
dl%d%isingle = iv
end subroutine end subroutine
end module data_type end module data_type
......
import dtype import dtype
dat = dtype.P_datatype(1) dat = dtype.P_datatype(1, 'new from p', 12., 5)
...@@ -19,11 +19,10 @@ cdef class pytype(object): ...@@ -19,11 +19,10 @@ cdef class pytype(object):
@cython.locals(a=numpy.ndarray) @cython.locals(a=numpy.ndarray)
def __init__(self,a): def __init__(self,a):
a = numpy.asfarray(a) a = numpy.asfarray(a)
self.x = {'i':a.shape[0],'j':a.shape[1],'mat':<double *>a.data} self.x = {'i':a.shape[0],'j':a.shape[1],'mat':<double *>a.data}
#cdef double * params = cython.address(self.x).params
#self.p_params = numpy.asfarray(<numpy.double64_t[:4]> params)
#self.p_params = numpy.asfarray(<numpy.double64_t[:4]> cython.address(self.x).params)
cdef numpy.npy_intp s[1] cdef numpy.npy_intp s[1]
s[0] = <numpy.npy_intp> 4 s[0] = <numpy.npy_intp> 4
self.p_params = numpy.PyArray_SimpleNewFromData(1, s, numpy.NPY_DOUBLE, cython.address(self.x).params) self.p_params = numpy.PyArray_SimpleNewFromData(1, s, numpy.NPY_DOUBLE, cython.address(self.x).params)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment