This commit is contained in:
Michael Zhang 2023-10-31 01:43:43 +00:00
parent f700a2dfd8
commit 6525ef419f

View file

@ -16,6 +16,7 @@
#define MAX(type, x, y) (type) GENERIC_MAX(ENSURE_##type(x), ENSURE_##type(y)) #define MAX(type, x, y) (type) GENERIC_MAX(ENSURE_##type(x), ENSURE_##type(y))
#define MIN(type, x, y) (type) GENERIC_MIN(ENSURE_##type(x), ENSURE_##type(y)) #define MIN(type, x, y) (type) GENERIC_MIN(ENSURE_##type(x), ENSURE_##type(y))
void init_ctl(int *ctl, int len);
void local_quicksort(int *arr, int lo, int hi); void local_quicksort(int *arr, int lo, int hi);
char *string_of_list(int *arr, int len); char *string_of_list(int *arr, int len);
void recursive_quicksort(int *integers, int n, int root, MPI_Comm comm); void recursive_quicksort(int *integers, int n, int root, MPI_Comm comm);
@ -56,14 +57,15 @@ int main(int argc, char **argv) {
// sendcount, MPI_INT, void *recvbuf, // sendcount, MPI_INT, void *recvbuf,
// int recvcount, MPI_INT, 0, MPI_COMM_WORLD); // int recvcount, MPI_INT, 0, MPI_COMM_WORLD);
int recvbuf[n]; int recvbuf[n];
MPI_Allgather(integers, n_over_p, MPI_INT, recvbuf, n_over_p, MPI_INT, MPI_Gather(integers, n_over_p, MPI_INT, recvbuf, n_over_p, MPI_INT, 0,
MPI_COMM_WORLD); MPI_COMM_WORLD);
if (rank == 0) { if (rank == 0) {
FILE *f = fopen(argv[2], "w"); FILE *f = fopen(argv[2], "w");
// printf("integers: %s\n", string_of_list(recvbuf, n)); // printf("integers: %s\n", string_of_list(recvbuf, n));
printf("[%d] ==== FINAL ====\n", rank);
for (int i = 0; i < p; i += 1) { for (int i = 0; i < p; i += 1) {
printf("[%d] integers: %s\n", rank, printf("[%d] %s\n", rank,
string_of_list(&recvbuf[i * n_over_p], n_over_p)); string_of_list(&recvbuf[i * n_over_p], n_over_p));
} }
fclose(f); fclose(f);
@ -217,19 +219,16 @@ void recursive_quicksort(int *integers, int n, int root, MPI_Comm comm) {
int send_displs[p]; int send_displs[p];
int recv_counts[p]; int recv_counts[p];
int recv_displs[p]; int recv_displs[p];
init_ctl(S_ctl, p);
init_ctl(L_ctl, p);
init_ctl(S_send_ctl, p);
init_ctl(L_send_ctl, p);
for (int i = 0; i < p; ++i) { for (int i = 0; i < p; ++i) {
send_counts[i] = n_over_p; send_counts[i] = n_over_p;
send_displs[i] = i * n_over_p; send_displs[i] = i * n_over_p;
S_ctl[i * CTL_SIZE] = 0;
S_ctl[i * CTL_SIZE + 1] = -1;
S_ctl[i * CTL_SIZE + 2] = -1;
S_ctl[i * CTL_SIZE + 3] = -1;
L_ctl[i * CTL_SIZE] = 0;
L_ctl[i * CTL_SIZE + 1] = -1;
L_ctl[i * CTL_SIZE + 2] = -1;
L_ctl[i * CTL_SIZE + 3] = -1;
ctl_send_counts[i] = CTL_SIZE; ctl_send_counts[i] = CTL_SIZE;
ctl_send_displs[i] = i * CTL_SIZE; ctl_send_displs[i] = i * CTL_SIZE;
recv_counts[i] = CTL_SIZE; recv_counts[i] = CTL_SIZE;
@ -238,13 +237,6 @@ void recursive_quicksort(int *integers, int n, int root, MPI_Comm comm) {
// Send S to the correct target // Send S to the correct target
{ {
for (int i = 0; i < p; ++i) {
S_send_ctl[i * CTL_SIZE] = 0;
S_send_ctl[i * CTL_SIZE + 1] = -1;
S_send_ctl[i * CTL_SIZE + 2] = -1;
S_send_ctl[i * CTL_SIZE + 3] = -1;
}
for (int i = S_lo, dest_pos = S_global_start, for (int i = S_lo, dest_pos = S_global_start,
processor = S_starting_process; processor = S_starting_process;
i < S_hi;) { i < S_hi;) {
@ -261,11 +253,11 @@ void recursive_quicksort(int *integers, int n, int root, MPI_Comm comm) {
int to_local_start = to_global_start - processor * n_over_p, int to_local_start = to_global_start - processor * n_over_p,
to_local_end = to_global_end - processor * n_over_p; to_local_end = to_global_end - processor * n_over_p;
printf("[%d] S ->> (count=%d), from local [%d..%d] {%d..%d} -to-> " // printf("[%d] S ->> (count=%d), from local [%d..%d] {%d..%d} -to-> "
"p#%d [%d..%d] {%d..%d}\n", // "p#%d [%d..%d] {%d..%d}\n",
rank, count, from_local_start, from_local_end, from_global_start, // rank, count, from_local_start, from_local_end,
from_global_end, processor, to_local_start, to_local_end, // from_global_start, from_global_end, processor, to_local_start,
to_global_start, to_global_end); // to_local_end, to_global_start, to_global_end);
S_send_ctl[processor * CTL_SIZE] = count; S_send_ctl[processor * CTL_SIZE] = count;
S_send_ctl[processor * CTL_SIZE + 1] = from_global_start; S_send_ctl[processor * CTL_SIZE + 1] = from_global_start;
S_send_ctl[processor * CTL_SIZE + 2] = to_local_start; S_send_ctl[processor * CTL_SIZE + 2] = to_local_start;
@ -282,13 +274,6 @@ void recursive_quicksort(int *integers, int n, int root, MPI_Comm comm) {
// Send L to the correct target // Send L to the correct target
{ {
for (int i = 0; i < p; ++i) {
L_send_ctl[i * CTL_SIZE] = 0;
L_send_ctl[i * CTL_SIZE + 1] = -1;
L_send_ctl[i * CTL_SIZE + 2] = -1;
L_send_ctl[i * CTL_SIZE + 3] = -1;
}
for (int i = L_lo, dest_pos = L_global_start, for (int i = L_lo, dest_pos = L_global_start,
processor = L_starting_process; processor = L_starting_process;
i < L_hi;) { i < L_hi;) {
@ -305,11 +290,11 @@ void recursive_quicksort(int *integers, int n, int root, MPI_Comm comm) {
int to_local_start = to_global_start - processor * n_over_p, int to_local_start = to_global_start - processor * n_over_p,
to_local_end = to_global_end - processor * n_over_p; to_local_end = to_global_end - processor * n_over_p;
printf("[%d] L ->> (count=%d), from local [%d..%d] {%d..%d} -to-> " // printf("[%d] L ->> (count=%d), from local [%d..%d] {%d..%d} -to-> "
"p#%d [%d..%d] {%d..%d}\n", // "p#%d [%d..%d] {%d..%d}\n",
rank, count, from_local_start, from_local_end, from_global_start, // rank, count, from_local_start, from_local_end,
from_global_end, processor, to_local_start, to_local_end, // from_global_start, from_global_end, processor, to_local_start,
to_global_start, to_global_end); // to_local_end, to_global_start, to_global_end);
L_send_ctl[processor * CTL_SIZE] = count; L_send_ctl[processor * CTL_SIZE] = count;
L_send_ctl[processor * CTL_SIZE + 1] = from_global_start; L_send_ctl[processor * CTL_SIZE + 1] = from_global_start;
L_send_ctl[processor * CTL_SIZE + 2] = to_local_start; L_send_ctl[processor * CTL_SIZE + 2] = to_local_start;
@ -364,8 +349,9 @@ void recursive_quicksort(int *integers, int n, int root, MPI_Comm comm) {
continue; continue;
} }
printf("[%d] - S inbound from host %d to [%d..%d] (%d)\n", rank, // printf("[%d] - S inbound from host %d to [%d..%d] (%d)\n", rank,
sender_p, to_local_start, to_local_start + S_count, S_count); // sender_p, to_local_start, to_local_start + S_count,
// S_count);
MPI_Recv(&integers_recv_2[to_local_start], S_count, MPI_INT, sender_p, MPI_Recv(&integers_recv_2[to_local_start], S_count, MPI_INT, sender_p,
124, comm, MPI_STATUS_IGNORE); 124, comm, MPI_STATUS_IGNORE);
for (int k = 0; k < S_count; ++k) { for (int k = 0; k < S_count; ++k) {
@ -380,15 +366,15 @@ void recursive_quicksort(int *integers, int n, int root, MPI_Comm comm) {
int S_count = S_send_ctl[dest_p * CTL_SIZE]; int S_count = S_send_ctl[dest_p * CTL_SIZE];
if (S_count > 0 && dest_p == host_p) { if (S_count > 0 && dest_p == host_p) {
int from_local_start = S_send_ctl[dest_p * CTL_SIZE + 3]; int from_local_start = S_send_ctl[dest_p * CTL_SIZE + 3];
printf("[%d] - S outbound to host %d from [%d..%d] (%d)\n", rank, // printf("[%d] - S outbound to host %d from [%d..%d] (%d)\n", rank,
dest_p, from_local_start, from_local_start + S_count, S_count); // dest_p, from_local_start, from_local_start + S_count,
// S_count);
MPI_Send(&integers[from_local_start], S_count, MPI_INT, dest_p, 124, MPI_Send(&integers[from_local_start], S_count, MPI_INT, dest_p, 124,
comm); comm);
} }
} }
} }
} }
printf("[%d] S done\n", rank);
for (int host_p = 0; host_p < p; ++host_p) { for (int host_p = 0; host_p < p; ++host_p) {
if (rank == host_p) { if (rank == host_p) {
@ -408,8 +394,9 @@ void recursive_quicksort(int *integers, int n, int root, MPI_Comm comm) {
continue; continue;
} }
printf("[%d] - L inbound from host %d to [%d..%d] (%d)\n", rank, // printf("[%d] - L inbound from host %d to [%d..%d] (%d)\n", rank,
sender_p, to_local_start, to_local_start + L_count, L_count); // sender_p, to_local_start, to_local_start + L_count,
// L_count);
MPI_Recv(&integers_recv_2[to_local_start], L_count, MPI_INT, sender_p, MPI_Recv(&integers_recv_2[to_local_start], L_count, MPI_INT, sender_p,
125, comm, MPI_STATUS_IGNORE); 125, comm, MPI_STATUS_IGNORE);
for (int k = 0; k < L_count; ++k) { for (int k = 0; k < L_count; ++k) {
@ -424,49 +411,15 @@ void recursive_quicksort(int *integers, int n, int root, MPI_Comm comm) {
int L_count = L_send_ctl[dest_p * CTL_SIZE]; int L_count = L_send_ctl[dest_p * CTL_SIZE];
if (L_count > 0 && dest_p == host_p) { if (L_count > 0 && dest_p == host_p) {
int from_local_start = L_send_ctl[dest_p * CTL_SIZE + 3]; int from_local_start = L_send_ctl[dest_p * CTL_SIZE + 3];
printf("[%d] - L outbound to host %d from [%d..%d] (%d)\n", rank, // printf("[%d] - L outbound to host %d from [%d..%d] (%d)\n", rank,
dest_p, from_local_start, from_local_start + L_count, L_count); // dest_p, from_local_start, from_local_start + L_count,
// L_count);
MPI_Send(&integers[from_local_start], L_count, MPI_INT, dest_p, 125, MPI_Send(&integers[from_local_start], L_count, MPI_INT, dest_p, 125,
comm); comm);
} }
} }
} }
} }
printf("[%d] L done\n", rank);
// for (int i = 0; i < p; ++i) {
// int count = S_ctl[i * CTL_SIZE];
// int from_global_start = S_ctl[i * CTL_SIZE + 1];
// int to_local_start = S_ctl[i * CTL_SIZE + 2];
// if (count > 0) {
// printf(
// "[%d] <<- S received (%d) from processor %d {%d..%d} to
// [%d..%d]\n", rank, count, i, from_global_start, from_global_start +
// count, to_local_start, to_local_start + count);
// for (int j = 0; j < count; ++j) {
// integers[to_local_start + j] = integers_recv_buf[from_global_start +
// j];
// }
// }
// }
// for (int i = 0; i < p; ++i) {
// int count = L_ctl[i * CTL_SIZE];
// int from_global_start = L_ctl[i * CTL_SIZE + 1];
// int to_local_start = L_ctl[i * CTL_SIZE + 2];
// if (count > 0) {
// printf(
// "[%d] <<- S received (%d) from processor %d {%d..%d} to
// [%d..%d]\n", rank, count, i, from_global_start, from_global_start +
// count, to_local_start, to_local_start + count);
// for (int j = 0; j < count; ++j) {
// integers[to_local_start + j] = integers_recv_buf[from_global_start +
// j];
// }
// }
// }
printf("[%d] after: %s\n", rank, string_of_list(integers_recv_3, n_over_p)); printf("[%d] after: %s\n", rank, string_of_list(integers_recv_3, n_over_p));
for (int i = 0; i < n_over_p; ++i) { for (int i = 0; i < n_over_p; ++i) {
@ -526,4 +479,12 @@ void recursive_quicksort(int *integers, int n, int root, MPI_Comm comm) {
// printf("[%d] Done recursing.\n", rank); // printf("[%d] Done recursing.\n", rank);
// MPI_Comm_free(&child); // MPI_Comm_free(&child);
}
void init_ctl(int *ctl, int len) {
for (int i = 0; i < len; ++i) {
for (int j = 0; j < CTL_SIZE; ++j) {
ctl[i * CTL_SIZE + j] = -1;
}
}
} }