From 61816ad0464e578876e4b8074bd4932707c1d92a Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 30 Oct 2023 03:04:21 +0000 Subject: [PATCH] L --- assignments/02/qs_mpi.c | 69 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 5 deletions(-) diff --git a/assignments/02/qs_mpi.c b/assignments/02/qs_mpi.c index 01a66fd..715728e 100644 --- a/assignments/02/qs_mpi.c +++ b/assignments/02/qs_mpi.c @@ -2,6 +2,18 @@ #include #include +#define ORDER_FORWARDS 1 +#define ORDER_BACKWARDS 2 + +#define GENERIC_MAX(x, y) ((x) > (y) ? (x) : (y)) +#define GENERIC_MIN(x, y) ((x) < (y) ? (x) : (y)) + +#define ENSURE_int(i) _Generic((i), int : (i)) +#define ENSURE_float(f) _Generic((f), float : (f)) + +#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)) + void local_quicksort(int *arr, int lo, int hi); char *string_of_list(int *arr, int len); @@ -62,20 +74,67 @@ int main(int argc, char **argv) { int S_lo = 0, S_hi = boundary - 1; int L_lo = boundary, L_hi = n_over_p - 1; int S_size = S_hi - S_lo + 1, L_size = L_hi - L_lo + 1; - // printf("[%d] S: [%d - %d] (%d), L: [%d - %d] (%d)\n", rank, S_lo, S_hi, - // S_size, L_lo, L_hi, L_size); + printf("[%d] S: [%d - %d] (%d), L: [%d - %d] (%d)\n", rank, S_lo, S_hi, + S_size, L_lo, L_hi, L_size); // Perform global arrangement - int S_global_end, L_global_end; + int S_global_end, L_reverse_end; MPI_Scan(&S_size, &S_global_end, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); - MPI_Scan(&L_size, &L_global_end, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); + MPI_Scan(&L_size, &L_reverse_end, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); int S_global_start = S_global_end - S_size, - L_global_start = L_global_end - L_size; + L_reverse_start = L_reverse_end - L_size, + L_global_start = n - L_reverse_end, L_global_end = n - L_reverse_start; printf("[%d] S: [%d - %d], L: [%d - %d]\n", rank, S_global_start, S_global_end - 1, L_global_start, L_global_end - 1); // Send it to the correct target + int S_starting_process = S_global_start / n_over_p; + int S_offset = S_global_start % n_over_p, + L_offset = L_global_start % n_over_p; + int i, local_start; + + for (i = S_starting_process, local_start = 0; i * n_over_p < S_global_end; + ++i) { + int processor_start = S_offset; + int mod = S_global_end % n_over_p; + int processor_end = MIN(int, n_over_p, mod == 0 ? n_over_p : mod); + + int total_start = i * n_over_p + processor_start; + int total_end = i * n_over_p + processor_end; + + int n_bytes = total_end - total_start; + int local_end = local_start + n_bytes; + + printf("[%d] - sending %d elements from local S [%d..%d] to destination S " + "[%d..%d]\n", + rank, n_bytes, local_start, local_end, processor_start, + processor_end); + // MPI_Sendrecv(const void *sendbuf, int sendcount, MPI_Datatype sendtype, + // int dest, int sendtag, void *recvbuf, int recvcount, + // MPI_Datatype recvtype, int source, int recvtag, MPI_Comm + // comm, MPI_Status *status) + + local_start += n_bytes; + } + + int L_starting_process = L_global_start / n_over_p; + for (i = L_starting_process; i * n_over_p < L_global_end; ++i) { + int processor_start = L_offset; + int mod = L_global_end % n_over_p; + int processor_end = MIN(int, n_over_p, mod == 0 ? n_over_p : mod); + + int total_start = i * n_over_p + processor_start; + int total_end = i * n_over_p + processor_end; + + int n_bytes = total_end - total_start; + int local_end = local_start + n_bytes; + + printf("[%d] - sending %d elements from local L [%d..%d] to destination L " + "[%d..%d]\n", + rank, n_bytes, local_start, local_end, processor_start, + processor_end); + } // The first node is responsible for collecting all the data and then printing // it out to the file