public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
To: Lipeng Zhu <lipeng.zhu@intel.com>,
	Lipeng Zhu via Fortran <fortran@gcc.gnu.org>,
	tkoenig@netcologne.de, fortran@gcc.gnu.org,
	gcc-patches@gcc.gnu.org
Cc: hongjiu.lu@intel.com, tianyou.li@intel.com, pan.deng@intel.com,
	wangyang.guo@intel.com
Subject: Re: [PATCH v3] libgfortran: Replace mutex with rwlock
Date: Wed, 19 Apr 2023 23:49:33 +0200	[thread overview]
Message-ID: <F9DBBD75-9026-4FA9-B4A8-9F2D82D65A53@gmail.com> (raw)
In-Reply-To: <20230419070628.1011624-1-lipeng.zhu@intel.com>

On 19 April 2023 09:06:28 CEST, Lipeng Zhu via Fortran <fortran@gcc.gnu.org> wrote:
>This patch try to introduce the rwlock and split the read/write to
>unit_root tree and unit_cache with rwlock instead of the mutex to
>increase CPU efficiency. In the get_gfc_unit function, the percentage
>to step into the insert_unit function is around 30%, in most instances,
>we can get the unit in the phase of reading the unit_cache or unit_root
>tree. So split the read/write phase by rwlock would be an approach to
>make it more parallel.
>
>BTW, the IPC metrics can gain around 9x in our test
>server with 220 cores. The benchmark we used is
>https://github.com/rwesson/NEAT
>

>+#define RD_TO_WRLOCK(rwlock) \
>+  RWUNLOCK (rwlock);\
>+  WRLOCK (rwlock);
>+#endif
>+


>diff --git a/libgfortran/io/unit.c b/libgfortran/io/unit.c
>index 82664dc5f98..4312c5f36de 100644
>--- a/libgfortran/io/unit.c
>+++ b/libgfortran/io/unit.c

>@@ -329,7 +335,7 @@ get_gfc_unit (int n, int do_create)
>   int c, created = 0;
> 
>   NOTE ("Unit n=%d, do_create = %d", n, do_create);
>-  LOCK (&unit_lock);
>+  RDLOCK (&unit_rwlock);
> 
> retry:
>   for (c = 0; c < CACHE_SIZE; c++)
>@@ -350,6 +356,7 @@ retry:
>       if (c == 0)
> 	break;
>     }
>+  RD_TO_WRLOCK (&unit_rwlock);

So I'm trying to convince myself why it's safe to unlock and only then take the write lock.

Can you please elaborate/confirm why that's ok?

I wouldn't mind a comment like
We can release the unit and cache read lock now. We might have to allocate a (locked) unit, below in do_create.
Either way, we will manipulate the cache and/or the unit list so we have to take a write lock now.

We don't take the write bit in *addition* to the read lock because..

(that needlessly complicates releasing the respective locks / it triggers too much contention when we.. / ...?)

thanks,

> 
>   if (p == NULL && do_create)
>     {
>@@ -368,8 +375,8 @@ retry:
>   if (created)
>     {
>       /* Newly created units have their lock held already
>-	 from insert_unit.  Just unlock UNIT_LOCK and return.  */
>-      UNLOCK (&unit_lock);
>+	 from insert_unit.  Just unlock UNIT_RWLOCK and return.  */
>+      RWUNLOCK (&unit_rwlock);
>       return p;
>     }
> 
>@@ -380,7 +387,7 @@ found:
>       if (! TRYLOCK (&p->lock))
> 	{
> 	  /* assert (p->closed == 0); */
>-	  UNLOCK (&unit_lock);
>+	  RWUNLOCK (&unit_rwlock);
> 	  return p;
> 	}
> 
>@@ -388,14 +395,14 @@ found:
>     }
> 
> 
>-  UNLOCK (&unit_lock);
>+  RWUNLOCK (&unit_rwlock);
> 
>   if (p != NULL && (p->child_dtio == 0))
>     {
>       LOCK (&p->lock);
>       if (p->closed)
> 	{
>-	  LOCK (&unit_lock);
>+	  WRLOCK (&unit_rwlock);
> 	  UNLOCK (&p->lock);
> 	  if (predec_waiting_locked (p) == 0)
> 	    destroy_unit_mutex (p);
>@@ -593,8 +600,8 @@ init_units (void)
> #endif
> #endif
> 
>-#ifndef __GTHREAD_MUTEX_INIT
>-  __GTHREAD_MUTEX_INIT_FUNCTION (&unit_lock);
>+#if (!defined(__GTHREAD_RWLOCK_INIT) && !defined(__GTHREAD_MUTEX_INIT))
>+  __GTHREAD_MUTEX_INIT_FUNCTION (&unit_rwlock);
> #endif
> 
>   if (sizeof (max_offset) == 8)


  parent reply	other threads:[~2023-04-19 21:49 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20221230001607.2232962-1-lipeng.zhu () intel ! com>
2023-04-19  7:06 ` Lipeng Zhu
2023-04-19 12:51   ` Bernhard Reutner-Fischer
2023-04-19 14:50     ` Bernhard Reutner-Fischer
2023-04-19 21:49   ` Bernhard Reutner-Fischer [this message]
2023-04-20 13:13 Zhu, Lipeng
2023-04-24 19:45 ` Bernhard Reutner-Fischer
2023-05-08  9:31 Zhu, Lipeng
2023-05-08 10:04 ` Bernhard Reutner-Fischer

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=F9DBBD75-9026-4FA9-B4A8-9F2D82D65A53@gmail.com \
    --to=rep.dot.nop@gmail.com \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hongjiu.lu@intel.com \
    --cc=lipeng.zhu@intel.com \
    --cc=pan.deng@intel.com \
    --cc=tianyou.li@intel.com \
    --cc=tkoenig@netcologne.de \
    --cc=wangyang.guo@intel.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).