$ time foo
real 0m0.003s
user 0m0.000s
sys 0m0.004s
$
Run Code Online (Sandbox Code Playgroud)
"真实","用户"和"系统"在时间输出中意味着什么?
在对我的应用进行基准测试时哪一个有意义?
任何人都可以告诉我std::sort()在<algorithm>头文件中定义的函数中实现了哪种类型的排序技术(冒泡,插入,选择,快速,合并,计数......)?
我正在阅读cppreference.com上的std :: algorithm文档,我注意到很多很酷的东西上我还没有使用C++ 17标签.我最关注的是新的执行政策.我从阅读中得到的是,我可以通过指定执行策略来创建任何我想要多线程的循环.for_each
例如,我有一个程序输出一个带有2D图形的图像.
int main(){
std::for_each(
img.buffer().begin(),
img.buffer().end(),
renderer(
{-0.5, 0.0, 2.666, 2.0, M_PI / 2.0, 0.0},
img,
16
)
);
fout << img;
}
Run Code Online (Sandbox Code Playgroud)
如果我想让这个程序多线程,我应该能够用一行来完成.
int main(){
std::for_each(
std::execution::par_unseq, // c++17 feature
img.buffer().begin(),
img.buffer().end(),
renderer(
{-0.5, 0.0, 2.666, 2.0, M_PI / 2.0, 0.0},
img,
16
)
);
fout << img;
}
Run Code Online (Sandbox Code Playgroud)
然而,当我第一次尝试这个(有g++ -std=c++17)时,我得到一个错误告诉我‘std::execution’ has not been declared,所以我尝试添加,#include <execution>但它说execution: No such file or directory …
我想知道如何从源代码安装TBB并使其在Linux系统上运行.我在使用它时遇到了一些问题,如果我通过软件包管理器安装TBB,则不会出现问题.
在TBB网页中,有一些关于如何执行此操作的指南,例如设置LD_LIBRARY_PATH和CPATH变量,或者获取tbbvars.sh文件.即使我这样做,当我尝试编译一个例子时,g ++说找不到tbb.
所以问题是如果它是如何设置所有东西(编译源代码,我应该设置哪些变量......)的简单方法,以便使用TBB.
谢谢.
注意:询问此问题时的库版本号是2(如果我没记错的话).我亲自测试了4.1版本的解决方案,但我认为它对当前版本也应该有效,4.2 (update 3)因为构建方法保持不变.
我想并行化一个 for 循环并发现 aboutstd::for_each以及它的execution policies. 令人惊讶的是,它在使用时没有并行化GCC:
#include <iostream>
#include <algorithm>
#include <execution>
#include <chrono>
#include <thread>
#include <random>
int main() {
std::vector<int> foo;
foo.reserve(1000);
for (int i = 0; i < 1000; i++) {
foo.push_back(i);
}
std::for_each(std::execution::par_unseq,
foo.begin(), foo.end(),
[](auto &&item) {
std::cout << item << std::endl;
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist6(10, 100);
std::this_thread::sleep_for(std::chrono::milliseconds(dist6(rng)));
std::cout << "Thread ID: " << std::this_thread::get_id() << std::endl;
});
}
Run Code Online (Sandbox Code Playgroud)
此代码仍按顺序运行。
使用MSVC代码是并行的,并且完成得更快。
GCC:
$ gcc …Run Code Online (Sandbox Code Playgroud) 我想使用并行版本,std::sort我可以在其中指定执行策略,例如std::execution::par_unseq.
我目前正在 Ubuntu Linux 下使用clang++-10和g++ 7.5.0,但是两者都没有找到所需的包含文件execution,因此显然这些编译器版本尚不支持并行算法。
有人可以告诉我哪个版本clang支持gcc此功能吗?
我需要对存储在结构数组中的数据块进行排序.结构没有指针.每个块都有一个计数器编号和一个数组中等于结构块的数组的位置坐标.例如,如果我们有一个数据数组,我们可以划分为4个NxN块,我们在结构块的索引数组中有4个结构块,每个结构块在数据数组中有自己的数字和位置,借助我们可以计算使用索引块的数据数组中块的指针.应该使用比较器来进行排序,该比较器以这样的方式比较两个块,使得两个块中的至少两个具有最少的第i个数据.例如比较器:
for( i = 0; i < N * N; ++i )
{
if( a[i] < b[i] ) return -1;
if( a[i] > b[i] ) return 1;
}
Run Code Online (Sandbox Code Playgroud)
where a和b是指向数据数组的指针,由于索引数组和数据数组开始的指针,我们可以得到它们.排序不应该排序数据数组而是排序索引数组.所以问题是:我可以使用哪种并行算法(除了框架,库,我需要完全算法或标准语言工具包,如pthread或qt libs,或c/c ++标准库)以避免同步错误?代码或伪代码也会有所帮助.
我读了这些主题:
使用 gcc 在 Linux 上运行线程构建模块 (Intel TBB)
无法将英特尔 TBB 库与 /usr/lib 中的 libtbb 链接
但仍然收到此错误:
piCalc.cpp:8:17: fatal error: ttb.h: No such file or directory
#include "ttb.h"
Run Code Online (Sandbox Code Playgroud)
我计算 pi 的代码始于
#include <iostream>
#include "ttb.h"
#include "parallel_for.h"
Run Code Online (Sandbox Code Playgroud)
运行安装了 nvidia 和 cuda 的 Ubuntu 14.04。为了安装 TBB,我从以下开始:
sudo apt-get install libtbb-dev
Run Code Online (Sandbox Code Playgroud)
它说
Reading package lists... Done
Building dependency tree
Reading state information... Done
libtbb-dev is already the newest version.
The following package was automatically installed and is no …Run Code Online (Sandbox Code Playgroud) 我尝试以vector<vector<int>>非循环的方式实现对a的所有元素的总结。
之前,我已经检查了一些相关的问题,如何总结C ++向量的元素?。
因此,我尝试使用std::accumulate它来实现它,但是我发现很难重载Binary Operatorin std::accumulate并实现它。
因此,我对如何实现它感到困惑,std::accumulate还是有更好的方法?
如果不介意有人可以帮助我吗?
提前致谢。
我正在尝试使用 C++ 执行器。这是我在https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p0443r14.html中找到的代码。gcc 11 应该支持它。
\n我用命令编译了这段代码
\ng++-11 main.cpp -ltbb\nRun Code Online (Sandbox Code Playgroud)\n#include <iostream>\n#include <execution>\nusing namespace std::execution;\n\nint main() {\n std::static_thread_pool pool(16);\n executor auto ex = pool.executor();\n perform_business_logic(ex);\n execute(ex, []{ std::cout << "Hello world from the thread pool!"; });\n}\nRun Code Online (Sandbox Code Playgroud)\n它给出了很多错误。
\nerror: \xe2\x80\x98thread_pool\xe2\x80\x99 is not a member of \xe2\x80\x98std\xe2\x80\x99\nerror: \xe2\x80\x98executor\xe2\x80\x99 was not declared in this scope\nerror: \xe2\x80\x98ex\xe2\x80\x99 was not declared in this scope\nRun Code Online (Sandbox Code Playgroud)\n c++ ×9
c++17 ×2
gcc ×2
linux ×2
sorting ×2
tbb ×2
algorithm ×1
benchmarking ×1
c ×1
c++20 ×1
optimization ×1
std ×1
stdvector ×1
stl ×1
terminology ×1
time ×1
unix ×1
visual-c++ ×1