Simulation Core
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Redis_Interface.i90 6.8 KiB

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