[argobots-discuss] Can I move a static mutex around in memory?

Iwasaki, Shintaro siwasaki at anl.gov
Tue Jun 15 10:46:49 CDT 2021

Hi Matthieu,

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?
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.

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 ABT_MUTEX_INITIALIZER, though I cannot come up with a good example that can cause such.

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.
1. When std::vector<ABT_mutex_memory> is resized, none of them are "locked" (implied by "no ULT cares about m1 anymore")
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").

The code should look like this:
std::vector<ABT_mutex_memory> mutex_mems;
for (int i = 0; i < mutex_mems.size(); i++) {
    // Clean up mutex_mems.  ABT_mutex_free is not needed to release statically allocated ABT_mutex.
    const ABT_mutex_memory mutex_mem_init = ABT_MUTEX_INITIALIZER;
    memcpy(&mutex_mems[i], &mutex_mem_init, sizeof(ABT_mutex_memory));
If this memcpy() looks awkward, Argobots can provide a single-line macro.

I would appreciate any suggestions!


From: Dorier, Matthieu <mdorier at anl.gov>
Sent: Tuesday, June 15, 2021 10:09 AM
To: Iwasaki, Shintaro <siwasaki at anl.gov>; discuss at argobots.org <discuss at argobots.org>; discuss at lists.argobots.org <discuss at lists.argobots.org>
Subject: Re: Can I move a static mutex around in memory?

Hi Shintaro,

Thank you for your answer.

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?

Maybe other objects in Argobots (such as an ES) are keeping pointers to mutexes, and these pointers would be invalidated?



From: "Iwasaki, Shintaro" <siwasaki at anl.gov>
Date: Tuesday, 15 June 2021 at 14:57
To: "discuss at argobots.org" <discuss at argobots.org>, "discuss at lists.argobots.org" <discuss at lists.argobots.org>
Cc: "Dorier, Matthieu" <mdorier at anl.gov>
Subject: Re: Can I move a static mutex around in memory?

Hi Matthieu,

Thanks for the question!

ABT_mutex -> Yes, it's internally just a pointer (more specifically, a handle), so you can copy it.

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.


The same thing goes to ABT_cond_memory: do not move ABT_cond_memory around.


ABT_mutex_memory m1 = ABT_MUTEX_INITIALIZER;


ABT_mutex_memory m2;

memcpy(&m2, &m1, sizeof(ABT_mutex_memory));

// m2 is locked since m1 is locked. The mutex "entity" is copied, so even if you unlock m2, m1 is not unlocked.

// It can cause something even worse since this mutex entity may store the waiters or even a pointer to its own memory.

// Do not use ABT_mutex_memory in this way.


In your case, reallocation on resizing std::vector can break the mutex data (even if ABT_mutex_memory is somehow moved "uniquely" ).

Please dynamically allocate each ABT_mutex_memory individually (std::vector<ABT_mutex_memory *>).




From: Dorier, Matthieu via discuss <discuss at lists.argobots.org>
Sent: Tuesday, June 15, 2021 7:04 AM
To: discuss at argobots.org <discuss at argobots.org>
Cc: Dorier, Matthieu <mdorier at anl.gov>
Subject: [argobots-discuss] Can I move a static mutex around in memory?


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)?

This would come up in C++ containers, for instance:


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?

Same question for condition variables?



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.argobots.org/pipermail/discuss/attachments/20210615/fd4cd4f2/attachment-0003.html>

More information about the discuss mailing list