Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add array_map specializer for demonstrating tree specializers; fix re…
…cently introduced bugs in ast_tools using Scala nodes in C++ code by being explicit; clean up print output of Asp unit tests
- Loading branch information
Derrick Coetzee
committed
Dec 10, 2012
1 parent
a5a29ae
commit f2b9fc1
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# really dumb example of using tree transformations w/asp | ||
|
||
class ArrayMap(object): | ||
|
||
def __init__(self): | ||
self.pure_python = True | ||
|
||
def map_using_trees(self, arr): | ||
import asp.codegen.templating.template as template | ||
import inspect | ||
import asp.codegen.python_ast as ast | ||
import asp.codegen.ast_tools as ast_tools | ||
|
||
src = inspect.getsource(self.operation) | ||
operation_ast = ast.parse(src.lstrip()) | ||
return_ast = operation_ast.body[0] | ||
expr_ast = return_ast.body[0].value | ||
expr_cpp = ast_tools.ConvertAST().visit(expr_ast) | ||
|
||
mytemplate = template.Template(filename="templates/map_template.mako", disable_unicode=True) | ||
rendered = mytemplate.render(num_items=len(arr), expr=expr_cpp) | ||
|
||
import asp.jit.asp_module as asp_module | ||
mod = asp_module.ASPModule() | ||
mod.add_function("map_in_c", rendered) | ||
return mod.map_in_c(arr) | ||
|
||
def map(self, arr): | ||
for i in range(0, len(arr)): | ||
arr[i] = self.operation(arr[i]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/bash | ||
|
||
PYTHONPATH=../../:$PYTHONPATH | ||
|
||
echo PYTHONPATH | ||
echo ${PYTHONPATH} | ||
|
||
if [ -z "${PYTHON}" ] | ||
then | ||
PYTHON=python | ||
fi | ||
if [ -z "${PYTHONARGS}" ] | ||
then | ||
PYTHONARGS= | ||
fi | ||
|
||
PYTHONPATH=`pwd`:${PYTHONPATH} ${PYTHON} ${PYTHONARGS} tests/arraymap_test.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using namespace boost::python; | ||
|
||
void map_in_c(list arr) | ||
{ | ||
for (int i=0; i < ${num_items}; i++) | ||
{ | ||
double x = extract<double>(arr[i]); | ||
x = ${expr}; | ||
arr[i] = x; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import unittest | ||
|
||
from array_map import * | ||
|
||
class ArrayMapExample(ArrayMap): | ||
def operation(self, x): | ||
return 2*x+5 | ||
|
||
class BasicTests(unittest.TestCase): | ||
def test_pure_python(self): | ||
example = ArrayMapExample() | ||
arr = [1.0, 2.0, 3.0, 4.0] | ||
example.map(arr) | ||
self.assertEquals(arr[0], 7.0) | ||
|
||
def test_generated(self): | ||
example = ArrayMapExample() | ||
arr = [1.0, 2.0, 3.0, 4.0] | ||
example.map_using_trees(arr) | ||
self.assertEquals(arr[0], 7.0) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |