1 / 4

Efficient Parallel Programming in MPI

Learn how to implement efficient parallel programming using MPI for distributed memory systems, with sample answers and code snippets provided.

terena
Download Presentation

Efficient Parallel Programming in MPI

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Parallel Programming in MPIAnswer January 18, 2012 1

  2. Sample answer of the last week's report. (1) int my_reduce(int *a, int *b, int c) { int i, p, myid, procs; int *t; MPI_Status st; MPI_Comm_rank(MPI_COMM_WORLD, &myid); MPI_Comm_size(MPI_COMM_WORLD, &procs); if (myid == 0){ t = (int *)malloc(c*sizeof(int)); MPI_Recv(t, c, MPI_INT, 1, 0, MPI_COMM_WORLD, &st); for (i = 0; i < N; i++) b[i] = a[i] + t[i]; for (p = 2; p < procs; p++){ MPI_Recv(t, c, MPI_INT, p, 0, MPI_COMM_WORLD, &st); for (i = 0; i < N; i++) b[i] += t[i]; } } else{ MPI_Send(a, c, MPI_INT, 0, 0, MPI_COMM_WORLD); } return 0; }

  3. Sample answer of the last week's report. (2) int my_reduce(int *a, int *b, int c) { int i, mask, myid, procs, isfirst; int *t1, *t2; MPI_Status st; MPI_Comm_rank(MPI_COMM_WORLD, &myid); MPI_Comm_size(MPI_COMM_WORLD, &procs); mask = 1; isfirst = 1; t1 = (int *)malloc(c*sizeof(int)); if (myid == 0) t2 = b; else t2 = (int *)malloc(c*sizeof(int)); continue to the next page

  4. Sample answer of the last week's report. (2) cont. while (mask < procs){ if (myid & mask){ if (mask == 1) MPI_Send(a, c, MPI_INT, myid-mask, 0, MPI_COMM_WORLD); else MPI_Send(t2, c, MPI_INT, myid-mask, 0, MPI_COMM_WORLD); break; } else{ MPI_Recv(t1, c, MPI_INT, myid+mask, 0, MPI_COMM_WORLD, &st); if (isfirst){ for (i=0; i<N; i++) t2[i] = a[i]+t1[i]; isfirst = 0; } else for (i=0; i<N; i++) t2[i] += t1[i]; mask <<= 1; } } free(t1); free(t2); return 0; }

More Related