You are on page 1of 4

PRCTICA

Speed-up

OBJETIVO

El alumno conocer la medida de la mejora que se consigue al


ejecutar un programa en varios procesadores

Al final de esta prctica el alumno:


Entender las ventajas del cmputo paralelo y del uso de una
arquitectura con mas de un procesador .

EJERCICIO

Dado el programa que calcula la integral de la funcin x2 mediante la suma del rea de un cierto nmero de trapecios que
cubren la curva de la funcin entre dos puntos. El programa solicita los dos extremos de la funcin y el nmero de trapecios.

El cdigo serie.c es el programa serie y paralelo.c la versin paralela MPI.

El objetivo del ejercicio es medir el tiempo de ejecucin de ambos programas, usando los siguientes
parmetros:

- lmites de la integral: 0 y 1
- nmero de trapecios: 120 - 1.200 - 12.000 - 120.000 - 1.200.000 - 12.000.000 - 120.000.000
- nmero de procesadores (caso paralelo): 2, 4, 6, 8, 10 y 12

Actividad

Obtener una grfica de los tiempos de ejecucin serie y paralelo en funcin del nmero de
trapecios, y en otra donde se muestre el factor de aceleracin (speed-up) obtenido en relacin al caso serie.

Utilizar las escalas ms adecuadas para las grficas, de manera que se observe correctamente el comportamiento del
programa. Interpreta y justifica los resultados obtenidos.
PRCTICA
Speed-up
Ejercicio 1: serie.c
/*********************************************************
serie.c
Integral de y = x * x. Suma de area de trapecios
Programa Serie
*********************************************************/
#include <stdio.h>
#include <sys/time.h>

double f(double x);


int main()
{
struct timeval t0, t1; /* tiempo de ejecucin */
double tej;
float a, b; /* Limites de la integral */
int n; /* Numero de trapecios a sumar */
double h; /* Anchura de los trapecios */
double resul; /* Resultado */
int i;
double x;

printf("Introduce a, b (limites) y n (num. de trap.) \n");


scanf("%f %f %d", &a, &b, &n);
a = (double) a;
b = (double) b;
gettimeofday(&t0,0);

/* calculo de la integral */
h = (b-a)/n; /* anchura de los trapecios */
resul = (f(a) + f(b)) / 2.0;
x = a;
for (i=1; i<n; i++)
{
x = x + h;
resul = resul + f(x);
}

resul = resul * h;
gettimeofday(&t1,0);

tej = (t1.tv_sec t0.tv_sec) + (t1.tv_usec t0.tv_usec)/1e6;

printf("\n Integral de x*x (%1.1f - %1.1f, con %d trap.) = %1.12f \n", a, b, n, resul);
printf(" Error = %1.12f \n", resul - (b*b*b-a*a*a)/3.0 );
printf(" Tiempo ejecucion (serie) = %1.3f ms \n\n", tej*1000);
return 0;

} /* main */

double f(double x) // Funcion a integrar: x * x


{
double y;
y = x * x;
return y;
}
PRCTICA
Speed-up
Ejercicio 1: paralelo.c
/************************************************************
MPI: paralelo.c
Integral de y = x * x. Suma de area de trapecios
Programa Paralelo MPI
*************************************************************/
#include <stdio.h>
#include <mpi.h>
double t0, t1;

void Leer_datos(double* a_ptr, double* b_ptr, int* n_ptr, int pid, int npr);
double Integrar(double a_loc, double b_loc, int n_loc, double h);
double f(double x);

int main(int argc, char** argv)


{
int pid, npr, root;
double a, b, h, a_loc, b_loc;
int n, n_loc;
double resul, resul_loc; /* Resultado de la integral */

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &pid);
MPI_Comm_size(MPI_COMM_WORLD, &npr);

/* Lectura y distribucion de parametros a todos los procesadores */


Leer_datos(&a, &b, &n, pid, npr);
h = (b-a) / n;
n_loc = n / npr;
a_loc = a + pid * n_loc * h;
b_loc = a_loc + n_loc * h;

/*Calculo de la integral local */


resul_loc = Integrar(a_loc, b_loc, n_loc, h);

/* Suma de resultados parciales */


resul = 0.0; root = 0;
MPI_Reduce(&resul_loc, &resul, 1, MPI_DOUBLE, MPI_SUM, root, MPI_COMM_WORLD);

/* Impresion de resultados */
if (pid == 0)
{
t1 = MPI_Wtime();
printf("\n Integral MPI de x*x (%1.1f - %1.1f, con %d trap.) = %1.12f\n", a, b, n, resul);
printf(" Error = %1.12f \n", resul - (b*b*b-a*a*a)/3.0 );
printf(" Tiempo ejecucion (%d proc.) = %1.3f ms \n\n", npr, (t1-t0)*1000);
}

MPI_Finalize();
return 0;
} /* main */
PRCTICA
Speed-up
/* Funcion Leer_datos */
void Leer_datos(double* a_ptr, double* b_ptr, int* n_ptr, int pid, int npr)
{
float aux_a, aux_b;
int root;
if (pid == 0)
{
printf("\n Introduce a, b (limites) y n (num. de trap.) \n");
scanf("%f %f %d", &aux_a, &aux_b, n_ptr);
t0 = MPI_Wtime();
(*a_ptr) = (double)aux_a;
(*b_ptr) = (double)aux_b;
}
root = 0;

MPI_Bcast(a_ptr, 1, MPI_DOUBLE, root, MPI_COMM_WORLD);


MPI_Bcast(b_ptr, 1, MPI_DOUBLE, root, MPI_COMM_WORLD);
MPI_Bcast(n_ptr, 1, MPI_INT, root, MPI_COMM_WORLD);
} /* Leer_datos */

/*Funcion a integrar */
double f(double x)
{
double y;
y = x*x;
return y;
}

/* FUNCION Integrar: calculo local de la integral */


double Integrar(double a_loc, double b_loc, int n_loc, double h)
{
double resul_loc, x;
int i;

/* calculo de la integral */

resul_loc = (f(a_loc) + f(b_loc)) / 2.0;


x = a_loc;
for (i=1; i<n_loc; i++)
{
x = x + h;
resul_loc = resul_loc + f(x);
}
resul_loc = resul_loc * h;
return resul_loc;
} /* Integrar */

You might also like