Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-codegen
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Christian Heinigk
dune-codegen
Commits
aec6e23b
Commit
aec6e23b
authored
8 years ago
by
Dominic Kempf
Browse files
Options
Downloads
Patches
Plain Diff
Allow syntax like '@preamble(kernel=...)'
parent
e559ebec
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
python/dune/perftool/generation/cache.py
+28
-20
28 additions, 20 deletions
python/dune/perftool/generation/cache.py
python/test/dune/perftool/generation/test_cache.py
+8
-0
8 additions, 0 deletions
python/test/dune/perftool/generation/test_cache.py
with
36 additions
and
20 deletions
python/dune/perftool/generation/cache.py
+
28
−
20
View file @
aec6e23b
"""
This module provides the memoization infrastructure for code
generating functions.
"""
from
dune.perftool.generation.context
import
get_global_context_value
from
dune.perftool.generation.context
import
(
get_global_context_value
,
global_context
,
)
from
dune.perftool.generation.counter
import
get_counter
# Store a global list of generator functions
...
...
@@ -54,6 +56,7 @@ class _RegisteredFunction(object):
on_store
=
lambda
x
:
x
,
item_tags
=
(),
context_tags
=
(),
**
kwargs
):
self
.
func
=
func
self
.
cache_key_generator
=
cache_key_generator
...
...
@@ -61,6 +64,7 @@ class _RegisteredFunction(object):
self
.
on_store
=
on_store
self
.
item_tags
=
item_tags
self
.
context_tags
=
context_tags
self
.
kwargs
=
kwargs
# Initialize the memoization cache
self
.
_memoize_cache
=
{}
...
...
@@ -81,7 +85,7 @@ class _RegisteredFunction(object):
else
:
return
self
.
_memoize_cache
[
key
]
def
__
call
__
(
self
,
*
args
,
**
kwargs
):
def
call
(
self
,
*
args
,
**
kwargs
):
# Get the cache key from the given arguments
cache_key
=
self
.
cache_key_generator
(
*
args
,
**
kwargs
)
...
...
@@ -105,6 +109,10 @@ class _RegisteredFunction(object):
# Return the result for immediate usage
return
self
.
_get_content
(
cache_key
)
def
__call__
(
self
,
*
args
,
**
kwargs
):
with
global_context
(
**
self
.
kwargs
):
return
self
.
call
(
*
args
,
**
kwargs
)
def
generator_factory
(
**
factory_kwargs
):
"""
A function decorator factory
...
...
@@ -172,24 +180,24 @@ cached = generator_factory(item_tags=("default_cached",))
class
_ConditionDict
(
dict
):
def
__init__
(
self
,
tags
):
dict
.
__init__
(
self
)
self
.
tags
=
tags
def
__getitem__
(
self
,
i
):
# If we do not add these special cases the dictionary will return False
# when we execute the following code:
#
# eval ("True", _ConditionDict(v.tags)
#
# But in this case we want to return True! A normal dictionary would not attempt
# to replace "True" if "True" is not a key. The _ConditionDict obviously has no
# such concerns ;).
if
i
==
"
True
"
:
return
True
if
i
==
"
False
"
:
return
False
return
i
in
self
.
tags
def
__init__
(
self
,
tags
):
dict
.
__init__
(
self
)
self
.
tags
=
tags
def
__getitem__
(
self
,
i
):
# If we do not add these special cases the dictionary will return False
# when we execute the following code:
#
# eval ("True", _ConditionDict(v.tags)
#
# But in this case we want to return True! A normal dictionary would not attempt
# to replace "True" if "True" is not a key. The _ConditionDict obviously has no
# such concerns ;).
if
i
==
"
True
"
:
return
True
if
i
==
"
False
"
:
return
False
return
i
in
self
.
tags
def
_filter_cache_items
(
gen
,
condition
):
...
...
This diff is collapsed.
Click to expand it.
python/test/dune/perftool/generation/test_cache.py
+
8
−
0
View file @
aec6e23b
...
...
@@ -253,3 +253,11 @@ def test_multiple_kernels():
k2
,
=
retrieve_cache_items
(
"
k2
"
)
assert
k2
==
"
bla
"
@preamble
(
kernel
=
"
k3
"
)
def
pre3
():
return
"
foo
"
pre3
()
k3
,
=
retrieve_cache_items
(
"
k3
"
)
assert
k3
==
"
foo
"
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