Descargar

Comunicaciones colectivas y Tipos de datos derivados (página 2)

Enviado por Pablo Turmero


Partes: 1, 2
edu.red Ejemplo. Integración numérica #include #include #include /*Gather.c*/

float integral(float ai, float h, int n);

int main(int argc, char* argv[]) { int n, p, myid, tag, proc, ierr, i; float h, integral_sum, a, b, ai, pi, my_int, buf[50]; int master = 0; /* processor performing total sum */ MPI_Comm comm;

comm = MPI_COMM_WORLD; ierr = MPI_Init(&argc, &argv); MPI_Comm_rank(comm, &myid); MPI_Comm_size(comm, &p);

edu.red Ejemplo. Integración numérica pi = acos(-1.0); /* = 3.14159… */ a = 0.; /* lower limit of integration */ b = pi*1./2.; /* upper limit of integration */ n = 500; /* number of increment within each process */ h = (b-a)/n/p; /* length of increment */ ai = a + myid*n*h; /* lower limit of integration for partition myid */ my_int = integral(ai, h, n);

printf("Process %d has the partial sum of %fn", myid,my_int);

MPI_Gather( &my_int, 1, MPI_FLOAT, buf, 1, MPI_FLOAT, master, comm);

edu.red Ejemplo. Integración numérica if(myid == master) { integral_sum = 0.0; for (i=0; i #define SIZE 4 int main(int argc,char *argv[]) { nt numtasks, rank, sendcount, recvcount, source; float sendbuf[SIZE][SIZE] = {{1.0, 2.0, 3.0, 4.0}, {5.0, 6.0,7.0,8.0}, {9.0, 10.0, 11.0, 12.0}, {13.0, 14.0, 15.0, 16.0}}; float recvbuf[SIZE]; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &numtasks); if (numtasks == SIZE) { source = 1; sendcount = SIZE; recvcount = SIZE; MPI_Scatter(sendbuf,sendcount,MPI_FLOAT,recvbuf,recvcount, MPI_FLOAT,source,MPI_COMM_WORLD); printf("rank= %d Results: %f %f %f %fn",rank,recvbuf[0], recvbuf[1],recvbuf[2],recvbuf[3]); } else printf("Must specify %d processors.Terminating.n",SIZE); MPI_Finalize(); }

edu.red Ejemplos Scatter en las filas de una matriz. Salida del programa rank= 0 Results: 1.000000 2.000000 3.000000 4.000000 rank= 1 Results: 5.000000 6.000000 7.000000 8.000000 rank= 2 Results: 9.000000 10.000000 11.000000 12.000000 rank= 3 Results: 13.000000 14.000000 15.000000 16.000000

edu.red Comunicaciones colectivas. Distribución all-to-all La comunicación de todos con todos supone que, inicialmente, cada proceso tiene un vector con tantos elementos como procesos hay en el comunicador.

Para i, j y k entre 0 y N-1 (donde N es el número de procesos del comunicador), cada proceso i envía una copia de sendbuf[j] al proceso j, y recibe del proceso k un elemento, que almacena en recvbuf[k].

MPI_Alltoall() equivale, por tanto, a una sucesión de N operaciones de distribución, en cada una de las cuales el proceso i toma el rol de raíz.

int MPI_Alltoall(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm);

int MPI_Alltoallv(void* sendbuf, int *sendcounts, int *sdispls, MPI_Datatype sendtype, void* recvbuf, int *recvcounts, int *rdispls, MPI_Datatype recvtype, MPI_Comm comm);

edu.red Comunicaciones colectivas. Distribución all-to-all

edu.red Comunicaciones colectivas. Reducción Una reducción es una operación realizada de forma cooperativa entre todos los procesos de un comunicador, de tal forma que se obtiene un resultado final que se almacena en el proceso raíz.

int MPI_Reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm);

edu.red Ejemplo. Integración numérica #include #include #include /*reduce.c*/

float fct(float x) { return cos(x); } float integral(float ai, float h, int n);

int main(int argc, char* argv[]) { int n, p, myid, tag, proc, ierr; float h, integral_sum, a, b, ai, pi, my_int; char line[10]; int master = 0; MPI_Comm comm; comm = MPI_COMM_WORLD;

edu.red Ejemplo. Integración numérica ierr = MPI_Init(&argc,&argv); MPI_Comm_rank(comm, &myid); MPI_Comm_size(comm, &p);

pi = acos(-1.0); /* = 3.14159… */ a = 0.; /* lower limit of integration */ b = pi*1./2.; /* upper limit of integration */

if(myid == master) { printf("The requested number of processors is %dn",p); printf("Enter number of increments within each processn"); (void) fgets(line, sizeof(line), stdin); (void) sscanf(line, "%d", &n); }

edu.red Ejemplo. Integración numérica /* Broadcast "n" to all processes */ MPI_Bcast( &n, 1, MPI_INT, master, comm);

h = (b-a)/n/p; /* length of increment */ ai = a + myid*n*h; /* lower limit of integration for partition myid */ my_int = integral(ai, h, n); /* 0<=myid<=p-1 */

printf("Process %d has the partial result of %fn", myid,my_int);

MPI_Reduce( &my_int, &integral_sum, 1, MPI_FLOAT, MPI_SUM, master, comm);

if(myid == 0) { printf("The result =%fn",integral_sum); } MPI_Finalize(); }

edu.red Ejemplo. Integración numérica float integral(float ai, float h, int n) { int j; float aij, integ; integ = 0.0; /* initialize */ for (j=0;j #include

/* Run with four processes */

void main(int argc, char *argv[]) { int rank; MPI_Status status; struct { int x; int y; int z; } point; MPI_Datatype ptype;

MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&rank); MPI_Type_contiguous(3,MPI_INT,&ptype); MPI_Type_commit(&ptype); if(rank==3){ point.x=15; point.y=23; point.z=6; MPI_Send(&point,1,ptype,1,52,MPI_COMM_WORLD); } else if(rank==1) { MPI_Recv(&point,1,ptype,3,52,MPI_COMM_WORLD,&status); printf("P:%dreceived coordsare (%d,%d,%d) n",rank,point.x,point.y,point.z); } MPI_Finalize(); }

edu.red Ejemplos. vector.c #include #include #include

void main(int argc, char *argv[]) { int rank,i,j; MPI_Status status; double x[4][8]; MPI_Datatype coltype;

MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD,&rank); MPI_Type_vector(4,1,8,MPI_DOUBLE,&coltype); MPI_Type_commit(&coltype);

if(rank==3){ for(i=0;i<4;++i) for(j=0;j<8;++j) x[i][j]=pow(10.0,i+1)+j; MPI_Send(&x[0][7],1,coltype,1,52,MPI_COMM_WORLD); } else if(rank==1) { MPI_Recv(&x[0][2],1,coltype,3,52,MPI_COMM_WORLD,&status); for(i=0;i<4;++i) printf("P:%d my x[%d][2]=%1fn",rank,i,x[i][2]); } MPI_Finalize(); }

edu.red Ejemplos. extent.c #include #include

void main(intargc, char *argv[]) { int rank,i; MPI_Status status; struct{ int num; float x; double data[4]; } a;

int blocklengths[3]={1,1,4}; MPI_Datatype types[3]={MPI_INT,MPI_FLOAT,MPI_DOUBLE}; MPI_Aint displacements[3]; MPI_Datatype restype; MPI_Aint intex,floatex;

MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&rank); MPI_Type_extent(MPI_INT,&intex); MPI_Type_extent(MPI_FLOAT,&floatex);

edu.red Ejemplos displacements[0]= (MPI_Aint)0; displacements[1]=intex; displacements[2]=intex+floatex; MPI_Type_struct(3,blocklengths,displacements,types,&restype); MPI_Type_commit(&restype);

if (rank==3){ a.num=6; a.x=3.14; for(i=0;i<4;++i) a.data[i]=(double) i; MPI_Send(&a,1,restype,1,52,MPI_COMM_WORLD); } else if(rank==1) { MPI_Recv(&a,1,restype,3,52,MPI_COMM_WORLD,&status); printf("P:%dmy a is %d %f %lf %lf %lf %lfn", rank,a.num,a.x,a.data[0],a.data[1],a.data[2],a.data[3]); } MPI_Finalize(); }

edu.red Ejemplos. bcast.c #include #include

void main (int argc, char *argv[]) { int rank; double param;

MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD,&rank); if(rank==5) param=23.0; MPI_Bcast(¶m,1,MPI_DOUBLE,5,MPI_COMM_WORLD); printf("P:%d after broadcast parameter is %fn",rank,param); MPI_Finalize(); }

edu.red Ejemplos. scatterdos.c #include #include void main (int argc, char *argv[]) { int rank,size,i,j; double param[4],mine; int sndcnt,revcnt;

MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD,&rank); MPI_Comm_size(MPI_COMM_WORLD,&size);

revcnt=1; if(rank==3){ for(i=0;i<4;i++) param[i]=23.0+i; sndcnt=1; } MPI_Scatter(param,sndcnt,MPI_DOUBLE,&mine,revcnt,MPI_DOUBLE,3,MPI_COMM_WORLD); printf("P:%dmine is %fn",rank,mine); MPI_Finalize(); }

edu.red Ejemplos. reducedos.c #include #include

void main (int argc, char *argv[]) { int rank; struct{ double value; int rank;} in, out; int root;

MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD,&rank); in.value=rank+1; in.rank=rank; root=7; MPI_Reduce(&in,&out,1,MPI_DOUBLE_INT,MPI_MAXLOC,root,MPI_COMM_WORLD); if(rank==root) printf("PE:%dmax=%lf at rank %dn",rank,out.value,out.rank); MPI_Reduce(&in,&out,1,MPI_DOUBLE_INT,MPI_MINLOC,root,MPI_COMM_WORLD); if(rank==root) printf("PE:%dmin=%lf at rank %dn",rank,out.value,out.rank); MPI_Finalize(); }

edu.red Ejemplos. create.c #include #include /*Con 4 procesadores*/

#define TRUE 1 typedef struct{ double real,imag; } complex; void cprod(complex *in, complex *inout, int *len, MPI_Datatype *dptr) { int i; complex c; for (i=0; i<*len; ++i) { c.real=(*in).real * (*inout).real-(*in).imag* (*inout).imag; c.imag=(*in).real * (*inout).imag+ (*in).imag* (*inout).real; *inout=c; in++; inout++; } }

edu.red Ejemplos. create.c void main (int argc, char *argv[]) { int rank; int root; complex source,result; MPI_Op myop; MPI_Datatypectype;

MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD,&rank); MPI_Type_contiguous(2,MPI_DOUBLE,&ctype); MPI_Type_commit(&ctype); MPI_Op_create(cprod,TRUE,&myop);

root=2; source.real=rank+1; source.imag=rank+2; MPI_Reduce(&source,&result,1,ctype,myop,root,MPI_COMM_WORLD); if(rank==root) printf("PE:%d result is %lf + %lfin",rank, result.real, result.imag); MPI_Finalize(); } ——————————————– P:2 result is -185.000000 + -180.000000i

Partes: 1, 2
 Página anterior Volver al principio del trabajoPágina siguiente