boost::mpi::scatter 可以把一個vector中的元素傳遞給其他所有進程。
#include "stdafx.h"
#include <boost/mpi.hpp>
#include <boost/serialization/string.hpp>
#include <vector>
#include <string>
#include <iostream>
namespace mpi = boost::mpi;
int main(int argc, char *argv[]) {
boost::mpi::environment env(argc, argv);
boost::mpi::communicator world;
std::vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
//std::string s;
int s;
boost::mpi::scatter(world, v, s, 0);
std::cout << world.rank() << ": " << s << '\n';
}
如果用2個進程運行:
1 : 20
0 : 10
如果用4個進程運行:
1 : 20
0 : 10
2 : 30
3 : 40
如果用5個進程,由於vector只有4個元素,所以第5個元素無效,程序傳遞一個未定義的整數給進程4:
1 : 20
0 : 10
2 : 30
3 : 40
4 : -33686019
現在問題是如果vector中有4N個元素,現在想把他們分配到4個處理器上,應該怎麼弄?