在GCC中运行程序时出现Segmentation Fault错误。这是一个相当长的计划,所以我只发布我认为相关的部分;如果需要更多,请告诉我。
void *RelaxThread(void *para) {
printf("Entering RelaxThread...\n");
struct Parameter *parameters;
parameters = (struct Parameter *) para;
/*Some code here*/
pthread_exit(NULL);
}
int relax(double **matrix, int size, int threads, double precision) {
keepRunning = 0;
int rowAllocation = (size-2) / threads;
struct Parameter para[threads];
pthread_t threadcount[threads];
int current;
int startRow = 1;
long t;
for(t=0; t<threads; t++){
para[t].matrix = matrix;
para[t].size = size;
para[t].threads = threads;
para[t].precision = precision;
para[t].allocation = rowAllocation;
para[t].threadId = t;
para[t].startRow = startRow;
startRow += rowAllocation;
printf("DebugMsg1\n");
current = pthread_create(&threadcount[t], NULL, RelaxThread, (void *) ¶[t]);
printf("DebugMsg2\n");
}
void *status;
int rc;
for(t=0; t<threads; t++) {
rc = pthread_join(threadcount[t], &status);
if (rc) {
exit(-1);
}
}
if (keepRunning) {
printf("Iterating...\n");
relax(matrix, size, threads, precision);
}
}
int main(int argc, char *argv[])
{
int const size = 8;
int const threads = 2;
double const precision = 1e-6;
double **matrix = (double **) malloc(size * sizeof(double));
populateMatrix(matrix, size);
relax(matrix, size, threads, precision);
free(matrix);
pthread_exit(NULL);
return 0;
}
当size设置为8或更低时,代码运行正常。然而,任何大于此的东西(它现在只支持偶数)会在relax()的第二次迭代中产生分段错误。因此调试消息运行如下:
Debug1
Debug2
Debug1
Debug2
Entering RelaxThread...
Entering RelaxThread...
Iterating...
Debug1
Debug2
Debug1
Segmentation Fault
这个大小为8或更小但是在这个数字之上崩溃的事实让我感到很困惑,而且我花了很长时间试图弄清楚为什么会发生这种情况。我完全承认我的代码既不是最好的也不是最有效的,但我会非常感谢为什么这个代码失败了。
编辑1:按要求附加GDB积压。
Debug1
[New Thread 0x40040940 (LWP 23270)]
Debug2
Debug1
Entering RelaxThread...
Entering RelaxThread 0
[New Thread 0x40081940 (LWP 23271)]
Debug2
Entering RelaxThread...
Entering RelaxThread 1
[Thread 0x40040940 (LWP 23270) exited]
Main: completed join with thread 0 having a status of 0
Main: completed join with thread 1 having a status of 0
Iterating...
Debug1
[Thread 0x40081940 (LWP 23271) exited]
[New Thread 0x40081940 (LWP 23272)]
Debug2
Debug1
Edit2:在更改后更新了回溯日志。
Edit3:以下是完整的RelaxThread
void *RelaxThread(void *para) {
struct Parameter *parameters;
parameters = (struct Parameter *) para;
double **matrix = parameters->matrix;
int size = parameters->size;
double precision = parameters->precision;
int threads = parameters->threads;
int threadId = parameters->threadId;
int startRow = parameters->startRow;
int allocation = parameters->allocation;
int finishRow = (startRow + allocation);
int i;
int j;
double oldValue;
double newValue;
double difference;
for(i=startRow;i<finishRow;i++) {
for (j=1;j<size-1;j++) {
pthread_mutex_lock (&mutexmatrix); //Tested with and without the mutex, which is globally defined and initialised elsewhere
oldValue = matrix[i][j];
newValue = ((matrix[i+1][j] + matrix[i-1][j] + matrix[i][j+1] + matrix[i][j-1]) / 4);
matrix[i][j] = newValue;
pthread_mutex_unlock (&mutexmatrix);
difference = oldValue - newValue;
if(difference < 0) {
difference = -difference;
}
//printf("Precision on thread%d: %f , aiming for %f\n",threadId, difference,precision);
if (difference > precision) {
keepRunning = 1;
}
}
}
}
答案 0 :(得分:3)
正确的初始化,假设矩阵的平方为:
double **matrix = malloc(size * sizeof(double *));
for(int i=0; i<size; i++)
{
matrix[i] = malloc(size * sizeof(double));
for(int j=0; j<size; j++)
{
matrix[i][j] = 0; // removing the junk contents
}
}
编辑:
我也意识到你从未真正初始化void *status
。由于您没有使用它,请将其更改为rc = pthread_join(threadcount[t], NULL);
,这肯定是另一个问题。
EDIT3:
我发现的另一个问题是int startRow = 1;
,我不知道你是如何访问矩阵的,但它的索引应该从0开始。
EDIT4: 此外,当你释放二维数组时,你需要这样做:
for(int i=0; i<size; i++)
{
free(matrix[i]); //Free each row pointer
}
free(matrix);