def piFunc(NumSteps): step = 1.0 / NumSteps sum = 0.0 startTime = omp_get_wtime() with openmp("parallel"): with openmp("single"): print(" pi with tasks using ", omp_get_num_threads(), " threads") sum = piComp(0, NumSteps, step) pi = step * sum runTime = omp_get_wtime() - startTime print(" pi = ", pi, " with ", NumSteps, " steps in ", runTime, " seconds") return pi
def piFunc(NumSteps): step = 1.0 / NumSteps sum = 0.0 startTime = omp_get_wtime() with openmp("parallel private(x)"): # print(omp_get_num_threads()) with openmp("for reduction(+:sum)"): for i in range(NumSteps): x = (i + 0.5) * step sum += 4.0 / (1.0 + x * x) pi = step * sum runTime = omp_get_wtime() - startTime print(" pi = ", pi, " with ", NumSteps, " steps in ", runTime, " seconds") return pi
def piComp(Nstart, Nfinish, step): iblk = Nfinish - Nstart if (iblk < MIN_BLK): sum = 0.0 for i in range(Nstart, Nfinish): x = (i + 0.5) * step sum += 4.0 / (1.0 + x * x) else: sum1 = 0.0 sum2 = 0.0 with openmp("task shared(sum1)"): sum1 = piComp(Nstart, Nfinish - iblk / 2, step) with openmp("task shared(sum2)"): sum2 = piComp(Nfinish - iblk / 2, Nfinish, step) with openmp("taskwait"): sum = sum1 + sum2 return sum
def hello(): with openmp("parallel"): print("hello") print("world")