parallel-io/heat-restart/c/io.c
void read_field(field *temperature1, field *temperature2, char *filename,
                parallel_data *parallel)
{
    FILE *fp;
    int nx, ny, i, j;
    double **full_data;
    double **inner_data;

    int nx_local, count;

    fp = fopen(filename, "r");
    /* Read the header */
    count = fscanf(fp, "# %d %d \n", &nx, &ny);
    if (count < 2) {
        fprintf(stderr, "Error while reading the input file!\n");
        MPI_Abort(MPI_COMM_WORLD, -1);
    }

    parallel_setup(parallel, nx, ny);
    set_field_dimensions(temperature1, nx, ny, parallel);
    set_field_dimensions(temperature2, nx, ny, parallel);

    /* Allocate arrays (including ghost layers) */
    temperature1->data =
        malloc_2d(temperature1->nx + 2, temperature1->ny + 2);
    temperature2->data =
        malloc_2d(temperature2->nx + 2, temperature2->ny + 2);

    inner_data = malloc_2d(temperature1->nx, temperature1->ny);

    if (parallel->rank == 0) {
        /* Full array */
        full_data = malloc_2d(nx, ny);

        /* Read the actual data */
        for (i = 0; i < nx; i++) {
            for (j = 0; j < ny; j++) {
                count = fscanf(fp, "%lf", &full_data[i][j]);
            }
        }
    } else {
        /* Dummy array for full data. Some MPI implementations
         * require that this array is actually allocated... */
        full_data = malloc_2d(1, 1);
    }

    nx_local = temperature1->nx;

    MPI_Scatter(full_data[0], nx_local * ny, MPI_DOUBLE, inner_data[0],
                nx_local * ny, MPI_DOUBLE, 0, MPI_COMM_WORLD);

    /* Copy to the array containing also boundaries */
    for (i = 0; i < nx_local; i++)
        memcpy(&temperature1->data[i + 1][1], &inner_data[i][0],
               ny * sizeof(double));

    /* Set the boundary values */
    for (i = 1; i < nx_local + 1; i++) {
        temperature1->data[i][0] = temperature1->data[i][1];
        temperature1->data[i][ny + 1] = temperature1->data[i][ny];
    }
    for (j = 0; j < ny + 2; j++) {
        temperature1->data[0][j] = temperature1->data[1][j];
        temperature1->data[nx_local + 1][j] =
            temperature1->data[nx_local][j];
    }

    copy_field(temperature1, temperature2);

    free_2d(full_data);
    free_2d(inner_data);
    fclose(fp);
}
/* Read the initial temperature distribution from a file and
 * initialize the temperature fields temperature1 and
 * temperature2 to the same initial state. */
Read the initial temperature distribution from a file and
initialize the temperature fields temperature1 and
temperature2 to the same initial state.
Read the initial temperature distribution from a file and initialize the temperature fields temperature1 and temperature2 to the same initial state .
void read_field(field *temperature1, field *temperature2, char *filename,
                parallel_data *parallel)
{
    FILE *fp;
    int nx, ny, i, j;
    double **full_data;
    double **inner_data;
    int nx_local, count;
    fp = fopen(filename, "r");
    count = fscanf(fp, "# %d %d \n", &nx, &ny);
    if (count < 2) {
        fprintf(stderr, "Error while reading the input file!\n");
        MPI_Abort(MPI_COMM_WORLD, -1);
    }
    parallel_setup(parallel, nx, ny);
    set_field_dimensions(temperature1, nx, ny, parallel);
    set_field_dimensions(temperature2, nx, ny, parallel);
    temperature1->data =
        malloc_2d(temperature1->nx + 2, temperature1->ny + 2);
    temperature2->data =
        malloc_2d(temperature2->nx + 2, temperature2->ny + 2);
    inner_data = malloc_2d(temperature1->nx, temperature1->ny);
    if (parallel->rank == 0) {
        full_data = malloc_2d(nx, ny);
        for (i = 0; i < nx; i++) {
            for (j = 0; j < ny; j++) {
                count = fscanf(fp, "%lf", &full_data[i][j]);
            }
        }
    } else {
        full_data = malloc_2d(1, 1);
    }
    nx_local = temperature1->nx;
    MPI_Scatter(full_data[0], nx_local * ny, MPI_DOUBLE, inner_data[0],
                nx_local * ny, MPI_DOUBLE, 0, MPI_COMM_WORLD);
    for (i = 0; i < nx_local; i++)
        memcpy(&temperature1->data[i + 1][1], &inner_data[i][0],
               ny * sizeof(double));
    for (i = 1; i < nx_local + 1; i++) {
        temperature1->data[i][0] = temperature1->data[i][1];
        temperature1->data[i][ny + 1] = temperature1->data[i][ny];
    }
    for (j = 0; j < ny + 2; j++) {
        temperature1->data[0][j] = temperature1->data[1][j];
        temperature1->data[nx_local + 1][j] =
            temperature1->data[nx_local][j];
    }
    copy_field(temperature1, temperature2);
    free_2d(full_data);
    free_2d(inner_data);
    fclose(fp);
}
void read_field ( field * temperature1 , field * temperature2 , char * filename , parallel_data * parallel ) { FILE * fp ; int nx , ny , i , j ; double * * full_data ; double * * inner_data ; int nx_local , count ; fp = fopen ( filename , " " ) ; count = fscanf ( fp , " \n " , & nx , & ny ) ; if ( count < 2 ) { fprintf ( stderr , " \n " ) ; MPI_Abort ( MPI_COMM_WORLD , -1 ) ; } parallel_setup ( parallel , nx , ny ) ; set_field_dimensions ( temperature1 , nx , ny , parallel ) ; set_field_dimensions ( temperature2 , nx , ny , parallel ) ; temperature1 -> data = malloc_2d ( temperature1 -> nx + 2 , temperature1 -> ny + 2 ) ; temperature2 -> data = malloc_2d ( temperature2 -> nx + 2 , temperature2 -> ny + 2 ) ; inner_data = malloc_2d ( temperature1 -> nx , temperature1 -> ny ) ; if ( parallel -> rank == 0 ) { full_data = malloc_2d ( nx , ny ) ; for ( i = 0 ; i < nx ; i ++ ) { for ( j = 0 ; j < ny ; j ++ ) { count = fscanf ( fp , " " , & full_data [ i ] [ j ] ) ; } } } else { full_data = malloc_2d ( 1 , 1 ) ; } nx_local = temperature1 -> nx ; MPI_Scatter ( full_data [ 0 ] , nx_local * ny , MPI_DOUBLE , inner_data [ 0 ] , nx_local * ny , MPI_DOUBLE , 0 , MPI_COMM_WORLD ) ; for ( i = 0 ; i < nx_local ; i ++ ) memcpy ( & temperature1 -> data [ i + 1 ] [ 1 ] , & inner_data [ i ] [ 0 ] , ny * sizeof ( double ) ) ; for ( i = 1 ; i < nx_local + 1 ; i ++ ) { temperature1 -> data [ i ] [ 0 ] = temperature1 -> data [ i ] [ 1 ] ; temperature1 -> data [ i ] [ ny + 1 ] = temperature1 -> data [ i ] [ ny ] ; } for ( j = 0 ; j < ny + 2 ; j ++ ) { temperature1 -> data [ 0 ] [ j ] = temperature1 -> data [ 1 ] [ j ] ; temperature1 -> data [ nx_local + 1 ] [ j ] = temperature1 -> data [ nx_local ] [ j ] ; } copy_field ( temperature1 , temperature2 ) ; free_2d ( full_data ) ; free_2d ( inner_data ) ; fclose ( fp ) ; }
Read the initial temperature distribution from a file and
 initialize the temperature fields temperature1 and
 temperature2 to the same initial state.
Read the initial temperature distribution from a file and initialize the temperature fields temperature1 and temperature2 to the same initial state .
/* Read the header */ /* Allocate arrays (including ghost layers) */ /* Full array */ /* Read the actual data */ /* Dummy array for full data. Some MPI implementations
         * require that this array is actually allocated... */ /* Copy to the array containing also boundaries */ /* Set the boundary values */