Permalink
Browse files

Merge branch 'pgxn' into master

  • Loading branch information...
dvarrazzo committed Apr 22, 2011
2 parents 0af14e6 + 08863e4 commit a225104e47debf55b2d46f74fe18f28ade7797e8
View
@@ -11,4 +11,5 @@ docs/html
docs/_static/jsMath/
sandbox/hello/hello
sandbox/bench/*.png
+sandbox/pi/*.txt
dist/*
View
@@ -0,0 +1,33 @@
+{
+ "name": "pgmp",
+ "abstract": "PostgreSQL Multiple Precision Arithmetic extension",
+ "description": "The pgmp extension adds PostgreSQL data types wrapping the high performance integer and rational data types offered by the GMP library.",
+ "version": "1.0b2",
+ "maintainer": "Daniele Varrazzo <daniele.varrazzo@gmail.com>",
+ "tags": [ "arithmetic", "gmp", "rational", "integer", "data types" ],
+ "release_status": "testing",
+ "license": "lgpl_3_0",
+ "prereqs": {
+ "runtime": {
+ "requires": {
+ "PostgreSQL": "8.4.0"
+ }
+ }
+ },
+ "resources": {
+ "homepage": "http://pgmp.projects.postgresql.org/",
+ "bugtracker": {
+ "web": "https://github.com/dvarrazzo/pgmp/issues"
+ },
+ "repository": {
+ "url": "git://github.com/dvarrazzo/pgmp.git",
+ "web": "https://github.com/dvarrazzo/pgmp/",
+ "type": "git"
+ }
+ },
+ "meta-spec": {
+ "version": "1.0.0",
+ "url": "http://pgxn.org/meta/spec.txt"
+ }
+}
+
View
@@ -33,30 +33,31 @@ PG91 = $(shell $(PG_CONFIG) --version | grep -qE " 8\.| 9\.0" && echo pre91 || e
EXTENSION=pgmp
MODULEDIR=pgmp
MODULE_big = pgmp
-OBJS = src/pgmp.o src/pgmp_utils.o \
- src/pmpz.o src/pmpz_io.o src/pmpz_arith.o src/pmpz_agg.o \
- src/pmpz_roots.o src/pmpz_theor.o src/pmpz_bits.o src/pmpz_rand.o \
- src/pmpq.o src/pmpq_io.o src/pmpq_arith.o src/pmpq_agg.o
-SRCFILES = $(wildcard src/*.[ch])
-TESTFILES = $(wildcard sql/*.sql) $(wildcard expected/*.out)
+SRC_C = $(wildcard src/*.c)
+SRC_H = $(wildcard src/*.h)
+SRCFILES = $(SRC_C) $(SRC_H)
+OBJS = $(patsubst %.c,%.o,$(SRC_C))
+TESTFILES = $(wildcard test/sql/*.sql) $(wildcard test/expected/*.out)
DOCS = $(wildcard docs/*.rst) docs/conf.py docs/Makefile docs/_static/pgmp.css
-PKGFILES = AUTHORS COPYING README Makefile \
- pgmp.control pgmp.pysql uninstall_pgmp.sql \
+PKGFILES = AUTHORS COPYING README.rst Makefile \
+ pgmp.control META.json \
+ sql/pgmp.pysql sql/uninstall_pgmp.sql \
$(SRCFILES) $(DOCS) $(TESTFILES) \
$(wildcard tools/*.py)
ifeq ($(PG91),91)
-INSTALLSCRIPT=pgmp--$(PGMP_VERSION).sql
-UPGRADESCRIPT=pgmp--unpackaged--$(PGMP_VERSION).sql
+INSTALLSCRIPT=sql/pgmp--$(PGMP_VERSION).sql
+UPGRADESCRIPT=sql/pgmp--unpackaged--$(PGMP_VERSION).sql
DATA = $(INSTALLSCRIPT) $(UPGRADESCRIPT)
else
-INSTALLSCRIPT=pgmp.sql
-DATA = $(INSTALLSCRIPT) uninstall_pgmp.sql
+INSTALLSCRIPT=sql/pgmp.sql
+DATA = $(INSTALLSCRIPT) sql/uninstall_pgmp.sql
endif
-REGRESS = setup-$(PG91) mpz mpq
+# the += doesn't work if the user specified his own REGRESS_OPTS
+REGRESS = --inputdir=test setup-$(PG91) mpz mpq
EXTRA_CLEAN = $(INSTALLSCRIPT) $(UPGRADESCRIPT)
PKGNAME = pgmp-$(PGMP_VERSION)
@@ -69,7 +70,7 @@ include $(PGXS)
# added to the targets defined in pgxs
all: $(INSTALLSCRIPT) $(UPGRADESCRIPT)
-$(INSTALLSCRIPT): pgmp.pysql
+$(INSTALLSCRIPT): sql/pgmp.pysql
tools/unmix.py < $< > $@
$(UPGRADESCRIPT): $(INSTALLSCRIPT)
View
File renamed without changes.
View
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+"""
+A script to calculate pi using parallel PostgreSQL processes.
+
+Copyright (C) 2011 Daniele Varrazzo
+"""
+
+import eventlet
+eventlet.patcher.monkey_patch()
+
+import sys
+import psycopg2
+
+def main():
+ dsn = 'dbname=regression'
+ nprocs = 4
+ ndigits = int(sys.argv[1])
+
+ def arccot((mult, arg)):
+ print >>sys.stderr, 'start', arg
+ cnn = psycopg2.connect(dsn)
+ cnn.set_isolation_level(0)
+ cur = cnn.cursor()
+ cur.execute("""
+ insert into pi
+ values (%s, arccot(%s, 10::mpz ^ (%s + 10)))
+ returning %s;
+ """, (mult, arg, ndigits, arg))
+ rv = cur.fetchone()[0]
+ cnn.close()
+ print >>sys.stderr, 'end', arg
+
+ cnn = psycopg2.connect(dsn)
+ cnn.set_isolation_level(0)
+ cur = cnn.cursor()
+ cur.execute("""
+ drop table if exists pi;
+ create table pi (mult int4, arccot mpz);
+ """)
+
+ pool = eventlet.GreenPool(nprocs)
+ list(pool.imap(arccot, [
+ (183, 239),
+ (32, 1023),
+ (-68, 5832),
+ (12, 110443),
+ (-12, 4841182),
+ (-100, 6826318), ]))
+
+ cur.execute("select 4 * sum(mult * arccot) / (10::mpz ^ 10) from pi;")
+ print cur.fetchone()[0]
+
+if __name__ == '__main__':
+ main()
+
View
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+# A script to calculate pi using parallel backends
+# See calc_pi.py for a more advanced version.
+#
+# Copyright (C) 2011 Daniele Varrazzo
+
+export DBARGS=regression
+export NDIGITS=$1
+
+psql -q -c "drop table if exists pi;" $DBARGS
+psql -q -c "create table pi (mult int4, arccot mpz);" $DBARGS
+
+psql -Atq -c "insert into pi values (183, arccot(239, 10::mpz ^ ($NDIGITS + 10))) returning 1;" $DBARGS >&2 &
+psql -Atq -c "insert into pi values (32, arccot(1023, 10::mpz ^ ($NDIGITS + 10))) returning 2;" $DBARGS >&2 &
+psql -Atq -c "insert into pi values (-68, arccot(5832, 10::mpz ^ ($NDIGITS + 10))) returning 3;" $DBARGS >&2 &
+wait;
+psql -Atq -c "insert into pi values (12, arccot(110443, 10::mpz ^ ($NDIGITS + 10))) returning 4;" $DBARGS >&2 &
+psql -Atq -c "insert into pi values (-12, arccot(4841182, 10::mpz ^ ($NDIGITS + 10))) returning 5;" $DBARGS >&2 &
+psql -Atq -c "insert into pi values (-100, arccot(6826318, 10::mpz ^ ($NDIGITS + 10))) returning 6;" $DBARGS >&2 &
+wait;
+
+psql -At -c "select 4 * sum(mult * arccot) / (10::mpz ^ 10) from pi;" $DBARGS
+
View
@@ -0,0 +1,81 @@
+-- Some functions to calculate pi digits using mpz integers.
+--
+-- Reference:
+-- http://en.literateprograms.org/Pi_with_Machin%27s_formula_%28Python%29
+--
+-- Copyright (C) 2011 Daniele Varrazzo
+
+CREATE FUNCTION arccot(x mpz, unity mpz) RETURNS mpz
+ LANGUAGE plpgsql IMMUTABLE STRICT
+ AS $$
+DECLARE
+ xp mpz := unity / x;
+ xp2 mpz := -(x ^ 2);
+ acc mpz := xp;
+ term mpz;
+ n mpz := 3;
+BEGIN
+ LOOP
+ xp := xp / xp2;
+ term := xp / n;
+ EXIT WHEN term = 0;
+ acc := acc + term;
+ n := n + 2;
+ END LOOP;
+ RETURN acc;
+END
+$$;
+
+
+CREATE FUNCTION pi_machin(ndigits integer) RETURNS mpz
+ LANGUAGE plpgsql IMMUTABLE STRICT
+ AS $$
+DECLARE
+ unity mpz = 10::mpz ^ (ndigits + 10);
+BEGIN
+ RETURN 4 * (
+ 4 * arccot(5, unity)
+ - arccot(239, unity)
+ ) / (10::mpz ^ 10);
+END
+$$;
+
+
+CREATE FUNCTION pi_hwang_97(ndigits integer) RETURNS mpz
+ LANGUAGE plpgsql IMMUTABLE STRICT
+ AS $$
+DECLARE
+ unity mpz = 10::mpz ^ (ndigits + 10);
+BEGIN
+ RETURN 4 * (
+ 183 * arccot(239, unity)
+ + 32 * arccot(1023, unity)
+ - 68 * arccot(5832, unity)
+ + 12 * arccot(110443, unity)
+ - 12 * arccot(4841182, unity)
+ - 100 * arccot(6826318, unity)
+ ) / (10::mpz ^ 10);
+END
+$$;
+
+
+CREATE FUNCTION pi_hwang_03(ndigits integer) RETURNS mpz
+ LANGUAGE plpgsql IMMUTABLE STRICT
+ AS $$
+DECLARE
+ unity mpz = 10::mpz ^ (ndigits + 10);
+BEGIN
+ RETURN 4 * (
+ 183 * arccot(239, unity)
+ + 32 * arccot(1023, unity)
+ - 68 * arccot(5832, unity)
+ + 12 * arccot(113021, unity)
+ - 100 * arccot(6826318, unity)
+ - 12 * arccot(33366019650, unity)
+ + 12 * arccot(43599522992503626068::mpz, unity)
+ ) / (10::mpz ^ 10);
+END
+$$;
+
+
+
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -5,7 +5,7 @@ SET client_min_messages = warning;
-- Setup the extension on PostgreSQL before 9.1
-\i pgmp.sql
+\i sql/pgmp.sql
\set ECHO all
RESET client_min_messages;

0 comments on commit a225104

Please sign in to comment.