rpc_example1.cpp

00001 #include <iostream>
00002 #include <graphlab/util/mpi_tools.hpp>
00003 #include <graphlab/rpc/dc.hpp>
00004 #include <graphlab/rpc/dc_init_from_mpi.hpp>
00005 using namespace graphlab;
00006 
00007 
00008 void print(int val) {
00009   std::cout << val << std::endl;
00010 }
00011 
00012 int add_one(int val) {
00013   return val + 1;
00014 }
00015 
00016 
00017 int main(int argc, char ** argv) {
00018   // init MPI
00019   mpi_tools::init(argc, argv);
00020   
00021   if (mpi_tools::size() != 2) {
00022     std::cout<< "RPC Example 1: Basic Synchronous RPC\n";
00023     std::cout << "Run with exactly 2 MPI nodes.\n";
00024     return 0;
00025   }
00026   // set up parameters
00027   dc_init_param param;
00028   ASSERT_TRUE(init_param_from_mpi(param));
00029   // not necessary. Set log level. Change to LOG_WARNING to get less output
00030   global_logger().set_log_level(LOG_INFO);
00031   // create distributed control
00032   distributed_control dc(param);
00033   
00034   if (dc.procid() == 0) {
00035     dc.control_call(1, print, 10);
00036     std::cout << "5 plus 1 is : " << dc.remote_request(1, add_one, 5) << std::endl;
00037     std::cout << "11 plus 1 is : " << dc.remote_request(1, add_one, 11) << std::endl;
00038   }
00039   dc.barrier();
00040   // terminate MPI
00041   mpi_tools::finalize();
00042 }