Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. program performance_test
  2. use iso_c_binding, only: C_CHAR, C_NULL_CHAR, C_INT, C_PTR, C_LOC
  3. implicit none
  4.  
  5. ! Interfaces that ensure type compatability between Fortran and C.
  6. interface
  7. subroutine client(ipaddr, portnum) bind(C, name="client")
  8. use iso_c_binding, only: c_char, c_int
  9. character(kind=c_char) :: ipaddr(*)
  10. integer(kind=c_int), value :: portnum
  11. end subroutine client
  12.  
  13. subroutine calc(indata, length) bind(C, name="calc")
  14. use iso_c_binding, only: c_ptr, c_int
  15. implicit none
  16. integer(c_int), value :: length
  17. type(c_ptr), value :: indata
  18. end subroutine calc
  19.  
  20. end interface
  21.  
  22. ! type declaration statements
  23. integer(c_int), allocatable, target :: array(:)
  24. type(c_ptr) :: cptr
  25. integer portno, length, i
  26.  
  27. ! executable statements
  28. ! Call client.c and connect to localhost on port number `portno'.
  29. portno=55555
  30. call client(C_CHAR_"localhost"//C_NULL_CHAR, portno)
  31.  
  32. ! Put numbers in the array
  33. length = 1000000
  34. allocate(array(0:length))
  35. cptr=c_loc(array(1))
  36.  
  37. do i=1, length
  38. array(i) = 2
  39. end do
  40.  
  41. ! Call client.c and pass the query on towards calcs.f90.
  42. call calc(cptr, length)
  43.  
  44. deallocate(array)
  45.  
  46. end program performance_test
  47.  
  48. /* The original code for this client can be found here:
  49. * http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/client.c */
  50. #include <stdio.h>
  51. #include <sys/types.h>
  52. #include <sys/socket.h>
  53. #include <netinet/in.h>
  54. #include <netdb.h>
  55.  
  56. // Static variables
  57. static int sockfd;
  58.  
  59.  
  60. /* This function is called when a system call fails. It displays a message about
  61. * the error on stderr and then aborts the program. */
  62. void error(char *msg)
  63. {
  64. perror(msg);
  65. exit(0);
  66. }
  67.  
  68. /* Callable function from Fortran, used by calcf.f90, to connect to a server.
  69. */
  70. int client(char *ipaddr, int in_portno)
  71. {
  72. int portno;
  73.  
  74. struct sockaddr_in serv_addr;
  75. struct hostent *server;
  76.  
  77. portno = in_portno;
  78. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  79. if (sockfd < 0)
  80. error("ERROR opening socket");
  81.  
  82. server = gethostbyname(ipaddr);
  83. if (server == NULL) {
  84. fprintf(stderr,"ERROR, no such hostn");
  85. exit(0);
  86. }
  87.  
  88. memset(((char *) &serv_addr), 0, (sizeof(serv_addr)));
  89. serv_addr.sin_family = AF_INET;
  90. memcpy(((char *)server->h_addr),
  91. ((char *)&serv_addr.sin_addr.s_addr),
  92. (server->h_length));
  93. serv_addr.sin_port = htons(portno);
  94.  
  95. if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
  96. error("ERROR connecting");
  97.  
  98. return 0;
  99. }
  100.  
  101. /* Callable function from Fortran, used by calcf.f90, as a calculator.
  102. * calc passes the query stored in buffer to server and returns the
  103. * answer. */
  104. int *calc(int *indata, int length)
  105. {
  106.  
  107. int n;
  108. n = write(sockfd, indata, sizeof(int)*length);
  109. if (n < 0)
  110. error("ERROR writing to socket");
  111.  
  112. n = read(sockfd, indata, sizeof(int)*length);
  113. if (n < 0)
  114. error("ERROR reading from socket");
  115.  
  116. return indata;
  117. }
  118.  
  119. /* The original code for this server can be found here:
  120. * http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/server.c */
  121. #include <stdio.h>
  122. #include <stdlib.h>
  123. #include <errno.h>
  124. #include <math.h>
  125. #include <sys/types.h>
  126. #include <sys/socket.h>
  127. #include <netinet/in.h>
  128.  
  129. /* This function is called when a system call fails. It displays a message about
  130. * the error on stderr and then aborts the program. */
  131. void error(char *msg)
  132. {
  133. perror(msg);
  134. exit(1);
  135. }
  136.  
  137. /* Callable function from Fortran, used by calcs.f90, to start a server that
  138. * can recieve queries. */
  139. int server(int in_portno)
  140. {
  141. int sockfd, newsockfd, portno, clilen;
  142.  
  143. struct sockaddr_in serv_addr, cli_addr; int n;
  144.  
  145. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  146. if (sockfd < 0)
  147. error("ERROR opening socket");
  148.  
  149. memset(((char *) &serv_addr), 0, (sizeof(serv_addr)));
  150.  
  151. portno = in_portno;
  152.  
  153. serv_addr.sin_family = AF_INET;
  154.  
  155. serv_addr.sin_addr.s_addr = INADDR_ANY;
  156.  
  157. serv_addr.sin_port = htons(portno);
  158.  
  159. if (bind(sockfd, (struct sockaddr *) &serv_addr,
  160. sizeof(serv_addr)) < 0)
  161. error("ERROR on binding");
  162.  
  163. listen(sockfd,5);
  164.  
  165. clilen = sizeof(cli_addr);
  166.  
  167. while(1)
  168. {
  169. int length = 1000000;
  170. int indata[length];
  171. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  172. if (newsockfd < 0)
  173. error("ERROR on accept");
  174.  
  175. // Here comes the query from client.c
  176. n = read(newsockfd, indata, sizeof(int)*length);
  177. if (n < 0) error("ERROR reading from socket");
  178.  
  179. square(indata);
  180.  
  181. // write returns the data to the client
  182. n = write(newsockfd, indata, sizeof(int)*length);
  183.  
  184. if (n < 0) error("ERROR writing to socket");
  185. }
  186.  
  187. return 0;
  188.  
  189. }
  190.  
  191. program calculator_server
  192.  
  193. ! Calculator at the server side of the client/server calculator
  194. ! Responsible for starting up the server server.c
  195.  
  196. use iso_c_binding, only: C_CHAR, C_NULL_CHAR, C_INT, C_PTR
  197. implicit none
  198.  
  199. ! type declaration statements
  200. integer calc, ans, portnum, calculate
  201.  
  202. ! Interface that ensures type compatibility between Fortran and C
  203. interface
  204. subroutine server(portnum) bind(C, name="server")
  205. use iso_c_binding, only: c_int
  206. integer(kind=c_int), value :: portnum
  207. end subroutine server
  208. subroutine square(intarray) bind(C, name="square")
  209. use iso_c_binding
  210. type(c_ptr), value :: intarray
  211. end subroutine square
  212. end interface
  213.  
  214. ! Start the server with portnumber
  215. portnum = 55555
  216. call server(portnum)
  217.  
  218. end program calculator_server
  219.  
  220.  
  221. ! **********************************************************************
  222. subroutine square(cptr) bind(C, name="square")
  223. use iso_c_binding
  224. implicit none
  225.  
  226. ! Variable declarations
  227. type(c_ptr) :: cptr
  228. integer*8 :: iptr
  229. integer :: length, i
  230. integer pointee(1000000)
  231. pointer(iptr, pointee)
  232. iptr = loc(cptr)
  233. length = 1000000
  234.  
  235. ! Execution
  236. do i = 1, length
  237. pointee(i)= pointee(i)**2
  238. end do
  239. end subroutine square
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement