zoukankan      html  css  js  c++  java
  • C++基础-并行计算求和(async)

    并行计算使用的是async, 通过每一个线程都进行相同的计算,最后在vector<future<int>>result; 将结果进行相加

    全部代码

    //
    // Created by Administrator on 2021/6/29.
    //
    #include<iostream>
    #include<thread>
    #include<future>
    #include<vector>
    #include<cstdlib>
    
    
    using namespace std;
    #define COUNT 1000000
    
    int add(vector<int>*arr, int start, int count)
    {
        static mutex m; //只会初始化一次
        int sum(0); //保存结果的作用
        for(int i = 0; i < count; i++){
            sum += (*arr)[start + i];
        }
        {
            //显示结果必须, 仅仅计算多余, 加锁
            lock_guard<mutex> lck(m); //锁定
            cout << "thread" << this_thread::get_id << ",count" <<
            ",sum=" << sum << endl; //打印结果
        }
        return sum;
    }
    
    
    int main()
    {
        vector<int>data(COUNT); //数组, 100万
        for(int i = 0; i < COUNT; i++)
        {
            data[i] = (i + 1) % 1000; //0-999
        }
    
        vector<future<int>>result; //结果数组
        int cpus = thread::hardware_concurrency(); //CPU核心的个数
        for(int i = 0; i < cpus * 2; i++)
        {
            int batch_each = COUNT / (cpus * 2); //
            if(i == (cpus * 2) - 1) {
                batch_each = COUNT - COUNT / (cpus * 2) * i; //最后一个承担更多的计算
            }
            //不断压入结果
            result.push_back(async(add, &data, i * batch_each, batch_each)); //返回结果
        }
        int lastresult(0);
        for(int i = 0; i < cpus * 2; i++)
        {
            lastresult += result[i].get();  //汇总结果
        }
        cout << "lastresult=" << lastresult << endl;
        cin.get();
    }
    //
    // Created by Administrator on 2021/6/29.
    //
    #include<iostream>
    #include<thread>
    #include<future>
    #include<vector>
    #include<cstdlib>
    
    
    using namespace std;
    #define COUNT 1000000
    
    int add(vector<int>*arr, int start, int count)
    {
        static mutex m; //只会初始化一次
        int sum(0); //保存结果的作用
        for(int i = 0; i < count; i++){
            sum += (*arr)[start + i];
        }
        {
            //显示结果必须, 仅仅计算多余, 加锁
            lock_guard<mutex> lck(m); //锁定
            cout << "thread" << this_thread::get_id << ",count" <<
            ",sum=" << sum << endl; //打印结果
        }
        return sum;
    }
    
    
    int main()
    {
        vector<int>data(COUNT); //数组, 100万
        for(int i = 0; i < COUNT; i++)
        {
            data[i] = (i + 1) % 1000; //0-999
        }
    
        vector<future<int>>result; //结果数组
        int cpus = thread::hardware_concurrency(); //CPU核心的个数
        for(int i = 0; i < cpus * 2; i++)
        {
            int batch_each = COUNT / (cpus * 2); //
            if(i == (cpus * 2) - 1) {
                batch_each = COUNT - COUNT / (cpus * 2) * i; //最后一个承担更多的计算
            }
            //不断压入结果
            result.push_back(async(add, &data, i * batch_each, batch_each)); //返回结果
        }
        int lastresult(0);
        for(int i = 0; i < cpus * 2; i++)
        {
            lastresult += result[i].get();  //汇总结果
        }
        cout << "lastresult=" << lastresult << endl;
        cin.get();
    }
  • 相关阅读:
    Universal-image-loader Mason 修复版(ImageLoader Image can't be decoded)
    118、通过solid来定义不同边框的颜色,可以只定义一个边框的颜色
    Freeline的快速集成(转载)
    SQL存储过程解密
    Bootstrap
    PHP5.6.15连接Sql Server 2008配置方案
    php使用saop简单例子和注意事项
    php使用mysql和mysqli连接查询数据
    MYSQL中UNIX时间戳与日期的转换
    xp和win7安装telnet服务
  • 原文地址:https://www.cnblogs.com/my-love-is-python/p/14948191.html
Copyright © 2011-2022 走看看