-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add errors and assertions to Acc computations #494
base: master
Are you sure you want to change the base?
Conversation
The PR for accelerate-llvm is now open: AccelerateHS/accelerate-llvm#68 |
@robbert-vdh Could we improve the callstack of those new functions with HasCallStack? |
@ivogabe Yes, it would be possible to add the use site of |
One use case which would be good is to add an assertion/error at this point in LULESH (which to now we just silently ignore..): https://github.com/tmcdonell/lulesh-accelerate/blob/master/src/LULESH.hs#L192 |
We just discussed in our meeting that we should also add Assertion instances for:
The last instance would be useful for the LULESH application for instance, as we could add an assertion Tom suggested we could also extend the messages by allowing any interleaving of strings and arrays. That would allow us to define those assertions in terms of this more generic one. |
I added the option to use functions for assertions, for instance:
The downside is that it does require a type annotation, as the compiler cannot resolve the typeclass without it. I also detected that aerror does not work properly yet. When a worker thread encounters it, it will not crash the entire program, causing that the program gets stuck:
We should abort the entire execution then. |
Description
This pull requests adds throwing functions, similar to
error
from the Prelude, and assertions to our Acc language.I added three functions to throw an error:
aerror :: Arrays a => String -> Acc a
: given a string, throws an error containing that stringaerrorArray :: (Arrays a, Arrays b, Show a) => String -> Acc a -> Acc b
: takes a string and an array, similar toatraceArray
aerrorExp :: (Elt e, Show e, Arrays a) => String -> Exp e -> Acc a
: takes a string and an expression, similar toatraceExp
Assertions can be written using
assert :: (Assertion a, Arrays bs) => String -> a -> Acc bs -> Acc bs
. I'm not sure about its interface yet, so feel free to share some thoughts about that :)aassert
for array level assertions, similar to howacond
,awhile
andatrace
are array-level variants of functionalities which are or may be (in the future) available on expressions. Though I didn't like howaassert
looked. Hence my idea was to useassert
here, and in the future we could useexpect
for expression-level assertions (note that assert still starts with an a and expect starts with the e of expression, nice coincidence).Exp Bool
, but I also wanted to give better error messages than just saying that the assertion failed. For instance, when asserting that two values are equal I'd like to show those two values. This could be done by making multiple assertion functions, inspecting the expression and detecting several patterns, or having a type class ranging over types of assertions. I didn't really like the first option, that resulted in signatures likeassertArraysEqual :: (Shape sh, Eq sh, Elt a, Eq a, Arrays bs) => String -> Acc (Array sh a) -> Acc (Array sh a) -> Acc bs -> Acc bs
. With the second approach it would be difficult to detect patterns like array equivalence, as that consists not only of Exp code but also Acc code. I chose the latter. Currently the instances of the type class areExp Bool
(which has no further message),data AssertArraysEqual as = AssertArraysEqual (Acc as) (Acc as)
which asserts that arrays are equal, anddata AssertEqual e = AssertEqual (Exp e) (Exp e)
.Motivation and context
These functions should make it easier for users to detect problems in their code. It is also possible to use these functions for parts of the algorithm which haven't been implemented, similar to
undefined
from the Prelude, as the program now still compiles and will only crash when such part is actually used.How has this been tested?
Just a quick test, no automated tests yet.
Types of changes
What types of changes does your code introduce? Put an
x
in all the boxes that apply:Checklist
Go over all the following points, and put an
x
in all the boxes that apply. If you're unsure about any of these, don't hesitate to ask. We're here to help!I still need to make the accompanying change in accelerate-llvm.