Skip to content

Commit 0b50fea

Browse files
author
Ilmar Wilbers
committed
Add new test demonstrating numpy.i typemaps.
1 parent 749b912 commit 0b50fea

File tree

4 files changed

+119
-63
lines changed

4 files changed

+119
-63
lines changed

AUTHORS

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
21
Main authors:
32
Martin Alnæs (martinal@simula.no)
43
Kent-Andre Mardal (kent-and@simula.no)
54
Magne Westlie (magnew@simula.no)
5+
Ilmar Wilbers (ilmarw@simula.no)
66

77
Suggestions, bugfixes etc:
8-
Anders Logg
9-
Ola Skavhaug
10-
Ilmar Wilbers
118
Johan Hake
12-
13-
14-
15-
16-
9+
Anders Logg
10+
Johannes Ring
11+
Ola Skavhaug

ChangeLog

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

2+
Release 0.9.7
3+
Use typemaps from the NumPy SWIG interface file (numpy.i),
4+
enabling the use of many new data types.
5+
Removed support for Numeric and numarray.
6+
27
Release 0.9.6
38
Minor update with some new utility functions required by FFC.
49

TODO

-54
Original file line numberDiff line numberDiff line change
@@ -23,57 +23,3 @@ TODO:
2323
- Add tests for all variants of cache mechanisms available now
2424

2525
- Clean up imports, don't use import *
26-
27-
Explain this in manual:
28-
29-
Use cases: (M - modulename, S - signature, C - enable_cache == True)
30-
- 'MS' - Invalid
31-
32-
- 'M' - Use given M and no cache, simply compile in current directory.
33-
import_module(M) will work in the current directory, but always
34-
using the version compiled first in this python process, because
35-
of limitations in the Python extension module system.
36-
The user can't change flags during the current python process lifetime!
37-
38-
use_cache = False
39-
moduleids = []
40-
41-
module_path = cwd
42-
modulename = modulename
43-
44-
module = None # Depend on code checking checksum-file to avoid recompilation
45-
46-
- 'S' - Construct modulename from S, lookup in cache, place in cache after building.
47-
Three options for this implementation:
48-
Compiler args must be part of S for import_module(S) to work consistently.
49-
50-
use_cache = True
51-
moduleids = [signature, signature.signature(), compute_checksum(signature.signature())]
52-
53-
module_path = temp_dir (copied after building)
54-
modulename = modulename_from_checksum(compute_checksum(S)) # Or just S if valid filename?
55-
56-
module = None # Depend on code checking checksum-file to avoid recompilation
57-
58-
- '' - Construct S from user file contents and compiler args, see 'S'.
59-
60-
signature = compute_signature_from_user_files(...)
61-
62-
use_cache = True
63-
moduleids = [signature, signature.signature(), compute_checksum(signature.signature())]
64-
65-
module_path = temp_dir (copied after building)
66-
modulename = modulename_from_checksum(compute_checksum(S)) # Or just S if valid filename?
67-
68-
module = None # Depend on code checking checksum-file to avoid recompilation
69-
70-
I want to do:
71-
b = jit(a, options)
72-
b = jit(a, options)
73-
-> signature = repr(a) + repr(options)
74-
-> m = import_module(signature)
75-
-> if not m: m = build_module(..., signature)
76-
-> return m.myform()
77-
and:
78-
b = build_module(modulename, ...)
79-
b = build_module(modulename, ...)

tests/test10.py

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/usr/bin/env python
2+
3+
import numpy
4+
import time
5+
from instant import inline_with_numpy
6+
7+
# Example 1: two arrays, one in, one inout
8+
c_code = """
9+
double sum (int x1, int y1, int z1, double* array1, int x2, double* array2){
10+
double tmp = 0.0;
11+
for (int i=0; i<x1; i++)
12+
for (int j=0; j<y1; j++)
13+
for (int k=0; k<z1; k++){
14+
tmp += array1[i*y1*z1 + j*z1 + k];
15+
array2[1] = 2;
16+
}
17+
return tmp;
18+
}
19+
"""
20+
21+
sum_func = inline_with_numpy(c_code, arrays = [['x1', 'y1', 'z1', 'array1'],
22+
['x2', 'array2', 'in']],
23+
cache_dir="test_ex1_cache")
24+
25+
a = numpy.arange(27000); a = numpy.sin(a)
26+
c = a.copy()
27+
a.shape = (30, 30, 30)
28+
29+
print 'b = (1,1)'
30+
b = (1., 1.)
31+
sum1 = sum_func(a, b)
32+
print b, 'b not changed when list'
33+
34+
b = numpy.ones(2)
35+
sum1 = sum_func(a, b)
36+
print b, 'b not changed when numpy array'
37+
38+
# Example 2: two array, both inout and of same size
39+
# Cannot avoid specifying all dimensions for both arrays
40+
c_code = """
41+
double sum (int x1, int y1, double* array1, int x2, int y2, double* array2){
42+
double tmp = 0.0;
43+
for (int i=0; i<x1; i++)
44+
for (int j=0; j<y1; j++){
45+
tmp = array1[i*y1 + j];
46+
array1[i*y1 + j] = array2[i*y1 + j];
47+
array2[i*y1 + j] = tmp;
48+
}
49+
return tmp;
50+
}
51+
"""
52+
53+
sum_func = inline_with_numpy(c_code, arrays = [['x1', 'y1', 'array1'],
54+
['x2', 'y2', 'array2']],
55+
cache_dir="test_ex2_cache")
56+
57+
a = numpy.ones(4)
58+
a.shape = (2, 2)
59+
b = a.copy()
60+
a *= 2
61+
62+
sum1 = sum_func(a, b)
63+
print 'a and b changed'
64+
print a
65+
print b
66+
67+
# Example 3: two arrays, one in, one out
68+
c_code = """
69+
void sum (int x1, int y1, double* array1, int xy2, double* array2){
70+
for (int i=0; i<x1; i++)
71+
for (int j=0; j<y1; j++)
72+
array2[i*y1 + j] = array1[i*y1 + j]*2;
73+
}
74+
"""
75+
76+
sum_func = inline_with_numpy(c_code, arrays = [['x1', 'y1', 'array1', 'in'],
77+
['xy2', 'array2', 'out']],
78+
cache_dir="test_ex3_cache")
79+
80+
a = numpy.ones(4)
81+
a.shape = (2, 2)
82+
a *= 2
83+
84+
c = sum_func(a, a.size)
85+
c.shape = a.shape
86+
print c
87+
88+
# Example 4: three arrays, one in, one inout, and one out
89+
c_code = """
90+
void sum (int x1, int y1, long* array1, int x2, int* array2, int x3, double* array3){
91+
for (int i=0; i<x1; i++){
92+
array3[i] = 0;
93+
for (int j=0; j<y1; j++)
94+
array3[i] += array1[i*y1 + j]*array2[j];
95+
}
96+
}
97+
"""
98+
99+
sum_func = inline_with_numpy(c_code, arrays = [['x1', 'y1', 'array1', 'in', 'long'],
100+
['x2', 'array2', 'int'],
101+
['x3', 'array3', 'out', 'double']],
102+
cache_dir="test_ex4_cache")
103+
104+
a = numpy.arange(9)#, dtype='int32')
105+
a.shape = (3, 3)
106+
b = numpy.arange(3)#, dtype='int32')
107+
108+
c = sum_func(a, b, b.size)
109+
print c
110+
print numpy.dot(a, b)

0 commit comments

Comments
 (0)