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也是相当有趣的一件事,非常欢迎私信讨论
指正的价值要比打赏更重要,下面是个人联系方式,希望能结交到志同道合的朋友
最新文章
- .NET开发笔记(二十三) 谷歌地图下载
- 鼠绘漫画 for wp8.1
- 非对称加密算法--DH
- 性能改善之For与Foreach
- Python中is和==的区别
- js封装tab标签页
- Linux fdisk 命令
- POJ 3261 Milk Patterns(后缀数组+二分答案+离散化)
- springcloud(九):配置中心和消息总线(配置中心终结版)
- python写的一个集合
- 780. Reaching Points
- tile38 复制配置
- Inno Setup安装程序单例运行
- JAVA程序 从命令行接受多个数字,求和之后输出结果
- 11.10 Daily Scrum
- 搭建持续集成单元测试平台(Jenkins+Ant+Java+Junit+SVN)
- Java并发编程原理与实战十:单例问题与线程安全性深入解析
- fakeapp, faceswap, deepfacelab等deepfakes换脸程序的简单对比
- UI到底应该用xib/storyboard完成,还是用手写代码来完成?
- 从零开始的Python学习Episode 6——字符串操作
热门文章
- 初次邂逅 EasyExcel
- 【Shell案例】【打印指定行用sed、for循环、head和tail配合使用】4、输出第5行的内容
- 5V升压12.6V芯片电路图,三节锂电池充电
- 使用python脚本传递参数:(三种方式可收藏)
- python之xlsx合并单元格
- 数据结构学习——BST删除特定节点
- Logseq001笔记类--视频悬浮插件--Helium
- [Webcast]Silverlight探秘系列课程
- 7、解决swagger测试接口报错:TypeError: Failed to execute ‘fetch‘ on ‘Window‘: Request with GET/HEAD method cannot have body
- [编程基础] Python中的绝对导入与相对导入