Example #1
0
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);
}
Example #2
0
int main(int argc, char **argv) {
  FILE *in = stdin, *out = stdout;
  float sigma = STD_SIGMA;
  int kernel_width = STD_KX, kernel_height = STD_KY;
  char *comment = NULL;

  char *arg_in = NULL, *arg_out = NULL, *arg_sigma = NULL, *arg_kernelsize = NULL;
  BOOL arg_comment = FALSE, arg_binary = TRUE;

  int choice;

  // Parsing options
  opterr = 0;

  while ((choice = getopt(argc, argv, "f:o:s:k:cvh")) != -1)
    switch (choice) {
    case 'f':
      arg_in = optarg;
      break;
    case 'o':
      arg_out = optarg;
      break;
    case 's':
      arg_sigma = optarg;
      break;
    case 'k':
      arg_kernelsize = optarg;
      break;
    case 'c':
      arg_comment = TRUE;
      break;
    case 'v':
      arg_binary = FALSE;
      break;
    case 'h':
      print_help(argv[0]);
      return 0;
    case '?':
      if (isprint(optopt))
	fprintf(stderr, "Unknown option '-%c'.\n", optopt);
      else
	fprintf(stderr, "Unknown option character '\\x%x'.\n", optopt);
      return 1;
    default:
      abort();
    }

  for (choice = optind; choice < argc; ++choice)
    if (arg_in == NULL)
      arg_in = argv[choice];
    else if (arg_out == NULL)
      arg_out = argv[choice];
    else
      fprintf(stderr, "Unknown non-option argument %s\n", argv[choice]);

  // Processing options
  if (arg_in != NULL)
    if (!(in = fopen(arg_in, "rb"))) {
      fprintf(stderr, "Couldn't open file for reading: %s\n", arg_in);
      return 2;
    }
  if (arg_out != NULL)
    if (!(out = fopen(arg_out, "wb"))) {
      fprintf(stderr, "Couldn't open file for writing: %s\n", arg_out);
      return 3;
    }
  if (arg_sigma != NULL)
    if ((sigma = atof(arg_sigma)) <= 0.0f) {
      fprintf(stderr, "Standard deviation sigma must be greater than zero.\n");
      return 7;
    }
  if (arg_kernelsize != NULL)
    if ((kernel_width = kernel_height = atoi(arg_kernelsize)) < 1) {
      fprintf(stderr, "Kernel size must be greater than zero.\n");
      return 8;
    }
  if (arg_comment) {
    if (arg_in)
      asprintf(&comment, "Convolution with Gaussian\nInitial image:\t%s\nStandard deviation sigma: %f\nKernel size:\t%d * %d", arg_in, sigma, kernel_width, kernel_height);
    else
      asprintf(&comment, "Convolution with Gaussian\nStandard deviation sigma: %f\nKernel size:\t%d * %d", sigma, kernel_width, kernel_height);
  }

  // Performing convolution operation
  int result = perform_convolution(in, out, sigma, kernel_width, kernel_height, comment, arg_binary);

  fclose(out);
  fclose(in);
  if (comment)
    free(comment);

  switch (result) {
  case 4:
    if (arg_in == NULL)
      fprintf(stderr, "Couldn't read image data from standard input.\n");
    else
      fprintf(stderr, "Couldn't read data in image file: %s.\n", arg_in);
    break;
  case 5:
    fprintf(stderr, "Couldn't create Gaussian kernel.\n");
    break;
  case 6:
    fprintf(stderr, "Error while performing convolution.\n");
    break;
  }

  return result;
}
Example #3
0
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);
}