gpt4 book ai didi

捕获信号 11(段错误 : address not mapped to object at address (nil))

转载 作者:行者123 更新时间:2023-12-05 05:47:06 25 4
gpt4 key购买 nike

我现在正在使用 MPI 练习简单的并行编程。该代码旨在通过随机生成 N*N 矩阵并使用简单的邻域加权平均滤波器来模拟图像处理,而不处理第一行和最后一行和列。我在编译时没有出错,但在运行时出现了一些我无法弄清楚的错误。请帮忙!谢谢!这是下面的代码:

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int rank, size; // for storing this process' rank, and the number of processes
int i, j; //misc
int rec_buf[100]; // buffer where the received data should be stored
void initialize_data (int **, int );
void distribute_data (int **, int );
void mask_operation (int **, int , int**);
void collect_results (int **, int );

/* ttype: type to use for representing time */
typedef double ttype;
ttype tdiff(struct timespec a, struct timespec b)
/* Find the time difference. */
{
ttype dt = (( b.tv_sec - a.tv_sec ) + ( b.tv_nsec - a.tv_nsec ) / 1E9);
return dt;
}

struct timespec now()
/* Return the current time. */
{
struct timespec t;
clock_gettime(CLOCK_REALTIME, &t);
return t;
}

//clock_t begin, end;
struct timespec begin, end;
double time_spent;

int main(int argc, char **argv)
{
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

int N = atof(argv[1]);
int A[N][N];
int Ap[N][N];
begin = now();

initialize_data(A, N);
distribute_data (A, N); // use scatterv
mask_operation(A, N, Ap);
collect_results(Ap, N); // use gatherv

end = now();
time_spent = tdiff(begin, end);
printf("Total Spent Time: %.8f sec.\n", time_spent);

MPI_Finalize();
return 0;
}

/*----------------------------------------------------*/
void initialize_data (int **A, int N){
//generating the random matrix
for(i=0;i<N;i++)
for(j=0;j<N;j++)
A[i][j]=rand()%256;

//print test for verify random
/*
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%d\t", A[i][j]);
printf("\n");
} */
}
void distribute_data (int **A, int N){
int rem = (N-2)%size; // rows remaining after division among processes
int *sendcounts = malloc(sizeof(int)*size);
// array describing how many elements to send to each process
int *displs = malloc(sizeof(int)*size);
// array describing the displacements where each segment begins

// calculate send counts and displacements & print for each process
for (i = 0; i < size; i++) {
int sum = 0;
sendcounts[i] = ((int)((N-2)/size))*N; //to guarantee all rows will be calculated except the first and the last one
if (rem > 0) {
sendcounts[i] = sendcounts[i] + N;
rem--;
}
displs[i] = sum;
sum += sendcounts[i];
sendcounts[i] = sendcounts[i] + 2*N; //to add the local first and last row
//printf("sendcounts[%d] = %d\tdispls[%d] = %d\n", i, sendcounts[i], i, displs[i]);
}

// divide the data among processes as described by sendcounts and displs
MPI_Scatterv(&A, sendcounts, displs, MPI_INT, &rec_buf, 100, MPI_INT, 0, MPI_COMM_WORLD);

// print what each process received
printf("%d: ", rank);
for (i = 0; i < sendcounts[rank]; i++) {
printf("%d\t", rec_buf[i]);
}
printf("\n");
}
void mask_operation (int **A, int N, int **Ap){
int local_i, local_j; //local row and column 5*5, misc
int local_row_number = sizeof(A) / sizeof(int) / N;

for(i = 0; i < local_row_number-2; i++){
for(j = 0; j < N-2; j++){
if(rank == 0 && i ==0)
Ap[i][j] = A[i][j];
if(rank == size-1 && i ==N-3)
Ap[i+2][j] = A[i+2][j];
if(j == 0)
Ap[i][j] = A[i][j];
if(j==local_row_number-3)
Ap[i][j+2] = A[i][j+2];

for(local_j = 0; local_j < 5; local_j++){
for(local_i = 0; local_i < 5; local_i++){
Ap[i + local_i + 1][j + local_j + 1] = (Ap[i + local_i][j + local_j] + Ap[i + local_i][j + local_j + 1] + Ap[i + local_i][j + local_j + 2] + Ap[i + local_i + 1][j + local_j] + 2 * Ap[i + local_i + 1][j + local_j + 1] + Ap[i + local_i + 1][j + local_j + 2] + Ap[i + local_i + 2][j + local_j] + Ap[i + local_i + 2][j + local_j + 1] + Ap[i + local_i + 2][j + local_j + 2]) / 10;
}
}
}
}
}
void collect_results (int **Ap, int N){
int rem = (N-2)%size; // rows remaining after division among processes
int *sendcounts = malloc(sizeof(int)*size);
// array describing how many elements to send to each process
int *displs = malloc(sizeof(int)*size);
// array describing the displacements where each segment begins

// calculate send counts and displacements & print for each process
for (i = 0; i < size; i++) {
int sum = 0;
sendcounts[i] = ((int)((N-2)/size))*N; //to guarantee all rows will be calculated except the first and the last one
if (rem > 0) {
sendcounts[i] = sendcounts[i] + N;
rem--;
}
displs[i] = sum;
sum += sendcounts[i];
sendcounts[i] = sendcounts[i] + 2*N; //to add the local first and last row
//printf("sendcounts[%d] = %d\tdispls[%d] = %d\n", i, sendcounts[i], i, displs[i]);
}

//collect the processed matrix by sendcounts and displs
MPI_Gatherv(&rec_buf, sendcounts[rank], MPI_INT, &Ap, sendcounts, displs, MPI_INT, 0, MPI_COMM_WORLD);
//print the result
if(rank == 0){
printf("******************************************************\n");
printf ("Result: \n");
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf ("%d\t", Ap[i][j]);
}
}
printf ("\n");
free(sendcounts);
free(displs);
}
}

我在.sh文件中使用了4个等级并正确设置了N=15,我的错误报告如下:

[c0706a-s2:82816:0:82816] Caught signal 11 (Segmentation fault: address not mapped to object at address 0x4)
==== backtrace (tid: 82816) ====
0 0x000000000004ee05 ucs_debug_print_backtrace() ???:0
1 0x0000000000402285 main() ???:0
2 0x0000000000022545 __libc_start_main() ???:0
3 0x00000000004020a9 _start() ???:0
=================================
[c0706a-s2:82818:0:82818] Caught signal 11 (Segmentation fault: address not mapped to object at address (nil))
[c0706a-s2:82820:0:82820] Caught signal 11 (Segmentation fault: address not mapped to object at address (nil))
[c0706a-s2:82815:0:82815] Caught signal 11 (Segmentation fault: address not mapped to object at address (nil))
==== backtrace (tid: 82818) ====
0 0x000000000004ee05 ucs_debug_print_backtrace() ???:0
1 0x0000000000402285 main() ???:0
2 0x0000000000022545 __libc_start_main() ???:0
3 0x00000000004020a9 _start() ???:0
=================================
==== backtrace (tid: 82820) ====
0 0x000000000004ee05 ucs_debug_print_backtrace() ???:0
1 0x0000000000402285 main() ???:0
2 0x0000000000022545 __libc_start_main() ???:0
3 0x00000000004020a9 _start() ???:0
=================================
==== backtrace (tid: 82815) ====
0 0x000000000004ee05 ucs_debug_print_backtrace() ???:0
1 0x0000000000402285 main() ???:0
2 0x0000000000022545 __libc_start_main() ???:0
3 0x00000000004020a9 _start() ???:0
=================================
srun: error: c0706a-s2: tasks 1-3: Segmentation fault (core dumped)
srun: error: c0706a-s2: task 0: Segmentation fault (core dumped)

最佳答案

在我的工作站上,这个问题是由堆栈限制引起的。只需取消限制即可。

修改/etc/security/limits.conf

添加:

“*软堆栈无限”

“*硬堆栈无限”

修改/etc/pam.d/login

添加:

需要 session /usr/lib64/security/pam_limits.so

然后,重启ssh

关于捕获信号 11(段错误 : address not mapped to object at address (nil)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71074993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com