<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi Matthieu,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Thanks.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<blockquote itemscope="" itemtype="https://schemas.microsoft.com/QuotedText" style="border-left: 3px solid rgb(200, 200, 200); border-top-color: rgb(200, 200, 200); border-right-color: rgb(200, 200, 200); border-bottom-color: rgb(200, 200, 200); padding-left: 1ex; margin-left: 0.8ex; color: rgb(102, 102, 102);">
In the example you show, you say “even if you unlock m2, m1 is not unlocked”. That said, if no ULT cares about m1 anymore, and all the ULTs know they should use m2, then why shouldn’t it be fine?</blockquote>
<div>It depends on the implementation. In my understanding, it is fine in the current Argobots implementation, but it can cause an issue in the future update. So far, I do not plan to ensure this property. For example, a possible Argobots implementation
that can break this m1->m2 scenario is using a doubly circular linked list to manage waiters while one of the elements (maybe a head/tail marker of this linked list) is allocated within ABT_mutex_memory (= the internal mutex entity). If you memory-copy m1
to m2, a waiter of this mutex points to m1's memory address though m1 is no longer valid.</div>
<div><br>
</div>
<div>Even if m1 is unlocked, I am not fully sure if copying m1 to m2 and discarding m1 is okay. In other words, locked-and-then-unlocked ABT_mutex_memory's state can be different from clean ABT_mutex_memory's state just after
<span style="background-color:rgb(255, 255, 255);display:inline !important">ABT_MUTEX_INITIALIZER, though I cannot come up with a good example that can cause such.</span></div>
<div><br>
</div>
<div>Note that if you can guarantee the following, you might want to reinitialize std::vector<ABT_mutex_memory> every time, which should be robust and not affected by the future update.</div>
<div>1. When std::vector<ABT_mutex_memory> is resized, none of them are "locked" (implied by "no ULT cares about m1 anymore")</div>
<div>2. When std::vector<ABT_mutex_memory> is resized, there is no access to the old one: all ULTs can access new ABT_mutex without any data race (implied by "all the ULTs know they should use m2").</div>
<div><br>
</div>
<div>The code should look like this:</div>
<div>```</div>
<div>std::vector<ABT_mutex_memory> mutex_mems;</div>
<div>mutex_mems.resize(16);</div>
<div>for (int i = 0; i < mutex_mems.size(); i++) {</div>
<div> // Clean up mutex_mems. ABT_mutex_free is not needed to release statically allocated ABT_mutex.</div>
<div> const ABT_mutex_memory mutex_mem_init = ABT_MUTEX_INITIALIZER;</div>
<div> memcpy(&mutex_mems[i], &<span style="background-color:rgb(255, 255, 255);display:inline !important">mutex_mem_init</span>, sizeof(ABT_mutex_memory));</div>
<div>}</div>
<span>```</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span>If this <span style="background-color:rgb(255, 255, 255);display:inline !important">memcpy() looks awkward, Argobots can provide a single-line macro.</span></span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt;"><br>
</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt;">I would appreciate any suggestions!</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Thanks,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Shintaro</div>
<div id="appendonsend"></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Dorier, Matthieu <mdorier@anl.gov><br>
<b>Sent:</b> Tuesday, June 15, 2021 10:09 AM<br>
<b>To:</b> Iwasaki, Shintaro <siwasaki@anl.gov>; discuss@argobots.org <discuss@argobots.org>; discuss@lists.argobots.org <discuss@lists.argobots.org><br>
<b>Subject:</b> Re: Can I move a static mutex around in memory?</font>
<div> </div>
</div>
<div lang="EN-GB" style="word-wrap:break-word">
<div class="x_WordSection1">
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style="">Hi Shintaro,</span></p>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style=""> </span></p>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style="">Thank you for your answer.</span></p>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style=""> </span></p>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style="">In the example you show, you say “even if you unlock m2, m1 is not unlocked”. That said, if no ULT cares about m1 anymore, and all the ULTs know they should use m2, then why shouldn’t it be fine?</span></p>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style=""> </span></p>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style="">Maybe other objects in Argobots (such as an ES) are keeping pointers to mutexes, and these pointers would be invalidated?</span></p>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style=""> </span></p>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style="">Thanks,</span></p>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style=""> </span></p>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style="">Matthieu</span></p>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US" style=""> </span></p>
<div style="border:none; border-top:solid #B5C4DF 1.0pt; padding:3.0pt 0cm 0cm 0cm">
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<b><span style="font-size:12.0pt; color:black">From: </span></b><span style="font-size:12.0pt; color:black">"Iwasaki, Shintaro" <siwasaki@anl.gov><br>
<b>Date: </b>Tuesday, 15 June 2021 at 14:57<br>
<b>To: </b>"discuss@argobots.org" <discuss@argobots.org>, "discuss@lists.argobots.org" <discuss@lists.argobots.org><br>
<b>Cc: </b>"Dorier, Matthieu" <mdorier@anl.gov><br>
<b>Subject: </b>Re: Can I move a static mutex around in memory?</span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
</p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black">Hi Matthieu,</span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black">Thanks for the question! </span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black">ABT_mutex -> Yes, it's internally just a pointer (more specifically, a handle), so you can copy it.</span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black">ABT_mutex_memory -> No, it saves it states in this memory (directly used as ABTI_mutex), so if you copy it, you basically corrupt the ABT_mutex structure.</span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black"><a href="https://github.com/pmodels/argobots/blob/main/src/include/abt.h.in#L1114-L1115">https://github.com/pmodels/argobots/blob/main/src/include/abt.h.in#L1114-L1115</a></span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black">The same thing goes to ABT_cond_memory: do not move <span style="background:white">ABT_cond_memory around.</span></span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black; background:white">// Example (THE FOLLOWING IS FORBIDDEN)</span><span style="font-size:12.0pt; color:black">
</span></p>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black">ABT_mutex_memory m1 = ABT_MUTEX_INITIALIZER;</span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black; background:white">ABT_mutex_lock(ABT_MUTEX_MEMORY_GET_HANDLE(&m1));</span><span style="font-size:12.0pt; color:black"></span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black; background:white">ABT_mutex_memory m2;</span><span style="font-size:12.0pt; color:black"></span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black; background:white">memcpy(&m2, &m1, sizeof(ABT_mutex_memory));</span><span style="font-size:12.0pt; color:black"></span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black; background:white">// m2 is locked since m1 is locked. The mutex "entity" is copied, so even if you unlock m2, m1 is not unlocked.</span><span style="font-size:12.0pt; color:black"></span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black; background:white">// It can cause something even worse since this mutex entity may store the waiters or even a pointer to its own memory.</span><span style="font-size:12.0pt; color:black"></span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black; background:white">// Do not use ABT_mutex_memory in this way.</span><span style="font-size:12.0pt; color:black"></span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black; background:white">ABT_mutex_lock(ABT_MUTEX_MEMORY_GET_HANDLE(&m2));</span><span style="font-size:12.0pt; color:black"></span></p>
</div>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black">In your case, reallocation on resizing std::vector can break the mutex data (even if ABT_mutex_memory is somehow
<span style="background:white">moved </span>"uniquely" ).</span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black">Please dynamically allocate each ABT_mutex_memory individually (std::vector<ABT_mutex_memory *>).</span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black"> </span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black; background:white">Thanks,</span><span style="font-size:12.0pt; color:black"></span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black; background:white">Shintaro</span><span style="font-size:12.0pt; color:black"></span></p>
</div>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt; color:black; background:white"><br>
<br>
</span><span style="font-size:12.0pt; color:black"></span></p>
</div>
<div class="x_MsoNormal" align="center" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;text-align:center">
<hr size="0" width="100%" align="center">
</div>
<div id="x_divRplyFwdMsg">
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<b><span style="color:black">From:</span></b><span style="color:black"> Dorier, Matthieu via discuss <discuss@lists.argobots.org><br>
<b>Sent:</b> Tuesday, June 15, 2021 7:04 AM<br>
<b>To:</b> discuss@argobots.org <discuss@argobots.org><br>
<b>Cc:</b> Dorier, Matthieu <mdorier@anl.gov><br>
<b>Subject:</b> [argobots-discuss] Can I move a static mutex around in memory?</span>
</p>
<div>
<p class="x_MsoNormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
</p>
</div>
</div>
<div>
<div>
<p class="x_xmsonormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US">Hi,</span><span style="font-size:12.0pt"></span></p>
<p class="x_xmsonormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US"> </span><span style="font-size:12.0pt"></span></p>
<p class="x_xmsonormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US">I would like to understand statically initialized mutex and condition variables better, in particular: can I move them around in memory (assuming all the ULTs in my code have a consistent way of locating them)?</span><span style="font-size:12.0pt"></span></p>
<p class="x_xmsonormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US"> </span><span style="font-size:12.0pt"></span></p>
<p class="x_xmsonormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US">This would come up in C++ containers, for instance:</span><span style="font-size:12.0pt"></span></p>
<p class="x_xmsonormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US">std::vector<</span>ABT_mutex_memory> <span style="font-size:12.0pt">
</span></p>
<p class="x_xmsonormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
If I increase the size of the vector, reallocation may take place, and the existing mutexes will be moved somewhere else in memory. Assuming all the ULTs are accessing them by their index in the vector rather than by an ABT_mutex opaque pointer, then this should
be fine as long as I am allowed to move ABT_mutex_memory around (regardless of their state). Is that the case?<span style="font-size:12.0pt"></span></p>
<p class="x_xmsonormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt"></span></p>
<p class="x_xmsonormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
Same question for condition variables?<span style="font-size:12.0pt"></span></p>
<p class="x_xmsonormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt"></span></p>
<p class="x_xmsonormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
Thanks,<span style="font-size:12.0pt"></span></p>
<p class="x_xmsonormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span style="font-size:12.0pt"></span></p>
<p class="x_xmsonormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
Matthieu<span style="font-size:12.0pt"></span></p>
<p class="x_xmsonormal" style="margin: 0cm; font-size: 11pt; font-family: Calibri, sans-serif;">
<span lang="EN-US"> </span><span style="font-size:12.0pt"></span></p>
</div>
</div>
</div>
</div>
</body>
</html>