0% found this document useful (0 votes)
51 views6 pages

U1 Programa4 S12021

The document contains 4 C programs that calculate the dot product of two vectors in parallel using different approaches: serial, OpenMP, MPI, and a hybrid OpenMP/MPI version. The student is asked to review the programs and comment on the differences in terms of granularity, communication, memory type, and parallelization type. They are also asked to make a comparative table of execution times using many data points.

Uploaded by

karla estefania
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views6 pages

U1 Programa4 S12021

The document contains 4 C programs that calculate the dot product of two vectors in parallel using different approaches: serial, OpenMP, MPI, and a hybrid OpenMP/MPI version. The student is asked to review the programs and comment on the differences in terms of granularity, communication, memory type, and parallelization type. They are also asked to make a comparative table of execution times using many data points.

Uploaded by

karla estefania
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SECRETARÍA DE EDUCACIÓN PÚBLICA

TECNOLÓGICO NACIONAL DE MÉXICO


INSTITUTO TECNOLÓGICO DE CIUDAD MADERO

Ingeniería en Sistemas Computacionales Dra. Claudia Guadalupe Gómez Santillán


UNIDAD1. Principios de Programación Paralela Programa 4
Nombre del Alumno: ________________________________________________________________________________

Revisar los siguientes programas

/*****************************************************************************
* FILE: omp_dotprod_serial.c
* DESCRIPTION:
* This simple program is the serial version of a dot product and the
* first of four codes used to show the progression from a serial program to a
* hybrid MPI/OpenMP program. The relevant codes are:
* - omp_dotprod_serial.c - Serial version
* - omp_dotprod_openmp.c - OpenMP only version
* - omp_dotprod_mpi.c - MPI only version
* - omp_dotprod_hybrid.c - Hybrid MPI and OpenMP version
* SOURCE: Blaise Barney
* LAST REVISED: 06/02/17 Blaise Barney
******************************************************************************/

#include <stdio.h>
#include <stdlib.h>

/* Define length of dot product vectors */


#define VECLEN 100

int main (int argc, char* argv[])


{
int i,len=VECLEN;
double *a, *b;
double sum;

printf("Starting omp_dotprod_serial\n");

/* Assign storage for dot product vectors */


a = (double*) malloc (len*sizeof(double));
b = (double*) malloc (len*sizeof(double));

/* Initialize dot product vectors */


for (i=0; i<len; i++) {
a[i]=1.0;
b[i]=a[i];
}

/* Perform the dot product */


sum = 0.0;
for (i=0; i<len; i++)
{
sum += (a[i] * b[i]);
SECRETARÍA DE EDUCACIÓN PÚBLICA
TECNOLÓGICO NACIONAL DE MÉXICO
INSTITUTO TECNOLÓGICO DE CIUDAD MADERO
}

printf ("Done. Serial version: sum = %f \n", sum);

free (a);
free (b);
}

============================
/*****************************************************************************
* FILE: omp_dotprod_openmp.c
* DESCRIPTION:
* This simple program is the OpenMP version of a dot product and the
* second of four codes used to show the progression from a serial program to a
* hybrid MPI/OpenMP program. The relevant codes are:
* - omp_dotprod_serial.c - Serial version
* - omp_dotprod_openmp.c - OpenMP only version
* - omp_dotprod_mpi.c - MPI only version
* - omp_dotprod_hybrid.c - Hybrid MPI and OpenMP version
* SOURCE: Blaise Barney
* LAST REVISED: 06/02/17 Blaise Barney
******************************************************************************/

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

/* Define length of dot product vectors and number of OpenMP threads */


#define VECLEN 100
#define NUMTHREADS 8

int main (int argc, char* argv[])


{
int i, tid, len=VECLEN, threads=NUMTHREADS;
double *a, *b;
double sum, psum;

printf("Starting omp_dotprod_openmp. Using %d threads\n",threads);

/* Assign storage for dot product vectors */


a = (double*) malloc (len*threads*sizeof(double));
b = (double*) malloc (len*threads*sizeof(double));

/* Initialize dot product vectors */


for (i=0; i<len*threads; i++) {
a[i]=1.0;
b[i]=a[i];
}
/* Initialize global sum */
sum = 0.0;

/*
SECRETARÍA DE EDUCACIÓN PÚBLICA
TECNOLÓGICO NACIONAL DE MÉXICO
INSTITUTO TECNOLÓGICO DE CIUDAD MADERO
Perform the dot product in an OpenMP parallel region for loop with
a sum reduction
For illustration purposes:
- Explicitly sets number of threads
- Each thread keeps track of its partial sum
*/

#pragma omp parallel private(i,tid,psum) num_threads(threads)


{
psum = 0.0;
tid = omp_get_thread_num();

#pragma omp for reduction(+:sum)


for (i=0; i<len*threads; i++)
{
sum += (a[i] * b[i]);
psum = sum;
}
printf("Thread %d partial sum = %f\n",tid, psum);
}

printf ("Done. OpenMP version: sum = %f \n", sum);

free (a);
free (b);
}

============================
/*****************************************************************************
* FILE: omp_dotprod_mpi.c
* DESCRIPTION:
* This simple program is the MPI version of a dot product and the third
* of four codes used to show the progression from a serial program to a
* hybrid MPI/OpenMP program. The relevant codes are:
* - omp_dotprod_serial.c - Serial version
* - omp_dotprod_openmp.c - OpenMP only version
* - omp_dotprod_mpi.c - MPI only version
* - omp_dotprod_hybrid.c - Hybrid MPI and OpenMP version
* SOURCE: Blaise Barney
* LAST REVISED: 06/02/17 Blaise Barney
******************************************************************************/

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

/* Define length of dot product vectors */


#define VECLEN 100

int main (int argc, char* argv[])


{
int i,myid, numprocs, len=VECLEN;
double *a, *b;
SECRETARÍA DE EDUCACIÓN PÚBLICA
TECNOLÓGICO NACIONAL DE MÉXICO
INSTITUTO TECNOLÓGICO DE CIUDAD MADERO
double mysum, allsum;

/* MPI Initialization */
MPI_Init (&argc, &argv);
MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank (MPI_COMM_WORLD, &myid);

/*
Each MPI task performs the dot product, obtains its partial sum, and then
calls
MPI_Reduce to obtain the global sum.
*/
if (myid == 0)
printf("Starting omp_dotprod_mpi. Using %d tasks...\n",numprocs);

/* Assign storage for dot product vectors */


a = (double*) malloc (len*sizeof(double));
b = (double*) malloc (len*sizeof(double));

/* Initialize dot product vectors */


for (i=0; i<len; i++) {
a[i]=1.0;
b[i]=a[i];
}

/* Perform the dot product */


mysum = 0.0;
for (i=0; i<len; i++)
{
mysum += a[i] * b[i];
}

printf("Task %d partial sum = %f\n",myid, mysum);

/* After the dot product, perform a summation of results on each node */


MPI_Reduce (&mysum, &allsum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (myid == 0)
printf ("Done. MPI version: global sum = %f \n", allsum);

free (a);
free (b);
MPI_Finalize();
}
=========================================

/*****************************************************************************
* FILE: omp_dotprod_hybrid.c
* DESCRIPTION:
* This simple program is the hybrid version of a dot product and the fourth
* of four codes used to show the progression from a serial program to a
* hybrid MPI/OpenMP program. The relevant codes are:
* - omp_dotprod_serial.c - Serial version
* - omp_dotprod_openmp.c - OpenMP only version
SECRETARÍA DE EDUCACIÓN PÚBLICA
TECNOLÓGICO NACIONAL DE MÉXICO
INSTITUTO TECNOLÓGICO DE CIUDAD MADERO
* - omp_dotprod_mpi.c - MPI only version
* - omp_dotprod_hybrid.c - Hybrid MPI and OpenMP version
* SOURCE: Blaise Barney
* LAST REVISED: 06/02/17 Blaise Barney
******************************************************************************/

#include <mpi.h>
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

/* Define length of dot product vectors and number of OpenMP threads */


#define VECLEN 100
#define NUMTHREADS 8

int main (int argc, char* argv[])


{
int i, myid, tid, numprocs, len=VECLEN, threads=NUMTHREADS;
double *a, *b;
double mysum, allsum, sum, psum;

/* MPI Initialization */
MPI_Init (&argc, &argv);
MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank (MPI_COMM_WORLD, &myid);

/*
Each MPI task uses OpenMP to perform the dot product, obtains its partial sum,
and then calls MPI_Reduce to obtain the global sum.
*/
if (myid == 0)
printf("Starting omp_dotprod_hybrid. Using %d tasks...\n",numprocs);

/* Assign storage for dot product vectors */


a = (double*) malloc (len*threads*sizeof(double));
b = (double*) malloc (len*threads*sizeof(double));

/* Initialize dot product vectors */


for (i=0; i<len*threads; i++) {
a[i]=1.0;
b[i]=a[i];
}

/*
Perform the dot product in an OpenMP parallel region for loop with a sum
reduction
For illustration purposes:
- Explicitly sets number of threads
- Gets and prints number of threads used
- Each thread keeps track of its partial sum
*/

/* Initialize OpenMP reduction sum */


SECRETARÍA DE EDUCACIÓN PÚBLICA
TECNOLÓGICO NACIONAL DE MÉXICO
INSTITUTO TECNOLÓGICO DE CIUDAD MADERO
sum = 0.0;

#pragma omp parallel private(i,tid,psum) num_threads(threads)


{
psum = 0.0;
tid = omp_get_thread_num();
if (tid ==0)
{
threads = omp_get_num_threads();
printf("Task %d using %d threads\n",myid, threads);
}

#pragma omp for reduction(+:sum)


for (i=0; i<len*threads; i++)
{
sum += (a[i] * b[i]);
psum = sum;
}
printf("Task %d thread %d partial sum = %f\n",myid, tid, psum);
}

/* Print this task's partial sum */


mysum = sum;
printf("Task %d partial sum = %f\n",myid, mysum);

/* After the dot product, perform a summation of results on each node */


MPI_Reduce (&mysum, &allsum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (myid == 0)
printf ("Done. Hybrid version: global sum = %f \n", allsum);

free (a);
free (b);
MPI_Finalize();
}

Revise los programas


Comente las diferencias en función de:
a) Granularidad
b) Comunicación
c) Tipo de memoria
d) Tipo de paralelización

Haga una tabla comparativa de los tiempos de ejecución entre los tiempos de ejecución, recuerde usar muchos datos.

You might also like