How to implement a MPI filter on C code? -
i trying implement mpi of filter code below, i'm facing difficulties doing it. how should done?:
filter code:
int a[100000][100000]; int b[100000][100000]; (int i=1; i<(100000 - 1); i++) (int i=1; j<(100000 - 1); j++) b[i][j] = a[i-1][j] + a[i+1][j] + a[i][j-1] + a[i][j+1] - 4*a[i][j];
this have tried while following 6 functions of mpi:
int myrank; /* rank of process */ int numprocs; /* number of processes */ int source; /* rank of sender */ int dest; /* rank of receiver */ char message[100]; /* storage message */ mpi_status status; /* return status receive */ mpi_init( & argc, & argv); mpi_comm_size(mpi_comm_world, & numprocs); mpi_comm_rank(mpi_comm_world, & myrank); if (myrank != 0) { dest = 0; mpi_send(message, strlen(message) + 1, mpi_char, dest, 15, mpi_comm_world); } else { (source = 1; source < numprocs; source++) { mpi_recv(message, 100, mpi_char, source, 15, mpi_comm_world, & status); } } mpi_finalize();
i'd go this. first of all, i'd have code
int a[100000][100000]; int b[100000][100000];
replaced dynamic allocations. don't need memory each , every process.
then, i'd send array different processes. rows.
what "height" of data frame (number of rows):
delta = (100000 - 2) / (numprocs-1); // don't count first , last row reminder = (100000 - 2) % (numprocs-1); // might need give // little bit more calculate // 1 of processes // starting row idx=1 (second row) , want finish when // hit last row if(myrank == 0) { for( int i=1; < numprocs; i++ ) { // +100000 - need 2 more rows calculate data int how_many_bytes = delta * 100000 + 200000; if(reminder != 0 && == (numprocs-1)) { how_many_bytes += reminder * 100000; } mpi_send(&(a[(i-1)*delta][0]), how_many_bytes, mpi_int, i, 0, mpi_comm_world); } } else { // allocate memory bytes int *local_array = null; int how_many_bytes = delta * 100000 + 200000; if(reminder != 0 && == (numprocs-1)) { how_many_bytes += reminder * 100000; } local_array = malloc(how_many_bytes * sizeof(int)); mpi_status status; mpi_recv( local_array, how_many_bytes, mpi_int, 0, 0, mpi_comm_world, &status); } // perform calculations each , every slice // remembering have on row on // top , 1 @ bottom // send data master (as above, vice versa).
Comments
Post a Comment