2016-01-24 33 views
0

我在OPen MP(C)中有以下程序。调试并行程序在C中

它有时给出0或3作为fibonnaci数字或崩溃给出分段错误。

#include <stdlib.h> 
#include <stdio.h> 
#include <omp.h> 

static int fib(int); 

int main(){ 
int nthreads, tid; 
int n =8; 
#pragma omp parallel num_threads(4) private(tid) 

{ 
#pragma omp single 

{ 
tid = omp_get_thread_num(); 
printf("Hello world from (%d)\n", tid); 
printf("Fib(%d) = %d by %d\n", n, fib(n), tid); 

} 
} // all threads join master thread and terminates 
} 
static int fib(int n){ 
int i, j, id; 
if(n < 2) 
return n; 
#pragma omp task shared (i) private (id) 

{ 

i = fib(n-1); 

} 
#pragma omp task shared (j) private (id) 
{ 
j = fib(n-2); 

} 

return (i+j); 
} 

该程序有什么问题?

输出是这样的:

世界,你好从(3)

纤维蛋白原(8)= 3×3

+1

所以你听说过SelçukCihan的治疗方法。它有用吗?我真的很想听听演奏的数字是什么;以这种方式实施的Fib(n)是在地球上最难并行化的一种。有关比较编号,请参阅http://stackoverflow.com/q/5086040/120163 –

回答

1

您需要正确之前,有一个taskwait在#pragma omp taskwait返回(i+j)。否则它将在数字完全计算之前返回,而不等待其他任务。