File: rdkexample2.cpp

package info (click to toggle)
rdkit 201203-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 37,840 kB
  • sloc: cpp: 93,902; python: 51,897; java: 5,192; ansic: 3,497; xml: 2,499; sql: 1,641; yacc: 1,518; lex: 1,076; makefile: 325; fortran: 183; sh: 153; cs: 51
file content (103 lines) | stat: -rw-r--r-- 2,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// $Id: rdkexample2.cpp 1528 2010-09-26 17:04:37Z glandrum $
//
//  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 <RDGeneral/Invariant.h>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/MolPickler.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>
#include <RDGeneral/RDLog.h>
#include <vector>
#include <algorithm>
#include <boost/mpi.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <cstdlib>
#include <string>

namespace mpi = boost::mpi;

void broadcastMols(mpi::communicator &world,std::vector<RDKit::ROMOL_SPTR> &mols){
  std::vector<std::string> data;
  if (world.rank() == 0) {
    data.reserve(mols.size());
    BOOST_FOREACH( const RDKit::ROMOL_SPTR &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());
    BOOST_FOREACH( 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<RDKit::ROMOL_SPTR> 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<unsigned int> res;
  std::vector<std::vector<unsigned int> > 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<unsigned int>(world.size());++i){
      std::cout<<"results from process "<<i<<": ";
      std::copy(allRes[i].begin(),allRes[i].end(),std::ostream_iterator<int,char>(std::cout, " "));
      std::cout<<std::endl;
    }

  }
  return 0;
}