高性能并行编程与优化 | 第03讲回家作业

Posted yantuguiguziPGJ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高性能并行编程与优化 | 第03讲回家作业相关的知识,希望对你有一定的参考价值。

目录

一 题目三

二 代码答案抄写

三 成功运行


 

一 题目三

# 高性能并行编程与优化 - 第0x讲的回家作业

通过 pull request 提交作业。会批分数,但是:

没有结业证书,回家作业仅仅作为评估学习效果和巩固知识的手段,不必为分数感到紧张 :)
量力而行,只要能在本课中,学到昨天的自己不懂的知识,就是胜利,没必要和别人攀比。
注意不要偷看别人的作业哦!

- 课件:https://github.com/parallel101/course
- 录播:https://space.bilibili.com/263032155

作业提交时间不限 :) 即使完结了还想交的话我也会看的~ 不过最好在下一讲开播前完成。

- 如何开 pull request:https://zhuanlan.zhihu.com/p/51199833
- 如何设置 https 代理:https://www.jianshu.com/p/b481d2a42274

## 评分规则

- 完成作业基本要求 50 分(作业要求详见作业代码)
- 能够在 PR 描述中用自己的话解释 25 分
- 代码格式规范、能够跨平台 5 分
- 有自己独特的创新点 20 分
- 明显抄袭现象 -100 分

## 关于内卷

如果你把 variant 的 operator<< 改成了基于变长模板参数的,或是实现了其他运算符:
只要是在 **满足作业要求的基础** 上,这是件好事!
老师会酌情加分,视为“独特的创新点”,但最多不超过 20 分。

#include <iostream>
#include <vector>
#include <variant>

// 请修复这个函数的定义:10 分
std::ostream &operator<<(std::ostream &os, std::vector<T> const &a) 
    os << "";
    for (size_t i = 0; i < a.size(); i++) 
        os << a[i];
        if (i != a.size() - 1)
            os << ", ";
    
    os << "";
    return os;


// 请修复这个函数的定义:10 分
template <class T1, class T2>
std::vector<T0> operator+(std::vector<T1> const &a, std::vector<T2> const &b) 
    // 请实现列表的逐元素加法!10 分
    // 例如 1, 2 + 3, 4 = 4, 6


template <class T1, class T2>
std::variant<T1, T2> operator+(std::variant<T1, T2> const &a, std::variant<T1, T2> const &b) 
    // 请实现自动匹配容器中具体类型的加法!10 分


template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, std::variant<T1, T2> const &a) 
    // 请实现自动匹配容器中具体类型的打印!10 分


int main() 
    std::vector<int> a = 1, 4, 2, 8, 5, 7;
    std::cout << a << std::endl;
    std::vector<double> b = 3.14, 2.718, 0.618;
    std::cout << b << std::endl;
    auto c = a + b;

    // 应该输出 1
    std::cout << std::is_same_v<decltype(c), std::vector<double>> << std::endl;

    // 应该输出 4.14, 6.718, 2.618
    std::cout << c << std::endl;

    std::variant<std::vector<int>, std::vector<double>> d = c;
    std::variant<std::vector<int>, std::vector<double>> e = a;
    d = d + c + e;

    // 应该输出 9.28, 17.436, 7.236
    std::cout << d << std::endl;

    return 0;

二 代码答案抄写

//参考代码:https://github.com/parallel101/hw03/pull/24/files

#include <iostream>
#include <vector>
#include <variant>
#include <utility>

// 请修复这个函数的定义:10 分
template<class T>
std::ostream &operator<<(std::ostream &os, std::vector<T> const &a) 
    os << "";
    for (size_t i = 0; i < a.size(); ++i) 
    //for (size_t i = 0; i < a.size(); i++) 
        os << a[i];
        if (i != a.size() - 1)
            os << ", ";
    
    os << "";
    return os;


// 支持加减乘除
enum class Operator 
    Add,
    Sub,
    Mul,
    Div
;

template <class T1, class T2, Operator ops>
constexpr decltype(auto) cal_ops(const T1& a, const T2& b) noexcept 
    static_assert(ops == Operator::Add || ops == Operator::Sub || ops == Operator::Mul || ops == Operator::Div);
    if constexpr (ops == Operator::Add) return a + b;
    else if constexpr (ops == Operator::Sub) return a - b;
    else if constexpr (ops == Operator::Mul) return a * b;
    else if constexpr (ops == Operator::Div) return a / b; // RVO


template <class T1, class T2, Operator ops>
struct ops_result 
    using type = decltype(cal_ops<T1, T2, ops>(T1, T2));
;

template <class T1, class T2, Operator ops>
using ops_result_t = ops_result<T1, T2, ops>::type;

template <class T1, class T2, Operator ops>
constexpr decltype(auto) vector_operator(std::vector<T1> const& a, std::vector<T2> const& b) 
    std::vector<ops_result_t<T1, T2, ops>> out;
    out.reserve(std::min(a.size(), b.size()));
    auto x = a.cbegin();
    auto y = b.cbegin();
    for (; x != a.cend() && y != b.cend(); ++x, ++y)
        out.push_back(std::move(cal_ops<T1, T2, ops>(*x, *y))); // NO RVO
    return out; //NRVO



// 请修复这个函数的定义:10 分
template <class T1, class T2>
//std::vector<T0> operator+(std::vector<T1> const &a, std::vector<T2> const &b) 
constexpr decltype(auto) operator+(std::vector<T1> const& a, std::vector<T2> const& b) noexcept 
    // 请实现列表的逐元素加法!10 分
    // 例如 1, 2 + 3, 4 = 4, 6
    return vector_operator<T1, T2, Operator::Add>(a, b);


template <class T1, class T2>
//std::variant<T1, T2> operator+(std::variant<T1, T2> const &a, std::variant<T1, T2> const &b) 
constexpr decltype(auto) operator-(std::vector<T1> const& a, std::vector<T2> const& b) noexcept 
    return vector_operator<T1, T2, Operator::Sub>(a, b);



template <class T1, class T2>
constexpr decltype(auto) operator*(std::vector<T1> const& a, std::vector<T2> const& b) noexcept 
    return vector_operator<T1, T2, Operator::Mul>(a, b);



template <class T1, class T2>
//std::ostream& operator<<(std::ostream& os, std::variant<T1, T2> const& a) 
    // 请实现自动匹配容器中具体类型的打印!10 分
constexpr decltype(auto) operator/(std::vector<T1> const& a, std::vector<T2> const& b) noexcept 
        return vector_operator<T1, T2, Operator::Div>(a, b);



template<class O, class V, class R, Operator ops, size_t N>
constexpr decltype(auto) op_vector_v(O& o, V const& a, R const& b) noexcept 
    if (N == a.index()) 
        auto& e = std::get<N>(a);
        if constexpr (ops == Operator::Add) o = e + b;
        else if constexpr (ops == Operator::Sub) o = e - b;
        else if constexpr (ops == Operator::Mul) o = e * b;
        else if constexpr (ops == Operator::Div) o = e / b;
    


template<class O, class V, class R, Operator ops, size_t...N>
constexpr decltype(auto) unpack_n_vector(O& o, V const& a, R const& b, std::index_sequence<N...>) noexcept 
    static_cast<void>(std::initializer_list<int>(op_vector_v<O, V, R, ops, N>(o, a, b), 0)...);


template <class... Args, class T>
constexpr decltype(auto) operator+(std::variant<Args...> const& a, std::vector<T> const& b) noexcept 
    // 请实现自动匹配容器中具体类型的加法!10 分
    std::variant<Args...> out;
    unpack_n_vector<decltype(out), decltype(a), decltype(b), Operator::Add>(out, a, b, std::index_sequence_for<Args...>);
    return out;


template <class... Args, class T>
constexpr decltype(auto) operator-(std::variant<Args...> const& a, std::vector<T> const& b) noexcept 
    std::variant<Args...> out;
    unpack_n_vector<decltype(out), decltype(a), decltype(b), Operator::Sub>(out, a, b, std::index_sequence_for<Args...>);
    return out;


template <class... Args, class T>
constexpr decltype(auto) operator*(std::variant<Args...> const& a, std::vector<T> const& b) noexcept 
    std::variant<Args...> out;
    unpack_n_vector<decltype(out), decltype(a), decltype(b), Operator::Mul>(out, a, b, std::index_sequence_for<Args...>);
    return out;


template <class... Args, class T>
constexpr decltype(auto) operator/(std::variant<Args...> const& a, std::vector<T> const& b) noexcept 
    std::variant<Args...> out;
    unpack_n_vector<decltype(out), decltype(a), decltype(b), Operator::Div>(out, a, b, std::index_sequence_for<Args...>);
    return out;


template <class... Args, class T>
constexpr decltype(auto) operator+(std::vector<T> const& b, std::variant<Args...> const& a) noexcept 
    // 请实现自动匹配容器中具体类型的加法!10 分
    return operator+(a, b);


template <class... Args, class T>
constexpr decltype(auto) operator-(std::vector<T> const& b, std::variant<Args...> const& a) noexcept 
    return operator-(a, b);


template <class... Args, class T>
constexpr decltype(auto) operator*(std::vector<T> const& b, std::variant<Args...> const& a) noexcept 
    return operator*(a, b);


template <class... Args, class T>
constexpr decltype(auto) operator/(std::vector<T> const& b, std::variant<Args...> const& a) noexcept 
    return operator/(a, b);


template<class O, class V, Operator ops, size_t N>
constexpr decltype(auto) op_variant_v(O& o, V const& a) noexcept 
    static_assert(ops == Operator::Add || ops == Operator::Sub || ops == Operator::Mul || ops == Operator::Div);
    if (N == a.index()) 
        auto& e = std::get<N>(a);
        if constexpr (ops == Operator::Add) o = o + e;
        else if constexpr (ops == Operator::Sub) o = o - e;
        else if constexpr (ops == Operator::Mul) o = o * e;
        else if constexpr (ops == Operator::Div) o = o / e;
    


template<class O, class V, Operator ops, size_t...N>
constexpr decltype(auto) unpack_n_variant(O& o, V const& a, std::index_sequence<N...>) noexcept 
    static_cast<void>(std::initializer_list<int>(op_variant_v<O, V, ops, N>(o, a), 0)...);


template <class... Args>
constexpr decltype(auto) operator+(std::variant<Args...> const& a, std::variant<Args...> const& b) noexcept 
    // 请实现自动匹配容器中具体类型的加法!10 分
    auto out = a;
    unpack_n_variant<decltype(out), decltype(a), Operator::Add>(out, b, std::index_sequence_for<Args...>);
    return out;


template <class... Args>
constexpr decltype(auto) operator-(std::variant<Args...> const& a, std::variant<Args...> const& b) noexcept 
    auto out = a;
    unpack_n_variant<decltype(out), decltype(a), Operator::Sub>(out, b, std::index_sequence_for<Args...>);
    return out;


template <class... Args>
constexpr decltype(auto) operator*(std::variant<Args...> const& a, std::variant<Args...> const& b) noexcept 
    auto out = a;
    unpack_n_variant<decltype(out), decltype(a), Operator::Mul>(out, b, std::index_sequence_for<Args...>);
    return out;


template <class... Args>
constexpr decltype(auto) operator/(std::variant<Args...> const& a, std::variant<Args...> const& b) noexcept 
    auto out = a;
    unpack_n_variant<decltype(out), decltype(a), Operator::Div>(out, b, std::index_sequence_for<Args...>);
    return out;


template<class V, size_t N>
decltype(auto) print_v(std::ostream& os, V const& a) 
    if (N == a.index())
        os << std::get<N>(a);


template<class V, size_t...N>
std::ostream& unpack_n_print(std::ostream& os, V const& a, std::index_sequence<N...>) 
    static_cast<void>(std::initializer_list<int>(print_v<V, N>(os, a), 0)... );
    return os;


template <class... Args>
    requires (sizeof...(Args) > 0)  // no std::variant<>
std::ostream& operator<<(std::ostream& os, std::variant<Args...> const& a) 
    // 请实现自动匹配容器中具体类型的打印!10 分
    return unpack_n_print(os, a, std::index_sequence_for<Args...>);


int main() 
    std::vector<int> a = 1, 4, 2, 8, 5, 7;
    std::cout << a << std::endl;
    std::vector<double> b = 3.14, 2.718, 0.618;
    std::cout << b << std::endl;
    auto c = a + b;

    // 应该输出 1
    std::cout << std::is_same_v<decltype(c), std::vector<double>> << std::endl;

    // 应该输出 4.14, 6.718, 2.618
    std::cout << c << std::endl;

    std::variant<std::vector<int>, std::vector<double>> d = c;
    std::variant<std::vector<int>, std::vector<double>> e = a;
    d = d + c + e;

    // 应该输出 9.28, 17.436, 7.236
    std::cout << d << std::endl;

    return 0;

三 成功运行

以上是关于高性能并行编程与优化 | 第03讲回家作业的主要内容,如果未能解决你的问题,请参考以下文章

高性能并行编程与优化 | 第02讲回家作业

高性能并行编程与优化 | 第02讲回家作业

高性能并行编程与优化 | 第01讲回家作业

高性能并行编程与优化 | 第04讲回家作业

高性能并行编程与优化 | 第04讲回家作业

高性能并行编程与优化 | 第01讲回家作业