Simulation Core
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

187 linhas
7.1 KiB

  1. Module RedisInterface
  2. use iso_c_binding, only: c_char
  3. ! USE Kinds, ONLY: rk, ck
  4. TYPE FString
  5. CHARACTER(KIND=c_char,LEN=:),allocatable :: item
  6. END TYPE FString
  7. character(len=20000)::buffer
  8. Interface
  9. SUBROUTINE addnums(a, b) BIND(C,name='addnums')
  10. USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_INT
  11. IMPLICIT NONE
  12. INTEGER(C_INT) :: a, b
  13. END SUBROUTINE addnums
  14. SUBROUTINE initConnection_C(address,port,password,datakey,status) BIND(C, name='initConnection')
  15. use iso_c_binding, only: c_char,c_int
  16. character(kind=c_char) :: address(*)
  17. integer(kind=c_int)::port,status
  18. character(kind=c_char) :: password(*),datakey(*)
  19. END SUBROUTINE initConnection_C
  20. SUBROUTINE setData_C(part,str) BIND(C,name='setData')
  21. use iso_c_binding, only: c_char
  22. character(kind=c_char) :: str(*)
  23. character(kind=c_char) :: part(*)
  24. END SUBROUTINE setData_C
  25. FUNCTION getData_C(len) BIND(C,name='getData')
  26. use iso_c_binding, only: c_ptr,c_int
  27. integer(kind=c_int)::len
  28. type(c_ptr) :: getData_C
  29. END FUNCTION getData_C
  30. SUBROUTINE getData_C_bystr(str) BIND(C,name='getData_bystr')
  31. use iso_c_binding, only: c_ptr,c_int,c_char
  32. type(c_ptr) :: str
  33. END SUBROUTINE getData_C_bystr
  34. SUBROUTINE getData_C_byfile() BIND(C,name='getData_byfile')
  35. END SUBROUTINE getData_C_byfile
  36. SUBROUTINE deallocateData() BIND(C, name='deallocData')
  37. END SUBROUTINE deallocateData
  38. End Interface
  39. contains
  40. SUBROUTINE initConnection(address,port,password, datakey,status)
  41. use iso_c_binding, only: c_null_char,c_char
  42. ! use json_module
  43. character(len=*) :: password,address,datakey
  44. integer::port,status
  45. character(len=:),allocatable::c_address,c_password,c_datakey
  46. c_datakey = datakey//c_null_char
  47. c_password = password//c_null_char
  48. c_address = address//c_null_char
  49. call initConnection_C(c_address,port,c_password,c_datakey,status)
  50. ! print *,"returned to initConnection"
  51. END SUBROUTINE initConnection
  52. SUBROUTINE setData(str)
  53. use SimulationVariables
  54. use iso_c_binding, only: c_null_char
  55. character(len=*):: str
  56. character(len=len_trim(str)+1)::c_str
  57. character(len=4)::part
  58. part = "out"//c_null_char
  59. c_str = str//c_null_char
  60. if(log_level>4) print *,"setting Data: ",len_trim(str)
  61. call setData_C(part,c_str)
  62. END SUBROUTINE setData
  63. SUBROUTINE setInput(str)
  64. use SimulationVariables
  65. use iso_c_binding, only: c_null_char
  66. character(len=*):: str
  67. character(len=len_trim(str)+1)::c_str
  68. character(len=4)::part
  69. part = "in"//c_null_char
  70. c_str = str//c_null_char
  71. if(log_level>4) print *,"setting Data: ",len_trim(str)
  72. call setData_C(part,c_str)
  73. END SUBROUTINE setInput
  74. SUBROUTINE getData(string,len)
  75. use iso_c_binding, only: c_char,c_ptr,c_f_pointer,c_null_ptr,c_int
  76. ! character(:),allocatable :: getData2
  77. character(:),allocatable :: string
  78. integer :: string_shape(1)
  79. integer(c_int):: len
  80. type(c_ptr) :: c_string
  81. integer::l
  82. l = 30000
  83. string_shape(1) = l!int(l,kind=kind(Integer))
  84. print *,"reading data l=",l
  85. if(.not. allocated(string)) then
  86. print *,"allocate string"
  87. allocate(character(l) :: string)
  88. endif
  89. len = l
  90. c_string = getData_C(len)
  91. ! print *,"data read. l=",l
  92. ! len = int(c_len,kind=kind(len))
  93. call c_f_pointer(c_string, string, string_shape)
  94. c_string=c_null_ptr
  95. ! string = trim(string)
  96. ! print *,len_trim(string), "chars read."
  97. ! print *,string(1:l)
  98. ! getData2=string
  99. END SUBROUTINE getData
  100. SUBROUTINE getData2(string) !result(string)
  101. use iso_c_binding, only: c_char,c_ptr,c_f_pointer,c_null_ptr,c_loc
  102. ! character(:),allocatable :: getData2
  103. character(len=:,kind=c_char),allocatable :: string
  104. integer :: string_shape(1)
  105. type(c_ptr) :: c_string
  106. TYPE(FString), TARGET :: f_str
  107. integer::l
  108. ! print *,"reading data (getData2)"
  109. call getData_C_bystr(c_loc(f_str))
  110. ! CALL MOVE_ALLOC(f_str%item, string)
  111. string = trim(buffer)
  112. ! string = c_str
  113. ! len = int(c_len,kind=kind(len))
  114. ! string_shape(1) = l! int(l,kind=kind(integer))
  115. ! if(.not. allocated(string)) allocate(character(l) :: string)
  116. ! call c_f_pointer(c_string, string, string_shape)
  117. ! c_string=c_null_ptr
  118. ! print *,len_trim(string), "chars read."
  119. ! print *,string(1:l)
  120. END SUBROUTINE getData2
  121. SUBROUTINE getData3(key,string)
  122. use iso_c_binding, only: c_char,c_ptr,c_f_pointer,c_null_ptr,c_loc
  123. ! character(:),allocatable :: getData2
  124. character(len=*) :: key
  125. character(len=:),allocatable :: string
  126. character(len=100) :: filename
  127. integer::iostat
  128. ! print *,"reading data (getData2)"
  129. filename = "/var/tmp/"//key
  130. call getData_C_byfile()
  131. open(unit=10, file=trim(filename)//".txt", status='old', action='read', iostat=iostat)
  132. if (iostat /= 0) then
  133. write(*, '(A)', advance='no') "Failed to open the file."
  134. stop
  135. end if
  136. ! Read the content of the file into the string
  137. read(10, '(A)', iostat=iostat) string
  138. if (iostat /= 0) then
  139. write(*, '(A)', advance='no') "Failed to read the file."
  140. stop
  141. end if
  142. ! Close the file
  143. close(10)
  144. END SUBROUTINE getData3
  145. SUBROUTINE set_fortran_string(fstring_ptr, length, str) BIND(C, NAME='set_fortran_string')
  146. USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR, C_INT, C_F_POINTER
  147. TYPE(C_PTR), INTENT(IN), VALUE :: fstring_ptr
  148. INTEGER(C_INT), INTENT(IN), VALUE :: length
  149. CHARACTER(KIND=C_CHAR), INTENT(IN) :: str(length)
  150. ! Fortran pointer to the object referenced by fstring_ptr that
  151. ! holds the deferred length character component.
  152. TYPE(FString), POINTER :: f_str
  153. character(:),allocatable :: string
  154. INTEGER :: i
  155. integer :: string_shape(1)
  156. !****
  157. ! Associate the Fortran pointer with the object referenced by the
  158. ! C address.
  159. string_shape(1) = length
  160. allocate(character(length) :: string)
  161. CALL C_F_POINTER(fstring_ptr, string,string_shape)
  162. ! Allocate the deferred length component to the given length.
  163. ! deallocate(f_str%item)
  164. ! ALLOCATE(CHARACTER(length) :: f_str%item)
  165. ! Copy over the data.
  166. ! FORALL (i=1:length) f_str%item(i:i) = str(i)
  167. buffer(1:length) = string(1:length)
  168. buffer(length+1:20000) = ' '
  169. END SUBROUTINE set_fortran_string
  170. END Module RedisInterface