设为首页 加入收藏

TOP

Binary &Op是什么
2023-07-23 13:38:40 】 浏览:47
Tags:Binary &Op

前言

在并行开发时我们经常会用到Pstream::gather()函数或是全局函数reduce()或者其他,需要输入参数Binary &Op,本篇主要讨论Binary &Op是什么

template<class T, class BinaryOp>
void reduce
(
    T& Value,
    const BinaryOp& bop, // 这里要输入什么参数
    const int tag,
    const label comm,
    label& request
)
{
    NotImplemented;
}

Binary &Op

单从名字上看,猜是一个二进制的操作,类似一组操作返回一个二进制的标记
然后去openfoam官网去找,找不到Binary &Op的任何释义
去网上找,发现了一点端倪
c++标准库中有应用Binary &Op这个输入参数,使用的函数也叫reduce,详情可见该网页

这里顺便说几个学习C++不错的网址,查询起来很方便

https://en.cppreference.com/w/
https://learncpp-cn.github.io/
https://cplusplus.com/
https://www.tutorialspoint.com/index.htm
https://awesome-cpp.readthedocs.io/en/latest/README.html#
https://stackoverflow.org.cn/

标准库对输入参数binary_op的解释是:
将以未指定顺序应用于解引用输入迭代器结果、其他 binary_op 结果及 init 上的二元函数对象 (FunctionObject) 。
我们再看下类似的std::transform,详情可见该网页
可能实现的版本有

template<class InputIt1, class InputIt2, 
         class OutputIt, class BinaryOperation>
OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2, 
                   OutputIt d_first, BinaryOperation binary_op)
{
    while (first1 != last1) {
        *d_first++ = binary_op(*first1++, *first2++);
    }
    return d_first;
}

在这个程序中我们看到输入的参数可以传的参数可以函数指针,也可以是仿函数,而且这个仿函数需要传两个参数,按要求送达至返回值中
但按照C++的风格更喜欢是仿函数了
所以我们经常能看到类似这样的函数使用模式

#include <iostream>
#include <functional>
#include <algorithm>

int main () {
   bool foo[] = {true,true,false,false};
   bool bar[] = {true,false,true,false};
   bool result[4];
   std::transform (foo, foo+4, bar, result, std::logical_or<bool>());
   std::cout << std::boolalpha << "Logical OR example as shown below:\n";
   for (int i=0; i<4; i++)
      std::cout << foo[i] << " OR " << bar[i] << " = " << result[i] << "\n";
   return 0;
}

以下是输出结果:

Logical OR example as shown below:
true OR true = true
true OR false = true
false OR true = true
false OR false = false

那么我类似的把std::logical_or ()放到openfoam的reduce函数里可不可以呢
答案是可以的

当然啊,std::logical_or ()放到reduce函数中是没什么意义的,但是我们这就可以清楚,我们需要写怎么样的函数,或者函数指针传送到Binary &Op参数中

对于reduce而言,在ops.H中内置了很丰富的仿函数以供调用

Op(sum, x + y)
Op(plus, x + y)
Op(minus, x - y)
Op(multiply, x * y)
Op(divide, x / y)
Op(cmptMultiply, cmptMultiply(x, y))
Op(cmptPow, cmptPow(x, y))
Op(cmptDivide, cmptDivide(x, y))
Op(stabilise, stabilise(x, y))
Op(max, max(x, y))
Op(min, min(x, y))
Op(minMagSqr, (magSqr(x)<=magSqr(y) ? x : y))
Op(maxMagSqr, (magSqr(x)>=magSqr(y) ? x : y))
Op(minMod, minMod(x, y))
Op(and, x && y)
Op(or, x || y)
Op(not, x != y)
Op(eqEq, x == y)
Op(less, x < y)
Op(lessEq, x <= y)
Op(greater, x > y)
Op(greaterEq, x >= y)

如果想写自己的Binary &Op操作,可以在ops.H的基础上去创建,要轻松很多


结语

一起探索openfoam也是相当有趣的一件事,非常欢迎私信讨论
指正的价值要比打赏更重要,下面是个人联系方式,希望能结交到志同道合的朋友
image

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++ 一种交换两个数的思路 下一篇QT MySQL 连接出错报 QSqlDatabas..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目