This commit is contained in:
Michael Zhang 2023-10-30 03:04:21 +00:00
parent 8533db7eb2
commit 61816ad046

View file

@ -2,6 +2,18 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#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); 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);
@ -62,20 +74,67 @@ int main(int argc, char **argv) {
int S_lo = 0, S_hi = boundary - 1; int S_lo = 0, S_hi = boundary - 1;
int L_lo = boundary, L_hi = n_over_p - 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; 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, printf("[%d] S: [%d - %d] (%d), L: [%d - %d] (%d)\n", rank, S_lo, S_hi,
// S_size, L_lo, L_hi, L_size); S_size, L_lo, L_hi, L_size);
// Perform global arrangement // 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(&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, 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, printf("[%d] S: [%d - %d], L: [%d - %d]\n", rank, S_global_start,
S_global_end - 1, L_global_start, L_global_end - 1); S_global_end - 1, L_global_start, L_global_end - 1);
// Send it to the correct target // 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 // The first node is responsible for collecting all the data and then printing
// it out to the file // it out to the file