动态分配二维数组用C差异用一个简单的二维数组Mpi的 [英] Dynamic Allocation 2d array in C difference with a simple 2d array Mpi

查看:184
本文介绍了动态分配二维数组用C差异用一个简单的二维数组Mpi的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MPIÇ图像处理(PGM文件)一个MPI程序,我使用动态分配一个二维数组如下:

 浮动** masterbuf;
masterbuf = arralloc(的sizeof(浮动); 2,M,N);

当我使用

 浮动masterbuf [M] [N];

该程序使图像看起来罚款。

的问题是,当我使用动态分配的图像失去一些像素在其左侧。因此,这些丢失的像素创建一条黑线。这就像图像被移位2个像素的权利。我没有做任何其他操作的形象,只是读它,并再次打印。

这是我用写的图像的功能是:

 无效pgmwrite(字符*文件名,无效* VX,诠释NX,诠释NY)
{
  FILE * FP;  INT I,J,K,灰色;  浮XMIN,XMAX,TMP,未来值;
  浮脱粒= 255.0;  浮动* X =(浮点*)VX;  如果(NULL ==(FP =的fopen(文件名,在w)))
  {
    fprintf中(标准错误,pgmwrite:无法创建<%S> \\ N,文件名);
    出口(-1);
  }  的printf(写%深x%d张图片到文件:%s \\ n,NX,NY,文件名);  / *
   *查找最大和最小数组的绝对值
   * /  XMIN =晶圆厂(X [0]);
  XMAX =晶圆厂(X [0]);  对于(i = 0; I< NX * NY;我++)
  {
    如果(晶圆厂(X [I])LT; XMIN)XMIN =晶圆厂(X [I]);
    如果(晶圆厂(X [I])GT; XMAX)XMAX =晶圆厂(X [I]);
  }  如果(XMIN == XMAX)XMIN = XMAX-1.0;  fprintf中(FP,P2 \\ n);
  fprintf中(FP#写的pgmwrite \\ n);
  fprintf中(FP,%D \\ n,NX,NY);
  fprintf中(FP,%d个\\ N(INT)脱粒);  K = 0;  为(J = NY-1; J> = 0; j--)
  {
    对于(i = 0; I< NX;我++)
    {
      / *
       *访问x的值[I] [J]。
       * /      TMP =×〔J + NY * I]      / *
       *适当缩放值,因此在0到脱粒之间
       * /      未来值= THRESH *((晶圆厂(TMP)-xmin)/(XMAX-XMIN))+ 0.5;
      灰色=(int)的未来值;      fprintf中(FP,%3D,灰色);      如果(0 ==第(k + 1)%16)fprintf中(FP,\\ n);      ķ++;
    }
  }  如果(0 = K%16!)fprintf中(FP,\\ n);
  FCLOSE(FP);
}


解决方案

您masterbuf的两个定义可以都创建2D阵列,但它们不以同样的方式这样做。该功能 arralloc()以创造空间数据指针 - 不仅仅是数据与单纯的静态数组的定义。这是什么工程以意思是说,在pgmwrite(),而X [I] [j]的将返回相同的结果而不管所使用的方法,其中x [Ⅰ]将意味着,因为指针的参与两个不同的东西。

这是值得注意的,你会得到编译器的线索,你应该改变无效* VX 中的原型问题浮* VX 。既然你立即和无条件地铸造了这一空白*为float *,它会是更好的做法,无论如何做到这一点。

(第2)另外,如果有兴趣,看看这个响应。它展示了如何使用二维成一个单一的malloc分配块索引,而不arralloc()。

I have a MPI program for image processing (pgm file) in MPI C and I use Dynamic allocation for a 2D Array as follows.

float **masterbuf;
masterbuf = arralloc(sizeof(float), 2, M, N);

When I use

float masterbuf[M][N];

the image that the program gives looks fine.

The problem is that when I use dynamic allocation the image loses some pixels in its left side. So these missing pixels create a black line. It's like the image has been shifted 2 pixels right. I don't do any other operations to the image, just read it and print it again.

The function that I use to write the image is:

void pgmwrite(char *filename, void *vx, int nx, int ny)
{
  FILE *fp;

  int i, j, k, grey;

  float xmin, xmax, tmp, fval;
  float thresh = 255.0;

  float *x = (float *) vx;

  if (NULL == (fp = fopen(filename,"w")))
  {
    fprintf(stderr, "pgmwrite: cannot create <%s>\n", filename);
    exit(-1);
  }

  printf("Writing %d x %d picture into file: %s\n", nx, ny, filename);

  /*
   *  Find the max and min absolute values of the array
   */

  xmin = fabs(x[0]);
  xmax = fabs(x[0]);

  for (i=0; i < nx*ny; i++)
  {
    if (fabs(x[i]) < xmin) xmin = fabs(x[i]);
    if (fabs(x[i]) > xmax) xmax = fabs(x[i]);
  }

  if (xmin == xmax) xmin = xmax-1.0;

  fprintf(fp, "P2\n");
  fprintf(fp, "# Written by pgmwrite\n");
  fprintf(fp, "%d %d\n", nx, ny);
  fprintf(fp, "%d\n", (int) thresh);

  k = 0;

  for (j=ny-1; j >=0 ; j--)
  {
    for (i=0; i < nx; i++)
    {
      /*
       *  Access the value of x[i][j]
       */

      tmp = x[j+ny*i];

      /*
       *  Scale the value appropriately so it lies between 0 and thresh
       */

      fval = thresh*((fabs(tmp)-xmin)/(xmax-xmin))+0.5;
      grey = (int) fval;

      fprintf(fp, "%3d ", grey);

      if (0 == (k+1)%16) fprintf(fp, "\n");

      k++;
    }
  }

  if (0 != k%16) fprintf(fp, "\n");
  fclose(fp);
}

解决方案

Your two definitions of masterbuf may both create 2D arrays, but they don't do so in the same way. The function arralloc() creates space for data and pointers--not just data as with the simple static array definition. What this works out to mean is that in pgmwrite(), while x[i][j] will return the same result regardless of the method used, x[i] will mean two different things because of the pointer involvement.

It's worth noting that you'll be given a clue by the compiler as to the problem should you change void *vx in the prototype to float *vx. Since you're immediately and unconditionally casting this void * to a float *, it'd be much better practice to do this anyhow.

(2nd edit:) Also, if interested, check out this response. It shows how to index using two dimensions into a single malloc'd block, without arralloc().

这篇关于动态分配二维数组用C差异用一个简单的二维数组Mpi的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆