Skip to content
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

Merged
merged 21 commits into from
Jan 24, 2022

Conversation

brenthuisman
Copy link
Contributor

@brenthuisman brenthuisman commented Oct 14, 2021

  • Pyarb specific.
  • proc_allocation_shim() throws error if user sets threads to zero.
  • arbor.context accepts avail_threads, which sets the number of threads to arbenv::thread_concurrency()
    • This introduces a dependency on arbenv for Pyarb.
  • Associated docs and tests added.

Fixes #1692

@brenthuisman brenthuisman changed the title Feat/moar cores Have option to set thread count to local maximum Oct 14, 2021
@halfflat
Copy link
Contributor

I'd really like this done according to the outline in #988, with the heuristics in the arborenv lib.

Copy link
Contributor

@thorstenhater thorstenhater left a 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 Show resolved Hide resolved
arbor/include/arbor/context.hpp Outdated Show resolved Hide resolved
arbor/include/arbor/context.hpp Outdated Show resolved Hide resolved
arbor/include/arbor/context.hpp Outdated Show resolved Hide resolved
arbor/include/arbor/context.hpp Outdated Show resolved Hide resolved
arbor/include/arbor/context.hpp Outdated Show resolved Hide resolved
arbor/include/arbor/context.hpp Outdated Show resolved Hide resolved
arbor/include/arbor/context.hpp Outdated Show resolved Hide resolved
arbor/include/arbor/context.hpp Outdated Show resolved Hide resolved
arbor/include/arbor/arbexcept.hpp Outdated Show resolved Hide resolved
@@ -13,6 +16,32 @@ struct dry_run_info {
num_cells_per_rank(cells_per_rank) {}
};

struct thread_count {
Copy link
Contributor

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.

Copy link
Contributor Author

@brenthuisman brenthuisman Oct 15, 2021

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.

}

static thread_count avail_threads() {
auto tc = thread_count{std::thread::hardware_concurrency()};
Copy link
Contributor

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?

Copy link
Contributor Author

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().

@brenthuisman brenthuisman marked this pull request as ready for review October 15, 2021 15:30
@brenthuisman
Copy link
Contributor Author

@thorstenhater Shall I use thread_concurrency() and add arbenv to the Python packages requirement? (see #1725)

@akuesters akuesters added this to the v0.6 milestone Nov 24, 2021
@brenthuisman
Copy link
Contributor Author

Yes, I shall.

Copy link
Contributor

@noraabiakar noraabiakar left a 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.

arbor/include/arbor/context.hpp Outdated Show resolved Hide resolved
@@ -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")),
Copy link
Contributor

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)

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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_errors in the near future.

Copy link
Contributor

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

  1. arbor is the core arbor C++ library and has it's own exceptions in arbor/arbexcept.*pp.
  2. pyarb is the python wrapper library, it needs arbor as a dependency but they are separate libraries. It has it's own exceptions in python/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.

Copy link
Contributor Author

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.

Copy link
Contributor

@noraabiakar noraabiakar Jan 21, 2022

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.

python/context.cpp Outdated Show resolved Hide resolved
test/unit/test_local_context.cpp Outdated Show resolved Hide resolved
python/test/unit/test_contexts.py Outdated Show resolved Hide resolved
python/context.cpp Outdated Show resolved Hide resolved
python/context.cpp Outdated Show resolved Hide resolved
python/context.cpp Show resolved Hide resolved
python/context.cpp Show resolved Hide resolved
doc/python/hardware.rst Outdated Show resolved Hide resolved
Copy link
Contributor

@thorstenhater thorstenhater left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@noraabiakar
Copy link
Contributor

bors try

bors bot added a commit that referenced this pull request Jan 24, 2022
@bors
Copy link

bors bot commented Jan 24, 2022

try

Build succeeded:

@brenthuisman brenthuisman merged commit 22bd848 into arbor-sim:master Jan 24, 2022
max9901 pushed a commit to max9901/arbor that referenced this pull request Feb 3, 2022
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Defaults: creating a context saturating local cpu
5 participants