next up previous contents
Next: permmpi.c Up: Source Codes Previous: poissonPlate.c   Contents

pppt.c

/* * poissonPlatePa.c * Poisson's equation in 2-D - explicit algorithm * parallel implementation * * based on code located at * http://www.physics.buffalo.edu/phy516/homework.html * * written by Jun Sasaki */

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

#include "mpi.h"

#define INITTMP 10

int num_procs; int my_rank; int my_N; int right_proc,left_proc;

double L = 1.0; /* linear size of square region */ int N = 16; /* number of interior points per dim */

double **u, **u_new; /* linear arrays to hold solution */ char* filename = "poisson.pgm";

/* startup function to take care of household stuff */ void init( void );

/* memory related functions. mallocs and frees * the global arrays used. */ void allocate_arrays ( void ); void free_arrays ( void );

void make_source( void );

/* computes one iteration of the explicit heat equation solver */ void explicit ( void );

void write_result();

int main (int argc, char *argv[]) int i,j,k=0, n; double change, wall_time, **swap;

MPI_Init(&argc,&argv); wall_time = MPI_Wtime(); MPI_Comm_size(MPI_COMM_WORLD, &num_procs); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

if (argc > 1) sscanf(argv[1], "if (argc > 2) sscanf(argv[2], "

if(my_rank == 0) printf("2-D heat equation using explicit algorithm"); printf("==========================================="); printf("Number of interior vertices = printf("Number of processes =

// printf("hello i am proc# init(); // printf("hello i am proc#

for(j=0;j<k;j++) explicit();

// interchange u and u_new swap = u; u = u_new; u_new = swap;

wall_time = MPI_Wtime() - wall_time;

// write_result(); free_arrays();

if(my_rank == 0) printf("Wall clock time - MPI_Finalize();

return 0;

void explicit (void) int i,j; int previ,prevj; int nexti,nextj;

double z = 0.42; MPI_Status status1, status2;

/* send info to all the processors to the left */ if(my_rank != 0) MPI_Send(u[1],N+2,MPI_DOUBLE,left_proc, 1,MPI_COMM_WORLD);

/* receive info from the right */

if(my_rank != num_procs-1) MPI_Recv(u[my_N+1],N+2,MPI_DOUBLE,right_proc, 1,MPI_COMM_WORLD, &status1);

/* now send info to all the processors to the right */

MPI_Send(u[my_N],N+2,MPI_DOUBLE,right_proc, 0,MPI_COMM_WORLD);

/* receive from left except for rank==0 */ if(my_rank != 0) MPI_Recv(u[0],N+2,MPI_DOUBLE,left_proc, 0,MPI_COMM_WORLD, &status2);

/* explicit update for vertices in my domain */ for (i = 1; i <= my_N; i++) for(j = 0; j <= N+1; j++) // Fixed for my_rank=0, first row if(my_rank==0 && i == 1)u[i][j]=INITTMP; else u_new[i][j] = (1 - 4*z) * u[i][j] + z * (u[i-1][j] + u[i+1][j] + u[i][j-1] + u[i][j+1]); /* end j for */ /* end i for */

/* a routine to initialize globals as well as allocate arrays */ void init() my_N=N / num_procs; if(my_rank==num_procs-1)my_N+=N

right_proc = (my_rank + 1) left_proc = (num_procs+my_rank - 1)

allocate_arrays(); make_source();

void make_source(void)

void allocate_arrays (void) int size, i,j;

size = N+2; u = malloc((my_N+2) * sizeof(void *)); u_new = malloc((my_N+2) * sizeof(void *));

for(i = 0; i < my_N+2; i++) u[i] = (double*) malloc(size * sizeof(double)); u_new[i] = (double*) malloc(size * sizeof(double));

for (i = 0; i < my_N+2; i++) for(j=0; j < size; j++) u[i][j] = u_new[i][j] = my_rank;

void free_arrays (void) int i; int size = (N+2);

for(i = 0; i < my_N; i++) free(u[i]); free(u_new[i]);

free(u); free(u_new);

void write_result() int i,j; //FILE *fp = fopen(filename,"w");

for(i=0;i<=my_N+1;i++) fprintf(stdout,"

for(j=1;j<=N;j++) fprintf(stdout,"

fprintf(stdout,"

//fclose(fp);


next up previous contents
Next: permmpi.c Up: Source Codes Previous: poissonPlate.c   Contents
J S 2002-08-14