From 9c3447d22e113c6b0b4693f854c7e2de73b0461f Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 30 Oct 2023 06:58:07 +0000 Subject: [PATCH] fucked up --- assignments/02/qs_mpi.c | 161 +++++++++++++++++++++++++++++++--------- 1 file changed, 125 insertions(+), 36 deletions(-) diff --git a/assignments/02/qs_mpi.c b/assignments/02/qs_mpi.c index 715728e..e97d0b1 100644 --- a/assignments/02/qs_mpi.c +++ b/assignments/02/qs_mpi.c @@ -88,52 +88,140 @@ int main(int argc, char **argv) { 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; + // TODO: This doesn't work :skull: substituting it for a slower method until i + // Figure it out tomorrow + if (0) { + // Send it to the correct target + int S_starting_process = S_global_start / n_over_p, + L_starting_process = L_global_start / n_over_p; + int S_offset = S_global_start % n_over_p, + L_offset = L_global_start % n_over_p; - 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); + for (int i = S_lo, dest_pos = S_global_start, + processor = S_starting_process; + i < S_hi;) { + int next_break = MIN(int, dest_pos + (S_hi - S_lo), + (dest_pos / n_over_p) * n_over_p + n_over_p); + int count = next_break - dest_pos; - int total_start = i * n_over_p + processor_start; - int total_end = i * n_over_p + processor_end; + int local_start = i, local_end = i + count; + int dest_start = dest_pos, dest_end = dest_pos + count; - int n_bytes = total_end - total_start; - int local_end = local_start + n_bytes; + printf("[%d] copying from S, local[%d..%d] to dest #%d [%d..%d]\n", rank, + local_start, local_end, processor, dest_start, dest_end); - 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) + int recvbuf[count]; + MPI_Sendrecv(&integers[local_start], count, MPI_INT, processor, 123, + recvbuf, count, MPI_INT, rank, 123, MPI_COMM_WORLD, + MPI_STATUS_IGNORE); - local_start += n_bytes; + i += count; + dest_pos += count; + processor += 1; + } + + for (int i = L_lo, dest_pos = L_global_start, + processor = L_starting_process; + i < L_hi;) { + int next_break = MIN(int, L_global_end, + MIN(int, dest_pos + (L_hi - L_lo), + (dest_pos / n_over_p) * n_over_p + n_over_p)); + int count = next_break - dest_pos; + + int local_start = i, local_end = i + count; + int dest_start = dest_pos, dest_end = dest_pos + count; + + printf("[%d] copying from L, local[%d..%d] to dest #%d [%d..%d]\n", rank, + local_start, local_end, processor, dest_start, dest_end); + + i += count; + dest_pos += count; + processor += 1; + } } - 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); + if (0) { + int *fucked = calloc(sizeof(int), n); - int total_start = i * n_over_p + processor_start; - int total_end = i * n_over_p + processor_end; + // Send it to the correct target + int S_starting_process = S_global_start / n_over_p, + L_starting_process = L_global_start / n_over_p; + int S_offset = S_global_start % n_over_p, + L_offset = L_global_start % n_over_p; - int n_bytes = total_end - total_start; - int local_end = local_start + n_bytes; + for (int i = S_lo, dest_pos = S_global_start, + processor = S_starting_process; + i < S_hi;) { + int next_break = MIN(int, dest_pos + (S_hi - S_lo), + (dest_pos / n_over_p) * n_over_p + n_over_p); + int count = next_break - dest_pos; - 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); + int local_start = i, local_end = i + count; + int dest_start = dest_pos, dest_end = dest_pos + count; + + printf("[%d] copying from S, local[%d..%d] to dest #%d [%d..%d]\n", rank, + local_start, local_end, processor, dest_start, dest_end); + + int recvbuf[count]; + // MPI_Sendrecv(&integers[local_start], count, MPI_INT, processor, 123, + // recvbuf, count, MPI_INT, rank, 123, MPI_COMM_WORLD, + // MPI_STATUS_IGNORE); + for (int j = 0; j < count; ++j) { + fucked[j] = integers[local_start + j]; + } + // MPI_Scatter(&integers[local_start], count, MPI_INT, + // &fucked[dest_start], + // count, MPI_INT, rank, MPI_COMM_WORLD); + // MPI_Bcast(&dest_start, 1, MPI_INT, rank, MPI_COMM_WORLD); + // printf("dest start is %d %d\n", dest_start, count); + // MPI_Bcast(&fucked[dest_start], count, MPI_INT, rank, MPI_COMM_WORLD); + // MPI_Alltoall(&fucked[dest_start], count, MPI_INT, &fucked[dest_start], + // count, MPI_INT, MPI_COMM_WORLD); + // MPI_Allgather(&fucked[dest_start], count, MPI_INT, &fucked[dest_start], + // count, MPI_INT, MPI_COMM_WORLD); + int recvs[p]; + int displs[p]; + MPI_Allgather(&count, 1, MPI_INT, &recvs, 1, MPI_INT, MPI_COMM_WORLD); + displs[0] = 0; + for (int j = 1; j < p; j++) { + displs[j] = displs[j - 1] + recvs[j - 1]; + } + MPI_Allgatherv(&fucked[dest_start], count, MPI_INT, &fucked[dest_start], + &recvs[0], &displs[0], MPI_INT, MPI_COMM_WORLD); + // MPI_Alltoall(&fucked[local_start], count, MPI_INT, + // &fucked[local_start], count, MPI_INT, + // MPI_COMM_WORLD); + + i += count; + dest_pos += count; + processor += 1; + + for (int i = 0; i < n; ++i) { + printf("%d, ", fucked[i]); + } + printf("\n"); + } + + /* + for (int i = L_lo, dest_pos = L_global_start, + processor = L_starting_process; + i < L_hi;) { + int next_break = MIN(int, L_global_end, + MIN(int, dest_pos + (L_hi - L_lo), + (dest_pos / n_over_p) * n_over_p + + n_over_p)); int count = next_break - dest_pos; + + int local_start = i, local_end = i + count; + int dest_start = dest_pos, dest_end = dest_pos + count; + + printf("[%d] copying from L, local[%d..%d] to dest #%d [%d..%d]\n", + rank, local_start, local_end, processor, dest_start, dest_end); + + i += count; + dest_pos += count; + processor += 1; + } + */ } // The first node is responsible for collecting all the data and then printing @@ -146,6 +234,7 @@ int main(int argc, char **argv) { } MPI_Finalize(); + printf("Done.\n"); return 0; }