over.core.types is now using reference cython acceleration
This commit is contained in:
parent
0df57ad386
commit
fd984be2f6
6 changed files with 165 additions and 109 deletions
|
@ -0,0 +1,4 @@
|
||||||
|
#! /bin/zsh
|
||||||
|
# encoding: utf-8
|
||||||
|
|
||||||
|
echo "- nothing to do"
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
for dir in ag core m serial
|
for dir in ag core m serial
|
||||||
do
|
do
|
||||||
echo "Building Cython implementations in ${dir}"
|
echo "Building Cython implementations in ${dir}:"
|
||||||
cd "$dir"
|
cd "$dir"
|
||||||
./build.sh
|
./build.sh
|
||||||
cd ..
|
cd ..
|
||||||
|
|
|
@ -8,56 +8,15 @@ function die() {
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
function translate() {
|
|
||||||
echo -n "Translating from Python to C: "
|
|
||||||
|
|
||||||
first=1
|
|
||||||
|
|
||||||
for item in ${@}
|
|
||||||
do
|
|
||||||
if [ $first -eq 1 ]; then
|
|
||||||
echo -n "${item}"
|
|
||||||
first=0
|
|
||||||
else
|
|
||||||
echo -n ", ${item}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
cython -3 ${item}.pyx -o ${item}.c || die "translating"
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "."
|
|
||||||
}
|
|
||||||
|
|
||||||
function combobulate() {
|
|
||||||
name="$1"
|
|
||||||
|
|
||||||
echo -n > "$name.pyx"
|
|
||||||
|
|
||||||
for part in src/$name.*.pyx
|
|
||||||
do
|
|
||||||
# echo "Combobulating $part..."
|
|
||||||
echo "###############################################################################" >> "$name.pyx"
|
|
||||||
echo "# Combobulated from file $part" >> "$name.pyx"
|
|
||||||
echo "###############################################################################\n" >> "$name.pyx"
|
|
||||||
cat "$part" >> "$name.pyx"
|
|
||||||
echo >> "$name.pyx"
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
CFLAGS=(-Wall -pedantic -std=c99 -fPIC)
|
CFLAGS=(-Wall -pedantic -std=c99 -fPIC)
|
||||||
LFLAGS=(-shared)
|
LFLAGS=(-shared)
|
||||||
|
|
||||||
combobulate core
|
echo "- translating from Python to C"
|
||||||
ln -s src/interface/* .
|
cython -3 cython_types.pyx -o cython_types.c || die "translating"
|
||||||
|
|
||||||
# call Cython
|
echo "- compiling and linking"
|
||||||
translate core
|
gcc $CFLAGS -I/usr/include/python3.3 -pthread -c cython_types.c || die "compilation"
|
||||||
|
gcc $LFLAGS -L/usr/lib -lpython3.3 cython_types.o -o cython_types.so || die "linking"
|
||||||
|
rm -f cython_types.{c,o}
|
||||||
|
|
||||||
echo -n "Compiling and linking: core"
|
echo "- done"
|
||||||
gcc $CFLAGS -I/usr/include/python3.3 -pthread -c core.c || die "compilation"
|
|
||||||
gcc $LFLAGS -L/usr/lib -lpython3.3 core.o -o core.so || die "linking"
|
|
||||||
rm -f core.{c,o}
|
|
||||||
echo "."
|
|
||||||
|
|
||||||
# remove temporary sources
|
|
||||||
rm -f *.pyx *.pxd
|
|
||||||
|
|
148
core/cython_types.pyx
Normal file
148
core/cython_types.pyx
Normal file
|
@ -0,0 +1,148 @@
|
||||||
|
#! /bin/env python3
|
||||||
|
# encoding: utf-8
|
||||||
|
|
||||||
|
# --------------------------------------------------
|
||||||
|
|
||||||
|
cdef class ndict(dict):
|
||||||
|
"""
|
||||||
|
A dictionary superclass whose keys are exposed as attributes.
|
||||||
|
|
||||||
|
>>> d = ndict()
|
||||||
|
>>> d.alpha = 1
|
||||||
|
>>> d["alpha"]
|
||||||
|
1
|
||||||
|
>>> d["beta"] = 42
|
||||||
|
>>> d.beta
|
||||||
|
42
|
||||||
|
>>> d
|
||||||
|
{'alpha': 1, 'beta': 42}
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
dict.__init__(self, *args, **kwargs)
|
||||||
|
|
||||||
|
def __getattr__(self, str name):
|
||||||
|
if name in self:
|
||||||
|
return self[name]
|
||||||
|
else:
|
||||||
|
raise AttributeError("'ndict' object has no attribute '%s'" %(name))
|
||||||
|
|
||||||
|
def __setattr__(self, str name, value):
|
||||||
|
self[name] = value
|
||||||
|
|
||||||
|
# --------------------------------------------------
|
||||||
|
|
||||||
|
cdef class map:
|
||||||
|
"""
|
||||||
|
An ordered dict.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, source=None):
|
||||||
|
"""
|
||||||
|
Initialize the map.
|
||||||
|
|
||||||
|
The source is a sequence of (key, value) tuples.
|
||||||
|
|
||||||
|
TODO fixme bug
|
||||||
|
>>> map(((1, 2, 3, 4), ("a", "b", "c", "d")))
|
||||||
|
"""
|
||||||
|
|
||||||
|
print(repr(source))
|
||||||
|
|
||||||
|
if source:
|
||||||
|
self.keys, self.vals = zip(*source)
|
||||||
|
else:
|
||||||
|
self.keys = []
|
||||||
|
self.vals = []
|
||||||
|
|
||||||
|
def __get_index(self, key):
|
||||||
|
if key in self.keys:
|
||||||
|
return self.keys.index(key)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
i = self.__get_index(key)
|
||||||
|
|
||||||
|
if i is None:
|
||||||
|
raise KeyError(key)
|
||||||
|
else:
|
||||||
|
return self.vals[i]
|
||||||
|
|
||||||
|
def __setitem__(self, key, val):
|
||||||
|
i = self.__get_index(key)
|
||||||
|
|
||||||
|
if i is None:
|
||||||
|
self.keys.append(key)
|
||||||
|
self.vals.append(val)
|
||||||
|
else:
|
||||||
|
self.vals[i] = val
|
||||||
|
|
||||||
|
def __contains__(self, item):
|
||||||
|
return item in self.keys
|
||||||
|
|
||||||
|
def index(self, item):
|
||||||
|
return self.keys.index(item)
|
||||||
|
|
||||||
|
def sort(self, key=None):
|
||||||
|
tmp_keys = self.keys
|
||||||
|
tmp_vals = self.vals
|
||||||
|
|
||||||
|
self.keys = []
|
||||||
|
self.vals = []
|
||||||
|
|
||||||
|
for K, V in sorted(zip(tmp_keys, tmp_vals), key=key):
|
||||||
|
self.keys.append(K)
|
||||||
|
self.vals.append(V)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def items(self):
|
||||||
|
return zip(self.keys, self.vals)
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.vals)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
pairs = []
|
||||||
|
|
||||||
|
for i in range(len(self.keys)):
|
||||||
|
pairs.append("%s: %s" %(repr(self.keys[i]), repr(self.vals[i])))
|
||||||
|
|
||||||
|
return "<{%s}>" %(", ".join(pairs))
|
||||||
|
|
||||||
|
# --------------------------------------------------
|
||||||
|
|
||||||
|
cdef class enum:
|
||||||
|
"""
|
||||||
|
Emulates a C++-like enum type.
|
||||||
|
|
||||||
|
Based on a py2 enum function by Alec Thomas and acjohnson55.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, str name, words, int start=0):
|
||||||
|
"""
|
||||||
|
Initializes the enum.
|
||||||
|
|
||||||
|
name is used for __repr__ only
|
||||||
|
words is a sequence of enum keys
|
||||||
|
start is the numerical value of the first key
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.typename = name
|
||||||
|
self.reverse_enums = dict(enumerate(words, start))
|
||||||
|
self.enums = dict((value, key) for key, value in self.reverse_enums.items())
|
||||||
|
|
||||||
|
def name(self, int value):
|
||||||
|
if value in self.reverse_enums:
|
||||||
|
return self.reverse_enums[value]
|
||||||
|
else:
|
||||||
|
raise AttributeError("no %s has a value of %s" %(self, value))
|
||||||
|
|
||||||
|
def __getattr__(self, aname):
|
||||||
|
if aname in self.enums:
|
||||||
|
return self.enums[aname]
|
||||||
|
else:
|
||||||
|
raise AttributeError("%s not in %s" %(aname, self))
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<enum %s>" %(self.typename)
|
61
m/build.sh
61
m/build.sh
|
@ -1,63 +1,4 @@
|
||||||
#! /bin/zsh
|
#! /bin/zsh
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
setopt extendedglob
|
echo "- nothing to do"
|
||||||
|
|
||||||
function die() {
|
|
||||||
echo "\n\n>>> Failed during ${1}, aborting."
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
function translate() {
|
|
||||||
echo -n "Translating from Python to C: "
|
|
||||||
|
|
||||||
first=1
|
|
||||||
|
|
||||||
for item in ${@}
|
|
||||||
do
|
|
||||||
if [ $first -eq 1 ]; then
|
|
||||||
echo -n "${item}"
|
|
||||||
first=0
|
|
||||||
else
|
|
||||||
echo -n ", ${item}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
cython -3 ${item}.pyx -o ${item}.c || die "translating"
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "."
|
|
||||||
}
|
|
||||||
|
|
||||||
function combobulate() {
|
|
||||||
name="$1"
|
|
||||||
|
|
||||||
echo -n > "$name.pyx"
|
|
||||||
|
|
||||||
for part in src/$name.*.pyx
|
|
||||||
do
|
|
||||||
# echo "Combobulating $part..."
|
|
||||||
echo "###############################################################################" >> "$name.pyx"
|
|
||||||
echo "# Combobulated from file $part" >> "$name.pyx"
|
|
||||||
echo "###############################################################################\n" >> "$name.pyx"
|
|
||||||
cat "$part" >> "$name.pyx"
|
|
||||||
echo >> "$name.pyx"
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
CFLAGS=(-Wall -pedantic -std=c99 -fPIC)
|
|
||||||
LFLAGS=(-shared)
|
|
||||||
|
|
||||||
combobulate core
|
|
||||||
ln -s src/interface/* .
|
|
||||||
|
|
||||||
# call Cython
|
|
||||||
translate core
|
|
||||||
|
|
||||||
echo -n "Compiling and linking: core"
|
|
||||||
gcc $CFLAGS -I/usr/include/python3.3 -pthread -c core.c || die "compilation"
|
|
||||||
gcc $LFLAGS -L/usr/lib -lpython3.3 core.o -o core.so || die "linking"
|
|
||||||
rm -f core.{c,o}
|
|
||||||
echo "."
|
|
||||||
|
|
||||||
# remove temporary sources
|
|
||||||
rm -f *.pyx *.pxd
|
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
#! /bin/zsh
|
||||||
|
# encoding: utf-8
|
||||||
|
|
||||||
|
echo "- nothing to do"
|
Loading…
Add table
Add a link
Reference in a new issue