如何在并行编程中收集从机到主机返回的值
How to collect values returned from slave to master in parallel programming?
我正试图找到一种正确的方法来收集并行编程中从机返回到主机的值。我之前也问过类似的问题,关于如何划分工作来计算mandelbrot的像素。我得到了如何发送作品的答案,但仍在努力收集数据并将其绘制为像素。
节点0:(主(
节点1:(从(
v[0] = {2,3,4,5,67,86,56,5} // core 0 holds value of 8 threads of this core
v[1] = {12,21,4,3,54,65,6,5,4} // core 1 holds value of 9 threads of this core
v[2] = {1,3,4,54,6,5,65,7,4} //core 2 holds value of 9 threads of this core
节点2:(从(
v[0] = {2,3,4,5,67,86,56,5} // core 0
v[1] = {12,21,4,3,54,65,6,5,4} // core 1
v[2] = {1,3,4,54,6,5,65,7,4} //core 2
节点3:(从(
v[0] = {2,3,4,5,67,86,56,5} // core 0
v[1] = {12,21,4,3,54,65,6,5,4} // core 1
v[2] = {1,3,4,54,6,5,65,7,4} //core 2
因此,当master想要这些值时,slave应该附加向量并发送,或者有其他更好的方法将值传递给master吗?
如果您使用的是C++11线程库(或Boost.thread(,那么您可能想要的是std::future
。它们可以通过以下三种方式之一获得:
- 通过将
std::promise
传递给线程并让它设置值 - 通过使用CCD_ 3
- 通过调用
std::async
以下是使用std::async
:的示例
some_return_type my_work_function( some_input_type const & );
some_input_type inputs_to_slave_threads[] = { /* ... */ };
std::future< some_return_type >
// launch the slaves, letting the OS decide whether to spawn new threads
slave_0_future = std::async( my_work_function, std::ref( inputs_to_slave_threads[0] ) ),
slave_1_future = std::async( my_work_function, std::ref( inputs_to_slave_threads[1] ) ),
// ...
slave_N_future = std::async( my_work_function, std::ref( inputs_to_slave_threads[N] ) );
some_return_type
// block until results are ready
result_of_slave_0 = slave_0_future.get(),
result_of_slave_1 = slave_1_future.get(),
// ...
result_of_slave_N = slave_N_future.get();
process_results_of_slaves( result_of_slave_0, ..., result_of_slave_N );
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- (C++)分析树以计算返回错误值的简单算术表达式
- 访问者访问变体并返回不同类型时出错
- 如何返回一个类的两个对象相加的结果
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- lower_bound()返回最后一个元素
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何取消对nullptr的屏蔽,返回正确的对象
- cudaMemcpy 在从设备读取到主机时返回 cudaErrorInvalidArgument,不清楚原因
- getPeername()在客户端在虚拟机上运行时返回我的本地主机地址
- 是在C 11中没有返回类型的主机的使用情况
- WinAPI:OpenProcess()返回错误5,并为主机进程启用SeDebugPrivilege
- 如何在并行编程中收集从机到主机返回的值