From a392bbd0c957a00e3782c96e7ced84a29ff9dd88 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Tue, 15 Mar 2016 05:33:02 +0000
Subject: [PATCH] Play along w/ alphago
---
src/server.c | 64 +++++++++++++++----------------
1 files changed, 31 insertions(+), 33 deletions(-)
diff --git a/src/server.c b/src/server.c
index 657ea7c..6e5105e 100644
--- a/src/server.c
+++ b/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,30 +46,10 @@
typedef struct{
int fd;
- int *counter;
+ int counter;
network net;
} connection_info;
-void read_all(int fd, char *buffer, size_t 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)
-{
- 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));
@@ -79,8 +60,14 @@
void handle_connection(void *pointer)
{
- printf("New Connection\n");
connection_info info = *(connection_info *) pointer;
+ free(pointer);
+ //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;
@@ -115,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);
- ++*(info.counter);
- if(*(info.counter)%10==0) save_network(net, "/home/pjreddie/imagenet_backup/alexnet.part");
}
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 */
- connection_info info;
- info.net = net;
- info.counter = &counter;
+ 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);
- info.fd = connection;
- pthread_create(&worker, NULL, (void *) &handle_connection, &info);
+ 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;
}
+ close(fd);
}
void client_update(network net, char *address)
@@ -166,6 +157,7 @@
/* send a message to the server */
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];
@@ -183,6 +175,7 @@
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){
@@ -192,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];
@@ -200,8 +195,11 @@
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");
close(fd);
}
--
Gitblit v1.10.0