【发布时间】:2014-03-24 19:25:54
【问题描述】:
我正在使用 OpenMP 和 MPI 来并行化 c 中的一些矩阵运算。对矩阵进行操作的一些函数是用 Fortran 编写的。 Fortran 函数需要传入一个缓冲区数组,该数组仅在函数内部使用。目前我在每个并行部分分配缓冲区,类似于下面的代码。
int i = 0;
int n = 1024; // Actually this is read from command line
double **a = createNbyNMat(n);
#pragma omp parallel
{
double *buf;
buf = malloc(sizeof(double)*n);
#pragma omp for
for (i=0; i < n; i++)
{
fortranFunc1_(a[i], &n, buf);
}
free(z);
}
// Serial code and moving data around in the matrix a using MPI
#pragma omp parallel
{
double *buf;
buf = malloc(sizeof(double)*n);
#pragma omp for
for (i=0; i < n; i++)
{
fortranFunc2_(a[i], &n, buf);
}
free(z);
}
// and repeat a few more times.
我知道使用类似于下面代码的方法可以避免重新分配缓冲区,但我很好奇是否有更简单的方法或 OpenMP 中的一些内置功能来处理这个问题。无论我们正在编译的系统上是否存在 OpenMP,如果能够在没有大量编译器指令的情况下编译代码,那就太好了。
double **buf;
buf = malloc(sizeof(double*) * num_openmp_threads);
int i = 0;
for (i = 0; i < num_openmp_threads; ++i)
{
buf[i] = malloc(sizeof(double) * n);
}
// skip ahead
#pragma omp for
for (i=0; i < n; i++)
{
fortranFunc1_(a[i], &n, buf[current_thread_num]);
}
【问题讨论】:
-
否则我认为不需要
fortran标签。 -
是的,你是对的,我删除了 Fortran 标签。这个问题类似,但关键区别在于动态分配的数组不会在另一个并行部分中重用。