我正在使用pthread_create()
来调用标题行为void * my_func(void *args)
的函数。此函数从数据结构中转换所需的所有参数。
但是,my_func()
还需要访问在其他位置创建的网格。
我尝试将网格作为输入参数与void *args
一起传递,但是这导致函数pthread_create()
抛出错误,因为它不允许/期望这样做。
我尝试改为将网格添加到结构中(不确定这是否合法),但是无论如何,这导致在my_func()
中“未声明”网格。
pthreads_create()
代码:
for (k = 0; k < num_threads; k++)
pthread_create (&thread_id[k], &attributes, my_func, (void *) &thread_data[k]);
my_func()
代码:
/* This function is executed by each thread to compute the overall Gauss-Seidel. */
void *
my_func (void *args)
{
/* Typecast the argument to a pointer to the thread_data_t structure. */
thread_data_t *thread_data = (thread_data_t *) args;
// int num_iter = 0;
int done = 0;
int i, j;
double diff;
float old, new;
float eps = 1e-2; /* Convergence criteria. */
int num_elements;
int num_iter = 0;
if (thread_data->tid < (thread_data->num_threads - 1)) {
for (int k = thread_data->offset; k < (thread_data->offset + thread_data->chunk_size); k++)
while(!done) { /* While we have not converged yet. */
diff = 0.0;
num_elements = 0;
for (i = 1; i < (grid->dim - 1); i++) {
for (j = 1; j < (grid->dim - 1); j++) {
old = grid->element[i * grid->dim + j]; /* Store old value of grid point. */
/* Apply the update rule. */
new = 0.25 * (grid->element[(i - 1) * grid->dim + j] +\
grid->element[(i + 1) * grid->dim + j] +\
grid->element[i * grid->dim + (j + 1)] +\
grid->element[i * grid->dim + (j - 1)]);
grid->element[i * grid->dim + j] = new; /* Update the grid-point value. */
diff = diff + fabs(new - old); /* Calculate the difference in values. */
num_elements++;
}
}
/* End of an iteration. Check for convergence. */
diff = diff/num_elements;
printf ("Iteration %d. DIFF: %f.\n", num_iter, diff);
num_iter++;
if (diff < eps)
done = 1;
} // end while
} //end if
else { /* This takes care of the number of elements that the final thread must process. */
int done2 = 0;
for (int k = thread_data->offset; k < thread_data->dim; k++)
while(!done2) { /* While we have not converged yet. */
diff = 0.0;
num_elements = 0;
for (i = 1; i < (grid->dim - 1); i++) {
for (j = 1; j < (grid->dim - 1); j++) {
old = grid->element[i * grid->dim + j]; /* Store old value of grid point. */
/* Apply the update rule. */
new = 0.25 * (grid->element[(i - 1) * grid->dim + j] +\
grid->element[(i + 1) * grid->dim + j] +\
grid->element[i * grid->dim + (j + 1)] +\
grid->element[i * grid->dim + (j - 1)]);
grid->element[i * grid->dim + j] = new; /* Update the grid-point value. */
diff = diff + fabs(new - old); /* Calculate the difference in values. */
num_elements++;
}
}
/* End of an iteration. Check for convergence. */
diff = diff/num_elements;
printf ("Iteration %d. DIFF: %f.\n", num_iter, diff);
num_iter++;
if (diff < eps)
done = 1;
} // end while
}// end else
/* Store num_iter into the num_iter array. */
thread_data->num_iter[thread_data->tid] = num_iter;
pthread_exit (NULL);
}
答案 0 :(得分:0)
创建头文件(将其命名为thread_func.h
或任何您喜欢的名称)
#ifndef THREAD_FUNC_H
#define THREAD_FUNC_H
void* my_func(void *args);
#endif
对您的thread_data_s
结构执行相同的操作,但是在不同的头文件中,并包括它的定义。
创建一个名为.c
的{{1}}文件,然后在其中放置thread_func.c
的定义。确保my_func(struct thread_data_s *data)
定义了结构的标头,但是除了#include
之外,请勿#include "thread_func.h"
。我也强烈建议您不要投射指针。
如果您想查看我所描述的布局示例,那么我有一个on my GitHub。
修改:
至于将网格传递到main.c
,只需使其成为my_func
的一部分即可。