Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
| 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 | /* |
| 125 | evalfit_ptr2 = (int *)init_peripheral(EVALFIT_PHYSBASE2, basemem); |
| 126 | evalfit_ptr3 = (int *)init_peripheral(EVALFIT_PHYSBASE3, basemem); |
| 127 | evalfit_ptr4 = (int *)init_peripheral(EVALFIT_PHYSBASE4, basemem); |
| 128 | */ |
| 129 | } |
| 130 | data_struct_ap1 = &data_struct1; |
| 131 | data_struct_ap2 = &data_struct2; |
| 132 | |
| 133 | /* preparar socket para recibir */ |
| 134 | data_socket_rx_ap = malloc(100000); |
| 135 | data_socket_tx_ap = malloc(100000); |
| 136 | fd = crear_socket(); |
| 137 | if(listen(fd, BACKLOG) == -1) |
| 138 | { printf("error en listen()\n"); |
| 139 | exit(-1); |
| 140 | } |
| 141 | |
| 142 | //int err, sndsize=8192; |
| 143 | //err = setsockopt(fd2, SOL_SOCKET, SO_RCVBUF, (char *)&sndsize, (int)sizeof(sndsize)); |
| 144 | |
| 145 | while(1) |
| 146 | { |
| 147 | sin_size=sizeof(struct sockaddr_in); |
| 148 | if ((fd2 = accept(fd,(struct sockaddr *)&client, &sin_size))==-1) /* llamada a accept() */ |
| 149 | { |
| 150 | printf("error en accept()\n"); |
| 151 | exit(-1); |
| 152 | } |
| 153 | /* printf("Se obtuvo una conexion desde %s\n", inet_ntoa(client.sin_addr) ); //mostrara la IP del cliente QUITAR PARA GANAR VELOCIDAD*/ |
| 154 | |
| 155 | numbytes=0; |
| 156 | do |
| 157 | { |
| 158 | if ((aux1=recv(fd2,data_socket_rx_ap+numbytes,8192,0)) == -1){ /* recibir datos del cliente*/ |
| 159 | perror("recv"); |
| 160 | printf("Error en recv() \n"); |
| 161 | exit(-1); |
| 162 | } |
| 163 | numbytes = numbytes+aux1; |
| 164 | }while(*(int *)(data_socket_rx_ap+numbytes-4)!=htonl(0xa55a9669)); |
| 165 | |
| 166 | ap1 = data_socket_rx_ap; /* Cargar datos en la estructura para la evolucion */ |
| 167 | data_struct_ap1->tamaobj = htonl(*(int *)ap1) & 0xFFFF; |
| 168 | maxgeneraciones = htonl(*(int *)ap1) >> 16; |
| 169 | //printf("\ntama obj:%0x maxgens:%i numbytes:%i-- ", data_struct_ap1->tamaobj, maxgeneraciones,numbytes); |
| 170 | fflush(stdout); |
| 171 | ap1 = ap1 + 4; |
| 172 | for(i=0;i < (data_struct_ap1->tamaobj); i++) |
| 173 | { |
| 174 | objetivo[i] =htonl(*(int *)ap1); //XX |
| 175 | //printf("obj:%i %i ",i,objetivo[i]); |
| 176 | ap1 = ap1 + 4; |
| 177 | } |
| 178 | data_struct_ap1->objetivo = objetivo; |
| 179 | data_struct_ap1->pentarboles = htonl(*(int *)ap1) & 0XFF; |
| 180 | pentarboles = data_struct_ap1->pentarboles; |
| 181 | vars = htonl(*(int *)ap1) >> 16; |
| 182 | data_struct_ap1->vars = vars; |
| 183 | ap1 = ap1 + 4; |
| 184 | data_struct_ap1->tamacrom = htonl(*(int *)ap1) & 0xFFFF; |
| 185 | poblacion = htonl(*(int *)ap1) >> 16; |
| 186 | //printf("\ntamacrom:%0x poblacion:%i vars:%i pentarboles:%i maxgeneraciones:%i ", data_struct_ap1->tamacrom, poblacion,vars,pentarboles,maxgeneraciones); |
| 187 | ap1 = ap1 + 4; |
| 188 | cromo_entrada = malloc(sizeof(cromo_entrada) * RESULTADOS * LONG_INDIV); if(cromo_entrada==0) printf("Error en malloc"); |
| 189 | data_struct_ap1->cromo_entrada = cromo_entrada; |
| 190 | |
| 191 | for(i = 0; i < (data_struct_ap1 -> tamacrom); i++) |
| 192 | { |
| 193 | *(char *)(cromo_entrada + i) = *(char *)ap1; |
| 194 | ap1 = ap1 + 1; |
| 195 | } |
| 196 | |
| 197 | data_struct_ap1->fitness_entrada = htonl(*(int *)ap1); |
| 198 | ap1 = ap1 + 4; |
| 199 | data_struct_ap1->nivel_max = htonl(*(int *)ap1) & 0xFFFF; |
| 200 | data_struct_ap1->en_cromo_entrada = htonl(*(int *)ap1) >> 16; //han enviado un cromosoma? |
| 201 | ap1 = ap1 + 4; |
| 202 | data_struct_ap1->aux = htonl(*(int *)ap1); |
| 203 | //printf("\nnivel_max:%0x-- ", data_struct_ap1->nivel_max); |
| 204 | data_struct_ap1->maxgen = maxgeneraciones; |
| 205 | |
| 206 | cromo_sal1 = malloc(sizeof(cromo_sal1) * RESULTADOS * LONG_INDIV); if(cromo_sal1==0) printf("Error en malloc"); /* reservar para cromosomas */ |
| 207 | fitness1 = malloc(sizeof(fitness1) * RESULTADOS); if(fitness1==0) printf("Error en malloc"); |
| 208 | cromo_sal2 = malloc(sizeof(cromo_sal2) * RESULTADOS * LONG_INDIV); if(cromo_sal2==0) printf("Error en malloc"); |
| 209 | fitness2 = malloc(sizeof(fitness2) * RESULTADOS); if(fitness2==0) printf("Error en malloc"); |
| 210 | generacion = malloc(sizeof(generacion)); if(generacion==0) printf("Error en malloc"); |
| 211 | tiempo = malloc(sizeof(tiempo)* RESULTADOS ); if(tiempo==0) printf("Error en malloc"); |
| 212 | aux_sal = malloc(sizeof(aux_sal)* maxgeneraciones * 2 ); if(aux_sal==0) printf("Error en malloc"); |
| 213 | |
| 214 | data_struct_ap1->generacion = generacion; |
| 215 | data_struct_ap1->cromo_sal = cromo_sal1; |
| 216 | data_struct_ap1->fitness = fitness1; |
| 217 | data_struct_ap1->fitness_entrada = 0; |
| 218 | data_struct_ap1->tiempo = tiempo; |
| 219 | data_struct_ap1->aux_sal = aux_sal; |
| 220 | |
| 221 | *generacion = 0; |
| 222 | |
| 223 | evolucionar(data_struct_ap1); // evolucionar |
| 224 | |
| 225 | /* |
| 226 | for(i=0; i < RESULTADOS ;i++) |
| 227 | { |
| 228 | printf(" %i-- fit:%i \t",i , *(fitness1 + i)); |
| 229 | mostrar_indiv(cromo_sal1 + (i*LONG_INDIV), pentarboles, vars); |
| 230 | } |
| 231 | */ |
| 232 | |
| 233 | ap1 = data_socket_tx_ap; /* devolver resultados */ |
| 234 | *(int *)ap1 = ntohl(LONG_INDIV); |
| 235 | ap1 = ap1 + 4; |
| 236 | |
| 237 | for(i=0; i < RESULTADOS ;i++) |
| 238 | { |
| 239 | for(j=0; j < LONG_INDIV ;j++) |
| 240 | { |
| 241 | *(char *)ap1 = *(cromo_sal1 + j + (LONG_INDIV * i)); |
| 242 | ap1 ++; |
| 243 | } |
| 244 | *(int *)ap1 = ntohl(((*generacion) << 16) | (*(fitness1 + i))); |
| 245 | ap1 = ap1 + 4; |
| 246 | *(int *)ap1 = ntohl(*tiempo); |
| 247 | ap1 = ap1 + 4; |
| 248 | } |
| 249 | for(i=0; i < maxgeneraciones*2 ;i++) //devolver mediciones |
| 250 | { |
| 251 | *(int *)ap1 = ntohl(*(aux_sal+i)); |
| 252 | ap1 = ap1 + 4; |
| 253 | } |
| 254 | numbytes = ap1 - data_socket_tx_ap; |
| 255 | send(fd2, data_socket_tx_ap, numbytes, 0); /* devuelve cromosoma y fitness al cliente */ |
| 256 | /* printf("\n");*/ |
| 257 | |
| 258 | close(fd2); |
| 259 | free(cromo_entrada); |
| 260 | free(cromo_sal1); |
| 261 | free(fitness1); |
| 262 | free(cromo_sal2); |
| 263 | free(fitness2); |
| 264 | free(generacion); |
| 265 | free(tiempo); |
| 266 | free(aux_sal); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | if(HW_ENABLE == 1){ |
| 271 | close_peripheral(evalfit_ptr1); |
| 272 | /* |
| 273 | close_peripheral(evalfit_ptr2); |
| 274 | close_peripheral(evalfit_ptr3); |
| 275 | close_peripheral(evalfit_ptr4); |
| 276 | */ |
| 277 | } |
| 278 | close(basemem); |
| 279 | |
| 280 | |
| 281 | free(funlut_ap); |
| 282 | free(puertas_ap); |
| 283 | free(data_socket_rx_ap); |
| 284 | free(data_socket_tx_ap); |
| 285 | shutdown(fd, SHUT_RDWR); |
| 286 | close(fd); |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 |
Branches:
master
