| | |
| | | #include <stdio.h> /* needed for sockaddr_in */ |
| | | #include <string.h> /* needed for sockaddr_in */ |
| | | #include <unistd.h> |
| | | #include <sys/types.h> |
| | | #include <sys/socket.h> |
| | | #include <netinet/in.h> /* needed for sockaddr_in */ |
| | | #include <stdio.h> /* needed for sockaddr_in */ |
| | | #include <string.h> /* needed for sockaddr_in */ |
| | | #include <netdb.h> |
| | | #include <pthread.h> |
| | | #include <time.h> |
| | | |
| | | #include "mini_blas.h" |
| | | #include "utils.h" |
| | | #include "parser.h" |
| | | #include "server.h" |
| | | #include "connected_layer.h" |
| | | #include "convolutional_layer.h" |
| | | |
| | | #define MESSAGESIZE 512 |
| | | #define SERVER_PORT 9876 |
| | | #define CLIENT_PORT 9879 |
| | | #define STR(x) #x |
| | | #define PARAMETER_SERVER localhost |
| | | |
| | | int socket_setup(int port) |
| | | int socket_setup(int server) |
| | | { |
| | | static int fd = 0; /* our socket */ |
| | | if(fd) return fd; |
| | | struct sockaddr_in myaddr; /* our address */ |
| | | int fd = 0; /* our socket */ |
| | | struct sockaddr_in me; /* our address */ |
| | | |
| | | /* create a UDP socket */ |
| | | |
| | | if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| | | perror("cannot create socket\n"); |
| | | fd=0; |
| | | return 0; |
| | | if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
| | | error("cannot create socket"); |
| | | } |
| | | |
| | | /* bind the socket to any valid IP address and a specific port */ |
| | | if (server == 1){ |
| | | bzero((char *) &me, sizeof(me)); |
| | | me.sin_family = AF_INET; |
| | | me.sin_addr.s_addr = htonl(INADDR_ANY); |
| | | me.sin_port = htons(SERVER_PORT); |
| | | |
| | | memset((char *)&myaddr, 0, sizeof(myaddr)); |
| | | myaddr.sin_family = AF_INET; |
| | | myaddr.sin_addr.s_addr = htonl(INADDR_ANY); |
| | | myaddr.sin_port = htons(port); |
| | | |
| | | if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { |
| | | perror("bind failed"); |
| | | fd=0; |
| | | return 0; |
| | | if (bind(fd, (struct sockaddr *)&me, sizeof(me)) < 0) { |
| | | error("bind failed"); |
| | | } |
| | | } |
| | | |
| | | return fd; |
| | | } |
| | | |
| | | void server_update() |
| | | { |
| | | int fd = socket_setup(SERVER_PORT); |
| | | struct sockaddr_in remaddr; /* remote address */ |
| | | socklen_t addrlen = sizeof(remaddr); /* length of addresses */ |
| | | int recvlen; /* # bytes received */ |
| | | unsigned char buf[MESSAGESIZE]; /* receive buffer */ |
| | | typedef struct{ |
| | | int fd; |
| | | int *counter; |
| | | network net; |
| | | } connection_info; |
| | | |
| | | recvlen = recvfrom(fd, buf, MESSAGESIZE, 0, (struct sockaddr *)&remaddr, &addrlen); |
| | | buf[recvlen] = 0; |
| | | printf("received %d bytes\n", recvlen); |
| | | printf("%s\n", buf); |
| | | void read_all(int fd, char *buffer, size_t bytes) |
| | | { |
| | | //printf("Want %d\n", bytes); |
| | | size_t n = 0; |
| | | while(n < bytes){ |
| | | int next = read(fd, buffer + n, bytes-n); |
| | | if(next <= 0) error("read failed"); |
| | | n += next; |
| | | } |
| | | } |
| | | |
| | | void client_update() |
| | | void write_all(int fd, char *buffer, size_t bytes) |
| | | { |
| | | int fd = socket_setup(CLIENT_PORT); |
| | | //printf("Writ %d\n", bytes); |
| | | size_t n = 0; |
| | | while(n < bytes){ |
| | | int next = write(fd, buffer + n, bytes-n); |
| | | if(next <= 0) error("write failed"); |
| | | n += next; |
| | | } |
| | | } |
| | | |
| | | void read_and_add_into(int fd, float *a, int n) |
| | | { |
| | | float *buff = calloc(n, sizeof(float)); |
| | | read_all(fd, (char*) buff, n*sizeof(float)); |
| | | axpy_cpu(n, 1, buff, 1, a, 1); |
| | | free(buff); |
| | | } |
| | | |
| | | void handle_connection(void *pointer) |
| | | { |
| | | connection_info info = *(connection_info *) pointer; |
| | | free(pointer); |
| | | //printf("New Connection\n"); |
| | | int fd = info.fd; |
| | | network net = info.net; |
| | | int i; |
| | | for(i = 0; i < net.n; ++i){ |
| | | if(net.types[i] == CONVOLUTIONAL){ |
| | | convolutional_layer layer = *(convolutional_layer *) net.layers[i]; |
| | | |
| | | read_and_add_into(fd, layer.bias_updates, layer.n); |
| | | int num = layer.n*layer.c*layer.size*layer.size; |
| | | read_and_add_into(fd, layer.filter_updates, num); |
| | | } |
| | | if(net.types[i] == CONNECTED){ |
| | | connected_layer layer = *(connected_layer *) net.layers[i]; |
| | | |
| | | read_and_add_into(fd, layer.bias_updates, layer.outputs); |
| | | read_and_add_into(fd, layer.weight_updates, layer.inputs*layer.outputs); |
| | | } |
| | | } |
| | | for(i = 0; i < net.n; ++i){ |
| | | if(net.types[i] == CONVOLUTIONAL){ |
| | | convolutional_layer layer = *(convolutional_layer *) net.layers[i]; |
| | | update_convolutional_layer(layer); |
| | | |
| | | write_all(fd, (char*) layer.biases, layer.n*sizeof(float)); |
| | | int num = layer.n*layer.c*layer.size*layer.size; |
| | | write_all(fd, (char*) layer.filters, num*sizeof(float)); |
| | | } |
| | | if(net.types[i] == CONNECTED){ |
| | | connected_layer layer = *(connected_layer *) net.layers[i]; |
| | | update_connected_layer(layer); |
| | | write_all(fd, (char *)layer.biases, layer.outputs*sizeof(float)); |
| | | write_all(fd, (char *)layer.weights, layer.outputs*layer.inputs*sizeof(float)); |
| | | } |
| | | } |
| | | //printf("Received updates\n"); |
| | | close(fd); |
| | | } |
| | | |
| | | void server_update(network net) |
| | | { |
| | | int fd = socket_setup(1); |
| | | int counter = 0; |
| | | listen(fd, 10); |
| | | struct sockaddr_in client; /* remote address */ |
| | | socklen_t client_size = sizeof(client); /* length of addresses */ |
| | | time_t t=0; |
| | | while(1){ |
| | | connection_info *info = calloc(1, sizeof(connection_info)); |
| | | info->net = net; |
| | | info->counter = &counter; |
| | | pthread_t worker; |
| | | int connection = accept(fd, (struct sockaddr *) &client, &client_size); |
| | | if(!t) t=time(0); |
| | | info->fd = connection; |
| | | pthread_create(&worker, NULL, (void *) &handle_connection, info); |
| | | ++counter; |
| | | printf("%d\n", counter); |
| | | if(counter == 1024) break; |
| | | if(counter%1000==0) save_network(net, "cfg/nist.part"); |
| | | } |
| | | printf("1024 epochs: %d seconds\n", time(0)-t); |
| | | close(fd); |
| | | } |
| | | |
| | | void client_update(network net, char *address) |
| | | { |
| | | int fd = socket_setup(0); |
| | | |
| | | struct hostent *hp; /* host information */ |
| | | struct sockaddr_in servaddr; /* server address */ |
| | | char *my_message = "this is a test message"; |
| | | struct sockaddr_in server; /* server address */ |
| | | |
| | | /* fill in the server's address and data */ |
| | | memset((char*)&servaddr, 0, sizeof(servaddr)); |
| | | servaddr.sin_family = AF_INET; |
| | | servaddr.sin_port = htons(SERVER_PORT); |
| | | bzero((char*)&server, sizeof(server)); |
| | | server.sin_family = AF_INET; |
| | | server.sin_port = htons(SERVER_PORT); |
| | | |
| | | /* look up the address of the server given its name */ |
| | | hp = gethostbyname("localhost"); |
| | | hp = gethostbyname(address); |
| | | if (!hp) { |
| | | perror("no such host"); |
| | | fprintf(stderr, "could not obtain address of %s\n", "localhost"); |
| | | } |
| | | |
| | | /* put the host's address into the server address structure */ |
| | | memcpy((void *)&servaddr.sin_addr, hp->h_addr_list[0], hp->h_length); |
| | | memcpy((void *)&server.sin_addr, hp->h_addr_list[0], hp->h_length); |
| | | if (connect(fd, (struct sockaddr *) &server, sizeof(server)) < 0) { |
| | | error("error connecting"); |
| | | } |
| | | |
| | | /* send a message to the server */ |
| | | if (sendto(fd, my_message, strlen(my_message), 0, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { |
| | | perror("sendto failed"); |
| | | int i; |
| | | //printf("Sending\n"); |
| | | for(i = 0; i < net.n; ++i){ |
| | | if(net.types[i] == CONVOLUTIONAL){ |
| | | convolutional_layer layer = *(convolutional_layer *) net.layers[i]; |
| | | write_all(fd, (char*) layer.bias_updates, layer.n*sizeof(float)); |
| | | int num = layer.n*layer.c*layer.size*layer.size; |
| | | write_all(fd, (char*) layer.filter_updates, num*sizeof(float)); |
| | | memset(layer.bias_updates, 0, layer.n*sizeof(float)); |
| | | memset(layer.filter_updates, 0, num*sizeof(float)); |
| | | } |
| | | if(net.types[i] == CONNECTED){ |
| | | connected_layer layer = *(connected_layer *) net.layers[i]; |
| | | write_all(fd, (char *)layer.bias_updates, layer.outputs*sizeof(float)); |
| | | write_all(fd, (char *)layer.weight_updates, layer.outputs*layer.inputs*sizeof(float)); |
| | | memset(layer.bias_updates, 0, layer.outputs*sizeof(float)); |
| | | memset(layer.weight_updates, 0, layer.inputs*layer.outputs*sizeof(float)); |
| | | } |
| | | } |
| | | //printf("Sent\n"); |
| | | |
| | | for(i = 0; i < net.n; ++i){ |
| | | if(net.types[i] == CONVOLUTIONAL){ |
| | | convolutional_layer layer = *(convolutional_layer *) net.layers[i]; |
| | | |
| | | read_all(fd, (char*) layer.biases, layer.n*sizeof(float)); |
| | | int num = layer.n*layer.c*layer.size*layer.size; |
| | | read_all(fd, (char*) layer.filters, num*sizeof(float)); |
| | | |
| | | push_convolutional_layer(layer); |
| | | } |
| | | if(net.types[i] == CONNECTED){ |
| | | connected_layer layer = *(connected_layer *) net.layers[i]; |
| | | |
| | | read_all(fd, (char *)layer.biases, layer.outputs*sizeof(float)); |
| | | read_all(fd, (char *)layer.weights, layer.outputs*layer.inputs*sizeof(float)); |
| | | |
| | | push_connected_layer(layer); |
| | | } |
| | | } |
| | | //printf("Updated\n"); |
| | | close(fd); |
| | | } |