使用 omp Parallel for 时出现分段错误,但不按顺序使用

Mut*_*key 5 c parallel-processing openmp libgomp

我在使用 #pragma omp parallel 时遇到问题

基本上我有数百个 DNA 序列,我想用一种称为 NNLS 的算法来运行它们。

我认为并行执行会给我带来相当好的速度,因此我应用了 #pragma 运算符。

当我顺序运行它时,没有问题,结果很好,但是当我使用 #pragma omp parallel 运行它时,我在算法中遇到了段错误(有时在不同的点)。

#pragma omp parallel for
for(int i = 0; i < dir_count; i++ ) {

  int z = 0;
  int w = 0;
  struct dirent *directory_entry;
  char filename[256];

  directory_entry = readdir(input_directory_dh);

  if(strcmp(directory_entry->d_name, "..") == 0 || strcmp(directory_entry->d_name, ".") == 0) {
    continue;
  }

  sprintf(filename, "%s/%s", input_fasta_directory, directory_entry->d_name);

  double *count_matrix = load_count_matrix(filename, width, kmer);

  //normalize_matrix(count_matrix, 1, width)
  for(z = 0; z < width; z++) 
    count_matrix[z] = count_matrix[z] * lambda;

  // output our matricies if we are in debug mode
  printf("running NNLS on %s, %d, %d\n", filename, i, z);
  double *trained_matrix_copy = malloc(sizeof(double) * sequences * width);
  for(w = 0; w < sequences; w++) {
    for(z = 0; z < width; z++) {
      trained_matrix_copy[w*width + z] = trained_matrix[w*width + z];
    }
  } 

  double *solution = nnls(trained_matrix_copy, count_matrix, sequences, width, i);


  normalize_matrix(solution, 1, sequences);
  for(z = 0; z < sequences; z++ )  {
    solutions(i, z) = solution[z]; 
  }

  printf("finished NNLS on %s\n", filename);

  free(solution);
  free(trained_matrix_copy);
}
Run Code Online (Sandbox Code Playgroud)

gdb 总是在我的线程中以不同的位置退出,所以我无法弄清楚出了什么问题。

我尝试过的:

  • 分配每个矩阵的副本,这样它们就不会互相写在上面
  • 对 #pragma 部分使用私有/共享运算符的混合
  • 使用不同的输入序列
  • 在调用 NNLS 之前写出我的训练矩阵和计数矩阵,确保它们看起来没问题。(他们是这样!)

我有点没主意了。有人有什么建议吗?

Mut*_*key 3

解决方案:确保在多线程时不要在函数中使用静态变量(该死的 f2c 翻译器)