0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ADDA > tool > read_chpoint.c > to read the file made by SaveIterChpoint() > v0.1-v0.4

Last updated at Posted at 2016-11-29
My_Environment
Ubuntu 14.04 LTS Japanese Remix
on VMWare Fusion v8.5.2 (4635224)
on OS X El Captian v10.11.4

GNU bash, version 4.3.11(1)-release

gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4 
(also for g++)
ADDA v.1.3b6

This article is related to ADDA (light scattering simulator based on the discrete dipole approximation).

Related: ADDA > runtime parameter > grid > Sets dimensions of the computation grid
Related: ADDA > chp_time > used to trigger SaveIterChpoint() > chp_time / chpoint

v0.1

Work in progress.

read_chpoint.c
#include <stdio.h>
#include <stdlib.h>

/*
v0.1 Nov. 30, 2016
	- add read_chpoint_file();
*/

static void read_chpoint_file(char *filename)
{
	int ind_m;
	size_t local_nRows;
	FILE *rdfp;

	rdfp = fopen(filename, "rb");
	if (rdfp == NULL) {
		printf("ERROR: file is not found\r\n");
		return;
	}
	fread(&ind_m,sizeof(int),1,rdfp);
	fread(&local_nRows, sizeof(size_t), 1, rdfp);
	fclose(rdfp);

	printf("ind_m:%d\r\n", ind_m);
	printf("local_nRows:%zu\r\n", local_nRows);
}

int main(int argc, char *argv[])
{
//	printf("argc=%d\n", argc);
	char *filename;

	if (argc < 2) {
		printf("ERROR: chpoint file is not specified\r\n");
		printf("   [cmd] [chpoint file]\r\n");
		exit(1);
	}

	read_chpoint_file(argv[1]);
}
run
$ gcc read_chpoint.c
$ ./a.out  LN-CHP 
ind_m:5
local_nRows:27984

v0.3

read_chpoint.c
#include <stdio.h>
#include <stdlib.h>

/*
v0.3 Dec. 14, 2016
    - read [sc_N],[sc_size],[vec_N],[vec_size]
    - add [auxiliary] file read option
v0.2 Dec. 14, 2016
    - read [niter],[counter],[inprodR],[prev_err],[resid_scale]
v0.1 Nov. 30, 2016
    - add read_chpoint_file();
*/

static void read_chpoint_file(char *chpFilename, char *auxFilename)
{
    int ind_m; // index of iterative method
    size_t local_nRows; // number of local rows of decomposition (only real dipoles)
    int niter; // iteration count
    int counter; // number of successive iterations without residual decrease
    double inprodR; // used as |r_0|^2 and best squared norm of residual up to some iteration
    double prev_err; // previous relative error; used in ProgressReport, initialized in IterativeSolver
    double resid_scale; // scale to get square of relative error
    //
    #define MAX_SIZE (20) // 20: arbitrary
    int sc_N; // 
    int sc_size[MAX_SIZE]; //
    int vec_N; // 
    int vec_size[MAX_SIZE]; // 
    //
    FILE *chpfp; // checkpoint file
    FILE *auxfp; // auxiliary file
    // 
    int idx; // for loop

    chpfp = fopen(chpFilename, "r");
    if (chpfp == NULL) {
        printf("ERROR: checkpoint file is not found\r\n");
        return;
    }
    auxfp = fopen(auxFilename, "r");
    if (auxfp == NULL) {
        printf("ERROR: auxiliary file is not found\r\n");
        return;
    }

    fread(&ind_m,sizeof(int),1,chpfp);
    fread(&local_nRows, sizeof(size_t), 1, chpfp);
    fread(&niter,sizeof(int),1,chpfp);
    fread(&counter,sizeof(int),1,chpfp);
    fread(&inprodR,sizeof(double),1,chpfp);
    fread(&prev_err,sizeof(double),1,chpfp);
    fread(&resid_scale,sizeof(double),1,chpfp);
    //
    fread(&sc_N, sizeof(int), 1, auxfp);
    fread(&sc_size[0], sizeof(int), sc_N, auxfp);
    fread(&vec_N, sizeof(int), 1, auxfp);
    fread(&vec_size[0], sizeof(int), vec_N, auxfp);
    //
    fclose(chpfp);
    fclose(auxfp);

    printf("ind_m:%d\r\n", ind_m);
    printf("local_nRows:%zu\r\n", local_nRows);
    printf("niter:%d\r\n", niter);
    printf("counter:%d\r\n", counter);
    printf("inprodR:%.3f\r\n", inprodR);
    printf("prev_err:%.3f\r\n", prev_err);
    printf("resid_scale:%.3f\r\n", resid_scale);
    //
    printf("sc_N:%d\r\n", sc_N);
    for(idx=0; idx<sc_N; idx++) {
        printf("sc_size[]:%d\r\n", sc_size[idx]);
    }
    printf("vec_N:%d\r\n", vec_N);
    for(idx=0; idx<vec_N; idx++) {
        printf("vec_size[]:%d\r\n", vec_size[idx]);
    }   
}

int main(int argc, char *argv[])
{
//  printf("argc=%d\n", argc);
    char *filename;

    if (argc < 3) {
        printf("ERROR: chpoint file is not specified\r\n");
        printf("   [cmd] [chpoint file] [auxiliary file]\r\n");
        exit(1);
    }

    read_chpoint_file(/*chpFilename=*/argv[1], /*auxFilename=*/argv[2]);
}
run
$ ln -fs seq/chpoint/chp.0 LN-CHP
$ ln -fs seq/chpoint/aux.0 LN-AUX

$ gcc read_chpoint.c

$ ./a.out  LN-CHP LN-AUX
ind_m:5
local_nRows:27984
niter:187
counter:117
inprodR:4.317
prev_err:0.321
resid_scale:0.020
sc_N:8
sc_size[]:8
sc_size[]:8
sc_size[]:8
sc_size[]:8
sc_size[]:16
sc_size[]:16
sc_size[]:16
sc_size[]:16
vec_N:3
vec_size[]:16
vec_size[]:16
vec_size[]:16

where, the size 8 means [double] type while the 16 means [double complex] type based on the ADDA source code.

v0.4

read_chpoint.c
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>

/*
v0.4 Dec. 17, 2016
    - read vectors[]
    - read [xvec],[rvec],[pvec]
    - read scalars[]
v0.3 Dec. 14, 2016
    - read [sc_N],[sc_sizes],[vec_N],[vec_sizes]
    - add [auxiliary] file read option
v0.2 Dec. 14, 2016
    - read [niter],[counter],[inprodR],[prev_err],[resid_scale]
v0.1 Nov. 30, 2016
    - add read_chpoint_file();
*/

#define SIZE_DOUBLE (8)
#define SIZE_DCOMPLEX (16)

static void read_chpoint_file(char *chpFilename, char *auxFilename)
{
    int ind_m; // index of iterative method
    size_t local_nRows; // number of local rows of decomposition (only real dipoles)
    int niter; // iteration count
    int counter; // number of successive iterations without residual decrease
    double inprodR; // used as |r_0|^2 and best squared norm of residual up to some iteration
    double prev_err; // previous relative error; used in ProgressReport, initialized in IterativeSolver
    double resid_scale; // scale to get square of relative error
    //
    #define MAXNUM_SIZEINFO (20) // 20: arbitrary
    int sc_N; // the number of scalars' sizes
    int sc_sizes[MAXNUM_SIZEINFO]; // scalars' sizes
    int vec_N; // the number of vercotrs' sizes
    int vec_sizes[MAXNUM_SIZEINFO]; // vectors' sizes
    //
    #define MAXNUM_SCALARS (20) // 20: arbitrary
    double complex scalars[MAXNUM_SCALARS];
    //
    #define MAXNUM_XRP_VECTORS (30000) // 30000: arbitrary
    double complex xvec[MAXNUM_XRP_VECTORS]; // total electric field on the dipoles
    double complex rvec[MAXNUM_XRP_VECTORS]; // current residual
    double complex pvec[MAXNUM_XRP_VECTORS]; // polarization of dipoles, also an auxiliary vector in iterative solvers
    #define MAXNUM_VECTORS (20) // 20: arbitrary
    double complex vectors[MAXNUM_VECTORS][MAXNUM_XRP_VECTORS];
    //
    FILE *chpfp; // checkpoint file
    FILE *auxfp; // auxiliary file
    // 
    int idx; // for loop (for C89)

    // 1. read from files
    chpfp = fopen(chpFilename, "r");
    if (chpfp == NULL) {
        printf("ERROR: checkpoint file is not found\r\n");
        return;
    }
    auxfp = fopen(auxFilename, "r");
    if (auxfp == NULL) {
        printf("ERROR: auxiliary file is not found\r\n");
        return;
    }

    fread(&ind_m,sizeof(int),1,chpfp);
    fread(&local_nRows, sizeof(size_t), 1, chpfp);
    fread(&niter,sizeof(int),1,chpfp);
    fread(&counter,sizeof(int),1,chpfp);
    fread(&inprodR,sizeof(double),1,chpfp);
    fread(&prev_err,sizeof(double),1,chpfp);
    fread(&resid_scale,sizeof(double),1,chpfp);
    //
    fread(&sc_N, sizeof(int), 1, auxfp);
    fread(&sc_sizes[0], sizeof(int), sc_N, auxfp);
    fread(&vec_N, sizeof(int), 1, auxfp);
    fread(&vec_sizes[0], sizeof(int), vec_N, auxfp);
    for(idx=0; idx<sc_N; idx++) {
        fread(&(scalars[idx]), sc_sizes[idx], 1, chpfp);
    }
    fread(xvec,sizeof(double complex),local_nRows,chpfp);
    fread(rvec,sizeof(double complex),local_nRows,chpfp);
    fread(pvec,sizeof(double complex),local_nRows,chpfp);
    for(idx=0; idx<vec_N; idx++) {
        fread(&(vectors[idx][0]), vec_sizes[idx], local_nRows, chpfp);
    }    
    //
    fclose(chpfp);
    fclose(auxfp);

    // 2. debug print
    printf("ind_m:%d\n", ind_m);
    printf("local_nRows:%zu\n", local_nRows);
    printf("niter:%d\n", niter);
    printf("counter:%d\n", counter);
    printf("inprodR:%.3f\n", inprodR);
    printf("prev_err:%.3f\n", prev_err);
    printf("resid_scale:%.3f\n", resid_scale);
    //
    printf("sc_N:%d\n", sc_N);
    for(idx=0; idx<sc_N; idx++) {
        printf("sc_sizes[]:%d\n", sc_sizes[idx]);
    }
    printf("vec_N:%d\n", vec_N);
    for(idx=0; idx<vec_N; idx++) {
        printf("vec_sizes[]:%d\n", vec_sizes[idx]);
    }   

    for(idx=0; idx<sc_N; idx++) {
        // debug print
        if (sc_sizes[idx] == SIZE_DCOMPLEX) {
            printf( "scalars:(%f,%f)\n", creal(scalars[idx]), cimag(scalars[idx]) );
        } else if (sc_sizes[idx] == SIZE_DOUBLE) {
            printf( "scalars:%f\n", creal(scalars[idx]) );
        } else {
            printf( "scalars: in other type\n" );
        }
    }

    for(idx=0; idx<5; idx++) { // only first 5 among 30000
        printf( "xvec:(%f,%f), rvec:(%f,%f), pvec:(%f,%f)\n", 
            creal(xvec[idx]), cimag(xvec[idx]),
            creal(rvec[idx]), cimag(rvec[idx]),
            creal(pvec[idx]), cimag(pvec[idx])
        );
    }

    for(idx=0; idx<vec_N; idx++) { // only first 3 among local_nRows
        printf( "vectors 1:(%f,%f), 2:(%f,%f), 3:(%f,%f)\n",
            creal(vectors[idx][0]), cimag(vectors[idx][0]),
            creal(vectors[idx][1]), cimag(vectors[idx][1]),
            creal(vectors[idx][2]), cimag(vectors[idx][2])
        );
    }
}

int main(int argc, char *argv[])
{
//  printf("argc=%d\n", argc);
    char *filename;

    if (argc < 3) {
        printf("ERROR: chpoint file is not specified\r\n");
        printf("   [cmd] [chpoint file] [auxiliary file]\r\n");
        exit(1);
    }

    read_chpoint_file(/*chpFilename=*/argv[1], /*auxFilename=*/argv[2]);
}
setting(to_avoid_core_dump)
$ ulimit -s unlimited
$ ln -fs seq/chpoint/chp.0 LN-CHP
$ ln -fs seq/chpoint/aux.0 LN-AUX

where aux.0 is created using
http://qiita.com/7of9/items/4e5b299ad405819af5f0

run
$ gcc read_chpoint.c && ./a.out LN-CHP LN-AUX 
ind_m:5
local_nRows:27984
niter:230
counter:160
inprodR:4.317
prev_err:0.316
resid_scale:0.020
sc_N:8
sc_sizes[]:8
sc_sizes[]:8
sc_sizes[]:8
sc_sizes[]:8
sc_sizes[]:16
sc_sizes[]:16
sc_sizes[]:16
sc_sizes[]:16
vec_N:3
vec_sizes[]:16
vec_sizes[]:16
vec_sizes[]:16
scalars:7.159807
scalars:12.236596
scalars:0.052110
scalars:0.038853
scalars:(2.581055,2.848436)
scalars:(-0.281006,0.239659)
scalars:(-0.800018,-0.597709)
scalars:(0.966484,-0.253771)
xvec:(0.000856,-0.000519), rvec:(-0.001961,0.001694), pvec:(-0.005346,0.005240)
xvec:(0.034051,0.088220), rvec:(0.004734,-0.019609), pvec:(0.021339,-0.016807)
xvec:(-0.004780,-0.003816), rvec:(0.005126,-0.004015), pvec:(0.015493,-0.013204)
xvec:(-0.000856,0.000519), rvec:(0.001961,-0.001694), pvec:(0.005346,-0.005240)
xvec:(0.034051,0.088220), rvec:(0.004734,-0.019609), pvec:(0.021339,-0.016807)
vectors 1:(-0.001543,-0.012333), 2:(0.076928,-0.018896), 3:(0.042558,0.058127)
vectors 1:(-0.085713,0.030229), 2:(0.307545,-0.248557), 3:(0.244432,-0.029081)
vectors 1:(-0.000710,0.000439), 2:(-0.005362,0.000514), 3:(0.003159,-0.007757)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?