// $Id$ // // Copyright (C) 2009 Greg Landrum // @@ All Rights Reserved @@ // This file is part of the RDKit. // The contents are covered by the terms of the BSD license // which is included in the file license.txt, found at the root // of the RDKit source tree. // #include #include #include #include #include #include #include #include #include #include #include #include namespace mpi = boost::mpi; void broadcastMols(mpi::communicator &world, std::vector &mols) { std::vector data; if (world.rank() == 0) { data.reserve(mols.size()); for (const auto &ptr : mols) { std::string pickle; RDKit::MolPickler::pickleMol(*ptr, pickle); data.push_back(pickle); } } broadcast(world, data, 0); if (world.rank() != 0) { mols.reserve(data.size()); for (const std::string &pickle : data) { RDKit::ROMol *mol = new RDKit::ROMol; RDKit::MolPickler::molFromPickle(pickle, mol); mols.push_back(RDKit::ROMOL_SPTR(mol)); } } } int main(int argc, char *argv[]) { mpi::environment env(argc, argv); mpi::communicator world; // construct the data: std::vector data; if (world.rank() == 0) { for (unsigned int i = 0; i < 100; ++i) { std::string txt(i + 1, 'C'); RDKit::ROMol *m = RDKit::SmilesToMol(txt); data.push_back(RDKit::ROMOL_SPTR(m)); } } // broadcast it: broadcastMols(world, data); // process it: std::vector res; std::vector> allRes; // start by finding our chunk: unsigned int nProcs = world.size(); unsigned int chunkSize = data.size() / nProcs; unsigned int extraBits = data.size() % nProcs; // handle extra bits on the root node: if (world.rank() == 0) { for (unsigned int i = 0; i < extraBits; ++i) { res.push_back(data[i]->getNumAtoms()); } } unsigned int pos = extraBits + world.rank() * chunkSize; for (unsigned int i = 0; i < chunkSize; ++i) { res.push_back(data[pos++]->getNumAtoms()); } if (world.rank() == 0) { gather(world, res, allRes, 0); } else { gather(world, res, 0); } // reporting: if (world.rank() == 0) { for (unsigned int i = 0; i < static_cast(world.size()); ++i) { std::cout << "results from process " << i << ": "; std::copy(allRes[i].begin(), allRes[i].end(), std::ostream_iterator(std::cout, " ")); std::cout << std::endl; } } return 0; }