Thursday, May 17, 2018

Dump and load and diff

---------------------------------------------------------------------------------
To dump binary data from application, and then see it
---------------------------------------------------------------------------------
void dump_data (float *a, size_t bytes, char *root, int count, int rank)
{
    char filename[256];
    sprintf (filename, "%s_%d_%d", root, rank, count);
    int fd = open (filename, O_RDWR|O_CREAT|O_TRUNC, 00644);
    if (fd == -1) {
        printf ("Could not open file %s for writing\n", filename);
        return;
    }
    ucsl_printf ("Dumping data into %s\n", filename);
    fflush (_UCSL_LOG);
    write (fd, a, bytes);
    close (fd);
}

Call like this:
---------------
int iter=0;
func() {
    ...
    dump_data ((float *)array, n*sizeof(float), "/tmp/a", iter++);
}

To visualize:
---------------
Say a and b are the two dumps where n1=2001, then run the script:
./xdiff a b 2001


Content of xdiff is as follows:
-------------------------------
$ cat xdiff
#! /bin/csh -Xf

if ($#argv != 3) then
    echo "Usage: $0 file1 file2 n1"
    exit 0
endif
diff $1 $2
cp -f $1 aaaaaa
cat $2 >> aaaaaa
cat diff.bin >> aaaaaa
ximage perc=90 n1=$3 < aaaaaa
exit 0
---------------------------------------------------------------------------------


diff is an executable produced from the following C code:

#include
#include
#include
#include
#include
#include
#include
#include




Using python to do the same:

---------------------------------------------------------------------------------
Using python: python2.7.9 is a script that loads python/2.7.9 and PrgEnv-gnu
---------------------------------------------------------------------------------
% source python2.7.9
This modulefile provides python (2.7.9)

More information about python can be found at:
    http://docs.python.org/2/

% which ipython
% ipython --pylab
Python 2.7.9 (default, Jan  7 2016, 09:44:23)
Type "copyright", "credits" or "license" for more information.

IPython 4.0.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
Using matplotlib backend: TkAgg

In [1]: run ./compute.py

In [2]: dd = data1-data2

In [4]: dd.min()
Out[4]: -12.869473

---------------------------------------------------------------------------------
To check diff between two binary files
---------------------------------------------------------------------------------
$ python --version
Python 2.6.6
$ python
Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.fromfile('/tmp/a',dtype=np.float32)
array([ 1536.7668457 ,  1534.49963379,  1528.29431152, ...,  3819.53222656,
        3827.30517578,  3833.81079102], dtype=float32)
>>> aa = np.fromfile('/tmp/a',dtype=np.float32)
>>> aa.shape
(40330178,)
>>> bb = np.fromfile('/tmp/a',dtype=np.float32)
>>> cc = aa - bb
>>> cc.sum

>>> cc.sum()
0.0
>>>

compute.py looks like this:
----------------------------------------------
import matplotlib.pylab as plt
import numpy as np
import time

fname1    = '/tmp/a'
fname2    = '/tmp/b'

nt = 2001 # number of time pts
nc = 190  # receiver samples (number of channel)

data1 = np.fromfile(fname1,dtype=np.float32)
data1 = data1.reshape((nc,nt)).T
data2 = np.fromfile(fname2,dtype=np.float32)
data2 = data2.reshape((nc,nt)).T

figlabel1 = fname1
figlabel2 = fname2

## plot parameters
#~ cmin = data.min()
#~ cmax = data.max()

#~ pct = 5.0 ## set the color range
#~ dmi = data.min()
#~ dmx = data.max()
#~ cmin = 0.5*(dmi+dmx) - pct/100.0*(dmx-dmi)
#~ cmax = 0.5*(dmi+dmx) + pct/100.0*(dmx-dmi)

pct = 10.0 ## set the color range
cmin = pct/100 * min(data1.min(), data2.min())
cmax = pct/100 * max(data1.max(), data2.max())

# # show data
cmap = 'jet'
#~ cmap = 'gray'
#~ cmap = 'bone'

fig1 = plt.figure()
dshow = plt.imshow(data1, vmin = cmin, vmax = cmax, cmap = cmap)
plt.colorbar(dshow)
#~ plt.axis('off')
plt.title(figlabel1, fontsize=16)
#~
mng = plt.get_current_fig_manager()
mng.resize(*mng.window.maxsize())
plt.show()

fig2 = plt.figure()
dshow = plt.imshow(data2, vmin = cmin, vmax = cmax, cmap = cmap)
plt.colorbar(dshow)
#~ plt.axis('off')
plt.title(figlabel2, fontsize=16)
#~
mng = plt.get_current_fig_manager()
mng.resize(*mng.window.maxsize())
plt.show()



# # show models
fig3 = plt.figure()

gfig = fig3.add_subplot(1,3,1)
gshow = plt.imshow(data1, vmin = cmin, vmax = cmax, cmap = cmap , interpolation='none')
plt.colorbar(gshow)
gfig.set_title('data1')

kfig = fig3.add_subplot(1,3,2)
kshow = plt.imshow(data2, vmin = cmin, vmax = cmax, cmap = cmap , interpolation='none')
plt.colorbar(kshow)
kfig.set_title('data2')

dfig = fig3.add_subplot(1,3,3)
dshow = plt.imshow(data1 - data2,  cmap = cmap , interpolation='none')
plt.colorbar(dshow)
dfig.set_title('difference')

mng = plt.get_current_fig_manager()
mng.resize(*mng.window.maxsize())
plt.show()

0 Comments:

Post a Comment

<< Home