C - MPI - 动态2d阵列 - 奇怪的问题

时间:2016-12-27 21:35:35

标签: c arrays parallel-processing mpi

我正在使用MPI在C中编写并行图像处理算法。

将图像解析为2d数组,根据处理器进行切片并正确发送到每个进程!

奇怪的问题是,当我打印出数组时,我将最后8个数组元素作为0。

我尝试添加sliceSize * width + 8 工作了!

但是当我尝试使用不同(更大)的尺寸时,0还有更多。

int main(int argc, char *argv[]) {
  TGA_Header header;
  char output_filename[100];

  if (argc != 2) {
    printf("\nUsage:\n");
    printf("\t./tga <filename>.tga\n\n");
    printf(
        "The input file should be a greyscale image with an 8-bit depth.\n\n");
    exit(0);
  }

  TGA *image = load_tga(argv[1], &header);

  /*
   * Image processing starts here
   */

  int my_rank = 0, size = 0;
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  MPI_Comm_size(MPI_COMM_WORLD, &size);
  // Variable Initialization
  int i = 0, k = 0, l = 0, j = 0, c = 0, d = 0, avg = 0, count = 0, sum = 0,
      sliceSizeExt = 0, upper = 0, start = 0, end = 0, extrapixels = 0,
      sliceSize = 0, height = 0, width = 0, **subArray, **subResult,
      **subArrayFext, **subResultFext, **array, **result, **s, **r,
      SLICEWIDTH = 0;
  MPI_Status status;
  MPI_Request request;
  // Set Height and width!
  width = image->w;
  height = image->h;

  // Dynamic allocation for both arrays

  sliceSize = height / size;
  SLICEWIDTH = sliceSize * width;

  subArray = (int **)malloc(sizeof(int *) * sliceSize);

  for (int i = 0; i < sliceSize; i++) {
    subArray[i] = (int *)malloc(sizeof(int) * width);
  }

  r = s = malloc(sizeof(int *) * sliceSize);
  for (i = 0; i < sliceSize; i++) {
    s[i] = malloc(sizeof(int) * width);
  }

  /*
     First and last arrays should be allocated with +1 line for the respected
     ghost borders
     Arrays for processes with rank 0 > my_rank > size should keep the same
     allocation

     Exception:
     If the height/size modulo is (>) Greater than (0) Zeroprintf("\n Computing
     bounds for process ")
     }
     Then it should be processed by the rank 0
     Just to keep the communication dynamic.
   */

  extrapixels = height % size;
  sliceSizeExt = sliceSize + extrapixels;

  if (my_rank == 0) {
    array = malloc(sizeof(int *) * height);

    for (i = 0; i < height; i++) {
      array[i] = malloc(sizeof(int) * width);
    }

    printf(
        "\nThese are the extra pixels! : %d\n This is the Ext dimension:%d\n",
        extrapixels, sliceSizeExt);

    subArrayFext = malloc(sizeof(int *) * sliceSizeExt);
    for (i = 0; i < sliceSizeExt; i++) {
      subArrayFext[i] = malloc(sizeof(int) * width);
    }

    printf("IM RANK %d , Array init!\n", my_rank);
    printf("\n***%d : Image dimensions %d %d\n", my_rank, height, width);
    printf("\n***********\n%d Some statistics: \nSlave Processes:%d\nArray "
           "Slice Size(height): %d\nMoving on to Compute slice "
           "bounds!\n************\n",
           my_rank, size - 1, sliceSize);
    printf("\n***%d Parsing pixel Array to 2d\n", my_rank);
    // Parse image pixel array to 2d array
    for (i = 0; i < height; i++) {
      for (j = 0; j < width; j++) {
        // use incremental counter for 1d array(: image->pixels[width*height])
        // to 2d transformation
        array[i][j] = image->pixels[upper];
        upper++;
      }
    }

    for (int i = 0; i < height; i++) {
      for (int j = 0; j < width; j++) {
        printf("%d\t", array[i][j]);
      }
      printf("\n");
    }

    // Compute the bounds for processes 1-2-3
    for (int target = 0; target < size; target++) {
      printf("\n\nBounds for PROCESS:%d\n", target);
      if (target == 0) {

        start = target * sliceSize;
        end = (start + sliceSize - 1) + extrapixels;
      } else {
        start = (target * sliceSize) + extrapixels;
        end = start + sliceSize - 1;
      }

      printf("\n\n START : %d \t END : %d\n", start, end);
      printf("\n\n*****SLICE FOR PROCESS %d******\n\n", target);
      for (i = start; i <= end; i++) {
        for (j = 0; j < width; j++) {
          if (target == 0) {
            subArrayFext[i - start][j] = array[i][j];
          } else {
            subArray[i - start][j] = array[i][j];
          }
        }
      }

      printf("\nSlice Done!\n");

      if (target > 0) {
        for (int k = 0; k < sliceSize; k++) {
          for (int l = 0; l < width; l++) {
            printf("%d\t", subArray[k][l]);
          }
          printf("\n");
        }
        printf("\n Sending array to process %d\n", target);
        MPI_Send(*subArray, sliceSize * width, MPI_INT, target, 10,
                 MPI_COMM_WORLD);
      }
    }

  } else {
    printf("\n-----------------\nrank %d\nmy size:%d\n------------------\n",
           my_rank, sliceSize * width);
    MPI_Recv(*subArray, sliceSize * width, MPI_INT, 0, 10, MPI_COMM_WORLD,
             &status);
    for (k = 0; k < sliceSize; k++) {
      for (l = 0; l < width; l++) {
        printf("R:%d-%d\t", my_rank, subArray[k][l]);
      }
      printf("\n");
    }
  }

  free(image);
  MPI_Finalize();
  // return (0);
}

这就是打印输出的样子,它与发送的数组不同

R:1-64  R:1-65  R:1-66  R:1-67  R:1-69  R:1-71  R:1-68  R:1-94  R:1-133 R:1-151 R:1-130 R:1-80  R:1-58  R:1-65  R:1-71  R:1-72  R:1-73  R:1-76  R:1-76  R:1-74  R:1-73  R:1-196 R:1-221 R:1-218 R:1-231 R:1-216 R:1-84  R:1-70  R:1-73  R:1-72
R:1-62  R:1-64  R:1-66  R:1-68  R:1-71  R:1-66  R:1-88  R:1-170 R:1-181 R:1-180 R:1-185 R:1-171 R:1-90  R:1-67  R:1-73  R:1-69  R:1-64  R:1-65  R:1-66  R:1-73  R:1-66  R:1-123 R:1-214 R:1-198 R:1-208 R:1-143 R:1-67  R:1-74  R:1-73  R:1-73
R:1-62  R:1-62  R:1-58  R:1-57  R:1-63  R:1-61  R:1-109 R:1-178 R:1-179 R:1-181 R:1-176 R:1-188 R:1-131 R:1-61  R:1-69  R:1-78  R:1-113 R:1-123 R:1-106 R:1-71  R:1-69  R:1-66  R:1-89  R:1-107 R:1-90  R:1-69  R:1-75  R:1-74  R:1-72  R:1-71
R:1-59  R:1-64  R:1-99  R:1-119 R:1-98  R:1-61  R:1-82  R:1-169 R:1-183 R:1-185 R:1-179 R:1-187 R:1-108 R:1-59  R:1-86  R:1-164 R:1-172 R:1-179 R:1-182 R:1-149 R:1-71  R:1-71  R:1-68  R:1-66  R:1-66  R:1-64  R:1-62  R:1-60  R:1-68  R:1-70
R:1-67  R:1-145 R:1-201 R:1-204 R:1-198 R:1-159 R:1-68  R:1-94  R:1-153 R:1-158 R:1-152 R:1-115 R:1-66  R:1-63  R:1-119 R:1-185 R:1-174 R:1-182 R:1-178 R:1-187 R:1-105 R:1-64  R:1-0   R:1-0   R:1-0   R:1-0   R:1-0   R:1-0   R:1-0   R:1-0

R:1代表Rank:1

0 个答案:

没有答案