0

I have been struggling with a small test code I made to write data to a netcdf format file. The code should write two variables with the same values but the memory is allocated differently. The code is the following:

int main(int argc, char** argv)
{

  int i, j, k;
  char filen2[80];

  int ncid, err, cmode;
  int dimid[2],varid[2];
  printf("Creating Netcdf Output file\n");
  int mx=5, my=5, mz=5;
    printf("mx my mz: %d %d %d %i \n", mx , my, mz,  rank);
    sprintf(filen2, "test.nc");
    printf("Writing file: %s \n",filen2);
    cmode = NC_CLOBBER;

    err = nc_create(filen2, cmode, &ncid); ERR

    /* free info object */
    if (MInfo != MPI_INFO_NULL) MPI_Info_free(&MInfo);

    /* define dimensions */
    err = nc_def_dim(ncid, "MZ", mz, &dimid[0]); ERR
    err = nc_def_dim(ncid, "MY", my, &dimid[1]); ERR

    err = nc_def_var(ncid, "Z", NC_DOUBLE, 2, dimid, &varid[0]); ERR
    err = nc_def_var(ncid, "Y", NC_DOUBLE, 2, dimid, &varid[1]); ERR
    /* exit define mode */
    err = nc_enddef(ncid); ERR
    size_t start2[2], count2[2];
    start2[0] = 0;
    start2[1] = 0;
    count2[0] = mz;
    count2[1] = my;
    printf("Start: %d  %d  %i \n", start2[0], start2[1],  rank);
    printf("Count: %d  %d %i \n", count[0], count[1], rank);
      double **buffertmp2=NULL;
      double buffertmp3[mz][my];
      buffertmp2 = (double **) malloc( sizeof(double *) * mz);
      for(k=0; k<mz; k++){
        buffertmp2[k] = (double *) malloc( sizeof(double) * my);
        for(j=0; j<my; j++){
          buffertmp2[k][j] = k;
          buffertmp3[k][j] = k;
        }
      }
    err = nc_put_vara_double(ncid, varid[0], start2, count2, &buffertmp2[0][0]); ERR
    err = nc_put_vara_double(ncid, varid[1], start2, count2, &buffertmp3[0][0]); ERR
    err = nc_close(ncid); ERR
  for(k=0; k<mz; k++){
  for(j=0; j<my; j++){
     printf("bff2 %le \n", buffertmp2[k][j]);
  }
}
  for(k=0; k<mz; k++){
  for(j=0; j<my; j++){
     printf("bff3 %le \n", buffertmp3[k][j]);
}  }
  printf("The End \n");
}

The output read on MATLAB gives:

X =

         0         0    1.0000    0.0000    2.0000
         0         0    1.0000    2.0000         0
         0    0.0000    1.0000    2.0000         0
         0    1.0000         0    2.0000    0.0000
         0    1.0000         0    2.0000    3.0000
Y =

     0     1     2     3     4
     0     1     2     3     4
     0     1     2     3     4
     0     1     2     3     4
     0     1     2     3     4

The output of Y is what I actually expected but the one of X is completely wrong. I was wondering if someone could point out how the memory allocation change the whole output. What can I do to write the data to a netcdf file if memory allocation was made with for loops as for the buffertmp2 without having to create an intermediate variable.

Thanks Chris

Chris
  • 1
  • 1

1 Answers1

0

I am not sure this is the correct answer, but you should note that buffertmp2 and buffertmp3 are two completely different data structures.

buffertmp3 is one big blob of memory containing a two dimensional array.

buffertmp2 is an array of pointers (rows) pointing to the row data.

When passing these different data structures to Matlab (or the netcdf functions), it should know that you are passing different data structures.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41