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 | ** powerpc-405-linux-gnu-gcc sintesishw_server.c -lm -lpthread -o sintesishw_server_ppc |
9 | ** |
10 | ** |
11 | **********************************************************************************************************/ |
12 | |
13 | #include <stdio.h> |
14 | #include <termios.h> |
15 | #include <sys/mman.h> |
16 | #include <stdlib.h> |
17 | #include <sys/types.h> |
18 | #include <sys/stat.h> |
19 | #include <fcntl.h> |
20 | #include <time.h> |
21 | #include <math.h> |
22 | #include <pthread.h> |
23 | #include <stdio.h> |
24 | #include <sys/socket.h> |
25 | #include <netinet/in.h> |
26 | #include <netdb.h> |
27 | #include <errno.h> |
28 | #include <sys/un.h> |
29 | #include <sintesishw_server.h> |
30 | |
31 | /************************************************************************************************************************************** |
32 | crea socket */ |
33 | int crear_socket() |
34 | { |
35 | int fd; |
36 | struct sockaddr_in server; |
37 | |
38 | if ((fd=socket(AF_INET, SOCK_STREAM, 0)) == -1 ) /* Crear socket */ |
39 | { |
40 | perror("socket"); |
41 | exit(-1); |
42 | } |
43 | server.sin_family = AF_INET; |
44 | server.sin_port = htons(PORT); /* cambiar endianismo */ |
45 | server.sin_addr.s_addr = INADDR_ANY; /* INADDR_ANY coloca nuestra direccion IP */ |
46 | bzero(&(server.sin_zero),8); /* escribimos ceros en el resto de la estructura */ |
47 | if (setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&server,sizeof(struct sockaddr)) == -1) /* permite reutilizar el puerto */ |
48 | { |
49 | perror("setsockopt"); |
50 | exit(1); |
51 | } |
52 | if(bind(fd,(struct sockaddr*) & server, sizeof(struct sockaddr))==-1) //se asigna el socket |
53 | { |
54 | perror("bind"); |
55 | exit(EXIT_FAILURE); |
56 | } |
57 | return fd; |
58 | } |
59 | |
60 | |
61 | /*******************************************************************************************************************************/ |
62 | /*******************************************************************************************************************************/ |
63 | /*******************************************************************************************************************************/ |
64 | /*******************************************************************************************************************************/ |
65 | |
66 | int main() |
67 | { |
68 | int k, a, i, j = 0, error, vars = 4, *tiempo; |
69 | int conta=0, aux1, aux2, pentarboles, *fitness1, *fitness2, *entrada, *generacion, aux, *aux_sal; |
70 | char o, *ap, *valor_devuelto;; |
71 | char *cromo_sal1, *cromo_sal2, *cromo_entrada, *cromo_ap_aux; |
72 | int objetivo[8192];//= {0,254,123,16,87,56,34,76,89,155,199}; |
73 | struct gen_datos_tipo data_struct1, data_struct2; |
74 | struct gen_datos_tipo *data_struct_ap1, *data_struct_ap2; |
75 | |
76 | int nivel2[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; |
77 | int nivel3[]={0,0,1,1,1,2,2,2,2,3,3, 3, 3, 4, 4, 4}; |
78 | int nivel4[]={0,0,0,0,0,1,1,1,1,1,1, 1, 1, 1, 1, 1}; |
79 | int nivel5[]={0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0}; |
80 | |
81 | /* Variables para tablas de lut*/ |
82 | FILE *f1, *f2; |
83 | int size1, size2; |
84 | |
85 | /* Variables para los sockets */ |
86 | int fd, fd2, numbytes; /* punteros para sockets */ |
87 | struct sockaddr_in server; |
88 | struct sockaddr_in client; |
89 | void *data_socket_rx_ap, *data_socket_tx_ap, *ap1; |
90 | int sin_size; |
91 | |
92 | /*para periferico*/ |
93 | int basemem; |
94 | |
95 | srand ( (time(NULL)) ); |
96 | |
97 | /* Tabla para las LUT*/ |
98 | f1 = fopen("funlut.dat","r"); |
99 | if(f1 == NULL){ printf("\nError de lectura de archivo!");return 0;} |
100 | fseek (f1, 0, SEEK_END); |
101 | size1 = ftell(f1); |
102 | funlut_ap = malloc(size1); if(funlut_ap==0) printf("Error en malloc"); |
103 | rewind (f1); |
104 | fread(funlut_ap,1,size1,f1); |
105 | fclose(f1); |
106 | |
107 | f2 = fopen("puertas.dat","r"); |
108 | if(f2 == NULL){ printf("\nError de lectura de archivo!");return 0;} |
109 | fseek (f2, 0, SEEK_END); |
110 | size2 = ftell(f2); |
111 | puertas_ap = malloc(size2); if(puertas_ap==0) printf("Error en malloc"); |
112 | rewind (f2); |
113 | fread(puertas_ap,1,size2,f2); |
114 | fclose(f2); |
115 | |
116 | basemem = open("/dev/mem", (O_RDWR | O_SYNC)); //abrir dispositivo memoria para mapear dir del periferico |
117 | if(basemem == -1) |
118 | { printf("Error al abrir /dev/mem \n"); |
119 | return -1;} |
120 | |
121 | /** iniciar periferico **/ |
122 | if(HW_ENABLE == 1){ |
123 | evalfit_ptr1 = (int *)init_peripheral(EVALFIT_PHYSBASE1, basemem); |
124 | evalfit_ptr2 = (int *)init_peripheral(EVALFIT_PHYSBASE2, basemem); |
125 | evalfit_ptr3 = (int *)init_peripheral(EVALFIT_PHYSBASE3, basemem); |
126 | evalfit_ptr4 = (int *)init_peripheral(EVALFIT_PHYSBASE4, basemem); |
127 | } |
128 | data_struct_ap1 = &data_struct1; |
129 | data_struct_ap2 = &data_struct2; |
130 | |
131 | /* preparar socket para recibir */ |
132 | data_socket_rx_ap = malloc(100000); |
133 | data_socket_tx_ap = malloc(100000); |
134 | fd = crear_socket(); |
135 | if(listen(fd, BACKLOG) == -1) |
136 | { printf("error en listen()\n"); |
137 | exit(-1); |
138 | } |
139 | |
140 | //int err, sndsize=8192; |
141 | //err = setsockopt(fd2, SOL_SOCKET, SO_RCVBUF, (char *)&sndsize, (int)sizeof(sndsize)); |
142 | |
143 | while(1) |
144 | { |
145 | sin_size=sizeof(struct sockaddr_in); |
146 | if ((fd2 = accept(fd,(struct sockaddr *)&client, &sin_size))==-1) /* llamada a accept() */ |
147 | { |
148 | printf("error en accept()\n"); |
149 | exit(-1); |
150 | } |
151 | /* printf("Se obtuvo una conexion desde %s\n", inet_ntoa(client.sin_addr) ); //mostrara la IP del cliente QUITAR PARA GANAR VELOCIDAD*/ |
152 | |
153 | numbytes=0; |
154 | do |
155 | { |
156 | if ((aux1=recv(fd2,data_socket_rx_ap+numbytes,8192,0)) == -1){ /* recibir datos del cliente*/ |
157 | perror("recv"); |
158 | printf("Error en recv() \n"); |
159 | exit(-1); |
160 | } |
161 | numbytes = numbytes+aux1; |
162 | }while(*(int *)(data_socket_rx_ap+numbytes-4)!=htonl(0xa55a9669)); |
163 | |
164 | ap1 = data_socket_rx_ap; /* Cargar datos en la estructura para la evolucion */ |
165 | data_struct_ap1->tamaobj = htonl(*(int *)ap1) & 0xFFFF; |
166 | maxgeneraciones = htonl(*(int *)ap1) >> 16; |
167 | // printf("\ntama obj:%0x maxgens:%i numbytes:%i-- ", data_struct_ap1->tamaobj, maxgeneraciones,numbytes); |
168 | fflush(stdout); |
169 | ap1 = ap1 + 4; |
170 | for(i=0;i < (data_struct_ap1->tamaobj); i++) |
171 | { |
172 | objetivo[i] =htonl(*(int *)ap1); //XX |
173 | // printf("obj:%i %i ",i,objetivo[i]); |
174 | ap1 = ap1 + 4; |
175 | } |
176 | data_struct_ap1->objetivo = objetivo; |
177 | data_struct_ap1->pentarboles = htonl(*(int *)ap1) & 0XFF; |
178 | pentarboles = data_struct_ap1->pentarboles; |
179 | vars = htonl(*(int *)ap1) >> 16; |
180 | data_struct_ap1->vars = vars; |
181 | ap1 = ap1 + 4; |
182 | data_struct_ap1->tamacrom = htonl(*(int *)ap1) & 0xFFFF; |
183 | poblacion = htonl(*(int *)ap1) >> 16; |
184 | // printf("\ntamacrom:%0x poblacion:%i vars:%i pentarboles:%i maxgeneraciones:%i ", data_struct_ap1->tamacrom, poblacion,vars,pentarboles,maxgeneraciones); |
185 | ap1 = ap1 + 4; |
186 | cromo_entrada = malloc(sizeof(cromo_entrada) * RESULTADOS * LONG_INDIV); if(cromo_entrada==0) printf("Error en malloc"); |
187 | data_struct_ap1->cromo_entrada = cromo_entrada; |
188 | |
189 | for(i = 0; i < (data_struct_ap1 -> tamacrom); i++) |
190 | { |
191 | *(char *)(cromo_entrada + i) = *(char *)ap1; |
192 | ap1 = ap1 + 1; |
193 | } |
194 | |
195 | data_struct_ap1->fitness_entrada = htonl(*(int *)ap1); |
196 | ap1 = ap1 + 4; |
197 | data_struct_ap1->nivel_max = htonl(*(int *)ap1) & 0xFFFF; |
198 | data_struct_ap1->en_cromo_entrada = htonl(*(int *)ap1) >> 16; //han enviado un cromosoma? |
199 | ap1 = ap1 + 4; |
200 | data_struct_ap1->aux = htonl(*(int *)ap1); |
201 | /* printf("\nnivel_max:%0x-- ", data_struct_ap1->nivel_max);*/ |
202 | data_struct_ap1->maxgen = maxgeneraciones; |
203 | |
204 | cromo_sal1 = malloc(sizeof(cromo_sal1) * RESULTADOS * LONG_INDIV); if(cromo_sal1==0) printf("Error en malloc"); /* reservar para cromosomas */ |
205 | fitness1 = malloc(sizeof(fitness1) * RESULTADOS); if(fitness1==0) printf("Error en malloc"); |
206 | cromo_sal2 = malloc(sizeof(cromo_sal2) * RESULTADOS * LONG_INDIV); if(cromo_sal2==0) printf("Error en malloc"); |
207 | fitness2 = malloc(sizeof(fitness2) * RESULTADOS); if(fitness2==0) printf("Error en malloc"); |
208 | generacion = malloc(sizeof(generacion)); if(generacion==0) printf("Error en malloc"); |
209 | tiempo = malloc(sizeof(tiempo)* RESULTADOS ); if(tiempo==0) printf("Error en malloc"); |
210 | aux_sal = malloc(sizeof(aux_sal)* maxgeneraciones * 2 ); if(aux_sal==0) printf("Error en malloc"); |
211 | |
212 | data_struct_ap1->generacion = generacion; |
213 | data_struct_ap1->cromo_sal = cromo_sal1; |
214 | data_struct_ap1->fitness = fitness1; |
215 | data_struct_ap1->fitness_entrada = 0; |
216 | data_struct_ap1->tiempo = tiempo; |
217 | data_struct_ap1->aux_sal = aux_sal; |
218 | |
219 | *generacion = 0; |
220 | |
221 | evolucionar(data_struct_ap1); // evolucionar |
222 | |
223 | /* for(i=0; i < RESULTADOS ;i++)*/ |
224 | /* {*/ |
225 | /* printf(" %i-- fit:%i \t",i , *(fitness1 + i));*/ |
226 | /* mostrar_indiv(cromo_sal1 + (i*LONG_INDIV), pentarboles, vars);*/ |
227 | /* }*/ |
228 | |
229 | ap1 = data_socket_tx_ap; /* devolver resultados */ |
230 | *(int *)ap1 = ntohl(LONG_INDIV); |
231 | ap1 = ap1 + 4; |
232 | |
233 | for(i=0; i < RESULTADOS ;i++) |
234 | { |
235 | for(j=0; j < LONG_INDIV ;j++) |
236 | { |
237 | *(char *)ap1 = *(cromo_sal1 + j + (LONG_INDIV * i)); |
238 | ap1 ++; |
239 | } |
240 | *(int *)ap1 = ntohl(((*generacion) << 16) | (*(fitness1 + i))); |
241 | ap1 = ap1 + 4; |
242 | *(int *)ap1 = ntohl(*tiempo); |
243 | ap1 = ap1 + 4; |
244 | } |
245 | for(i=0; i < maxgeneraciones*2 ;i++) //devolver mediciones |
246 | { |
247 | *(int *)ap1 = ntohl(*(aux_sal+i)); |
248 | ap1 = ap1 + 4; |
249 | } |
250 | numbytes = ap1 - data_socket_tx_ap; |
251 | send(fd2, data_socket_tx_ap, numbytes, 0); /* devuelve cromosoma y fitness al cliente */ |
252 | /* printf("\n");*/ |
253 | |
254 | close(fd2); |
255 | free(cromo_entrada); |
256 | free(cromo_sal1); |
257 | free(fitness1); |
258 | free(cromo_sal2); |
259 | free(fitness2); |
260 | free(generacion); |
261 | free(tiempo); |
262 | free(aux_sal); |
263 | } |
264 | |
265 | |
266 | if(HW_ENABLE == 1){ |
267 | close_peripheral(evalfit_ptr1); |
268 | close_peripheral(evalfit_ptr2); |
269 | close_peripheral(evalfit_ptr3); |
270 | close_peripheral(evalfit_ptr4); |
271 | } |
272 | close(basemem); |
273 | |
274 | |
275 | free(funlut_ap); |
276 | free(puertas_ap); |
277 | free(data_socket_rx_ap); |
278 | free(data_socket_tx_ap); |
279 | shutdown(fd, SHUT_RDWR); |
280 | close(fd); |
281 | |
282 | return 0; |
283 | } |
284 |
Branches:
master