-
Notifications
You must be signed in to change notification settings - Fork 61
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
Have option to set thread count to local maximum #1716
Conversation
I'd really like this done according to the outline in #988, with the heuristics in the arborenv lib. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the newtype pattern here, but as alternative consider a (set of) static function(s)
std::option<unsigned> thread_count_avail_cores() {...}
std::option<unsigned> thread_count_from_omp() {...}
...
maybe that's even cleaner.
arbor/include/arbor/context.hpp
Outdated
@@ -13,6 +16,32 @@ struct dry_run_info { | |||
num_cells_per_rank(cells_per_rank) {} | |||
}; | |||
|
|||
struct thread_count { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class is mixing up arbor
and arborenv
functionailty. I don't think this is the right place for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Users interact with these classes directly, so sanitizing user input (0 is not a good thread number) in this place makes sense.
Or do you mean to just move the struct to concurrency.cpp
? It would make arborenv
a dependency of the Python package though.
arbor/include/arbor/context.hpp
Outdated
} | ||
|
||
static thread_count avail_threads() { | ||
auto tc = thread_count{std::thread::hardware_concurrency()}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why doesn't this just return an unsigned?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does; unsigned is the return type of std::thread::hardware_concurrency()
.
@thorstenhater Shall I use |
Yes, I shall. |
…ption from C++, update test and doc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! I just have a few suggestions.
@@ -37,6 +37,11 @@ bad_global_property::bad_global_property(cell_kind kind): | |||
kind(kind) | |||
{} | |||
|
|||
zero_thread_requested_error::zero_thread_requested_error(unsigned nbt): | |||
arbor_exception(pprintf("threads must be a positive integer")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this PR seems to be restricted to pyarb changes, I think this exception would work better as a pyarb_error
(defined in python/error.hpp)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point, but I'm not entirely happy with 1. this split anyway and 2. the particular contents of python/error.hpp
. First of all, python/pyarb.cpp
already contained one error translator file_not_found_error
, which is a much clearer pattern: you register errors raised in C++ code, and translate them as you wish. Since the code of this particular change is C++ (and really all our code), even though restricted to being raised within Pyarb for the moment, it makes more sense to me to follow the same pattern. The pyarb_error
in python/error.hpp
should really be removed, it relies on implicit translation by pybind11
and it's uses broken up into more meaningful exception classes.
If you agree, I can do that in a follow up PR. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of translation was a test and especially aimed at FileNotFound, because users expect FNF instead of RuntimeError. But yes, now that this is in for a while and caused no issues, we should port all our errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you're saying, but I don't think the solution is to move all of our custom errors to the arbor library. The errors that are used only in the pyarb
library should be defined there, either in python/error.hpp
or somewhere else.
I agree that we should translate our exceptions into python exceptions whenever possible, but that has nothing to do with where the exceptions are defined.
I think it's better to follow the current convention of using pyarb_error
instead of arbor_exception
in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are already custom Arbor errors in arbor/arbexcept.*pp
, right? They are not specific to a part of the API, else they would not live in one file. I don't think Python needs to be an exception (ha), other parts of Arbor may very well raise a zero_thread_requested_error
at some point. I thought that having all errors in one place is so you can quickly find out if the exception you are raising already fits an existing class.
Now, if the exception was very specific to Python I would agree with you, but this is about user input, which is not specific to Python.
I prefer to start (well, continue!) writing new exceptions in this pattern, and tackle existing pyarb_error
s in the near future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're looking at 2 libraries
arbor
is the core arbor C++ library and has it's own exceptions inarbor/arbexcept.*pp
.pyarb
is the python wrapper library, it needsarbor
as a dependency but they are separate libraries. It has it's own exceptions inpython/error.*pp
.
An error that is only used in pyarb
doesn't need to live in arbor
.
Separating the errors keeps the code clear, you can look at the type of the error if it's pyarb_error
you know it's been raised from pyarb
, if it's arbor_exception
it's been raised from arbor
.
There's no need to accumulate all errors in the same file in the arbor
lib. We can replicate what we have in the arbor
lib in the pyarb
lib as long as we keep that separation between the libraries clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not a Python error, but an input error. It just happens to be only raised by the Python binding for the moment. In general, I think they should overlap as much as possible, not as little as possible.
I think "Python errors" should be seen as exceptions raised by Python machinery, e.g. C++ is unlikely to ever need a TabError
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a small issue and I don't want to keep going around in circles. The current setup is to use pyarb_error
in the pyarb
library and I think this error belong there. It's up to you at the end of the day.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
bors try |
tryBuild succeeded: |
* Pyarb specific. * `proc_allocation_shim()` throws error if user sets threads to zero. * `arbor.context` constructor accepts `threads` set to `"avail_threads"`, which sets the number of threads to `arbenv::thread_concurrency()` * This introduces a dependency on arbenv for Pyarb. * Docs and tests added. Fixes arbor-sim#1692
proc_allocation_shim()
throws error if user sets threads to zero.arbor.context
acceptsavail_threads
, which sets the number of threads toarbenv::thread_concurrency()
Fixes #1692