Skip to content

Commit 38397a6

Browse files
committed
Implement MPI parallelization with query distribution and result synchronization, closes #49
1 parent 66652d0 commit 38397a6

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

QPEMPI.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,11 @@ int main(int argc, char *argv[]) {
213213
token = strtok(NULL, ";");
214214
}
215215

216-
// Execute Queries
216+
// Execute Queries - Distribute across MPI ranks
217217
for (int i = 0; i < query_count; i++) {
218-
// Divide queries among ranks
218+
// Round-robin distribution: each rank processes queries where i % size == rank
219219
if (i % size != rank) continue;
220-
221-
// Get the current query and trim whitespace
220+
222221
char *query = trim(queries[i]);
223222
if (!*query) continue;
224223

@@ -283,9 +282,13 @@ int main(int argc, char *argv[]) {
283282
parseFailed = true;
284283
}
285284

286-
// Print results (Rank 0 only)
287-
if (rank == 0) {
288-
printf("Executing Query: %s\n", query);
285+
// Synchronize all ranks before printing to ensure ordered output
286+
MPI_Barrier(MPI_COMM_WORLD);
287+
288+
// Print results in rank order
289+
for (int print_rank = 0; print_rank < size; print_rank++) {
290+
if (rank == print_rank) {
291+
printf("Executing Query: %s\n", query);
289292

290293
if (parseFailed) {
291294
printf("Tokenization failed.\n");
@@ -313,6 +316,8 @@ int main(int argc, char *argv[]) {
313316
fprintf(stderr, "Unsupported command.\n");
314317
}
315318
}
319+
}
320+
MPI_Barrier(MPI_COMM_WORLD);
316321
}
317322

318323
// Cleanup

0 commit comments

Comments
 (0)