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

opal/mca/threads: remove code duplication #8790

Merged
merged 1 commit into from
Apr 23, 2021

Conversation

shintaro-iwasaki
Copy link
Contributor

@shintaro-iwasaki shintaro-iwasaki commented Apr 9, 2021

Problem

Pthreads, Qthreads, and Argobots share a lot of logics in common. This duplication hinders code maintenance.
(Specifically, Qthreads and Argobots code bases need to be updated frequently.)

This PR is what I promised in #8767. I'd like to thank @devreal for his suggestion.

Solution

Both Qthreads and Argobots have implementations that are similar to pthread_mutex_t and pthread_cond_t. If those raw mutex/cond objects are abstracted, a lot of duplicated code can be removed. This PR introduces opal_thread_internal_mutex_t and opal_thread_internal_cond_t to remove code duplication in mutex.h, mutex.c, wait_sync.h, and wait_sync.c.

Since this layer is very thin (static inline functions), this abstraction should not introduce an additional overhead.

I have checked Pthreads, Qthreads, and Argobots versions locally (not very thoroughly, though). I believe the Pthreads configuration will be tested by CI, so my point is that this change worked for Qthreads and Argobots as far as I checked.

Structure of this PR

Commits 1 - 3

Clean up each logic to unify functions later. These commits are either fixing bugs or removing something unnecessary.

Commits 4 - 10

Introduce opal_thread_internal_mutex_t and opal_thread_internal_cond_t and use them for each thread (Pthreads, Qthreads, Argobots).
The first commit implements opal_thread_internal_mutex_t and opal_thread_internal_cond_t and the second commit applies them.
Those commits do not change the logic; it just wraps pthread_mutex_xxx() and pthread_cond_xxx() by opal_thread_internal_mutex_xxx() and opal_thread_internal_cond_xxx().

Commit 11

Implement common mutex-related logics in base/mutex.c and mutex.h and remove the corresponding code in each threading directory.
All the macros and functions are copied from pthread/threads_pthreads_mutex.c and pthread/threads_pthreads_mutex.h.

Commits 12 - 13

Implement common wait_sync-related logics in base/wait_sync.c and wait_sync.h and remove the corresponding code in each threading directory.
All the macros and functions are copied from pthread/threads_pthreads_wait_sync.c and pthread/threads_pthreads_wait_sync.h.

Commits 14 - 16

Clean up opal/mca/threads code.

@ompiteam-bot
Copy link

Can one of the admins verify this patch?

@shintaro-iwasaki
Copy link
Contributor Author

I'd truly appreciate it if @devreal could review this PR, too. Of course, I appreciate any comments from anyone.

@jjhursey
Copy link
Member

jjhursey commented Apr 9, 2021

ok to test

Copy link
Member

@abouteiller abouteiller left a comment

Choose a reason for hiding this comment

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

Looking good otherwise (just eyeballing)

opal/mca/threads/wait_sync.h Show resolved Hide resolved
opal/mca/threads/argobots/threads_argobots_wait_sync.c Outdated Show resolved Hide resolved
@shintaro-iwasaki shintaro-iwasaki force-pushed the topic/thread_remove_dup branch from d7ed306 to 0745b45 Compare April 12, 2021 19:51
Copy link
Contributor

@devreal devreal left a comment

Choose a reason for hiding this comment

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

I thought I had put in my comments already but apparently not. Looks good, just a few comments 👍

opal/mca/threads/pthreads/threads_pthreads_mutex.h Outdated Show resolved Hide resolved
opal/mca/threads/pthreads/threads_pthreads_mutex.h Outdated Show resolved Hide resolved
opal/mca/threads/wait_sync.h Outdated Show resolved Hide resolved
@shintaro-iwasaki shintaro-iwasaki force-pushed the topic/thread_remove_dup branch from 0745b45 to a048664 Compare April 13, 2021 17:23
@shintaro-iwasaki shintaro-iwasaki force-pushed the topic/thread_remove_dup branch from a048664 to 4e48be9 Compare April 13, 2021 19:45
Copy link
Contributor

@devreal devreal left a comment

Choose a reason for hiding this comment

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

Thanks for the work on this 👍

@shintaro-iwasaki shintaro-iwasaki force-pushed the topic/thread_remove_dup branch 3 times, most recently from f90481b to fea82f5 Compare April 13, 2021 22:02
@shintaro-iwasaki
Copy link
Contributor Author

Thank you for reviewing this PR.

@bosilca Could you please review this PR when you have time?

@shintaro-iwasaki
Copy link
Contributor Author

Could one of the admins please merge this PR? This PR is necessary for compilation of Open MPI + Qthreads (@janciesko).

If rebase is needed, please let me know.

@bosilca
Copy link
Member

bosilca commented Apr 16, 2021

@shintaro-iwasaki please squash and then we can merge.

opal_pthread_mutex_t is not used.  This patch removes it.

Signed-off-by: Shintaro Iwasaki <[email protected]>

opal/mca/threads/qthreads,argobots: remove m_recursive

m_recursive is not used.  This patch removes it.

Signed-off-by: Shintaro Iwasaki <[email protected]>

opal/mca/threads/qthreads,argobots: update wait_sync implementation

This patch is corresponding to changes to the following commit for
Pthreads: 6a406fb

Signed-off-by: Shintaro Iwasaki <[email protected]>

opal/mca/threads/pthreads: implement opal_thread_internal_mutex/cond_t

opal_thread_internal_mutex_t and opal_thread_internal_cond_t are new
objects that are directly mapped to raw mutex/condition variable
implementations (e.g., pthread_mutex_t for Pthreads).  This abstraction
is different from opal_mutex_t, which has more functionalities.

Introducing this abstraction helps removal of code duplication among
other threading layers.

Signed-off-by: Shintaro Iwasaki <[email protected]>

opal/mca/threads/pthreads: use opal_thread_internal_mutex/cond_t

opal_thread_internal_mutex/cond routines are static inline functions, so
there should be no additional overhead.

Signed-off-by: Shintaro Iwasaki <[email protected]>

opal/mca/threads/argobots: implement opal_thread_internal_mutex/cond_t

Signed-off-by: Shintaro Iwasaki <[email protected]>

opal/mca/threads/argobots: use opal_thread_internal_mutex/cond_t

Signed-off-by: Shintaro Iwasaki <[email protected]>

opal/mca/threads/qthreads: implement opal_thread_internal_mutex/cond_t

Signed-off-by: Shintaro Iwasaki <[email protected]>

opal/mca/threads/qthreads: use opal_thread_internal_mutex/cond_t

Signed-off-by: Shintaro Iwasaki <[email protected]>

opal/mca/threads: move mutex implementation to threads/base

Now implementations of opal_mutex_xxx() and opal_cond_xxx() are the
same.  This patch moves them to base/mutex.c and mutex.h and removes the
duplication.

Note that those implementations are copied from
pthread/threads_pthreads_mutex.c and pthread/threads_pthreads_mutex.h.

Signed-off-by: Shintaro Iwasaki <[email protected]>

opal/mca/threads: use opal_thread_yield() in busy loop if requested

This commit is prerequisite for the next commit that unifies the
implementation of wait_sync.  Nonpreemptive threads like Qthreads and
Argobots need a yield in a busy loop to avoid a deadlock, while Pthreads
does not.

To unify the implementation in the next commit, this patch adds
opal_thread_yield() if opal_progress_yield_when_idle is true.

At the same time, this patch removes a yield operation after
opal_progress() since opal_progress() internally yields when it does not
make any progress.

Signed-off-by: Shintaro Iwasaki <[email protected]>

opal/mca/threads: move wait_sync implementation to threads/base

Now implementations of wait_sync_xxx() functions and macros are the
same.  This patch moves them to base/wait_sync.c and wait_sync.h and
removes the duplication.

Note that those implementations are copied from
pthread/threads_pthreads_wait_sync.c and
pthread/threads_pthreads_wait_sync.h.

Signed-off-by: Shintaro Iwasaki <[email protected]>

opal/mca/threads: change Pthread-specific names

This patch renames variables and functions that are no longer specific
to Pthread.

Signed-off-by: Shintaro Iwasaki <[email protected]>

opal/mca/threads: remove MCA_threads_wait_sync_base_include_xxx

MCA_threads_wait_sync_base_include_xxx is no longer used.  This patch
removes them.

Signed-off-by: Shintaro Iwasaki <[email protected]>

opal/mca/threads: minor cleanup

- Remove unnecessary "include"
- Remove weird empty lines
- Clean up parentheses for macros
- Remove EDEADLK check for trylock() and unlock()

Signed-off-by: Shintaro Iwasaki <[email protected]>
@shintaro-iwasaki shintaro-iwasaki force-pushed the topic/thread_remove_dup branch from fea82f5 to bcd376a Compare April 16, 2021 18:27
@shintaro-iwasaki
Copy link
Contributor Author

shintaro-iwasaki commented Apr 16, 2021

Thanks. I squashed all. If there is any formatting rules regarding "squash", please let me know.

@bosilca
Copy link
Member

bosilca commented Apr 16, 2021

There are no rules, it just just practice to have a clean history and avoid PR with many commits where each one only fixes a minor issue.

@jsquyres
Copy link
Member

jsquyres commented Apr 16, 2021

Actually, I looked at the series of commits before squashing -- I thought they were all nice, small, tidy commits. I would have been happy with them without squashing. Makes the history easier to understand.

So ya -- there's no rules. Just opinions. 😄

@bosilca
Copy link
Member

bosilca commented Apr 16, 2021

I might be a little bit more picky on this. When I see different commits with the same log message, I tend to assume they should be squashed.

@shintaro-iwasaki
Copy link
Contributor Author

Could someone merge this PR? This PR is necessary for compilation of Open MPI + Qthreads.

If I need to rebase/squash/ask additional reviews, please let me know. If other PRs are blocking this, I'd appreciate it if someone could teach me when this PR can be merged.

@hppritcha hppritcha merged commit 96d36f6 into open-mpi:master Apr 23, 2021
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.

8 participants