Root/Examples/ehw4/src/sintesishw_client.c

1/*********************************************************************************************************
2** Programa para probar la sintesis combinacional mediante programacion genetica,
3** se usan sockets para repartir carga de trabajo a otros clientes
4** Se usa periferico evalfit del proyecto ehw3
5** se aceleran 5 arboles, que en el presente codigo se llama pentarbol
6** compilar con math.h -lm
7** compilar con threads: -lpthread
8** gcc sintesishw_client.c -lm -lpthread -o sintesishw_client_ppc
9** ejecutar:
10** ./sintesishw_client_386 vars poblacion generaciones pentarboles sar.dat sar2.dat
11**
12**********************************************************************************************************/
13
14#include <stdio.h>
15#include <termios.h>
16#include <sys/mman.h>
17#include <stdlib.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <fcntl.h>
21#include <time.h>
22#include <math.h>
23#include <pthread.h>
24#include <stdio.h>
25#include <sys/socket.h>
26#include <netinet/in.h>
27#include <netdb.h>
28#include <errno.h>
29#include <sys/un.h>
30#include <sintesishw_client.h>
31
32/**************************************************************************************************************************************
33    */
34create_connect_socket(char *addr, int *fd_ap)
35{
36    int fd;
37    struct sockaddr_in input_addr;
38    struct sockaddr_in server;
39     if (inet_aton(addr, &input_addr.sin_addr)==-1)
40    { perror("inet_aton");
41      exit(-1);
42    }
43
44       if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){
45      printf("socket() error\n");
46      exit(-1);
47       }
48
49    server.sin_family = AF_INET;
50       server.sin_port = htons(PORT);
51       server.sin_addr = input_addr.sin_addr;
52       bzero(&(server.sin_zero),8);
53
54   if(connect(fd, (struct sockaddr *)&server, sizeof(struct sockaddr))==-1)
55    {
56 // printf("connect() error en %s\n",addr);
57      perror("connect");
58      exit(-1);
59       }
60/* printf(".");*/
61    *fd_ap = fd;
62}
63
64
65rx_cromo(int fd, void *data_socket_rx_ap, char *cromo_sal, int *fitness, int pentarboles, int *generacion, int *tiempo, int *aux_sal)
66{
67void *ap1;
68int i, j, numbytes_rx;
69int nivel2[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
70int nivel3[]={0,0,1,1,1,2,2,2,2,3,3, 3, 3, 4, 4, 4};
71int nivel4[]={0,0,0,0,0,1,1,1,1,1,1, 1, 1, 1, 1, 1};
72int nivel5[]={0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0};
73
74
75    if ((numbytes_rx=recv(fd,data_socket_rx_ap,MAXDATASIZE,0)) == -1){
76      printf("Error en recv() \n");
77      perror("recv");
78      exit(-1);
79       }
80    ap1 = data_socket_rx_ap;
81/* printf("Rx:%i ",ntohl(*(int *)ap1));*/
82    ap1 = ap1 + 4;
83    for(i=0; i < RESULTADOS ;i++)
84    {
85        for(j=0; j < LONG_INDIV ;j++)
86        {
87            *(cromo_sal + j + (LONG_INDIV*i)) = *(char *)ap1;
88            ap1 ++;
89        }
90        *(fitness + i) = ntohl(*(int *)ap1) & 0xFFFF;
91        *(generacion + i) = ntohl(*(int *)ap1) >> 16;
92        ap1 = ap1 + 4;
93        *(tiempo+i) = ntohl(*(int *)ap1);
94        ap1 = ap1 + 4;
95    }
96    for(i = 0; i < maxgeneraciones*2 ;i++)
97    {
98        *(aux_sal + i) = ntohl(*(int *)ap1);
99        ap1 = ap1 + 4;
100    }
101}
102
103tx_cromo(struct gen_datos_tipo *gen_datos, int fd)
104{
105void *data_socket_tx_ap, *ap1;
106int *objetivo, i, numbytes_tx;
107
108    data_socket_tx_ap = malloc((gen_datos->tamaobj)*sizeof(int) + gen_datos->tamacrom + 0xFF);
109    ap1= data_socket_tx_ap;
110
111    *(int *)ap1 = htonl(gen_datos->tamaobj | (maxgeneraciones << 16));
112    ap1 = ap1 + 4;
113    objetivo = gen_datos->objetivo;
114    for(i=0; i < (gen_datos->tamaobj); i++)
115    {
116        *(int *)ap1 = htonl(objetivo[i]);
117        ap1 = ap1 + 4;
118    }
119    *(int *)ap1 = htonl(gen_datos->pentarboles | (gen_datos->vars << 16));
120    ap1 = ap1 + 4;
121    *(int *)ap1 = htonl(gen_datos->tamacrom | (poblacion << 16));
122    ap1 = ap1 + 4;
123
124    for(i = 0; i < (gen_datos->tamacrom); i++)
125    {
126        *(char *)(ap1) = *(char *)(gen_datos->cromo_entrada + i);
127        ap1 = ap1 + 1;
128    }
129
130    *(int *)ap1 = htonl(gen_datos->fitness_entrada); //fitness de entrada?
131    ap1 = ap1 + 4;
132    *(int *)ap1 = htonl((gen_datos->en_cromo_entrada<<16) | gen_datos->nivel_max);
133    ap1 = ap1 + 4;
134    *(int *)ap1 = htonl(gen_datos->aux); //datos varios
135    ap1 = ap1 + 4;
136    *(int *)ap1 = htonl(0xa55a9669); //datos varios
137    ap1 = ap1 + 4;
138    numbytes_tx = ap1 - data_socket_tx_ap;
139    send(fd, data_socket_tx_ap, numbytes_tx ,0); //enviar
140    free(data_socket_tx_ap);
141
142}
143
144
145
146/**************************************************************************************************************************************
147    crea una poblacion, y envia a placas para evolucionar */
148iniciar_evol(struct gen_datos_tipo *gen_datos)
149{
150int *generacion, k, a, i, j = 0, vars;
151int conta=0, aux1, aux2, *fitness, *fitness2, *entrada, *objetivo, tamaobj, pentarboles, maxgens, *fitness_sal, fitness_entrada, nivel_max, *aux_sal;
152char o, *ap, *cromo, *ordenpoblacion, *cromo_sal, *cromo_entrada;
153int *tiempo;
154int nivel2[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
155int nivel3[]={0,0,1,1,1,2,2,2,2,3,3, 3, 3, 4, 4, 4};
156int nivel4[]={0,0,0,0,0,1,1,1,1,1,1, 1, 1, 1, 1, 1};
157int nivel5[]={0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0};
158
159/* variables para sockets */
160    int fds[8],fd1, fd2, fd3, fd4, fd5, fd6, fd7, fd8, numbytes_tx, numbytes_rx; /* ficheros descriptores para sockets*/
161    void *data_socket_rx_ap;
162    char server[][16] = {IP0, IP1, IP2, IP3, IP4, IP5, IP6, IP7};
163
164    pentarboles = gen_datos->pentarboles;
165    vars = gen_datos->vars;
166    generacion = gen_datos->generacion;
167    tiempo = gen_datos->tiempo;
168    aux_sal = gen_datos->aux_sal;
169
170    cromo = malloc(sizeof(cromo) * (poblacion + 2) * LONG_INDIV); if(cromo==0) printf("Error en malloc");
171    fitness = malloc(sizeof(fitness) * (poblacion+1)); if(fitness==0) printf("Error en malloc");
172    fitness2 = malloc(sizeof(fitness2) * (poblacion+1)); if(fitness2==0) printf("Error en malloc");
173    ordenpoblacion = malloc(sizeof(ordenpoblacion) * (poblacion+1)); if(ordenpoblacion==0) printf("Error en malloc");
174
175    for(i=0; i < nodos; i++) //enviar a nodos
176    {
177        create_connect_socket(server[i], &fds[i]);
178    }
179
180/* preparar socket para recibir */
181    data_socket_rx_ap = malloc(10000);
182
183if(PAR_ONLYFIT==1)
184{
185    gen_poblacion(cromo, pentarboles, vars);
186
187    // cruzar
188            for(i = ((poblacion*1)/8); i < ((poblacion*2)/8); i=i+4) //salvar los primeros 4 y cruzar
189            {
190                cross2point(cromo+i*LONG_INDIV, cromo + i*LONG_INDIV, cromo+((i)*LONG_INDIV), cromo+((i+1)*LONG_INDIV), pentarboles);
191                cross2point(cromo+i*LONG_INDIV, cromo + i*LONG_INDIV, cromo+((i+2)*LONG_INDIV), cromo+((i+3)*LONG_INDIV), pentarboles);
192            }//cruzar
193
194    // Mutacion
195            for(i = ((poblacion*2)/8); i < ((poblacion*3)/8); i++)
196            {
197                muta_indiv(cromo + i*LONG_INDIV, cromo + ((( i )) * LONG_INDIV), pentarboles, vars);
198            }
199            for(i = ((poblacion*3)/8); i < ((poblacion*4)/8); i++)
200            {
201                muta_indiv(cromo + i*LONG_INDIV, cromo + ((( i )) * LONG_INDIV), pentarboles, vars);
202            }
203
204    //crear nuevos indiv reemplazar por taras
205            for(i = ((poblacion*4)/8); i < poblacion; i++)
206            {
207                gen_indiv((cromo + ((( i)) * LONG_INDIV)), pentarboles, vars);
208            }
209
210            *generacion++;
211}
212
213    gen_datos->tamacrom = LONG_INDIV;
214
215for(j=0;j<1;j++)
216{
217    for(i=0; i<nodos;i++) //enviar a nodos
218    {
219        tx_cromo(gen_datos, fds[i]);
220    }
221
222    cromo_sal = gen_datos->cromo_sal;
223
224    for(i=0; i<nodos;i++) //recoger fitness o cromosomas resultantes
225    {
226        rx_cromo(fds[i], data_socket_rx_ap, cromo_sal+(LONG_INDIV*RESULTADOS*i), gen_datos->fitness+(RESULTADOS*i), pentarboles,generacion + (RESULTADOS*i),tiempo+(RESULTADOS*i), aux_sal+(maxgeneraciones*2*i));
227    }
228
229}
230
231    free(cromo);
232    free(fitness);
233    free(fitness2);
234    free(ordenpoblacion);
235    free(data_socket_rx_ap);
236    for(i=0; i<nodos;i++) close(fds[i]);
237}
238
239
240/*******************************************************************************************************************************/
241/*******************************************************************************************************************************/
242/*******************************************************************************************************************************/
243/*******************************************************************************************************************************/
244
245int main( int argc, char** argv )
246{
247    int *generacion, k, a, b, z, i, j= 0, error, nivel_max, vars, *tiempo, poblacion_total, m, p, iteraciones, T;
248    int conta=0, aux1, aux2, pentarboles, *fitness1, *fitness2, *entrada, *orderesult, aux, *aux_sal;
249    char o, *ap, *valor_devuelto;;
250    char *cromo_sal1, *cromo_sal2, *cromo_entrada;
251    int tamaobj, objetivo[8192], obj_combs;//= {0,254,123,16,87,56,34,76,89,155,199};
252    long int tiempo1, tiempo2;
253    float tiempof, tiempof2, Tfloat, float1, float2;
254
255    int nivel2[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
256    int nivel3[]={0,0,1,1,1,2,2,2,2,3,3, 3, 3, 4, 4, 4};
257    int nivel4[]={0,0,0,0,0,1,1,1,1,1,1, 1, 1, 1, 1, 1};
258    int nivel5[]={0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0};
259
260/* Estructuras para datos de cromosomas*/
261    struct gen_datos_tipo data_struct1, data_struct2;
262    struct gen_datos_tipo *data_struct_ap1, *data_struct_ap2;
263    data_struct_ap1 = &data_struct1;
264    data_struct_ap2 = &data_struct2;
265
266/* Variables para tablas de lut*/
267    FILE *f1;
268    int size1;
269    srand ( (time(NULL)) );
270
271/* Variables para almacenar datos para graficar*/
272    int *datos, *datos2, x, media=4, puntos;
273    FILE *fich, *fich2;
274
275    char output_file_name[128], output_file_fitness_name[128];
276    sscanf(argv[1], "%i",&vars);
277    sscanf(argv[2], "%i", &poblacion_total);
278    sscanf(argv[3], "%i", &maxgeneraciones);
279    sscanf(argv[4], "%i", &nodos);
280    sscanf(argv[5], "%s", output_file_name);
281    sscanf(argv[6], "%s", output_file_fitness_name);
282    printf("\nvars: %i indivs:%i generations:%i nodos:%i ", vars, poblacion_total, maxgeneraciones, nodos);
283    fflush(stdout);
284
285    pentarboles = 1; //MODIFIQUE NIVEL_MAX
286    nivel_max = 2;
287    poblacion = poblacion_total/nodos;
288    m = 1; //datos a migrar
289    p = maxgeneraciones;//8; //frecuencia de migracion
290    T = 4; //temperatura para crear nuevos indiv. A mayor T menor temperatura
291    i=0;
292    tamaobj=0;
293    aux = (T << 16) | p;
294    iteraciones = maxgeneraciones/p;
295    maxgeneraciones = p;
296
297    obj_combs = pow(2, (vars/2));
298
299/*Armar funcion objetivo comparador*/ /* OJO, SE ESTÁN METIENDO VALORES DE 1EXX EN LAS LUT Y ESOS INDIVIDUOS AL PARECER QUEDAN MAL */
300    for(a=0; a < obj_combs ;a++)
301    {
302        for(b=0; b < obj_combs ;b++)
303        {
304            if(a > b)z=1; if(a < b)z=2; if(a == b)z=4;
305            if((z & 0x4) != 0 )
306            {
307                objetivo[tamaobj] = i;
308                printf("%i ",objetivo[tamaobj]);
309                tamaobj++;
310            }
311            i++;
312        }
313    }
314// printf("Tama:%i ",tamaobj);
315
316/* Tabla para las LUT*/
317    f1 = fopen("funlut.dat","r");
318    if(f1 == NULL){
319            printf("\nError de lectura de archivo!");
320            return 0;}
321
322    fseek (f1, 0, SEEK_END);
323    size1 = ftell(f1);
324    funlut_ap = malloc(size1); if(funlut_ap==0) printf("Error en malloc");
325    rewind (f1);
326    fread(funlut_ap,1,size1,f1);
327    fclose(f1);
328
329    puntos = 16; /*numero de puntos para la grafica*/
330    datos = malloc(sizeof(datos)*puntos*3); if(datos==0) printf("Error en malloc");
331    fich=fopen(output_file_name,"wb");
332    datos2 = malloc(sizeof(datos2) * maxgeneraciones * p * nodos); if(datos2==0) printf("Error en malloc");
333    fich2=fopen(output_file_fitness_name,"wb");
334
335    cromo_sal1 = malloc(sizeof(cromo_sal1) * RESULTADOS * LONG_INDIV * nodos); if(cromo_sal1==0) printf("Error en malloc");
336    fitness1 = malloc(sizeof(fitness1) * RESULTADOS * nodos); if(fitness1==0) printf("Error en malloc");
337    cromo_sal2 = malloc(sizeof(cromo_sal2) * RESULTADOS * LONG_INDIV * nodos); if(cromo_sal2==0) printf("Error en malloc");
338    fitness2 = malloc(sizeof(fitness2) * RESULTADOS * nodos); if(fitness2==0) printf("Error en malloc");
339    cromo_sal2 = malloc(sizeof(cromo_sal2) * RESULTADOS * LONG_INDIV * nodos); if(cromo_sal2==0) printf("Error en malloc");
340    generacion = malloc(sizeof(generacion)* RESULTADOS * nodos); if(generacion==0) printf("Error en malloc");
341    tiempo = malloc(sizeof(tiempo)* RESULTADOS * nodos); if(tiempo==0) printf("Error en malloc");
342    cromo_entrada = malloc(sizeof(cromo_entrada)* LONG_INDIV * m); if(cromo_entrada==0) printf("Error en malloc");
343    orderesult = malloc(sizeof(orderesult) * nodos*RESULTADOS); if(orderesult==0) printf("Error en malloc");
344    aux_sal = malloc(sizeof(aux_sal) * nodos * maxgeneraciones * 2); if(aux_sal==0) printf("Error en malloc");
345
346    data_struct_ap1->objetivo = objetivo;
347    data_struct_ap1->tamaobj = tamaobj;
348    data_struct_ap1->pentarboles = pentarboles;
349    data_struct_ap1->maxgen = maxgeneraciones;
350    data_struct_ap1->cromo_sal = cromo_sal1;
351    data_struct_ap1->fitness = fitness1;
352    data_struct_ap1->cromo_entrada = cromo_entrada;
353    data_struct_ap1->fitness_entrada = 0;
354    data_struct_ap1->nivel_max = nivel_max;
355    data_struct_ap1->vars= vars;
356    data_struct_ap1->en_cromo_entrada = 0;
357    data_struct_ap1->generacion = generacion;
358    data_struct_ap1->tiempo = tiempo;
359    data_struct_ap1->aux = aux;
360    data_struct_ap1->aux_sal = aux_sal;
361
362/* printf("\npentarboles:%i nivel_max%i ", pentarboles ,nivel_max);*/
363/* fflush(stdout); */
364
365        /* Iniciar evolucion */
366        x = 0;
367    for(k = 0; k < iteraciones ; k++)
368    {
369        tiempo1 = get_timestamp();
370        iniciar_evol(data_struct_ap1); //evolution
371        tiempo2 = get_timestamp();
372        *tiempo = tiempo2 - tiempo1;
373        tiempof2 = *tiempo;
374        tiempof = tiempof2/(1000000);
375        printf("\n%i %i %i %5f",nodos, vars, poblacion_total, tiempof);
376        //fprintf(fich, "\n%i %i %i %5f",nodos, vars, poblacion_total, tiempof);
377
378        for(i = 0; i < nodos*RESULTADOS; i++) //Organizar lo q llego, ¡solo se indexa orderesult, que dice en q orden estan los cromosomas!
379        {
380            *(orderesult + i) = i; //se inicializa el stream para el orden
381        }
382
383        for(i=0; i< nodos*RESULTADOS; i++)
384        {
385            for(j=i+1; j< nodos*RESULTADOS; j++)
386            {
387                if(*(fitness1 + j) < *(fitness1 + i))
388                {
389                    aux1 = *(orderesult + i);
390                    *(orderesult + i) = *(orderesult + j);
391                    aux2 = *(fitness1 + i);
392                    *(fitness1 + i) = *(fitness1 + j);
393                    *(orderesult + j) = aux1;
394                    *(fitness1 + j) = aux2;
395                    aux1 = *(generacion + i);
396                    *(generacion + i) = *(generacion + j);
397                    *(generacion + j) = aux1;
398                }
399            }
400        }
401
402        for(i = 0; i < maxgeneraciones; i=i+maxgeneraciones) //revisar mediciones
403        {
404            aux1 = 0;
405            aux2 = 0;
406            for(j = 0; j < nodos; j++)
407            {
408                aux1 = aux1 + *(aux_sal + (maxgeneraciones * 2 * j) + i);
409                aux2 = aux2 + *(aux_sal + (maxgeneraciones * 2 * j) + maxgeneraciones + i);
410            }
411            aux1 = aux1 / nodos;
412            aux2 = aux2 / nodos;
413            if((x&(((iteraciones*p)/puntos)-1)) == 0x0)
414                fprintf(fich2, "%i %i %i\n",(k*p)+i, aux1, aux2);
415
416// printf("%i %i %i %i\n",(k*p)+i, aux1, aux2, T);
417        }
418
419        for(i = 0; i < LONG_INDIV; i++ )
420        {
421           *(char *)(cromo_entrada + i) = *(char *)(cromo_sal1 + (*(orderesult)*LONG_INDIV) + i);
422        }
423        data_struct_ap1->en_cromo_entrada = 1;
424        x = x + p;
425        float1 = k;
426        float2 = iteraciones;
427        Tfloat = (float1/float2)*4;
428        T = 1 + (int)Tfloat;
429        aux = (T << 16) | p;
430           data_struct_ap1->aux = aux;
431    }
432
433    for(i=0; i < RESULTADOS-1 ;i++)
434    {
435        printf("\nfit%i:%i gnrcn:%i ", i, *(fitness1), *(generacion + *(orderesult + i)));
436        mostrar_indiv(cromo_sal1 + ( *orderesult * LONG_INDIV ), pentarboles, vars);
437    }
438
439    x++;
440
441    free(cromo_sal1);
442    free(fitness1);
443    free(cromo_sal2);
444    free(fitness2);
445    free(cromo_entrada);
446    free(orderesult);
447    free(generacion);
448    free(tiempo);
449    free(aux_sal);
450
451fclose(fich);
452fclose(fich2);
453
454free(datos);
455free(datos2);
456free(funlut_ap);
457
458return 0;
459}
460

Archive Download this file

Branches:
master



interactive