Sunday, December 18, 2011

Extend Numpy with ctypes

The idea is to write extend numpy with c. Thus, you can enjoy the performance of c and also employ any c only libs. Passing data between numpy and c with numpy arrays and c pointers.

PS: ctypes dose not recognize c++ symbols. Make sure to put your wrap function between extern "C" { and }

Here is the example.
We need 3 files.
C implimentation func.c;
Python wrapping func.py;
and Python calling interface test.py.

/*func.c*/
/* You can complie it with the following command*/
/* gcc -O2 -fPIC func.c -shared -o libfunc.so */
#ifdef __cplusplus
extern "C" {
#endif
int skew_symmetric_matrix(double *t, double *tx)
{
    tx[0] = 0;
    tx[1] = -t[2];
    tx[2] = t[1];
    tx[3] = t[2];
    tx[4] = 0;
    tx[5] = -t[0];
    tx[6] = -t[1];
    tx[7] = t[0];
    tx[8] = 0;
    return 0;
}
#ifdef __cplusplus
}
#endif

##Python wrapping file
##func.py
import numpy as np
import ctypes as ct

##load the s
_func = np.ctypeslib.load_library('libfunc', '.')
ndpointer = np.ctypeslib.ndpointer
array_double = ndpointer(dtype = ct.POINTER(ct.c_double), flags='CONTIGUOUS,ALIGNED')
##Specify the arg types the numpy arrays which have to be aligned continuously. 
##Passing a[::2] directly might not work. Using a[::2].copy() instead.
##There also other arg types such as [array_double, ct.c_int, ct.c_double]
_func.skew_symmetric_matrix.argtypes = [array_double] * 2

##Define python wrapping funcion
##ctypes can only see pointers but no size or shape of your numpy array
def skew_symmetric_matrix(t):
if t.size != 3:
return None
tx = np.zeros((3, 3), dtype=np.double)
_func.skew_symmetric_matrix(t, tx)
return tx

##Python interface
##test.py
import scipy as sp, numpy as np
import func

t = np.array([1.0, 2.0, 3.0])
tx = func.skew_symmetric_matrix(t)
print (t)
print (tx)




Saturday, December 10, 2011

cblas of gotoblas2

According to
"http://shimingyoung.blogspot.com/2010/01/gotoblas-and-lapackwrapper.html"

Also edit the cblas.h by adding one line
#define blasint int
And edit the f2c.h by changing the line on the 10th line to
typedef int integer;

Otherwise, blasint is not defined while compiling.

Thursday, November 17, 2011

git remote

git pull ssh://usr@ip:port/~/path branch_name
git push ssh://usr@ip:port/~/path local_branch:remote_branch

Check disk usage

jdiskreport
There is another software visualize disk usage with squares, but I forget its name.

Sunday, October 30, 2011

linking libf2c.so and undefined reference to `MAIN__'

The problem is that f2c wants to link to the MAIN__ function which is

the "fortran main". Apparently it is just a dummy function call. And
the fix is quite simple:

Add "int  MAIN__( ) {  return 0; }" somewhere is the sources.

Or pass the option -u MAIN__ to the linker.

The attached patch correct the problem.

Friday, October 28, 2011

Friday, October 21, 2011

admin in win7

net user administrator /active:yes
net user administrator /active:no
to enable/disable admin in win7

Tuesday, October 18, 2011

cmake with cuda

 cmake .. -DCUDA_TOOLKIT_INCLUDE:=/usr/include/cuda/ -DCUDA_SDK_ROOT_DIR=/usr/share/cuda-sdk/ -DCUDA_NVCC_FLAGS=--compiler-bindir=/opt/gcc-4.4

歸去來兮

好久沒瞎寫點東西。現在算是又回來了。
本來想隨便twitter一下就好了,不必大動干戈。也許每天推推能把牆推倒。但twitter不給機會。寫錯個字都改不了。至少我沒找到簡單的方法。懶得查幫助了。還是blog吧。人老了,新的事物往往接受不來。