gpt4 book ai didi

c++ - 更改 lpthread 中的处理器数量时出现段错误(核心转储)

转载 作者:行者123 更新时间:2023-11-30 19:12:30 26 4
gpt4 key购买 nike

在 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 started\n", 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 threads\n");

for (i = 0; i < procs; i++){
pthread_join( threads[i], NULL);
}

printf("finished joining threads\n");
/* (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];

关于c++ - 更改 lpthread 中的处理器数量时出现段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37062870/

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