[pypy-commit] extradoc extradoc: MonteCarlo
hakanardo
noreply at buildbot.pypy.org
Sun Aug 12 16:11:45 CEST 2012
Author: Hakan Ardo <hakan at debian.org>
Branch: extradoc
Changeset: r4526:b41e5a73cc8e
Date: 2012-08-12 16:11 +0200
http://bitbucket.org/pypy/extradoc/changeset/b41e5a73cc8e/
Log: MonteCarlo
diff --git a/talk/iwtc11/benchmarks/benchmark.sh b/talk/iwtc11/benchmarks/benchmark.sh
--- a/talk/iwtc11/benchmarks/benchmark.sh
+++ b/talk/iwtc11/benchmarks/benchmark.sh
@@ -20,6 +20,7 @@
./runner.py -n 5 -c "$*" scimark/run_SOR.c 1000 256
./runner.py -n 5 -c "$*" scimark/run_SparseMatMult.c 1000 5000 262144
./runner.py -n 5 -c "$*" scimark/run_SparseMatMult.c 100000 1000000 1024
+ ./runner.py -n 5 -c "$*" scimark/run_MonteCarlo 268435456
rm a.out
else
if [ "$1" == "python2.7" ]; then
@@ -53,4 +54,5 @@
$* ./runner.py $EXTRA_OPTS scimark.py SOR 1000 256
$* ./runner.py $EXTRA_OPTS scimark.py SparseMatMult 1000 5000 262144
$* ./runner.py $EXTRA_OPTS scimark.py SparseMatMult 100000 1000000 1024
+ $* ./runner.py $EXTRA_OPTS scimark.py MonteCarlo 268435456
fi
diff --git a/talk/iwtc11/benchmarks/scimark.py b/talk/iwtc11/benchmarks/scimark.py
--- a/talk/iwtc11/benchmarks/scimark.py
+++ b/talk/iwtc11/benchmarks/scimark.py
@@ -104,4 +104,18 @@
SparseCompRow_matmult(N, y, val, row, col, x, cycles);
return "SparseMatMult(%d, %d, %d)" % (N, nz, cycles)
+def MonteCarlo_integrate(Num_samples):
+ rnd = Random(113)
+ under_curve = 0
+ for count in xrange(Num_samples):
+ x = rnd.nextDouble()
+ y = rnd.nextDouble()
+ if x*x + y*y <= 1.0:
+ under_curve += 1
+ return float(under_curve) / Num_samples * 4.0
+def MonteCarlo(args):
+ n = int(args[0])
+ MonteCarlo_integrate(n)
+ return 'MonteCarlo(%d)' % n
+
diff --git a/talk/iwtc11/benchmarks/scimark/Random.h b/talk/iwtc11/benchmarks/scimark/Random.h
--- a/talk/iwtc11/benchmarks/scimark/Random.h
+++ b/talk/iwtc11/benchmarks/scimark/Random.h
@@ -1,3 +1,6 @@
+#ifndef __RANDOM_H__
+#define __RANDOM_H__
+
typedef struct
{
int m[17];
@@ -16,3 +19,5 @@
void Random_delete(Random R);
double *RandomVector(int N, Random R);
double **RandomMatrix(int M, int N, Random R);
+
+#endif
diff --git a/talk/iwtc11/benchmarks/scimark/kernel.c b/talk/iwtc11/benchmarks/scimark/kernel.c
--- a/talk/iwtc11/benchmarks/scimark/kernel.c
+++ b/talk/iwtc11/benchmarks/scimark/kernel.c
@@ -89,6 +89,7 @@
cycles *= 2;
}
+ printf("MonteCarlo: cycles=%d\n", cycles);
/* approx Mflops */
result = MonteCarlo_num_flops(cycles) / Stopwatch_read(Q) * 1.0e-6;
Stopwatch_delete(Q);
diff --git a/talk/iwtc11/benchmarks/scimark/run_MonteCarlo.c b/talk/iwtc11/benchmarks/scimark/run_MonteCarlo.c
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/scimark/run_MonteCarlo.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+#include <assert.h>
+
+#include "Random.c"
+#include "MonteCarlo.c"
+
+int main(int ac, char **av) {
+ assert(ac==2);
+ int N = atoi(av[1]);
+ MonteCarlo_integrate(N);
+ fprintf(stderr, "MonteCarlo(%d): ", N);
+ return 0;
+}
+
+
diff --git a/talk/iwtc11/benchmarks/test_scimark.py b/talk/iwtc11/benchmarks/test_scimark.py
--- a/talk/iwtc11/benchmarks/test_scimark.py
+++ b/talk/iwtc11/benchmarks/test_scimark.py
@@ -1,4 +1,4 @@
-from scimark import SOR_execute, Array2D, Random
+from scimark import SOR_execute, Array2D, Random, MonteCarlo_integrate
from cffi import FFI
import os
@@ -10,15 +10,17 @@
double **RandomMatrix(int M, int N, Random R);
void SOR_execute(int M, int N,double omega, double **G, int num_iterations);
+ double MonteCarlo_integrate(int Num_samples);
""")
C = ffi.verify("""
#include <SOR.h>
#include <Random.h>
+ #include <MonteCarlo.h>
""",
extra_compile_args=['-I' + os.path.join(os.getcwd(), 'scimark')],
extra_link_args=['-fPIC'],
extra_objects=[os.path.join(os.getcwd(), 'scimark', f)
- for f in ['SOR.c', 'Random.c']])
+ for f in ['SOR.c', 'Random.c', 'MonteCarlo.c']])
def test_SOR():
width, height = 5, 7
@@ -35,5 +37,9 @@
rnd_py = Random(7)
for i in range(100000):
assert C.Random_nextDouble(rnd_C) == rnd_py.nextDouble()
-
+def test_montecarlo():
+ for n in [100, 200, 500, 1000]:
+ assert C.MonteCarlo_integrate(n) == MonteCarlo_integrate(n)
+
+
More information about the pypy-commit
mailing list