Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
Source at commit 1f2712e created 13 years 5 months ago. By Carlos Camargo, Adding evolvable hardware example | |
---|---|
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 | */ |
34 | create_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 | |
65 | rx_cromo(int fd, void *data_socket_rx_ap, char *cromo_sal, int *fitness, int pentarboles, int *generacion, int *tiempo, int *aux_sal) |
66 | { |
67 | void *ap1; |
68 | int i, j, numbytes_rx; |
69 | int nivel2[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; |
70 | int nivel3[]={0,0,1,1,1,2,2,2,2,3,3, 3, 3, 4, 4, 4}; |
71 | int nivel4[]={0,0,0,0,0,1,1,1,1,1,1, 1, 1, 1, 1, 1}; |
72 | int 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 | |
103 | tx_cromo(struct gen_datos_tipo *gen_datos, int fd) |
104 | { |
105 | void *data_socket_tx_ap, *ap1; |
106 | int *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 */ |
148 | iniciar_evol(struct gen_datos_tipo *gen_datos) |
149 | { |
150 | int *generacion, k, a, i, j = 0, vars; |
151 | int conta=0, aux1, aux2, *fitness, *fitness2, *entrada, *objetivo, tamaobj, pentarboles, maxgens, *fitness_sal, fitness_entrada, nivel_max, *aux_sal; |
152 | char o, *ap, *cromo, *ordenpoblacion, *cromo_sal, *cromo_entrada; |
153 | int *tiempo; |
154 | int nivel2[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; |
155 | int nivel3[]={0,0,1,1,1,2,2,2,2,3,3, 3, 3, 4, 4, 4}; |
156 | int nivel4[]={0,0,0,0,0,1,1,1,1,1,1, 1, 1, 1, 1, 1}; |
157 | int 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 | |
183 | if(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 | |
215 | for(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 | |
245 | int 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 = 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 | |
367 | x = 0; |
368 | fflush(stdout); |
369 | for(k = 0; k < iteraciones ; k++) |
370 | { |
371 | tiempo1 = get_timestamp(); |
372 | iniciar_evol(data_struct_ap1); |
373 | tiempo2 = get_timestamp(); |
374 | *tiempo = tiempo2 - tiempo1; |
375 | tiempof2 = *tiempo; |
376 | tiempof = tiempof2/(1000000); |
377 | // printf("\n%i %i %i %5f",nodos, vars, poblacion_total, tiempof); |
378 | fprintf(fich, "\n%i %i %i %5f",nodos, vars, poblacion_total, tiempof); |
379 | |
380 | for(i = 0; i < nodos*RESULTADOS; i++) //Organizar lo q llego, ¡solo se indexa orderesult, que dice en q orden estan los cromosomas! |
381 | { |
382 | *(orderesult + i) = i; //se inicializa el stream para el orden |
383 | } |
384 | |
385 | for(i=0; i< nodos*RESULTADOS; i++) |
386 | { |
387 | for(j=i+1; j< nodos*RESULTADOS; j++) |
388 | { |
389 | if(*(fitness1 + j) < *(fitness1 + i)) |
390 | { |
391 | aux1 = *(orderesult + i); |
392 | *(orderesult + i) = *(orderesult + j); |
393 | aux2 = *(fitness1 + i); |
394 | *(fitness1 + i) = *(fitness1 + j); |
395 | *(orderesult + j) = aux1; |
396 | *(fitness1 + j) = aux2; |
397 | aux1 = *(generacion + i); |
398 | *(generacion + i) = *(generacion + j); |
399 | *(generacion + j) = aux1; |
400 | } |
401 | } |
402 | } |
403 | |
404 | for(i = 0; i < maxgeneraciones; i=i+maxgeneraciones) //revisar mediciones |
405 | { |
406 | aux1 = 0; |
407 | aux2 = 0; |
408 | for(j = 0; j < nodos; j++) |
409 | { |
410 | aux1 = aux1 + *(aux_sal + (maxgeneraciones * 2 * j) + i); |
411 | aux2 = aux2 + *(aux_sal + (maxgeneraciones * 2 * j) + maxgeneraciones + i); |
412 | } |
413 | aux1 = aux1 / nodos; |
414 | aux2 = aux2 / nodos; |
415 | if((x&(((iteraciones*p)/puntos)-1)) == 0x0) |
416 | fprintf(fich2, "%i %i %i\n",(k*p)+i, aux1, aux2); |
417 | |
418 | // printf("%i %i %i %i\n",(k*p)+i, aux1, aux2, T); |
419 | } |
420 | |
421 | for(i = 0; i < LONG_INDIV; i++ ) |
422 | { |
423 | *(char *)(cromo_entrada + i) = *(char *)(cromo_sal1 + (*(orderesult)*LONG_INDIV) + i); |
424 | } |
425 | data_struct_ap1->en_cromo_entrada = 1; |
426 | x = x + p; |
427 | float1 = k; |
428 | float2 = iteraciones; |
429 | Tfloat = (float1/float2)*4; |
430 | T = 1 + (int)Tfloat; |
431 | aux = (T << 16) | p; |
432 | data_struct_ap1->aux = aux; |
433 | } |
434 | |
435 | for(i=0; i < RESULTADOS-1 ;i++) |
436 | { |
437 | printf("\nfit%i:%i gnrcn:%i ", i, *(fitness1), *(generacion + *(orderesult + i))); |
438 | mostrar_indiv(cromo_sal1 + ( *orderesult * LONG_INDIV ), pentarboles, vars); |
439 | } |
440 | |
441 | x++; |
442 | |
443 | free(cromo_sal1); |
444 | free(fitness1); |
445 | free(cromo_sal2); |
446 | free(fitness2); |
447 | free(cromo_entrada); |
448 | free(orderesult); |
449 | free(generacion); |
450 | free(tiempo); |
451 | free(aux_sal); |
452 | |
453 | fclose(fich); |
454 | fclose(fich2); |
455 | |
456 | free(datos); |
457 | free(datos2); |
458 | free(funlut_ap); |
459 | |
460 | return 0; |
461 | } |
462 |
Branches:
master