Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
arraycontext
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
Package registry
Model registry
Operate
Environments
Terraform modules
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
Andreas Klöckner
arraycontext
Commits
ed2374fd
Commit
ed2374fd
authored
3 years ago
by
Matthias Diener
Browse files
Options
Downloads
Patches
Plain Diff
refactor norm()
parent
295741d8
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
arraycontext/fake_numpy.py
+27
-1
27 additions, 1 deletion
arraycontext/fake_numpy.py
arraycontext/impl/pyopencl.py
+2
-24
2 additions, 24 deletions
arraycontext/impl/pyopencl.py
arraycontext/impl/pytato.py
+2
-48
2 additions, 48 deletions
arraycontext/impl/pytato.py
with
31 additions
and
73 deletions
arraycontext/fake_numpy.py
+
27
−
1
View file @
ed2374fd
...
...
@@ -24,7 +24,7 @@ THE SOFTWARE.
import
numpy
as
np
from
arraycontext.container
import
is_array_container
from
arraycontext.container
import
is_array_container
,
serialize_container
from
arraycontext.container.traversal
import
(
rec_map_array_container
,
multimapped_over_array_containers
)
...
...
@@ -174,6 +174,32 @@ class BaseFakeNumpyLinalgNamespace:
def
__init__
(
self
,
array_context
):
self
.
_array_context
=
array_context
def
norm
(
self
,
ary
,
ord
=
None
):
from
numbers
import
Number
if
isinstance
(
ary
,
Number
):
return
abs
(
ary
)
if
is_array_container
(
ary
):
import
numpy.linalg
as
la
return
la
.
norm
(
[
self
.
norm
(
subary
,
ord
=
ord
)
for
_
,
subary
in
serialize_container
(
ary
)],
ord
=
ord
)
if
len
(
ary
.
shape
)
!=
1
:
raise
NotImplementedError
(
"
only vector norms are implemented
"
)
if
ary
.
size
==
0
:
return
0
if
ord
==
np
.
inf
:
return
self
.
_array_context
.
np
.
max
(
abs
(
ary
))
elif
isinstance
(
ord
,
Number
)
and
ord
>
0
:
return
self
.
_array_context
.
np
.
sum
(
abs
(
ary
)
**
ord
)
**
(
1
/
ord
)
else
:
raise
NotImplementedError
(
f
"
unsupported value of
'
ord
'
:
{
ord
}
"
)
# }}}
...
...
This diff is collapsed.
Click to expand it.
arraycontext/impl/pyopencl.py
+
2
−
24
View file @
ed2374fd
...
...
@@ -39,7 +39,7 @@ from arraycontext.metadata import FirstAxisIsElementsTag
from
arraycontext.fake_numpy
import
\
BaseFakeNumpyNamespace
,
BaseFakeNumpyLinalgNamespace
from
arraycontext.container.traversal
import
rec_multimap_array_container
from
arraycontext.container
import
serialize_container
,
is_array_container
from
arraycontext.container
import
serialize_container
from
arraycontext.context
import
ArrayContext
...
...
@@ -165,10 +165,6 @@ def _flatten_array(ary):
class
_PyOpenCLFakeNumpyLinalgNamespace
(
BaseFakeNumpyLinalgNamespace
):
def
norm
(
self
,
ary
,
ord
=
None
):
from
numbers
import
Number
if
isinstance
(
ary
,
Number
):
return
abs
(
ary
)
if
ord
is
None
:
ord
=
2
...
...
@@ -192,25 +188,7 @@ class _PyOpenCLFakeNumpyLinalgNamespace(BaseFakeNumpyLinalgNamespace):
for
_
,
subary
in
serialize_container
(
ary
)],
ord
=
ord
)
if
is_array_container
(
ary
):
import
numpy.linalg
as
la
return
la
.
norm
(
[
self
.
norm
(
subary
,
ord
=
ord
)
for
_
,
subary
in
serialize_container
(
ary
)],
ord
=
ord
)
if
len
(
ary
.
shape
)
!=
1
:
raise
NotImplementedError
(
"
only vector norms are implemented
"
)
if
ary
.
size
==
0
:
return
0
if
ord
==
np
.
inf
:
return
self
.
_array_context
.
np
.
max
(
abs
(
ary
))
elif
isinstance
(
ord
,
Number
)
and
ord
>
0
:
return
self
.
_array_context
.
np
.
sum
(
abs
(
ary
)
**
ord
)
**
(
1
/
ord
)
else
:
raise
NotImplementedError
(
f
"
unsupported value of
'
ord
'
:
{
ord
}
"
)
return
super
().
norm
(
ary
,
ord
)
# }}}
...
...
This diff is collapsed.
Click to expand it.
arraycontext/impl/pytato.py
+
2
−
48
View file @
ed2374fd
...
...
@@ -36,57 +36,11 @@ from pytools.tag import Tag
from
numbers
import
Number
import
loopy
as
lp
from
arraycontext.container
import
serialize_container
,
is_array_container
class
_PytatoFakeNumpyLinalgNamespace
(
BaseFakeNumpyLinalgNamespace
):
def
norm
(
self
,
ary
,
ord
=
None
):
from
numbers
import
Number
if
isinstance
(
ary
,
Number
):
return
abs
(
ary
)
if
ord
is
None
:
ord
=
2
try
:
from
meshmode.dof_array
import
DOFArray
except
ImportError
:
pass
else
:
if
isinstance
(
ary
,
DOFArray
):
from
warnings
import
warn
warn
(
"
Taking an actx.np.linalg.norm of a DOFArray is deprecated.
"
"
(DOFArrays use 2D arrays internally, and
"
"
actx.np.linalg.norm should compute matrix norms of those.)
"
"
This will stop working in 2022.
"
"
Use meshmode.dof_array.flat_norm instead.
"
,
DeprecationWarning
,
stacklevel
=
2
)
import
numpy.linalg
as
la
return
la
.
norm
(
[
self
.
norm
(
_flatten_array
(
subary
),
ord
=
ord
)
for
_
,
subary
in
serialize_container
(
ary
)],
ord
=
ord
)
if
is_array_container
(
ary
):
import
numpy.linalg
as
la
return
la
.
norm
(
[
self
.
norm
(
subary
,
ord
=
ord
)
for
_
,
subary
in
serialize_container
(
ary
)],
ord
=
ord
)
if
len
(
ary
.
shape
)
!=
1
:
raise
NotImplementedError
(
"
only vector norms are implemented
"
)
if
ary
.
size
==
0
:
return
0
if
ord
==
np
.
inf
:
return
self
.
_array_context
.
np
.
max
(
abs
(
ary
))
elif
isinstance
(
ord
,
Number
)
and
ord
>
0
:
return
self
.
_array_context
.
np
.
sum
(
abs
(
ary
)
**
ord
)
**
(
1
/
ord
)
else
:
raise
NotImplementedError
(
f
"
unsupported value of
'
ord
'
:
{
ord
}
"
)
# FIXME: handle isinstance(ary, DOFArray) case
return
super
().
norm
(
ary
,
ord
)
class
_PytatoFakeNumpyNamespace
(
BaseFakeNumpyNamespace
):
...
...
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