GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rpc_example2.cpp
1 /*
2  * Copyright (c) 2009 Carnegie Mellon University.
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an "AS
13  * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14  * express or implied. See the License for the specific language
15  * governing permissions and limitations under the License.
16  *
17  * For more about this software visit:
18  *
19  * http://www.graphlab.ml.cmu.edu
20  *
21  */
22 
23 
24 
25 #include <iostream>
26 #include <string>
27 #include <sstream>
28 #include <vector>
29 #include <graphlab/rpc/dc.hpp>
30 #include <graphlab/serialization/serialization_includes.hpp>
31 using namespace graphlab;
32 
33 
34 void print(std::string val) {
35  std::cout << val << std::endl;
36 }
37 
38 std::vector<int> add_one(std::vector<int> val) {
39  val.push_back(1);
40  return val;
41 }
42 
43 
44 int main(int argc, char ** argv) {
45  // init MPI
46  mpi_tools::init(argc, argv);
48 
49  if (dc.numprocs() != 2) {
50  std::cout<< "RPC Example 2: Asynchronous RPC with Built-in Serialization\n";
51  std::cout << "Run with exactly 2 MPI nodes.\n";
52  return 0;
53  }
54 
55  dc.barrier();
56  if (dc.procid() == 0) {
57  dc.remote_call(1, print, "hello world!");
58  // Create a vector with a few elements
59  std::vector<int> vec;
60  vec.push_back(1); vec.push_back(2);
61  // Call the remote machine
62  vec = dc.remote_request(1, add_one, vec);
63 
64  std::stringstream strm;
65  // Print the vector
66  for (size_t i = 0; i < vec.size(); ++i) {
67  strm << vec[i] << ", ";
68  }
69  strm << std::endl;
70  strm.flush();
71  dc.remote_call(1, print, strm.str());
72  }
73  dc.barrier();
74 
75  mpi_tools::finalize();
76 }
77