#include #include #include #include #include #include #include #include "suma.h" #ifdef OPT #define N_INTENTOS 4 #define TOLERANCIA 1.5 #else #define N_INTENTOS 1 #define TOLERANCIA 0.01 #endif // ---------------------------------------------------- // Funcion que entrega el tiempo transcurrido desde el lanzamiento del // programa en milisegundos static int time0= 0; static int getTime0() { struct timeval Timeval; gettimeofday(&Timeval, NULL); return Timeval.tv_sec*1000+Timeval.tv_usec/1000; } void resetTime() { time0= getTime0(); } int getTime() { return getTime0()-time0; } // ---------------------------------------------------- Set buscarSeq(int a[], int n) { Set comb= (1<<(n-1)<<1)-1; // 2n-1: nro. de combinaciones for (Set k= 1; k<=comb; k++) { // k es el mapa de bits para el subconjunto { a[i] | bit ki de k es 1 } long long sum= 0; for (int i= 0; i=TOLERANCIA) break; } if (i>=N_INTENTOS) { fprintf(stderr, "Despues de %d intentos no obtuvo un speedup de %f\n", N_INTENTOS, TOLERANCIA); fprintf(stderr, "Revise la paralelizacion.\n"); exit(1); } printf("test 1 aprobado: speedup >= %f\n", TOLERANCIA); printf("\n--------------------------------------------------\n\n"); printf("Uno ejemplo muy grande con n=29, con solucion\n"); int c[]= { -3, -2, 5, 122737, -37364, 287373, 27267, 967923, -25383, 924973, -28973, 278363, 28272, 98734, -26735, 983267, 674998, 72537, 116725, 72537, 27263, 82739, 829276, 5383715, 675483, -28334, 38495, 374943, 278367}; printf("Calculando secuencialmente 2 veces\n"); resetTime(); k= buscarSeq(c, 29); tiempo_sec= getTime(); mostrar(c, k, 29); printf("Tiempo secuencial= %d milisegundos\n", tiempo_sec); printf("Calculando en paralelo\n"); resetTime(); int k29= buscar(c, 29); if (k!=k29) { printf("Solucion incorrecta:\n"); mostrar(c, k29, 29); printf("Debio ser:\n"); mostrar(c, k, 29); exit(1); } tiempo_par= getTime(); speedUp= (double)tiempo_sec/tiempo_par; printf("buscar par tiempo= %d miliseg., speedup= %f\n", tiempo_par, speedUp); printf("No se preocupe. Es normal que la version paralela se demore\n" "mucho mas. Pero piense por que.\n\n"); printf("Felicitaciones: su tarea funciona\n"); return 0; }