捕获信号 11(分段错误:地址未映射到地址 (nil) 处的对象
我正在尝试使用reduce命令计算数组A的总和;请注意,A 仅对主节点/根 (0) 可见。
我收到以下错误,但我似乎无法弄清楚为什么。此外,广播部分 (N) 仍然会产生相同的错误。
错误:
[kali:74924:0:74924] Caught signal 11 (Segmentation fault: address not mapped to object at address (nil))
==== backtrace (tid: 74924) ====
0 /lib/x86_64-linux-gnu/libucs.so.0(ucs_handle_error+0x2dc) [0x7f14b5486a9c]
1 /lib/x86_64-linux-gnu/libucs.so.0(+0x28c8f) [0x7f14b5486c8f]
2 /lib/x86_64-linux-gnu/libucs.so.0(+0x28e4a) [0x7f14b5486e4a]
3 /lib/x86_64-linux-gnu/libc.so.6(+0x3c910) [0x7f14b564e910]
4 ./parts(+0x14f1) [0x557de43984f1]
5 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xcd) [0x7f14b56397ed]
6 ./parts(+0x113a) [0x557de439813a]
=================================
[kali:74925:0:74925] Caught signal 11 (Segmentation fault: address not mapped to object at address (nil))
==== backtrace (tid: 74925) ====
0 /lib/x86_64-linux-gnu/libucs.so.0(ucs_handle_error+0x2dc) [0x7fb3324b0a9c]
1 /lib/x86_64-linux-gnu/libucs.so.0(+0x28c8f) [0x7fb3324b0c8f]
2 /lib/x86_64-linux-gnu/libucs.so.0(+0x28e4a) [0x7fb3324b0e4a]
3 /lib/x86_64-linux-gnu/libc.so.6(+0x3c910) [0x7fb332678910]
4 ./parts(+0x14f1) [0x5581e42d44f1]
5 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xcd) [0x7fb3326637ed]
6 ./parts(+0x113a) [0x5581e42d413a]
=================================
[kali:74926:0:74926] Caught signal 11 (Segmentation fault: address not mapped to object at address (nil))
==== backtrace (tid: 74926) ====
0 /lib/x86_64-linux-gnu/libucs.so.0(ucs_handle_error+0x2dc) [0x7f7e8e8f9a9c]
1 /lib/x86_64-linux-gnu/libucs.so.0(+0x28c8f) [0x7f7e8e8f9c8f]
2 /lib/x86_64-linux-gnu/libucs.so.0(+0x28e4a) [0x7f7e8e8f9e4a]
3 /lib/x86_64-linux-gnu/libc.so.6(+0x3c910) [0x7f7e8eac1910]
4 ./parts(+0x14f1) [0x558b09e094f1]
5 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xcd) [0x7f7e8eaac7ed]
6 ./parts(+0x113a) [0x558b09e0913a]
=================================
===================================================================================
= BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
= PID 74924 RUNNING AT kali
= EXIT CODE: 11
= CLEANING UP REMAINING PROCESSES
= YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
===================================================================================
YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault (signal 11)
This typically refers to a problem with your application.
Please see the FAQ page for debugging suggestions
代码:
不带广播
#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
#include <time.h>
int main (int argc, char** argv) {
int rank;
int size;
int sum = 0;
int grand_sum = 0;
int i;
int *A;
int N;
int part;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank == 0) {
do {
printf("Enter the array size: ");
scanf("%d", &N);
} while (N <= 0);
A = malloc(N * sizeof(int));
if (!A) {
printf("Array too big!\nExiting the program...\n");
return -1;
}
part = N / size;
srand(10);
for (i = 0; i < N; i++) {
A[i] = rand() % 10 + 1;
grand_sum += A[i];
printf("A[%d] = %d\n", i, A[i]);
}
for (i = 1; i < size; i++) {
MPI_Send(&part, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
MPI_Send(&A[i * part], part, MPI_INT, i, 0, MPI_COMM_WORLD);
}
} else {
int part;
MPI_Recv(&part, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
int Aw[part];
MPI_Recv(&Aw, part, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for (i = 0; i < part; i++) {
sum += A[i];
}
}
MPI_Reduce(&sum, &grand_sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (rank == 0) {
for (i = size * part; i < N; i++) {
grand_sum += A[i];
}
printf("\nThe grand sum is: %d", grand_sum);
}
MPI_Finalize();
}
带广播
#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
#include <time.h>
int main (int argc, char** argv) {
int rank;
int size;
int sum = 0;
int grand_sum = 0;
int i;
int *A;
int N;
int part;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank == 0) {
do {
printf("Enter the array size: ");
scanf("%d", &N);
} while (N <= 0);
A = malloc(N * sizeof(int));
if (!A) {
printf("Array too big!\nExiting the program...\n");
return -1;
}
part = N / size;
srand(10);
for (i = 0; i < N; i++) {
A[i] = rand() % 10 + 1;
printf("A[%d] = %d\n", i, A[i]);
}
for (i = 1; i < size; i++) {
//MPI_Send(&part, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
MPI_Send(&A[i * part], part, MPI_INT, i, 0, MPI_COMM_WORLD);
}
}
MPI_Bcast(&part, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (rank != 0) {
//MPI_Recv(&part, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
int Aw[part];
MPI_Recv(&Aw, part, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for (i = 0; i < part; i++) {
sum += A[i];
}
}
MPI_Reduce(&sum, &grand_sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (rank == 0) {
for (i = size * part; i < N; i++) {
grand_sum += A[i];
}
printf("\nThe grand sum is: %d", grand_sum);
}
MPI_Finalize();
}
I am trying to calculate the sum of an array A using the reduce command; note here that A is only visible to the master node/root (0).
I am getting the following error and I can't seem to figure out why. Also, broadcasting the part (N) still produces the same error.
Error:
[kali:74924:0:74924] Caught signal 11 (Segmentation fault: address not mapped to object at address (nil))
==== backtrace (tid: 74924) ====
0 /lib/x86_64-linux-gnu/libucs.so.0(ucs_handle_error+0x2dc) [0x7f14b5486a9c]
1 /lib/x86_64-linux-gnu/libucs.so.0(+0x28c8f) [0x7f14b5486c8f]
2 /lib/x86_64-linux-gnu/libucs.so.0(+0x28e4a) [0x7f14b5486e4a]
3 /lib/x86_64-linux-gnu/libc.so.6(+0x3c910) [0x7f14b564e910]
4 ./parts(+0x14f1) [0x557de43984f1]
5 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xcd) [0x7f14b56397ed]
6 ./parts(+0x113a) [0x557de439813a]
=================================
[kali:74925:0:74925] Caught signal 11 (Segmentation fault: address not mapped to object at address (nil))
==== backtrace (tid: 74925) ====
0 /lib/x86_64-linux-gnu/libucs.so.0(ucs_handle_error+0x2dc) [0x7fb3324b0a9c]
1 /lib/x86_64-linux-gnu/libucs.so.0(+0x28c8f) [0x7fb3324b0c8f]
2 /lib/x86_64-linux-gnu/libucs.so.0(+0x28e4a) [0x7fb3324b0e4a]
3 /lib/x86_64-linux-gnu/libc.so.6(+0x3c910) [0x7fb332678910]
4 ./parts(+0x14f1) [0x5581e42d44f1]
5 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xcd) [0x7fb3326637ed]
6 ./parts(+0x113a) [0x5581e42d413a]
=================================
[kali:74926:0:74926] Caught signal 11 (Segmentation fault: address not mapped to object at address (nil))
==== backtrace (tid: 74926) ====
0 /lib/x86_64-linux-gnu/libucs.so.0(ucs_handle_error+0x2dc) [0x7f7e8e8f9a9c]
1 /lib/x86_64-linux-gnu/libucs.so.0(+0x28c8f) [0x7f7e8e8f9c8f]
2 /lib/x86_64-linux-gnu/libucs.so.0(+0x28e4a) [0x7f7e8e8f9e4a]
3 /lib/x86_64-linux-gnu/libc.so.6(+0x3c910) [0x7f7e8eac1910]
4 ./parts(+0x14f1) [0x558b09e094f1]
5 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xcd) [0x7f7e8eaac7ed]
6 ./parts(+0x113a) [0x558b09e0913a]
=================================
===================================================================================
= BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
= PID 74924 RUNNING AT kali
= EXIT CODE: 11
= CLEANING UP REMAINING PROCESSES
= YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
===================================================================================
YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault (signal 11)
This typically refers to a problem with your application.
Please see the FAQ page for debugging suggestions
Codes:
w/out broadcast
#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
#include <time.h>
int main (int argc, char** argv) {
int rank;
int size;
int sum = 0;
int grand_sum = 0;
int i;
int *A;
int N;
int part;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank == 0) {
do {
printf("Enter the array size: ");
scanf("%d", &N);
} while (N <= 0);
A = malloc(N * sizeof(int));
if (!A) {
printf("Array too big!\nExiting the program...\n");
return -1;
}
part = N / size;
srand(10);
for (i = 0; i < N; i++) {
A[i] = rand() % 10 + 1;
grand_sum += A[i];
printf("A[%d] = %d\n", i, A[i]);
}
for (i = 1; i < size; i++) {
MPI_Send(&part, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
MPI_Send(&A[i * part], part, MPI_INT, i, 0, MPI_COMM_WORLD);
}
} else {
int part;
MPI_Recv(&part, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
int Aw[part];
MPI_Recv(&Aw, part, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for (i = 0; i < part; i++) {
sum += A[i];
}
}
MPI_Reduce(&sum, &grand_sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (rank == 0) {
for (i = size * part; i < N; i++) {
grand_sum += A[i];
}
printf("\nThe grand sum is: %d", grand_sum);
}
MPI_Finalize();
}
w/broadcast
#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
#include <time.h>
int main (int argc, char** argv) {
int rank;
int size;
int sum = 0;
int grand_sum = 0;
int i;
int *A;
int N;
int part;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank == 0) {
do {
printf("Enter the array size: ");
scanf("%d", &N);
} while (N <= 0);
A = malloc(N * sizeof(int));
if (!A) {
printf("Array too big!\nExiting the program...\n");
return -1;
}
part = N / size;
srand(10);
for (i = 0; i < N; i++) {
A[i] = rand() % 10 + 1;
printf("A[%d] = %d\n", i, A[i]);
}
for (i = 1; i < size; i++) {
//MPI_Send(&part, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
MPI_Send(&A[i * part], part, MPI_INT, i, 0, MPI_COMM_WORLD);
}
}
MPI_Bcast(&part, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (rank != 0) {
//MPI_Recv(&part, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
int Aw[part];
MPI_Recv(&Aw, part, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for (i = 0; i < part; i++) {
sum += A[i];
}
}
MPI_Reduce(&sum, &grand_sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (rank == 0) {
for (i = size * part; i < N; i++) {
grand_sum += A[i];
}
printf("\nThe grand sum is: %d", grand_sum);
}
MPI_Finalize();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重新阅读我的代码后,我在
else
语句的for
循环中发现了一个错误:A 在这里不可见;因此出现错误。这里应该使用Aw。
正确代码:
After rereading my code, I've spotted a mistake at the
for
loop, of theelse
statement:A is not visible here; thus the error. Aw should have been used here instead.
Correct code: