good progress

This commit is contained in:
Michael Zhang 2023-10-30 09:35:04 +00:00
parent fa08589cc7
commit 432f17726c
2 changed files with 110 additions and 55 deletions

View file

@ -21,4 +21,6 @@ for p in sorted(outputs.keys()):
print(f"---- {p} ----") print(f"---- {p} ----")
for line in lines: for line in lines:
print(line) print(line)
print() print()
print("Done.")

View file

@ -18,6 +18,7 @@
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, MPI_Comm comm);
int main(int argc, char **argv) { int main(int argc, char **argv) {
int rank, p; int rank, p;
@ -45,7 +46,80 @@ int main(int argc, char **argv) {
// printf(" - %d\n", integers[i]); // printf(" - %d\n", integers[i]);
} }
int group_root = 0; recursive_quicksort(integers, n, MPI_COMM_WORLD);
sleep(1);
printf("[%d] after: %s\n", rank, string_of_list(integers, n_over_p));
// The first node is responsible for collecting all the data and then
// printing it out to the file MPI_Gather(const void *sendbuf, int
// sendcount, MPI_INT, void *recvbuf,
// int recvcount, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0) {
FILE *f = fopen(argv[2], "w");
fclose(f);
}
MPI_Finalize();
printf("Done.\n");
return 0;
}
// hi not inclusive
void local_quicksort(int *arr, int lo, int hi) {
int temp;
if (lo >= hi || lo < 0)
return;
int pivot = arr[hi - 1];
int pivot_idx = lo - 1;
for (int j = lo; j < hi; ++j) {
if (arr[j] < pivot) {
pivot_idx += 1;
temp = arr[j];
arr[j] = arr[pivot_idx];
arr[pivot_idx] = temp;
}
}
pivot_idx += 1;
temp = arr[hi - 1];
arr[hi - 1] = arr[pivot_idx];
arr[pivot_idx] = temp;
// Recursive call
local_quicksort(arr, lo, pivot_idx);
local_quicksort(arr, pivot_idx + 1, hi);
}
char *string_of_list(int *arr, int len) {
char *buffer = calloc(sizeof(char), 1000);
int offset = 0; // Keep track of the current position in the buffer
for (int i = 0; i < len; i++) {
offset += sprintf(buffer + offset, "%d", arr[i]);
if (i < len - 1) {
// Add a separator (e.g., comma or space) if it's not the last element
offset += sprintf(buffer + offset, " ");
}
}
return buffer;
}
void recursive_quicksort(int *integers, int n, MPI_Comm comm) {
int rank, p;
MPI_Comm_size(comm, &p);
MPI_Comm_rank(comm, &rank);
if (p == 1) {
// Recursion base case: just sort it serially
local_quicksort(integers, 0, n);
return;
}
int n_over_p = n / p;
// Locally sort // Locally sort
// printf("[%d] Numbers before: %s\n", rank, // printf("[%d] Numbers before: %s\n", rank,
@ -62,7 +136,7 @@ int main(int argc, char **argv) {
// Not the real median though, need an existing element of the array // Not the real median though, need an existing element of the array
pivot = integers[n_over_p / 2]; pivot = integers[n_over_p / 2];
MPI_Bcast(&pivot, 1, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(&pivot, 1, MPI_INT, 0, MPI_COMM_WORLD);
printf("--- Broadcasted pivot: %d ---\n", pivot); // printf("--- Broadcasted pivot: %d ---\n", pivot);
// Determine where the boundary between S (lower) and L (higher) lies // Determine where the boundary between S (lower) and L (higher) lies
int boundary; int boundary;
@ -79,10 +153,14 @@ int main(int argc, char **argv) {
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_reverse_end; int S_global_end, L_reverse_end, S_global_max_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_reverse_end, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); MPI_Scan(&L_size, &L_reverse_end, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
// printf("[%d] bruh %d\n", rank, S_global_end);
MPI_Reduce(&S_global_end, &S_global_max_end, 1, MPI_INT, MPI_MAX, 0,
MPI_COMM_WORLD);
int S_global_start = S_global_end - S_size, int S_global_start = S_global_end - S_size,
L_reverse_start = L_reverse_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; L_global_start = n - L_reverse_end, L_global_end = n - L_reverse_start;
@ -253,62 +331,37 @@ int main(int argc, char **argv) {
} }
} }
sleep(1); // Now, determine which processes should be responsible for taking the S and L
printf("[%d] after: %s\n", rank, string_of_list(integers, n_over_p)); // arrays
// The first node is responsible for collecting all the data and then // Specifically, the part where it's split, break the tie to see if it goes
// printing it out to the file MPI_Gather(const void *sendbuf, int // down or up
// sendcount, MPI_INT, void *recvbuf, int colors[p];
// int recvcount, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0) { if (rank == 0) {
FILE *f = fopen(argv[2], "w"); int p_of_split = S_global_max_end / n_over_p;
fclose(f); int split_point = S_global_max_end % n_over_p;
} printf("[%d] shiet %d\n", rank, p_of_split);
MPI_Finalize(); int lo_start = 0, lo_end;
printf("Done.\n"); int hi_start, hi_end = p;
return 0; if (split_point > n_over_p / 2) {
} // Belongs to the lower group
lo_end = hi_start = p_of_split + 1;
} else {
// Belongs to the higher group
lo_end = hi_start = p_of_split;
}
// hi not inclusive for (int i = 0; i < p; ++i) {
void local_quicksort(int *arr, int lo, int hi) { if (i < lo_end)
int temp; colors[i] = 100;
else
if (lo >= hi || lo < 0) colors[i] = 200;
return;
int pivot = arr[hi - 1];
int pivot_idx = lo - 1;
for (int j = lo; j < hi; ++j) {
if (arr[j] < pivot) {
pivot_idx += 1;
temp = arr[j];
arr[j] = arr[pivot_idx];
arr[pivot_idx] = temp;
} }
} }
pivot_idx += 1; MPI_Comm child;
temp = arr[hi - 1]; MPI_Comm_split(comm, colors[rank], rank, &child);
arr[hi - 1] = arr[pivot_idx]; printf("[%d] Recursing...\n", rank);
arr[pivot_idx] = temp; MPI_Comm_free(&child);
// Recursive call
local_quicksort(arr, lo, pivot_idx);
local_quicksort(arr, pivot_idx + 1, hi);
}
char *string_of_list(int *arr, int len) {
char *buffer = calloc(sizeof(char), 1000);
int offset = 0; // Keep track of the current position in the buffer
for (int i = 0; i < len; i++) {
offset += sprintf(buffer + offset, "%d", arr[i]);
if (i < len - 1) {
// Add a separator (e.g., comma or space) if it's not the last element
offset += sprintf(buffer + offset, " ");
}
}
return buffer;
} }