Author: Hakan Ardo <ha...@debian.org>
Branch: extradoc
Changeset: r3621:a086ac51ff2b
Date: 2011-06-08 21:09 +0200
http://bitbucket.org/pypy/extradoc/changeset/a086ac51ff2b/

Log:    1D convolution with fixed kernel size

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
@@ -6,9 +6,12 @@
     $* sqrt/sqrt_double.c; /usr/bin/time -f %e ./a.out > /dev/null
     $* sqrt/sqrt_long.c; /usr/bin/time -f %e ./a.out > /dev/null
     $* sqrt/sqrt_fix16.c; /usr/bin/time -f %e ./a.out > /dev/null
+    $* convolution/conv3.c; /usr/bin/time -f %e ./a.out > /dev/null
+    $* convolution/conv5.c; /usr/bin/time -f %e ./a.out > /dev/null
     rm a.out
 else
     $* sqrt/time_sqrt.py float
     $* sqrt/time_sqrt.py int
     $* sqrt/time_sqrt.py Fix16
+    $* convolution/time_conv.py
 fi
diff --git a/talk/iwtc11/benchmarks/convolution/conv3.c 
b/talk/iwtc11/benchmarks/convolution/conv3.c
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/convolution/conv3.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+
+#define N 100000000
+double a[N], b[N-2];
+
+//void conv(double *a, double *k, double *b) {
+void conv(double *__restrict__ a, double *__restrict__ k, double *__restrict__ 
b) {
+  int i;
+  for (i=0; i<N-2; i++) {
+    b[i] = k[2]*a[i] + k[1]*a[i+1] + k[0]*a[i+2];
+  }
+}
+
+int main() {
+  double k[3] = {-1, 0, 1};
+    int i;
+    for (i=0; i<N; i++) a[i] = 1;
+    conv(a,k, b);
+    printf("%f\n", b[N/2]);
+    fprintf(stderr, "conv3:         ");
+    return 0;
+}
diff --git a/talk/iwtc11/benchmarks/convolution/conv5.c 
b/talk/iwtc11/benchmarks/convolution/conv5.c
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/convolution/conv5.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+
+#define N 100000000
+double a[N], b[N-4];
+
+//void conv(double *a, double *k, double *b) {
+void conv(double *__restrict__ a, double *__restrict__ k, double *__restrict__ 
b) {
+  int i;
+  for (i=0; i<N-4; i++) {
+    b[i] = k[4]*a[i] + k[3]*a[i+1] + k[2]*a[i+2] + k[1]*a[i+3] + k[0]*a[i+4];
+  }
+}
+
+int main() {
+    double k[5] = {1, 4, 6, 4, 1};
+    int i;
+    for (i=0; i<N; i++) a[i] = 1;
+    conv(a,k, b);
+    printf("%f\n", b[N/2]);
+    fprintf(stderr, "conv5:         ");
+    return 0;
+}
diff --git a/talk/iwtc11/benchmarks/convolution/convolution.py 
b/talk/iwtc11/benchmarks/convolution/convolution.py
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/convolution/convolution.py
@@ -0,0 +1,15 @@
+from array import array
+
+def conv3(a, k):
+    assert len(k)==3
+    b = array(a.typecode, [0]) * (len(a) - 2)
+    for i in range(len(b)):
+        b[i] = k[2]*a[i] + k[1]*a[i+1] + k[0]*a[i+2]
+    return b
+
+def conv5(a, k):
+    assert len(k)==5
+    b = array(a.typecode, [0]) * (len(a) - 4)
+    for i in range(len(b)):
+        b[i] = k[4]*a[i] + k[3]*a[i+1] + k[2]*a[i+2] + k[1]*a[i+3] + 
k[0]*a[i+4]
+    return b
diff --git a/talk/iwtc11/benchmarks/convolution/test_convolution.py 
b/talk/iwtc11/benchmarks/convolution/test_convolution.py
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/convolution/test_convolution.py
@@ -0,0 +1,13 @@
+from convolution import conv3, conv5
+from array import array
+
+def test_conv3():
+    b = conv3(array('d', [1, 2, 3, 4, 5, 6, 7, 8, 9]),
+              array('d', [-1, 0, 1]))
+    assert b == array('d', [-2]) * 7
+    
+def test_conv5():
+    b = conv5(array('d', [1, 2, 3, 4, 5, 6, 7, 8, 9]),
+              array('d', [1, 1, 2, 2, 3]))
+    assert b == array('d', [22, 31, 40, 49, 58])
+    
diff --git a/talk/iwtc11/benchmarks/convolution/time_conv.py 
b/talk/iwtc11/benchmarks/convolution/time_conv.py
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/convolution/time_conv.py
@@ -0,0 +1,16 @@
+from convolution import conv3, conv5
+from array import array
+import sys, time
+
+a = time.time()
+conv3(array('d', [1]) * 100000000,
+      array('d', [-1, 0, 1]))
+b = time.time()
+print 'conv3:        ', b - a
+
+a = time.time()
+conv5(array('d', [1]) * 100000000,
+      array('d', [1, 4, 6, 4, 1]))
+b = time.time()
+print 'conv5:        ', b - a
+
diff --git a/talk/iwtc11/benchmarks/result.txt 
b/talk/iwtc11/benchmarks/result.txt
new file mode 100644
--- /dev/null
+++ b/talk/iwtc11/benchmarks/result.txt
@@ -0,0 +1,45 @@
+pypy
+sqrt(float):   1.20120882988
+  sqrt(int):   2.41813898087
+sqrt(Fix16):   6.11410784721
+conv3:         2.14187502861
+conv5:         2.33459997177
+
+pypy --jit enable_opts=intbounds:rewrite:virtualize:heap:unroll
+sqrt(float):   1.2082631588
+  sqrt(int):   2.42825579643
+sqrt(Fix16):   6.13569307327
+conv3:         2.14451694489
+conv5:         2.36811304092
+
+pypy --jit enable_opts=intbounds:rewrite:virtualize:heap
+sqrt(float):   1.70357894897
+  sqrt(int):   3.12929701805
+sqrt(Fix16):   10.3343019485
+conv3:         3.14458608627
+conv5:         3.42248892784
+
+gcc
+sqrt(float):   1.42
+sqrt(int):     1.93
+sqrt(Fix16):   2.04
+conv3:         1.94
+conv5:         2.36
+
+gcc -O2
+sqrt(float):   1.14
+sqrt(int):     1.86
+sqrt(Fix16):   1.90
+conv3:         1.18
+conv5:         1.34
+
+gcc -O3 -march=native
+sqrt(float):   1.14
+sqrt(int):     1.82
+sqrt(Fix16):   1.89
+conv3:         1.10
+conv5:         1.16
+
+python
+sqrt(float):   43.5761749744
+  sqrt(int):   32.1061348915
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to