|
| 1 | +// Simple reconverse test for CmiCreateDecrementToEnqueue / CmiDecrementCounter |
| 2 | +// Intended to run with 1 PE on 1 process. |
| 3 | + |
| 4 | +#include "converse.h" |
| 5 | +#include <string.h> |
| 6 | + |
| 7 | +// Handler invoked when the decrement counter reaches zero. Exits the program. |
| 8 | +void exit_handler(void *msg) { |
| 9 | + // We don't need the message payload; just exit. |
| 10 | + CmiPrintf("Exit handler called, exiting...\n"); |
| 11 | + CmiExit(0); |
| 12 | +} |
| 13 | + |
| 14 | +// Start function invoked by ConverseInit. |
| 15 | +void test_start(int argc, char **argv) { |
| 16 | + CmiPrintf("Starting decrement_enqueue test...\n"); |
| 17 | + // Only PE 0 will create and drive the counter per the test spec. |
| 18 | + if (CmiMyPe() != 0) return; |
| 19 | + |
| 20 | + int handler = CmiRegisterHandler((CmiHandler)exit_handler); |
| 21 | + |
| 22 | + // Create a minimal message consisting only of the message header. |
| 23 | + int msgSize = (int)sizeof(CmiMessageHeader); |
| 24 | + void *msg = CmiAlloc(msgSize); |
| 25 | + memset(msg, 0, msgSize); |
| 26 | + |
| 27 | + // Set handler, destination and size on the header so CmiDecrementCounter |
| 28 | + // can inspect them when it enqueues the message. |
| 29 | + CmiSetHandler(msg, handler); |
| 30 | + CmiMessageHeader *hdr = (CmiMessageHeader *)msg; |
| 31 | + hdr->destPE = (CmiUInt4)CmiMyPe(); |
| 32 | + hdr->messageSize = msgSize; |
| 33 | + |
| 34 | + // Create the decrement-to-enqueue helper with initial count 16. |
| 35 | + DecrementToEnqueueMsg *dte = CmiCreateDecrementToEnqueue(16u, msg); |
| 36 | + |
| 37 | + // Decrement 16 times; on the 16th call the message will be sent and the |
| 38 | + // registered handler will call CmiExit. |
| 39 | + for (int i = 0; i < 16; ++i) { |
| 40 | + CmiDecrementCounter(dte); |
| 41 | + } |
| 42 | + |
| 43 | + // Return from start; scheduler will run and the exit handler will stop it. |
| 44 | +} |
| 45 | + |
| 46 | +int main(int argc, char **argv) { |
| 47 | + // Start the Converse runtime with our test_start function. |
| 48 | + ConverseInit(argc, argv, test_start, 0, 0); |
| 49 | + return 0; |
| 50 | +} |
0 commit comments