if (rank == 0) { double* p = new double[N]; for (int i = 1; i < p; i++) 该代码的操作数类型不接荣
时间: 2024-04-30 19:21:56 浏览: 23
该代码的错误在于第5行的循环条件,应该是 `i < N` 而不是 `i < p`。因为 `p` 是一个指向 `double` 类型数组的指针,不能用来比较。正确的代码应该是:
```c++
if (rank == 0) {
double* p = new double[N];
for (int i = 0; i < N; i++) {
// do something with p[i]
}
}
```
在循环中,应该使用 `i` 来访问数组 `p` 中的元素。
相关问题
改进以下代码:#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; }
以下是改进后的代码:
```
#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` 数组占用的内存。
修改以下代码:#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <math.h> #define MAX_THREADS 8 double a, b, h; int n, thread_count; double total_sum = 0; void* Trap(void* rank) { long my_rank = (long) rank; double local_a = a + my_rank * n * h / thread_count; double local_b = local_a + n * h / thread_count; double local_sum = (pow(local_a, 2) + pow(local_b, 2)) / 2.0; for (int i = 1; i < n / thread_count; i++) { double x = local_a + i * h; local_sum += pow(x, 2); } local_sum *= h; total_sum += local_sum; return NULL; } int main(int argc, char* argv[]) { long thread; pthread_t* thread_handles; a = 0.0; b = 1.0; n = 1000000; thread_count = (int) strtol(argv[1], NULL, 10); h = (b - a) / n; thread_handles = (pthread_t*) malloc(thread_count * sizeof(pthread_t)); for (thread = 0; thread < thread_count; thread++) { pthread_create(&thread_handles[thread], NULL, Trap, (void*) thread); } for (thread = 0; thread < thread_count; thread++) { pthread_join(thread_handles[thread], NULL); } printf("With n = %d trapezoids, our estimate\n", n); printf("of the integral from %f to %f = %.15f\n", a, b, total_sum); free(thread_handles); return 0; }
以下是修改后的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
#define MAX_THREADS 8
double a, b, h;
int n, thread_count;
double total_sum = 0;
pthread_mutex_t mutex; // 新增互斥锁
void* Trap(void* rank) {
long my_rank = (long) rank;
double local_a = a + my_rank * n * h / thread_count;
double local_b = local_a + n * h / thread_count;
double local_sum = (pow(local_a, 2) + pow(local_b, 2)) / 2.0;
for (int i = 1; i < n / thread_count; i++) {
double x = local_a + i * h;
local_sum += pow(x, 2);
}
local_sum *= h;
pthread_mutex_lock(&mutex); // 加锁
total_sum += local_sum;
pthread_mutex_unlock(&mutex); // 解锁
return NULL;
}
int main(int argc, char* argv[]) {
long thread;
pthread_t* thread_handles;
a = 0.0;
b = 1.0;
n = 1000000;
thread_count = (int) strtol(argv[1], NULL, 10);
h = (b - a) / n;
thread_handles = (pthread_t*) malloc(thread_count * sizeof(pthread_t));
pthread_mutex_init(&mutex, NULL); // 初始化互斥锁
for (thread = 0; thread < thread_count; thread++) {
pthread_create(&thread_handles[thread], NULL, Trap, (void*) thread);
}
for (thread = 0; thread < thread_count; thread++) {
pthread_join(thread_handles[thread], NULL);
}
printf("With n = %d trapezoids, our estimate\n", n);
printf("of the integral from %f to %f = %.15f\n", a, b, total_sum);
free(thread_handles);
pthread_mutex_destroy(&mutex); // 销毁互斥锁
return 0;
}
```
修改说明:
1. 新增了一个互斥锁 `pthread_mutex_t mutex`,以保证多个线程同时修改 `total_sum` 时不会出现竞争问题。
2. 在 `Trap` 函数里面,将 `total_sum += local_sum` 的操作放在加锁和解锁之间,以保证只有一个线程可以修改 `total_sum`。
3. 在 `main` 函数中,新增了互斥锁的初始化 `pthread_mutex_init(&mutex, NULL)` 和销毁 `pthread_mutex_destroy(&mutex)` 操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)