改进以下代码:#include <stdio.h> #include <stdlib.h> #include <mpi.h> #define N 4000 #define TAG 0 void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int L[4000], R[4000]; for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; i = 0; j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } void mergeSort(int arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; mergeSort(arr, l, m); mergeSort(arr, m + 1, r); merge(arr, l, m, r); } } int main(int argc, char** argv) { int rank, size; int i, j, k; int A[N], B[N]; int block_size, start, end; double start_time, end_time; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); block_size = N / size; start = rank * block_size; end = start + block_size - 1; if (rank == size - 1) { end = N - 1; } if (rank == 0) { printf("Generating random array...\n"); for (i = 0; i < N; i++) { A[i] = rand() % 100000; } printf("Sorting array...\n"); } MPI_Barrier(MPI_COMM_WORLD); start_time = MPI_Wtime(); MPI_Scatter(A, block_size, MPI_INT, &B[start], block_size, MPI_INT, 0, MPI_COMM_WORLD); mergeSort(B, start, end); for (i = 0; i < size; i++) { if (rank == i) { MPI_Send(&B[start], block_size, MPI_INT, (rank + 1) % size, TAG, MPI_COMM_WORLD); } else if (rank == (i + 1) % size) { MPI_Recv(&B[start], block_size, MPI_INT, i, TAG, MPI_COMM_WORLD, &status); } } MPI_Barrier(MPI_COMM_WORLD); end_time = MPI_Wtime(); if (rank == 0) { printf("Writing result to file...\n"); FILE* fp; errno_t err; err = fopen_s(&fp, "sorted_array.txt", "w"); for (i = 0; i < N; i++) { fprintf(fp, "%d\n", B[i]); } fclose(fp); printf("Done!\n"); printf("Time used: %.6f seconds\n", end_time - start_time); } MPI_Finalize(); return 0; }
时间: 2024-01-16 14:03:17 浏览: 77
以下是改进后的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define N 4000
#define TAG 0
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int *L = (int*)malloc(n1 * sizeof(int));
int *R = (int*)malloc(n2 * sizeof(int));
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
free(L);
free(R);
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main(int argc, char** argv) {
int rank, size;
int i, j, k;
int *A, *B;
int block_size, start, end;
double start_time, end_time;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
block_size = N / size;
start = rank * block_size;
end = start + block_size - 1;
if (rank == size - 1) {
end = N - 1;
}
if (rank == 0) {
printf("Generating random array...\n");
A = (int*)malloc(N * sizeof(int));
for (i = 0; i < N; i++) {
A[i] = rand() % 100000;
}
printf("Sorting array...\n");
}
B = (int*)malloc(block_size * sizeof(int));
MPI_Barrier(MPI_COMM_WORLD);
start_time = MPI_Wtime();
MPI_Scatter(A, block_size, MPI_INT, B, block_size, MPI_INT, 0, MPI_COMM_WORLD);
mergeSort(B, 0, block_size - 1);
for (i = 0; i < size; i++) {
if (rank == i) {
MPI_Send(B, block_size, MPI_INT, (rank + 1) % size, TAG, MPI_COMM_WORLD);
}
else if (rank == (i + 1) % size) {
MPI_Recv(B, block_size, MPI_INT, i, TAG, MPI_COMM_WORLD, &status);
}
}
MPI_Barrier(MPI_COMM_WORLD);
end_time = MPI_Wtime();
if (rank == 0) {
printf("Writing result to file...\n");
FILE* fp;
errno_t err;
err = fopen_s(&fp, "sorted_array.txt", "w");
for (i = 0; i < N; i++) {
fprintf(fp, "%d\n", B[i]);
}
fclose(fp);
free(A);
printf("Done!\n");
printf("Time used: %.6f seconds\n", end_time - start_time);
}
free(B);
MPI_Finalize();
return 0;
}
```
改进点如下:
1. 在 `merge` 函数中使用动态内存分配,避免了栈溢出的问题。
2. 在 `main` 函数中使用动态内存分配,避免了数组过大时栈溢出的问题。
3. 在 `MPI_Scatter` 函数中直接传递数组指针,避免了在发送和接收数据时进行复制的问题。
4. 在 `MPI_Send` 和 `MPI_Recv` 函数中,发送和接收的数据都是 `B` 数组中的一段,因此直接传递 `B` 数组的指针即可。同时,在发送和接收数据时,将 `TAG` 设为 0,以避免与其他消息混淆。
5. 在结束程序前,释放 `A` 和 `B` 数组占用的内存。
阅读全文