int main_replaced(int argc, char** argv){ //Initialization MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); //Reading image if(rank == 0){ image = read_bmp("Lenna_blur.bmp"); } //Creating cartesian communicator MPI_Dims_create(size, 2, dims); MPI_Cart_create( MPI_COMM_WORLD, 2, dims, periods, 0, &cart_comm ); MPI_Cart_coords( cart_comm, rank, 2, coords ); MPI_Cart_shift( cart_comm, 0, 1, &north, &south ); MPI_Cart_shift( cart_comm, 1, 1, &west, &east ); local_image_size[0] = image_size[0]/dims[0]; local_image_size[1] = image_size[1]/dims[1]; //Allocating buffers int lsize = local_image_size[0]*local_image_size[1]; int lsize_border = (local_image_size[0] + 2*BORDER)*(local_image_size[1] + 2*BORDER); local_image_orig = (unsigned char*)malloc(sizeof(unsigned char)*lsize); local_image[0] = (unsigned char*)calloc(lsize_border, sizeof(unsigned char)); local_image[1] = (unsigned char*)calloc(lsize_border, sizeof(unsigned char)); create_types(); distribute_image(); initialilze_guess(); //Main loop for(int i = 0; i < ITERATIONS; i++){ exchange_borders(i); perform_convolution(i); } gather_image(); MPI_Finalize(); //Write image if(rank==0){ write_bmp(image, image_size[0], image_size[1]); } exit(0); }
int main (int argc, char **argv) { // Reading command line arguments iterations = 100; imageSize = 512; if(argc == 3){ iterations = atoi(argv[1]); imageSize = atoi(argv[2]); } // MPI initialization, getting rank and size MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Creating cartesian communicator MPI_Dims_create(size, 2, dims); MPI_Cart_create( MPI_COMM_WORLD, 2, dims, periods, 0, &cart_comm ); MPI_Cart_coords( cart_comm, rank, 2, coords ); // Finding neighbours processes MPI_Cart_shift(cart_comm, 0, 1, &north, &south); MPI_Cart_shift(cart_comm, 1, 1, &west, &east); // Determining size of local subdomain local_height = imageSize/dims[0]; local_width = imageSize/dims[1]; // Creating and commiting MPI datatypes for message passing create_types(); // Allocating memory for local arrays local_pres = (float*)malloc(sizeof(float)*(local_width + 2*border)*(local_height+2*border)); local_pres0 = (float*)malloc(sizeof(float)*(local_width + 2*border)*(local_height+2*border)); local_diverg = (float*)malloc(sizeof(float)*local_width*local_height); // Initializing the CFD computation, only one process should do this. if(rank == 0){ initFluid( &config, imageSize, imageSize); pres = config.pres; diverg = config.div; imageBuffer = (unsigned char*)malloc(sizeof(unsigned char)*imageSize*imageSize); } // Solving the CFD equations, one iteration for each timestep. // These are not the same iterations used in the Jacobi solver. // The solveFluid function call the Jacobi solver, wich runs for // 100 iterations for each of these iterations. for(int i = 0; i < iterations; i++){ solveFluid(&config); } // Converting the density to an image and writing it to file. if(rank == 0){ densityToColor(imageBuffer, config.dens, config.N); write_bmp(imageBuffer, imageSize, imageSize); // Free fluid simulation memory freeFluid( &config ); } // Finalize MPI_Finalize(); }
int main(int argc, char** argv){ //Initialization MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); //Initializing test pattern image_size[0] = 256; image_size[1] = 256; if(rank == 0){ image = (unsigned char*)malloc(sizeof(unsigned char) * image_size[0] * image_size[1]); } //Creating cartesian communicator MPI_Dims_create(size, 2, dims); MPI_Cart_create( MPI_COMM_WORLD, 2, dims, periods, 0, &cart_comm ); MPI_Cart_coords( cart_comm, rank, 2, coords ); MPI_Cart_shift( cart_comm, 0, 1, &north, &south ); MPI_Cart_shift( cart_comm, 1, 1, &west, &east ); local_image_size[0] = image_size[0]/dims[0]; local_image_size[1] = image_size[1]/dims[1]; //Allocating buffers int lsize = local_image_size[0]*local_image_size[1]; int lsize_border = (local_image_size[0] + 2*BORDER)*(local_image_size[1] + 2*BORDER); local_image_orig = (unsigned char*)malloc(sizeof(unsigned char)*lsize); local_image[0] = (unsigned char*)calloc(lsize_border, sizeof(unsigned char)); local_image[1] = (unsigned char*)calloc(lsize_border, sizeof(unsigned char)); create_types(); for(int i = 0; i < local_image_size[0]; i++){ for(int j = 0; j < local_image_size[1]; j++){ G(i,j) = rank + 1; } } if(rank == 0) printf("--- Starting ---\n\n\n"); //Test initialize guess MPI_Barrier(cart_comm); int error = 0; if(rank == 0) printf("--- Testing initialize_guess() ---\n"); initialilze_guess(); for(int i = 0; i < local_image_size[0]; i++){ for(int j = 0; j < local_image_size[1]; j++){ if(G(i,j) != F(0,i,j)){ printf("\tinitialize_guess(): error at: %d,%d. Expected: %d, actual: %d. Rank: %d\n", G(i,j), F(0,i,j), i,j,rank); error = 1; break; } } if(error == 1) break; } MPI_Barrier(cart_comm); if(rank == 0) printf("--- Done ---\n\n\n"); //Test border_exchange if(rank == 0) printf("--- Testing exchange_borders() ---\n"); exchange_borders(0); if(north > 0){ for(int i = 0; i < local_image_size[0]; i++){ if(F(0,-1,i) != north + 1){ printf("\texchange_borders: error from north at: %d. Expected: %d, actual: %d. Rank: %d\n", i, north+1,F(0,-1,i), rank); error = 1; break; } } } if(south > 0 && error == 0){ for(int i = 0; i < local_image_size[0]; i++){ if(F(0,local_image_size[1],i) != south + 1){ printf("\texchange_borders: error from south at: %d. Expected: %d, actual: %d. Rank: %d\n", i, south+1, F(0,local_image_size[1],i), rank); error = 1; break; } } } if(west > 0 && error == 0){ for(int i = 0; i < local_image_size[0]; i++){ if(F(0,i,-1) != west + 1){ printf("\texchange_borders error from west at: %d. Expected: %d, actual: %d. Rank: %d\n", i, west+1, F(0,i,-1), rank); error = 1; break; } } } if(east > 0 && error == 0){ for(int i = 0; i < local_image_size[0]; i++){ if(F(0,i,local_image_size[0]) != east + 1){ printf("\texchange_borders: error from east at: %d. Expected: %d, actual: %d. Rank: %d\n", i, east+1, F(0,i,local_image_size[0]), rank); error = 1; break; } } } MPI_Barrier(cart_comm); if(rank == 0) printf("--- Done ---\n\n\n"); //Test perform_convolution error = 0; if(rank == 0) printf("Testing perform_convolution()\n"); MPI_Barrier(cart_comm); for(int i = -1; i < local_image_size[0] + 1; i++){ for(int j = -1; j < local_image_size[0] + 1; j++){ F(0,i,j) = ((j%2==0 && i%2==0) || (j%2!=0 && i%2!=0)) ? 10 + rank + 1 : 200 + rank + 1; } } perform_convolution(0); unsigned char even = (unsigned char)(10.0 + rank + 1.0 + lambda*(rank + 1.0 - ((850.0 + 9*(rank + 1)) * 0.111))); unsigned char odd = (unsigned char)(200.0 + rank + 1.0 - (1040.0/9.0 * lambda)); for(int i = 0; i < local_image_size[0]; i++){ for(int j = 0; j < local_image_size[1]; j++){ if(((j%2==0 && i%2==0) || (j%2==1 && i%2==1))){ if(F(1,i,j) != even){ printf("\tperform_convolution: error at: %d, %d. Expected: %d, actual: %d. Rank: %d\n", i,j,even,F(1,i,j),rank); error = 1; break; } } else{ if(F(1,i,j) != odd){ printf("\tperform_convolution: error at: %d, %d. Expected: %d, actual: %d. Rank: %d\n", i,j,even,F(1,i,j),rank); error = 1; break; } } } if(error == 1) break; } MPI_Barrier(cart_comm); if(rank == 0) printf("--- Done ---\n\n\n"); //Testing gather_image error = 0; if(rank == 0) printf("Testing gather_image()\n"); gather_image(); if(rank == 0){ int co[2]; for(int r = 0; r < 4; r++){ MPI_Cart_coords(cart_comm, r, 2, co); int x,y; for(int i = 0; i < image_size[0]/2; i++){ for(int j = 0; j < image_size[1]/2; j++){ x = i + co[0]*image_size[0]/2; y = j + co[1]*image_size[1]/2; if((x%2==0 && y%2==0) || (x%2==1 && y%2==1)){ if(image[x * image_size[0] + y] != 10 + r + 1){ printf("\tgather_image: error at: %d, %d. Expected: %d, actual: %d.\n", x,y,10+r+1,image[x*image_size[0]+y]); error = 1; break; } } else{ if(image[x * image_size[0] + y] != 200 + r + 1){ printf("\tgather_image: error at: %d, %d. Expected: %d, actual: %d.\n", x,y,10+r+1,image[x*image_size[0]+y]); error = 1; break; } } } if(error == 1) break; } if(error == 1) break; } } MPI_Barrier(cart_comm); if(rank == 0) printf("--- Done ---\n\n\n"); MPI_Finalize(); exit(0); }