MPI技术求助:向各从节点发不同数据却均收到相同内容
Troubleshooting MPI Slave Nodes Receiving Identical Data
Let's break down why all your slave ranks are ending up with the same data, even though you're trying to send unique structs to each. Here's a step-by-step plan to diagnose and fix the issue, starting with the master code you shared and covering common slave-side pitfalls (since your slave code is missing):
Master Node Checks
- Reset
positionat the start of each loop iteration: This is the most likely culprit! Your current code doesn't resetpositionto 0 before packing data for each slave. MPI_Pack usespositionto track the next available spot in the buffer—if you don't reset it, subsequent packs will append to the buffer instead of starting fresh. This means later sends might include leftover data from earlier iterations, and if slaves aren't handling the packed buffer correctly, they could end up reading the first entry every time. Fix this by addingposition = 0;at the top of your loop:for (int i = 1; i < slavesCounter; i++){ int position = 0; // Add this line! data = x[cnt]; MPI_Pack(&data.a, 1, MPI_INT, buffer, LENGTH, &position, MPI_COMM_WORLD); MPI_Pack(&data.b, 1, MPI_INT, buffer, LENGTH, &position, MPI_COMM_WORLD); MPI_Send(buffer, position, MPI_PACKED, i, tag, MPI_COMM_WORLD); cnt++; } - Validate
cntinitialization and increment: Double-check thatcntstarts at 0 before the loop runs. Ifcntwas accidentally set to a fixed value (like not being incremented each iteration), you'd send the samedatato every slave. Also confirm that yourxvector has at leastslavesCounter - 1elements (since your loop runs fromi=1toi < slavesCounter). - Ensure buffer sizing is correct: Make sure
LENGTHis large enough to hold the packed data for your struct (twoMPI_INTs plus any MPI packing overhead—2 * sizeof(int)is a safe minimum here). If the buffer is too small, MPI_Pack might overwrite memory, leading to corrupted data that appears identical across slaves.
Slave Node Checks (Common Issues to Look For)
Since your slave code is missing, here are the most frequent mistakes that cause duplicate received data:
- Reset
positionbefore unpacking: Slaves must initializepositionto 0 every time they unpack a new message. If they reuse apositionvalue from a previous unpack, they'll start reading from the wrong offset in the buffer. Your slave code should look something like this:MPI_Status status; char buffer[LENGTH]; // Match master's buffer size struct YourData recv_data; int position = 0; // Critical: Reset this every time you receive MPI_Recv(buffer, LENGTH, MPI_PACKED, 0, tag, MPI_COMM_WORLD, &status); MPI_Unpack(buffer, LENGTH, &position, &recv_data.a, 1, MPI_INT, MPI_COMM_WORLD); MPI_Unpack(buffer, LENGTH, &position, &recv_data.b, 1, MPI_INT, MPI_COMM_WORLD); - Match buffer size between master and slave: The slave's buffer used for
MPI_Recvmust be at least as large as the master'sLENGTHvalue. If it's smaller, the received data will be truncated, leading to incorrect unpacking (and potentially duplicate partial data). - Verify receive parameters: Ensure slaves are calling
MPI_Recvwith the correct source (0for the master) and matchingtagvalue from the master. UsingMPI_ANY_SOURCEor a wrong tag could cause slaves to pick up stale messages from previous runs or other processes, resulting in identical data.
Debugging Tips
- Add print statements in the master loop to log the slave rank (
i),cnt,data.a,data.b, andpositionbefore sending. This confirms you're actually packing unique data for each slave. - In the slave code, print the slave's rank, received
recv_data.a,recv_data.b, andpositionvalues before/after unpacking. This will show if the slave is receiving the wrong data or unpacking from the wrong offset.
内容的提问来源于stack exchange,提问作者aladar90
相关产品推荐
相关产品推荐

