I rewrite this code in stream style
Re: A fast scheme implementation?
http://newsgroups.derkeiler.com/Archive/Comp/comp.lang.scheme/2007-12/msg00061.html
ftp://ftp.ecn.purdue.edu/qobi/integ.tgz
It is seem to stalin does not optimize stream base loop.
But in another CPU, stalin's result smiler to integ2-c.
--------code----------
(define the-empty-stream '())
(define stream-null? null?)
(define (stream-car stream) (car stream))
(define (stream-cdr stream) (force (cdr stream)))
(define (stream-ref s n)
(if (= n 0)
(stream-car s)
(stream-ref (stream-cdr s) (- n 1))))
;; SICP prob 3.50 stream-map
(define (stream-map proc . argstreams)
(if (stream-null? (car argstreams))
the-empty-stream
(
;cons-stream
cons
(apply proc (map stream-car argstreams))
(delay (apply stream-map
(cons proc (map stream-cdr argstreams))))
)
))
;; sicp prob 3.55
(define (partial-sums s)
(cons
(stream-car s)
(delay
(add-streams (stream-cdr s)
(partial-sums s)))))
(define (add-streams s1 s2)
(stream-map + s1 s2))
(define (sum-of-squares-stream s1 s2)
(partial-sums (stream-map - s1 s2))
)
(define (integers-starting-from n)
(cons n (delay (integers-starting-from (+ n 1)))))
(define integers (integers-starting-from 1))
;;;;;;;end util;;;;;;;;;;
(define (integrate-1D L U F)
(let ((D (/ (- U L) 8.0)))
(* (+ (* (F L) 0.5)
(F (+ L D))
(F (+ L (* 2.0 D)))
(F (+ L (* 3.0 D)))
(F (+ L (* 4.0 D)))
(F (- U (* 3.0 D)))
(F (- U (* 2.0 D)))
(F (- U D))
(* (F U) 0.5))
D)))
(define (integrate-2D L1 U1 L2 U2 F)
(integrate-1D L2 U2 (lambda (y) (integrate-1D L1 U1 (lambda (x) (F x y))) )))
(define (zark U V)
(integrate-2D 0.0 U 0.0 V (lambda (X Y) (* X Y)) ))
(define (zark-i I)
(zark (* I 1.0) (* I 2.0)))
(define r-stream
(stream-map zark-i integers))
(define (i-fun I)
(let ((I2 (* (* I I) 1.0))) (* I2 I2)))
(define i-stream
(stream-map i-fun integers))
(define r-total-stream
(partial-sums r-stream)
)
(define i-total-stream
(partial-sums i-stream)
)
(begin
(display (stream-ref
(sum-of-squares-stream r-total-stream i-total-stream) 1000))
(newline))
--------code----------
Th following result shows that stalin does not optimize this code. Computation time is same to C
$ more /proc/cpuinfo
model name : Intel(R) Celeron(R) CPU E3300 @ 2.50GHz
$ time ./integ-stream
0.0
real 0m1.905s
user 0m1.760s
sys 0m0.110s
$ time ./integ-c
0.000000
real 0m1.767s
user 0m1.700s
sys 0m0.000s
$ time ./integ
0.0
real 0m0.299s
user 0m0.230s
sys 0m0.000s
./run
Stalin Version
0.11
GCC Version
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.1-4ubuntu9' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu9)
Stalin integ
0.0
0.200u 0.000s 0:00.16 125.0% 0+0k 0+0io 0pf+0w
Stalin integ-stream
0.0
1.610u 0.080s 0:01.73 97.6% 0+0k 0+0io 0pf+0w
Stalin integ2
0.0
0.040u 0.000s 0:00.04 100.0% 0+0k 0+0io 0pf+0w
Stalin integ3
0.0
0.290u 0.000s 0:00.29 100.0% 0+0k 0+0io 0pf+0w
GCC integ-c
0.000000
3.020u 0.000s 0:03.06 98.6% 0+0k 0+0io 0pf+0w
GCC integ2-c
0.000000
0.230u 0.000s 0:00.21 109.5% 0+0k 0+0io 0pf+0w
$ more /proc/cpuinfo
model name : Intel(R) Pentium(R) 4 CPU 3.00GHz
$time ./integ
0.0
real 0m0.277s
user 0m0.276s
$ time ./integ-c
0.000000
real 0m35.566s
user 0m35.274s
sys 0m0.084s
$ time ./integ-stream
0.0
real 0m4.235s
user 0m3.764s
sys 0m0.220s
./run
Stalin Version
0.11
GCC Version
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
Stalin integ
0.0
0.284u 0.000s 0:00.28 100.0% 0+0k 0+0io 0pf+0w
Stalin integ-stream
0.0
3.540u 0.164s 0:03.77 98.1% 0+0k 0+0io 0pf+0w
Stalin integ2
0.0
0.080u 0.004s 0:00.08 100.0% 0+0k 0+0io 0pf+0w
Stalin integ3
0.0
0.588u 0.000s 0:00.58 100.0% 0+0k 0+0io 0pf+0w
GCC integ-c
0.000000
31.649u 0.036s 0:31.98 99.0% 0+0k 0+0io 0pf+0w
GCC integ2-c
0.000000
1.248u 0.000s 0:01.28 96.8% 0+0k 0+0io 0pf+0w
2010年5月17日月曜日
2009年1月30日金曜日
lisp-> python interface via json and embeding
moved to http://www2s.biglobe.ne.jp/~niitsuma/pyjsonrpcembd.html
in Japanse http://d.hatena.ne.jp/niitsuma/20080209/1233260146
in Japanse http://d.hatena.ne.jp/niitsuma/20080209/1233260146
2009年1月29日木曜日
python embedding simple example
http://docs.python.org/extending/embedding.html
In some environment, this example fail to import python file multiple.py .
The following provides another example of assessing to python function's result and augments from C.
--------------------------
#include
int main()
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
Py_Initialize();
//def myfun
PyRun_SimpleString(
"def myfun(a):\n"
"\tprint a\n"
"\treturn a*a\n"
);
//extract interface to myfun
pName = PyString_FromString( "__main__");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
pFunc = PyObject_GetAttrString(pModule, "myfun");
pArgs = PyTuple_New(1);
pValue = PyInt_FromLong(atoi("3"));
PyTuple_SetItem(pArgs, 0, pValue);
//execute myfun
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
//get result
printf("Result of call: %ld\n", PyInt_AsLong(pValue));
Py_DECREF(pValue);
//simple do
PyRun_SimpleString("print myfun(3)\n");
//finalize
Py_DECREF(pModule);
Py_Finalize();
return 0;
}
In some environment, this example fail to import python file multiple.py .
The following provides another example of assessing to python function's result and augments from C.
--------------------------
#include
int main()
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
Py_Initialize();
//def myfun
PyRun_SimpleString(
"def myfun(a):\n"
"\tprint a\n"
"\treturn a*a\n"
);
//extract interface to myfun
pName = PyString_FromString( "__main__");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
pFunc = PyObject_GetAttrString(pModule, "myfun");
pArgs = PyTuple_New(1);
pValue = PyInt_FromLong(atoi("3"));
PyTuple_SetItem(pArgs, 0, pValue);
//execute myfun
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
//get result
printf("Result of call: %ld\n", PyInt_AsLong(pValue));
Py_DECREF(pValue);
//simple do
PyRun_SimpleString("print myfun(3)\n");
//finalize
Py_DECREF(pModule);
Py_Finalize();
return 0;
}
2008年2月1日金曜日
Interface python.numarray and c++ using boost.python and cmake
Let us call the following c++ functions from python
This c++ code can compile
with file CMakeLists.txt
Then libboopy.so is genereated. libboopy.so can call from python as the fllowing
output is
#include <boost/python.hpp>
#include <boost/python/numeric.hpp>
using namespace boost::python;
char const* say_yes(){return "YES!!";}
// See if we can invoke array() from C++
object new_array()
{
return numeric::array(make_tuple(
make_tuple(1,2.2,3)
, make_tuple(4,5,6)
, make_tuple(7,8,9)
),'d');
}
// test argument conversion
void take_array(numeric::array x){x[make_tuple(0,0)]= 0.5;}
BOOST_PYTHON_MODULE(libboopy)
{
def("yes", say_yes);
def("new_array", new_array);
def("take_array", take_array);
}
This c++ code can compile
$ cmake .
$ make
with file CMakeLists.txt
INCLUDE_DIRECTORIES( /usr/include/python2.4 )
ADD_LIBRARY(boopy SHARED boopy.cpp )
TARGET_LINK_LIBRARIES(boopy boost_python python2.4 )
Then libboopy.so is genereated. libboopy.so can call from python as the fllowing
def boopy_test():
import sys
import numarray
import libboopy
print libboopy.yes()
x = libboopy.new_array()
print x
libboopy.take_array(x)
print x
z=numarray.array([[0,1],[1,3]], numarray.Float)
print z
libboopy.take_array(z) # note here
print z
if __name__ == '__main__':
print "running..."
import sys
boopy_test()
output is
YES!!
[[ 1. 2.2 3. ]
[ 4. 5. 6. ]
[ 7. 8. 9. ]]
[[ 0.5 2.2 3. ]
[ 4. 5. 6. ]
[ 7. 8. 9. ]]
[[ 0. 1.]
[ 1. 3.]]
[[ 0.5 1. ]
[ 1. 3. ]]
2008年1月30日水曜日
BIAS - Basic Image AlgorithmS Library on ubuntu
BIAS - Basic Image AlgorithmS Library can be installed by the following simple procedure to ubuntu 7.10 linux
maybe apt-get install libwxgtk-dev libwxbase-dev also required
in japanese http://d.hatena.ne.jp/niitsuma/20080107/1201322983
- sudo apt-get install g++ checkinstall cmake libglew-dev libglut-dev libmagick++9-dev libcv-dev lapack3-dev libf2c2-dev uuid-dev libxmu-dev libxxf86vm-dev libgsl0-dev
- wget http://www.mip.informatik.uni-kiel.de/~wwwadmin/
Software/Download/BIAS-2.5.0.tar.gz - tar zxf BIAS-2.5.0.tar.gz
- cd BIAS-2.5.0
- ccmake .
- enable use gsl
- disaple wxWidgets
- [c]onfigre then [g]enerate
- close ccmake
- make -k
- sudo checkinstall (or sudo make install)
maybe apt-get install libwxgtk-dev libwxbase-dev also required
in japanese http://d.hatena.ne.jp/niitsuma/20080107/1201322983
登録:
投稿 (Atom)