public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: 김규래 <msca8h@naver.com>
Cc: gcc@gcc.gnu.org
Subject: Re: Re: [GSoC'19, libgomp work-stealing] Task parallelism runtime
Date: Mon, 05 Aug 2019 10:32:00 -0000	[thread overview]
Message-ID: <20190805103216.GQ2726@tucnak> (raw)
In-Reply-To: <ac16ab3c02b87c6fec8eed4b143862@cweb006.nm.nfra.io>

On Sat, Aug 03, 2019 at 06:11:58PM +0900, 김규래 wrote:
> I'm currently having trouble implementing the thread sleeping mechanism when the queue is out of tasks.
> Problem is, it's hard to maintain consistency between the thread sleeping routine and the queues.
> See the pseudocode below,
>  
> 1. check queue is empty
> 2. go to sleep
>  
> if we go lock-free, the consistency between 1 and 2 cannot be maintained.

I thought we don't want to go lock-free, the queue operations aren't easily
implementable lock-free, but instead with a lock for each of the queues,
so in the multi-queue setting having locks on the implicit tasks that hold
those queues.  What can and should be done without lock is perhaps some
preliminary check if a queue is empty, that can be done through
__atomic_load.
And, generally go to sleep is done outside of the critical section, inside
of the critical section we decide if we go to sleep or not, and then
go to sleep either (on Linux) using futexes, or otherwise using semaphores,
both have the properties that one can already post to them before some other
thread sleeps on it, and in that case the other thread doesn't actually go
to sleep.  The wake up (post on the semaphore or updating the memory + later
futex wake) is sometimes done inside of a critical section, the updating of
memory if it is not atomic increase/decrease and the latter depending on
whether we remember from the atomic operation whether the wake up is needed
or not and defer it until after the critical section.

Given say:
      ++team->task_count;
      ++team->task_queued_count;
      gomp_team_barrier_set_task_pending (&team->barrier);
      do_wake = team->task_running_count + !parent->in_tied_task
                < team->nthreads;
      gomp_mutex_unlock (&team->task_lock);
      if (do_wake)
        gomp_team_barrier_wake (&team->barrier, 1);
you can see the wake up is done outside of the critical section.
If team->task_lock isn't used, there will be of course problems, say
team->task_count and team->task_queued_count need to be bumped atomically,
ditto operations on team->barrier, and the question is what to do with the
team->task_running_count check, if that one is updated atomically too,
maybe __atomic_load might be good enough, though perhaps worst case it might
mean we don't in some cases wake anybody, so there will be threads idling
instead of doing useful work, but at least one thread probably should handle
it later.

	Jakub

  reply	other threads:[~2019-08-05 10:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <e2a9f7c55311795785d0f2c47f70acbd@cweb001.nm.nfra.io>
2019-06-24 19:55 ` 김규래
2019-06-24 20:13   ` Jakub Jelinek
2019-07-13  7:46   ` John Pinkerton
2019-07-09 12:56 ` 김규래
2019-07-13  6:28   ` Jakub Jelinek
2019-07-21  7:46     ` 김규래
2019-07-22 18:54       ` Jakub Jelinek
2019-07-22 19:00         ` 김규래
2019-08-03  9:12           ` 김규래
2019-08-05 10:32             ` Jakub Jelinek [this message]
2019-08-05 11:01               ` 김규래
     [not found]                 ` <20190819061020.GA27842@laptop.zalov.cz>
2019-08-25  7:49                   ` 김규래

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190805103216.GQ2726@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc@gcc.gnu.org \
    --cc=msca8h@naver.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).