OpenMP的简单测试 并行化

//测试之前要开启OpenMP支持

#include<iostream>

#include<omp.h>

using namespace std;


int main(){

    int num_procs = omp_get_num_procs();
    cout << "num procs: " << num_procs << endl;
    long long count = 0;
    long long* array = new long long[num_procs];
    for (int i = 0; i < num_procs; i++){
        array[i] = 0;
    }
    double start = omp_get_wtime();
#ifdef _OPENMP                                         //此处可判断是否开启OpenMP支持
    #pragma omp parallel for                     //注释串行执行否则并行执行
#endif
    for (int i = 0; i < num_procs; i++){
        int thread_num = omp_get_thread_num();
        cout << "thread num: " << thread_num << endl;
        for (int j = 0; j < 1000000000; j++){
            array[i]++;
        }
    }
    for (int i = 0; i < num_procs; i++){
        count += array[i];
    }
    delete[] array;
    cout << "count: " << count << endl;
    double end = omp_get_wtime();
    cout << "time: " << end - start << endl;

    return 0;

}

猜你喜欢

转载自blog.csdn.net/ysh126/article/details/54598891