Joseph Redmon
2016-08-11 aebe937710ced03d03f73ab23f410f29685655c1
src/server.c
@@ -6,6 +6,7 @@
#include <netinet/in.h> /* needed for sockaddr_in */
#include <netdb.h>
#include <pthread.h>
#include <time.h>
#include "mini_blas.h"
#include "utils.h"
@@ -14,7 +15,7 @@
#include "connected_layer.h"
#include "convolutional_layer.h"
#define SERVER_PORT 9876
#define SERVER_PORT 9423
#define STR(x) #x
int socket_setup(int server)
@@ -45,32 +46,10 @@
typedef struct{
    int fd;
    int *counter;
    int counter;
    network net;
} connection_info;
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 write_all(int fd, char *buffer, size_t bytes)
{
    //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));
@@ -83,7 +62,12 @@
{
    connection_info info = *(connection_info *) pointer;
    free(pointer);
    printf("New Connection\n");
    //printf("New Connection\n");
    if(info.counter%100==0){
        char buff[256];
        sprintf(buff, "unikitty/net_%d.part", info.counter);
        save_network(info.net, buff);
    }
    int fd = info.fd;
    network net = info.net;
    int i;
@@ -118,28 +102,32 @@
            write_all(fd, (char *)layer.weights, layer.outputs*layer.inputs*sizeof(float));
        }
    }
    printf("Received updates\n");
    //printf("Received updates\n");
    close(fd);
}
void server_update(network net)
{
    int fd = socket_setup(1);
    int counter = 0;
    listen(fd, 10);
    int counter = 18000;
    listen(fd, 64);
    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;
        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;
        if(counter%1000==0) save_network(net, "cfg/nist.part");
        printf("%d\n", counter);
        //if(counter == 1024) break;
    }
    close(fd);
}
void client_update(network net, char *address)
@@ -197,7 +185,9 @@
            int num = layer.n*layer.c*layer.size*layer.size;
            read_all(fd, (char*) layer.filters, num*sizeof(float));
#ifdef GPU
            push_convolutional_layer(layer);
            #endif
        }
        if(net.types[i] == CONNECTED){
            connected_layer layer = *(connected_layer *) net.layers[i];
@@ -205,7 +195,9 @@
            read_all(fd, (char *)layer.biases, layer.outputs*sizeof(float));
            read_all(fd, (char *)layer.weights, layer.outputs*layer.inputs*sizeof(float));
#ifdef GPU
            push_connected_layer(layer);
            #endif
        }
    }
    //printf("Updated\n");