2025년 1월 15일 수요일

profile fortran + mpi

어떻게 fortran+mpi 프로그램을 프로파일링 하는지 공부.

인터넷을 찾아보니 주로 사용하는 방법은 

(1) gprof 과 gprof2dot 조합 : 컴파일 시 -pg option필요

(2) valgrind, callgrind, kcashegrind 조합 : 컴파일 시 -g option 필요. 

인 것 같다. 


How to use gprof 

(1) compile code with option -pg 

(2) run the code  : (mpirun generate multiple gmon.out )

    export GMON_OUT_PREFIX=gmon.out  #optional? 

    mpirun -np 3 ./run 

(3) once gmon.out file is created, run gprof.  

    gprof run  gmon.out.xxx > prof_result.txt 

(4) reading prof_result.txt seems to show which subroutine is most time-consumming. 


2024년 11월 13일 수요일

interactive python arrow key problem

 Sometimes, the arrow key in python shell does not act like usually. Pressing arrow key does not gives history(previous line) but gives characters. This can be fixed by installing "gnureadline" package. 


   pip3 install gnureadline  



2024년 11월 10일 일요일

Binary file problem with intel fortran (ifort) and gfortran (GNU fortran)

 I found that reading a binary file created with the same source file but compiled by ifort and gfortan give different results. By searching internet, I found that the way binary file is written in ifort is not the same sa gfortran. Thus, to make the same code gives the same results, one have to use open binary files with ACCESS='stream', FORM='unformatted'. 

  • Non-standard: open(unit=20,file=fname,form='binary')
  • Standard: open(unit=20,file=fname,form='unformatted',access='stream',status='replace')


But, this seems to be not the full story. 

I need to check the correct way to convert following gfortran code to ifort code.

 open(pin_file_id, file=trim(pin_file), status='unknown',form='unformatted', &

                 access='direct',action='write',recl=nsize,iostat=ios)


* After testing, following seems to be equivalent to the above one. 

open(pin_file_id, file=trim(pin_file), status='unknown',form='BINARY', &

                 access='direct',action='write',recl=nsize,iostat=ios)


* According to ChatGPT, ifort and gfortran interpret differently the recl= argument in open statement. In gfortran, 'recl' is interpreted as length of bytes. In ifort, 'recl' is interpreted as length of 4 bytes. Thus, to get the same results, if "recl= nsize" in gfortran , "recl= nsize/4" in ifort have to be used. Or/And(?) one have to use compile option in "ifort -assume byterecl .

This also imply that the "recl" in gfortran must be a multiple of 4. 


* But, modifying the source code depending on the compiler or machine is not desirable. access='stream' option without 'recl=' parameter will behave similarily in both ifort and gfortran. keep form='unformatted'.  status='replace' create a new file if it does not exist, or replace it if it exist. Thus, it may be better than status='unknown' to ensure freshfile in each run. (Or default status='unknown' may be okay??)

But, to access specific data, one have to use 'pos=' parameter which should be counted in whole file. For example, if data is stored as  'real(8), integer(4), real(4)', to read these 

read(file_id, pos=1) real_value

read(file_id, pos=9) int_value

read(file_id, pos=13) real4_value


In other words, 'pos' specify the starting byte position, and type of variable specify the length of the data to read. 




2024년 10월 29일 화요일

Gradient of eigenvalue eigenvector of Matrix

Matrix 의 eigenvalue 와 eigen vector 는 다음과 같은 관계가 있다. 

$A u = v u$. 

여기서, eigenvalue와 eigenvector 를 일종의 matrix A에 대한 함수로 생각하고($v(A)$ and $u(A)$.) , 미분 Gradient  을 정의할 수 있다고 볼 수 있다.  


문제는 이러한 미분을 일반적인 Matrix 에 대해서는 정확하게 정의하기가 어렵다는 것이다. 다만, real symmetric matrix인 경우에는 다음과 같은 정의를 할 수 있다. matrix $A_0$ 에 대해서 eigenvalue, eigenvector가 $v_0$, $u_0$라고 하자. 그러면 매우 작은 Matrix변화에 대해 

$ d v =  u_0^T (d A) u_0 $ 또는 $ dv/d(A_{ij}) = (u_0)_i (u_0)_j$.

$ d u = ( v_0 I - A_0) ( d A) u_0$ 또는 $ (d u)_i = ( v_0 I - A_0)_{ij} ( d A)_{jk} (u_0)_k$

와 같이 정의할 수 있다고 한다.  (real symmetric matrix임에 유의.)


REF: https://www.janmagnus.nl/papers/JRM011.pdf


즉, matrix $A_0$ 가 matrix $A_0 + d A$ 로 변할 때, eigen value와 eigen vector는 

$ v_0 -> v_0 + d v $,

$ u_0 -> u_0 + d u$

로 계산할 수 있다는 것이다. 하지만, 논문을 자세히 읽어보진 않았지만 아마도 degeneracy 가 없어야 한다는 조건이 붙을 것 같다.   





2024년 10월 20일 일요일

Baldur's Gate 2 hex edit

 How To hex edit Baludur's gate save file.

(1) Open Baldur.gam file in the save folder.

(2) money: convert gold in hex number. Edit by searching in reverse order.( For example,2000 -> 07D0. Search "D0 07"). 

(3) Character status: convert status in hex number. serach in order of 

"STR STRMOD INT WIS DEX CON CHA". (STRMOD is for STR=18 case.)  


어나더에덴

https://m.cafe.naver.com/ca-fe/web/cafes/29617015/articles/247442?tc=cafe_member_profile

2024년 9월 10일 화요일

Caution for the copying of python list or dictionary ( '=' is not what you think!)

I found there is a surprising behavior of '=' in python!! 


a = np.array([1,2,3]);b=a;b[0]=0;print(a,b)

[0 2 3] [0 2 3]


* When one use '=' in python , it usually mean to copy the values of the right hand side variable to the left hand side variable, i.e. assignment.  

* However, in python, if the variable is a list or dictionary, '=' has different meaning. 

The operation depends on the variable. 


(1) If it is a default simple data type like, it creates different copy. 

     b=a  :  a and b are unrelated except they have the same values. 


(2) If it is a compound object like list, dictionary, or complex object,

    '=' means assign alternative name. 

     b=a  : a and b directs the same adress and shares the values. 

             In other words, a and b are linked. 

             Thus, any change in a or b affacts the other. 


To avoid this, one can copy the compound object. However, there is two different copies. 

(1) Shallow copies : if the object is just a list or shallow, one can use 


    b = a[:]  or b = a.copy() 


    This creates a shallow copy of a and changes in a or b does not affect the other 

    if the object is shallow(only one index). 

    However, if it is more complex object like list of lists, in fact they share memory. 

    Any change in deeper level of a or b affects the other. 


(2) Deep copies :   to make a completely decoupled copy of compound object. 

                     one have to make a deep copy using 'copy' module.

      import copy

      b = copy.deepcopy(a) 

     



REF: https://medium.com/@thawsitt/assignment-vs-shallow-copy-vs-deep-copy-in-python-f70c2f0ebd86

자주 틀리거나 헷갈리는 맞춤법 구분

 (1) 되다/돼다 :

      '되'와 '돼'가 들어가는 부분을 '하'와 '해'로 바꾸어 본다. 쓰지 않는 말이되더라도 둘중에 어느쪽이 더 자연스럽게 들리는지를 생각해보면 된다. (예를 들어, 앞의 글에서 '한다' 와 '핸다'를 비교하면 '한다'가 자연스럽기 때문에 '된다'를 쓰면 된다.) 


(2) 결재/결제 : 

     '재판'이라고 할때의 '재'와 경제라고 할 때의 '제'를 떠올리면 된다. '결재'는 '재판'이라고 할때 처럼 일종의 판단을 하는 것이고, '결제'는 경제 활동의 일종이므로 '제'를 쓴다.  


(3) 깨닫게, 깨닳게, 깨달케 :

     "깨달음"의 동사는 "깨닫다" 이다. ("깨닳다", "깨달다" 라는 단어는 없음.) 단지, "ㄷ" 받침이 불규칙적으로 변하여 "ㅇ" 앞에서 "ㄹ" 로 발음되는 것이다. (때문에 "깨달음", "깨달아" 는 맞는 맞춤법) 따라서, "깨닫게"를 제외하곤 전부 틀린 맞춤법이다.