diff --git a/assignments/02/qs_mpi.c b/assignments/02/qs_mpi.c index e97d0b1..ce84d06 100644 --- a/assignments/02/qs_mpi.c +++ b/assignments/02/qs_mpi.c @@ -88,61 +88,15 @@ 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); - // 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 (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 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); - - i += count; - dest_pos += count; - processor += 1; + { + int recvpart[n_over_p]; + int send_counts[p]; + int send_displs[p]; + for (int i = 0; i < p; ++i) { + send_counts[i] = 0; + send_displs[i] = 0; } - 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; - } - } - - if (0) { - int *fucked = calloc(sizeof(int), n); - // 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; @@ -161,67 +115,29 @@ int main(int argc, char **argv) { printf("[%d] copying from S, local[%d..%d] to dest #%d [%d..%d]\n", rank, local_start, local_end, processor, dest_start, dest_end); + send_counts[processor] = count; - int recvbuf[count]; + // 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; + for (int i = 1; i < p; ++i) { + send_displs[i] = send_displs[i - 1] + send_counts[i - 1]; + } - int local_start = i, local_end = i + count; - int dest_start = dest_pos, dest_end = dest_pos + count; + // MPI_Alltoallv(integers, send_counts, send_displs, MPI_INT, recvpart, + // const int *recvcounts, const int *rdispls, MPI_Datatype recvtype, + // MPI_Comm comm) - 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; - } - */ + printf("[%d] send_counts: %s\n", rank, string_of_list(send_counts, p)); + printf("[%d] send_displs: %s\n", rank, string_of_list(send_displs, p)); + printf("[%d] after: %s\n", rank, string_of_list(recvpart, n_over_p)); } // The first node is responsible for collecting all the data and then printing