ADDA > chpointをPythonで読込む > v0.1:niter読込み失敗 / v0.3: (size_t読込み対応, read [inprodR],[prev_err],[resid_scale])

```txt:動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
```

数値光散乱シミュレーションで出力されるチェックポイントファイルの読込みをCで実装した。
http://qiita.com/7of9/items/0fbea38b48cdfe4588dd

これをmatplotlibで読むためにPython実装に変更しようとしている。

参考 http://qiita.com/7of9/items/a2e032301b9771ae183e

## v0.1 (niter読込み失敗)

```read_chpoint.py
import numpy as np
import array
import sys

'''
v0.1 Dec. 18, 2016
	- read_chpoint_file() > read [ind_m],[local_nRows],[niter],[counter]
'''

def read_chpoint_file(chpFilename, auxFilename):
	print(chpFilename)
	print(auxFilename)

	with open(chpFilename, "rb") as chpfp:
		ind_m = array.array('i') # index of iterative method
		ind_m.fromfile(chpfp, 1)
		print ('ind_m:',ind_m) 
		local_nRows = array.array('i') # number of local rows of decomposition (only real dipoles)
		local_nRows.fromfile(chpfp, 1)
		print ('local_nRows:',local_nRows)
		niter = array.array('i') # iteration count
		niter.fromfile(chpfp, 1)
		print('niter:',niter)
		counter = array.array('i') # number of successive iterations without residual decrease
		counter.fromfile(chpfp, 1)
		print('counter:',counter)

argvs = sys.argv
argc = len(argvs)

print argvs
if (argc < 3):
	print("ERROR: chpoint file is not specified\r\n")
	print("   [cmd] [chpoint file] [auxiliary file]\r\n")
	sys.exit()

read_chpoint_file(chpFilename=argvs[1], auxFilename=argvs[2])
```

```txt:実行
$ python read_chpoint.py LN-CHP LN-AUX 
['read_chpoint.py', 'LN-CHP', 'LN-AUX']
LN-CHP
LN-AUX
('ind_m:', array('i', [5]))
('local_nRows:', array('i', [27984]))
('niter:', array('i', [0]))
('counter:', array('i', [209]))
```

上記はniterを読込み失敗している。
(Cで実装した時は以下となる。)

```txt:C実装
$ gcc read_chpoint.c && ./a.out LN-CHP LN-AUX 
ind_m:5
local_nRows:27984
niter:230
counter:160
```

## v0.3 (size_t読込み対応, read [inprodR],[prev_err],[resid_scale])

参考 http://qiita.com/7of9/items/a2e032301b9771ae183e
のv0.3 

```read_chpoint.py
import numpy as np
import array
import sys

'''
v0.3 Dec. 19, 2016
	- add wrap_fromfile()
	- read [inprodR],[prev_err],[resid_scale]
v0.2 Dec. 19, 2016
	- fix bug > read [local_nRows] for "size_t" type
v0.1 Dec. 18, 2016
	- read_chpoint_file() > read [ind_m],[local_nRows],[niter],[counter]
'''

def wrap_fromfile(rfp, typecode):
	res = array.array(typecode)
	res.fromfile(rfp, 1)
	return res

def read_chpoint_file(chpFilename, auxFilename):
	print(chpFilename)
	print(auxFilename)

	with open(chpFilename, "rb") as chpfp:
		ind_m = wrap_fromfile(chpfp, 'i') # index of iterative method
		print ('ind_m:',ind_m) 
		# L: unsigned long = size_t
		local_nRows = wrap_fromfile(chpfp, 'L') # number of local rows of decomposition (only real dipoles)
		print ('local_nRows:',local_nRows)
		niter = wrap_fromfile(chpfp, 'i') # iteration count
		print('niter:',niter)
		counter = wrap_fromfile(chpfp, 'i') # number of successive iterations without residual decrease
		print('counter:',counter)
		inprodR = wrap_fromfile(chpfp, 'd') # used as |r_0|^2 and best squared norm of residual up to some iteration
		print('inprodR:', inprodR)
		prev_err = wrap_fromfile(chpfp, 'd') # previous relative error; used in ProgressReport, initialized in IterativeSolver
		print('prev_err:', prev_err)
		resid_scale = wrap_fromfile(chpfp, 'd') # scale to get square of relative error
		print('resid_scale:', resid_scale) # scale to get square of relative error

argvs = sys.argv
argc = len(argvs)

print argvs
if (argc < 3):
	print("ERROR: chpoint file is not specified\r\n")
	print("   [cmd] [chpoint file] [auxiliary file]\r\n")
	sys.exit()

read_chpoint_file(chpFilename=argvs[1], auxFilename=argvs[2])
```

```txt:実行
$ python read_chpoint.py LN-CHP LN-AUX 
['read_chpoint.py', 'LN-CHP', 'LN-AUX']
LN-CHP
LN-AUX
('ind_m:', array('i', [5]))
('local_nRows:', array('L', [27984L]))
('niter:', array('i', [209]))
('counter:', array('i', [139]))
('inprodR:', array('d', [4.317011265934948]))
('prev_err:', array('d', [0.3198525404317458]))
('resid_scale:', array('d', [0.020384616751203153]))
```

```txt:C実装
$ ulimit -s unlimited && gcc read_chpoint.c && ./a.out LN-CHP LN-AUX
ind_m:5
local_nRows:27984
niter:209
counter:139
inprodR:4.317
prev_err:0.320
resid_scale:0.020
...
```

Python実装とC実装で同じになった。

v0.1の結果とcounterが異なるのは、ADDAの実行で異なるイテレーション回数の結果になっていたのかもしれない。