Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
hysop
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
particle_methods
hysop
Commits
e5abdb2a
Commit
e5abdb2a
authored
8 years ago
by
Jean-Baptiste Keck
Browse files
Options
Downloads
Patches
Plain Diff
cached stencils
parent
253d05fb
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/advection_gpu.py
+1
-1
1 addition, 1 deletion
examples/advection_gpu.py
hysop/codegen/maths/stencil/stencil.py
+57
-31
57 additions, 31 deletions
hysop/codegen/maths/stencil/stencil.py
with
58 additions
and
32 deletions
examples/advection_gpu.py
+
1
−
1
View file @
e5abdb2a
...
...
@@ -139,7 +139,7 @@ if __name__=='__main__':
'
use_builtin_copy
'
:
True
,
'
stretching
'
:
{
'
formulation
'
:
StretchingFormulation
.
GRAD_UW
,
'
order
'
:
2
'
order
'
:
64
}
}
}
...
...
This diff is collapsed.
Click to expand it.
hysop/codegen/maths/stencil/stencil.py
+
57
−
31
View file @
e5abdb2a
import
math
,
hashlib
import
os
,
math
,
hashlib
,
gzip
,
pickle
import
numpy
as
np
import
scipy
as
sp
import
sympy
as
sm
...
...
@@ -9,9 +9,34 @@ import scipy.sparse
import
gmpy2
as
gmp
from
gmpy2
import
mpq
from
hysop.tools.io_utils
import
IO
def
_mpqize
(
x
):
return
mpq
(
x
.
p
,
x
.
q
)
mpqize
=
np
.
vectorize
(
_mpqize
)
_hysop_cache_folder
=
IO
.
default_cache_path
()
_stencil_cache_file
=
_hysop_cache_folder
+
'
/
'
+
'
stencils.pklz
'
def
_load_cached_stencils
():
if
not
os
.
path
.
exists
(
_hysop_cache_folder
):
os
.
makedirs
(
_hysop_cache_folder
)
if
not
os
.
path
.
isfile
(
_stencil_cache_file
):
return
{}
else
:
with
gzip
.
open
(
_stencil_cache_file
,
'
rb
'
)
as
f
:
return
pickle
.
load
(
f
)
def
_export_stencils
():
with
gzip
.
open
(
_stencil_cache_file
,
'
wb
'
)
as
f
:
pickle
.
dump
(
_cached_stencils
,
f
)
def
_clear_stencil_cache
():
_cached_stencils
=
{}
if
os
.
path
.
isfile
(
_stencil_cache_file
):
os
.
remove
(
_stencil_cache_file
)
_cached_stencils
=
_load_cached_stencils
()
class
Stencil
(
object
):
def
__init__
(
self
,
stencil_data
,
origin
,
order
):
...
...
@@ -76,23 +101,6 @@ class Stencil(object):
return
not
np
.
any
(
self
.
stencil
*
mask
)
class
StencilGenerator
(
object
):
def
__init__
(
self
,
derivative
,
dim
,
dtype
=
mpq
):
self
.
derivative
=
derivative
self
.
dim
=
dim
self
.
dtype
=
dtype
#L = L if np.isscalar(L) else np.asarray(L))
#if R is None:
#R = k+order-L-1
#assert((R>=0).all(), 'R<0...')
#else:
#assert((L+R+1 == k+order).all()), 'L+R+1 != k+order, cannot compute coefficients !'
#R = R if np.isscalar(R) else np.asarray(R))
#N = L+R+1
class
Stencil1D
(
Stencil
):
def
__init__
(
self
):
self
.
Lx
=
0
...
...
@@ -101,10 +109,16 @@ class Stencil1D(Stencil):
self
.
order
=
0
self
.
N
=
0
self
.
dtype
=
None
@staticmethod
def
_hash_stencil_key
(
Lx
,
Rx
,
k
,
order
,
dtype
):
s
=
'
Stencil1D_{}_{}_{}_{}_{}
'
.
format
(
Lx
,
Rx
,
k
,
order
,
dtype
)
return
hashlib
.
sha256
(
s
).
hexdigest
()
# generate a discrete stencil of the 'k'-th derivative at order 'order' defined over [-Lx,Rx] grid points.
# Note: generated order can be > specified order if stencil coefficients are 0 at borders.
def
generate
(
self
,
Lx
,
Rx
,
k
,
order
,
dtype
=
mpq
):
def
generate
(
self
,
Lx
,
Rx
,
k
,
order
,
dtype
=
mpq
,
override_cache
=
False
):
assert
(
Lx
+
Rx
+
1
==
k
+
order
),
'
Lx+Rx != k+order, cannot compute coefficients !
'
self
.
Lx
=
Lx
self
.
Rx
=
Rx
...
...
@@ -113,19 +127,31 @@ class Stencil1D(Stencil):
self
.
dtype
=
dtype
N
=
Lx
+
Rx
+
1
A
=
np
.
zeros
((
N
,
N
),
dtype
=
object
)
b
=
np
.
zeros
(
N
,
dtype
=
object
)
for
i
in
xrange
(
N
):
b
[
i
]
=
dtype
(
i
==
k
)
for
j
in
xrange
(
0
,
N
):
A
[
i
,
j
]
=
dtype
(
j
-
Lx
)
**
i
A
=
sm
.
Matrix
(
N
,
N
,
A
.
flatten
())
b
=
sm
.
Matrix
(
N
,
1
,
b
.
flatten
())
S
=
A
.
LUsolve
(
b
)
S
*=
dtype
(
math
.
factorial
(
k
))
S
=
mpqize
(
np
.
asarray
(
S
.
tolist
(),
dtype
=
object
).
ravel
())
skey
=
Stencil1D
.
_hash_stencil_key
(
Lx
,
Rx
,
k
,
order
,
dtype
)
if
'
1D
'
not
in
_cached_stencils
:
_cached_stencils
[
'
1D
'
]
=
{}
if
(
not
override_cache
)
and
(
skey
in
_cached_stencils
[
'
1D
'
]):
S
=
_cached_stencils
[
'
1D
'
][
skey
]
else
:
A
=
np
.
zeros
((
N
,
N
),
dtype
=
object
)
b
=
np
.
zeros
(
N
,
dtype
=
object
)
for
i
in
xrange
(
N
):
b
[
i
]
=
dtype
(
i
==
k
)
for
j
in
xrange
(
0
,
N
):
A
[
i
,
j
]
=
dtype
(
j
-
Lx
)
**
i
A
=
sm
.
Matrix
(
N
,
N
,
A
.
flatten
())
b
=
sm
.
Matrix
(
N
,
1
,
b
.
flatten
())
S
=
A
.
LUsolve
(
b
)
S
*=
dtype
(
math
.
factorial
(
k
))
S
=
mpqize
(
np
.
asarray
(
S
.
tolist
(),
dtype
=
object
).
ravel
())
_cached_stencils
[
'
1D
'
][
skey
]
=
S
_export_stencils
()
self
.
N
=
N
self
.
stencil
=
S
return
self
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment