Skip to content

Commit 633ae9b

Browse files
committed
add counter reset and message free
1 parent d8f07b4 commit 633ae9b

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

include/converse.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,8 @@ typedef struct DecrementToEnqueueMsg{
760760

761761
DecrementToEnqueueMsg *CmiCreateDecrementToEnqueue(unsigned int initialCount, void *msg);
762762
void CmiDecrementCounter(DecrementToEnqueueMsg *dteMsg);
763+
void CmiResetCounter(unsigned int newCount, DecrementToEnqueueMsg *dteMsg);
764+
void CmiFreeDecrementToEnqueue(DecrementToEnqueueMsg *dteMsg);
763765

764766
// error checking
765767

src/convcore.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -700,14 +700,35 @@ void CmiDecrementCounter(DecrementToEnqueueMsg *dteMsg){
700700
destPE >= 0 && destPE < Cmi_npes,
701701
"CmiDecrementCounter: destPE out of range"
702702
);
703-
// enqueue the message (node send)
704-
CmiSyncSendAndFree(destPE, header->messageSize, dteMsg->msg);
705-
// free the counter and dteMsg
706-
free(dteMsg->counter);
707-
free(dteMsg);
703+
// enqueue the message without freeing
704+
CmiSyncSend(destPE, header->messageSize, dteMsg->msg);
708705
}
709706
}
710707

708+
void CmiResetCounter(unsigned int newCount, DecrementToEnqueueMsg *dteMsg){
709+
if(dteMsg == nullptr){
710+
CmiAbort("CmiResetCounter: dteMsg is nullptr\n");
711+
}
712+
if(dteMsg->counter == nullptr){
713+
CmiAbort("CmiResetCounter: counter is nullptr\n");
714+
}
715+
if(newCount == 0){
716+
CmiAbort("CmiResetCounter: newCount cannot be zero\n");
717+
}
718+
__atomic_store_n(dteMsg->counter, newCount, __ATOMIC_SEQ_CST);
719+
}
720+
721+
void CmiFreeDecrementToEnqueue(DecrementToEnqueueMsg *dteMsg){
722+
if(dteMsg == nullptr){
723+
CmiAbort("CmiFreeDecrementToEnqueue: dteMsg is nullptr\n");
724+
}
725+
if(dteMsg->counter == nullptr){
726+
CmiAbort("CmiFreeDecrementToEnqueue: counter is nullptr\n");
727+
}
728+
free(dteMsg->counter);
729+
free(dteMsg);
730+
}
731+
711732
void CmiNodeBarrier(void) {
712733
static Barrier nodeBarrier(CmiMyNodeSize());
713734
int64_t ticket = nodeBarrier.arrive();

tests/decrement_bcast/decrement_bcast.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88
#include "converse.h"
99
#include <string.h>
1010

11-
// Handler called when final message is delivered (counter reached zero).
12-
void exit_handler(void *msg) {
13-
CmiPrintf("Exit handler: counter reached zero on PE %d\n", CmiMyPe());
14-
CmiExit(0);
15-
}
16-
1711
// Global pointer to the DecrementToEnqueueMsg created on PE 0. Other PEs do not
1812
// need direct access to its fields; PE 0 will own and use this pointer when
1913
// decrementing. Making it global simplifies the broadcast message.
2014
static DecrementToEnqueueMsg *g_dte = NULL;
2115
static int g_decInvH = -1;
2216

17+
// Handler called when final message is delivered (counter reached zero).
18+
void exit_handler(void *msg) {
19+
CmiPrintf("Exit handler: counter reached zero on PE %d\n", CmiMyPe());
20+
CmiFreeDecrementToEnqueue(g_dte);
21+
CmiExit(0);
22+
}
23+
2324
// Handler that will be invoked on PE0 for each incoming decrement-invoker
2425
// message. It calls CmiDecrementCounter on the global DTE.
2526
void decrement_invoker(void *msg) {

tests/decrement_enqueue/decrement_enqueue.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
#include "converse.h"
55
#include <string.h>
66

7+
DecrementToEnqueueMsg *dte;
8+
79
// Handler invoked when the decrement counter reaches zero. Exits the program.
810
void exit_handler(void *msg) {
9-
// We don't need the message payload; just exit.
11+
// free DecrementToEnqueueMsg
12+
CmiFreeDecrementToEnqueue(dte);
1013
CmiPrintf("Exit handler called, exiting...\n");
1114
CmiExit(0);
1215
}
@@ -32,7 +35,7 @@ void test_start(int argc, char **argv) {
3235
hdr->messageSize = msgSize;
3336

3437
// Create the decrement-to-enqueue helper with initial count 16.
35-
DecrementToEnqueueMsg *dte = CmiCreateDecrementToEnqueue(16u, msg);
38+
dte = CmiCreateDecrementToEnqueue(16u, msg);
3639

3740
// Decrement 16 times; on the 16th call the message will be sent and the
3841
// registered handler will call CmiExit.

0 commit comments

Comments
 (0)