One aim of OmniRPC is to perform parallel programming with RPC. We execute the "calc_sin" function in parallel on multiple remote hosts by showing one example of parallel programming, as in 3. Programming with OmniRPC .
There are two ways to parallel program with OmniRPC.
We assume an execution environment like the following.
We show an example of an asynchronous call with OmniRPC as follows.
#include <OmniRpc.h> #include <stdio.h> #include <math.h> int main(int argc,char *argv[]){ int i; double x, res[10]; OmniRpcRequest req[10]; OmniRpcInit(&argc,&argv); x = 0.0; for(i = 0; i < 10; i++){ req[i] = OmniRpcCallAsync("calc_sin",x,&res[i]); x += 1.0; } OmniRpcWaitAll(10,req); for(i = 0; i < 10; i++) printf("sin(%d)=%g\n",i,res[i]); OmniRpcFinalize(); }
The OmniRpcCallAsync API activates the remote procedure call and returns without waiting for its call termination. Its API returns an OmniRpcRequest value which corresponds to the remote procedure call as a return value. We store its value in an array, and by using OmniRpcWaitAll API, the program waits for termination of all RPC calls. For more details about this API, see 15. C API index.
Before program execution, you should prepare a hostfile.
<?xml version="1.0" ?> <OmniRpcConfig> <Host name="alice" /> <Host name="dennis" /> <Host name="jack" /> </OmniRpcConfig>
The rest of the procedure is the same the former example. The relationship between the agent and rex is shown below.
We can do parallel programming by calling OmniRpcCall on a multi-thread program with Omni OpenMP, which is one of OpenMP compiler of an implementation using a thread. We show an example as follows.
#include <OmniRpc.h> #include <stdio.h> #include <math.h> int main(int argc,char *argv[]){ int i; double x, res[10]; OmniRpcRequest req[10]; OmniRpcInit(&argc,&argv); x = 0.0; #pragma omp parallel for for(i = 0; i < 10; i++){ req[i] = OmniRpcCall("calc_sin",x,&res[i]); x += 1.0; } for(i = 0; i < 10; i++) printf("sin(%d)=%g\n",i,res[i]); OmniRpcFinalize(); }
When you want to run this program, remember to set the OMP_NUM_THREADS environmental value, which specifies the number of OpenMP threads, to a value more than the number of remote hosts.