-
[OpenMP] Fibonacci 병렬화랭귀지/OpenMP 2012. 9. 20. 15:05
피보나치 병렬화 실패
#include <stdio.h> #include <omp.h> long Sequential_FIB(int n) { long fnm1, fnm2, fn; if(n == 0 || n == 1) return (n); fnm1 = Sequential_FIB(n-1); fnm2 = Sequential_FIB(n-2); fn = fnm1 + fnm2; return (fn); } long openMP_FIB(int n) { long fnm1, fnm2, fn; if(n == 0 || n == 1) return (n); #pragma omp task shared(fnm1) { fnm1 = openMP_FIB(n-1); } #pragma omp task shared(fnm2) { fnm2 = openMP_FIB(n-2); } #pragma omp taskwait fn = fnm1 + fnm2; return (fn); } int main(int argc, char* argv[]) { int n = 32; double before = omp_get_wtime(); if(argc < 2) { printf("FIBO : %d\n", Sequential_FIB(n)); printf("%f sec Sequential Complete.\n",omp_get_wtime()-before); } else { #pragma omp parallel { #pragma omp single nowait { printf("FIBO : %d\n", openMP_FIB(n)); } } printf("%f sec OpenMP Complete.\n",omp_get_wtime()-before); } return 0; }