<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
p.xmsonormal, li.xmsonormal, div.xmsonormal
        {mso-style-name:x_msonormal;
        margin:0cm;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
span.EmailStyle20
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US">Hi Shintaro,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US">Thank you for your answer.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US">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?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US">Maybe other objects in Argobots (such as an ES) are keeping pointers to mutexes, and these pointers would be invalidated?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US">Thanks,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US">Matthieu<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><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?<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black">Hi Matthieu,<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black">Thanks for the question! <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><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.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><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.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><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><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><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><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black;background:white">// Example (THE FOLLOWING IS FORBIDDEN)</span><span style="font-size:12.0pt;color:black">
<o:p></o:p></span></p>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black">ABT_mutex_memory m1 = ABT_MUTEX_INITIALIZER;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><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"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black;background:white">ABT_mutex_memory m2;</span><span style="font-size:12.0pt;color:black"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><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"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><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"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><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"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><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"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><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"><o:p></o:p></span></p>
</div>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><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" ).<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black">Please dynamically allocate each ABT_mutex_memory individually (std::vector<ABT_mutex_memory *>).<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black;background:white">Thanks,</span><span style="font-size:12.0pt;color:black"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black;background:white">Shintaro</span><span style="font-size:12.0pt;color:black"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black;background:white"><br>
<br>
</span><span style="font-size:12.0pt;color:black"><o:p></o:p></span></p>
</div>
<div class="MsoNormal" align="center" style="text-align:center">
<hr size="0" width="100%" align="center">
</div>
<div id="divRplyFwdMsg">
<p class="MsoNormal"><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>
<o:p></o:p></p>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="xmsonormal"><span lang="EN-US">Hi,</span><span style="font-size:12.0pt"><o:p></o:p></span></p>
<p class="xmsonormal"><span lang="EN-US"> </span><span style="font-size:12.0pt"><o:p></o:p></span></p>
<p class="xmsonormal"><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"><o:p></o:p></span></p>
<p class="xmsonormal"><span lang="EN-US"> </span><span style="font-size:12.0pt"><o:p></o:p></span></p>
<p class="xmsonormal"><span lang="EN-US">This would come up in C++ containers, for instance:</span><span style="font-size:12.0pt"><o:p></o:p></span></p>
<p class="xmsonormal"><span lang="EN-US">std::vector<</span>ABT_mutex_memory> <span style="font-size:12.0pt">
<o:p></o:p></span></p>
<p class="xmsonormal">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"><o:p></o:p></span></p>
<p class="xmsonormal"> <span style="font-size:12.0pt"><o:p></o:p></span></p>
<p class="xmsonormal">Same question for condition variables?<span style="font-size:12.0pt"><o:p></o:p></span></p>
<p class="xmsonormal"> <span style="font-size:12.0pt"><o:p></o:p></span></p>
<p class="xmsonormal">Thanks,<span style="font-size:12.0pt"><o:p></o:p></span></p>
<p class="xmsonormal"> <span style="font-size:12.0pt"><o:p></o:p></span></p>
<p class="xmsonormal">Matthieu<span style="font-size:12.0pt"><o:p></o:p></span></p>
<p class="xmsonormal"><span lang="EN-US"> </span><span style="font-size:12.0pt"><o:p></o:p></span></p>
</div>
</div>
</div>
</body>
</html>