在更改lpthreads中的处理器数量时出现分段故障(核心转储)
Segmentation fault, (core dumped) when changing number of processors in lpthreads
当我在8个处理器上运行代码时,遇到了分段错误,但它在1个和4个处理器上都能正常工作。
我使用的是lpthread库,这是我在每个线程中执行的函数。如果需要更多的代码,我可以添加更多。
void *compute_gauss(void *threadid){
int local_row, local_norm, col;
float multiplier;
long tid;
tid = (long)threadid;
fprintf(stdout, "Thread %ld has startedn", tid);
while (global_norm < N){
while (global_row < N) {
pthread_mutex_lock(&global_row_lock);
local_row = global_row;
global_row++;
pthread_mutex_unlock(&global_row_lock);
print_inputs();
multiplier = A[local_row][global_norm] / A[global_norm][global_norm];
for (col = global_norm; col < N; col++) {
A[local_row][col] -= A[global_norm][col] * multiplier;
}
B[local_row] -= B[global_norm] * multiplier;
}
pthread_barrier_wait(&barrier);
if (tid == 0){
global_norm++;
global_row=global_norm+1;
}
pthread_barrier_wait(&barrier); // wait until all threads arrive
}
}
这是我初始化屏障的调用函数:
void gauss() {
int norm, row, col; /* Normalization row, and zeroing
* element row and col */
int i = 0;
float multiplier;
pthread_t threads[procs]; //declared array of threads equal in size to # processors
global_norm = 0;
global_row = global_norm+1;
printf("Computing Parallelized Algorithm.n");
pthread_barrier_init(&barrier, NULL, procs);
/* Gaussian elimination */
for (i = 0; i < procs; i++){
pthread_create(&threads[i], NULL, &compute_gauss, (void *)i);
}
printf("finished creating threadsn");
for (i = 0; i < procs; i++){
pthread_join( threads[i], NULL);
}
printf("finished joining threadsn");
/* (Diagonal elements are not normalized to 1. This is treated in back
* * substitution.)
* */
fprintf(stdout, "pre back substition");
/* Back substitution */
for (row = N - 1; row >= 0; row--) {
X[row] = B[row];
for (col = N-1; col > row; col--) {
X[row] -= A[row][col] * X[col];
}
X[row] /= A[row][row];
}
fprintf(stdout, "post back substitution");
}
这里有一个代码如何侵入数组的例子,如果我错了,请指出:
// suppose global_row = N - 1;
while (global_row < N) {
pthread_mutex_lock(&global_row_lock); // thread 2 waits here, global_row is N - 1;
local_row = global_row; // thread 1 is here, global_row is N - 1;
global_row++;
pthread_mutex_unlock(&global_row_lock);
// when thread 2 goes here, local_row is going to be N, out of array boundary.
multiplier = A[local_row][global_norm] / A[global_norm][global_norm];
您没有包含足够的代码,因此我无法测试您的程序。但是,我敢肯定,问题是没有一个互斥体来保护global_norm
、global_row
和print_inputs()
。您需要使用互斥锁来保护它们,或者需要使用原子增量运算符。您没有在调试器下看到崩溃,因为它正在更改您的时间。
难道不应该检查pthread_barrier_wait的返回值并检查PTHRAD_BARRIAR_SERIAL_THREAD吗?
也不清楚为什么要调用pthread_barrier_wait两次。
相关文章:
- 分段故障(堆芯转储)矢量
- 数组的指针从不分段故障
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 分段故障 运行C++代码时出现 SIGSEGV
- 分段故障背包问题
- 分段故障 11,从类函数显示动态 C 字符串
- 面临分段故障 使用 ffmpeg 读取视频时,因为"pFormatCtx-> streams [i]-> codecpar"的地址0x00
- 在C++中,当指向删除和指向不同对象时,分段故障指针
- 分段故障说明
- 分段故障(核心转储)-不知道为什么
- 分段故障线程
- hiredis SET遇到分段故障
- 分段故障,合并排序算法
- 多线程程序中的分段故障和gdb回溯上的不完整信息
- 到达主C++之前分段故障
- 分段故障核心使用 IF流转储
- 使用向量的移动键盘排列(分段故障)
- 在二进制树插入和遍历期间,我得到了分段故障
- 分段故障在类之间返回整数
- C++分段故障BST