如何使用OpenMP处理两个for循环

时间:2017-11-12 00:24:23

标签: openmp

我是OpenMP的新手......请帮我解决这个愚蠢的问题。谢谢:))

基本上,我想使用OpenMP来加速两个for循环。但是我不知道它为什么一直说: for循环的无效控制谓词

顺便说一句,我的GCC版本是gcc(Ubuntu 6.2.0-5ubuntu12)6.2.0 20161005,而我使用的OS是Ubuntu 16.10。

基本上,我生成一个具有典型键值样式的玩具数据,如下所示:

Data = {
    "0": ["100","99","98","97",..."1"];
    "1": ["100","99","98","97",..."1"];
    ...
    "999":["100","99","98","97",..."1"];
}

然后,对于每个键,我想将其值与其余键进行比较。在这里,我通过" user1_list.size()+ user2_list.size();"来总结它们。对于每个键,总结过程完全独立于其他键,这意味着这适用于并行。

这是我的玩具示例代码。

#include <map>
#include <vector>
#include <string>
#include <iostream>

#include "omp.h"

using namespace std;

int main(){
    // Create Data
    map<string, vector<string>> data;
    for(int i=0; i != 1000; i++){
        vector<string> list;
        for (int j=100; j!=0; j--){
            list.push_back(to_string(j));
        }
        data[to_string(i)]=list;
    }
    cout << "Data Total size: " << data.size() << endl; 

    int count = 1;
    #pragma omp parallel for private(count)
    for (auto it=data.begin(); it!=data.end(); it++){
        //cout << "Evoke Thread: " << omp_get_thread_num();
        cout << " count: " << count << " / " << data.size() << endl;
        count ++;
        string user1 = it->first;
        vector<string> user1_list = it->second;

        for (auto it2=data.begin(); it2!=data.end(); it2++){
            string user2 = it2->first;
            vector<string> user2_list = it2->second;

            cout << "u1:" << user1 << " u2:" << user2;
            int total_size = user1_list.size()+user2_list.size();
            cout << " total size: " << total_size << endl;
        }
    }
    return 0;
}

0 个答案:

没有答案