Linux 通过 OpenMP 实现并行编程

2018-10-03 / C Linux

 

#include <iostream>
using namespace std;

#define PARALLEL_SIZE 4

int fibonacci(int n)
{
    return n <= 2 ? 1 : fibonacci (n - 1) + fibonacci (n - 2);
}

int main()
{
    #pragma omp parallel for
    for (int i = 0; i < PARALLEL_SIZE; i++)
    {
        cout << fibonacci(50) << endl;
    }
    return 0;
}
build:
    g++ -std=c++11 -c main.cpp -o main.o -fopenmp
    g++ main.o -o main -fopenmp -lpthread

run:
    export OMP_NUM_THREADS=4
    ./main