Commit 65df9c2a authored by Philip Carns's avatar Philip Carns
Browse files

swap hello-world for sum example

parent 16d69809
......@@ -177,11 +177,17 @@ module list
## Compiling an example Mochi code
```
cd ~/bootcamp/mochi-boot-camp/sessions/s2-hands-on/hello-world
cd ~/bootcamp/mochi-boot-camp/sessions/s2-hands-on/sum
make
```
The above example will compile a simple client and server program, linked
The above example is a very slightly modified (to use shared memory instead
of TCP for communication) copy of the [Sending arguments, returning
values](https://mochi.readthedocs.io/en/latest/margo/03_sum.html#) example
from the [Mochi Readthedocs
page](https://mochi.readthedocs.io/en/latest/index.html).
This will compile a simple client and server program, linked
against margo. If you inspect the Makfile you will see pkg-config commands
that are used to find the correct CFLAGS and LDFLAGS for the build.
......@@ -208,7 +214,7 @@ the original interactive terminal that was created with the qsub command.
## Running an example Mochi code
```
cd /home/carns/bootcamp/mochi-boot-camp/sessions/s2-hands-on/hello-world
cd /home/carns/bootcamp/mochi-boot-camp/sessions/s2-hands-on/sum
# in one terminal:
./server
......
#include <assert.h>
#include <stdio.h>
#include <margo.h>
int main(int argc, char** argv)
{
if(argc != 2) {
fprintf(stderr,"Usage: %s <server address>\n", argv[0]);
exit(0);
}
hg_return_t ret;
margo_instance_id mid = MARGO_INSTANCE_NULL;
mid = margo_init("na+sm",MARGO_CLIENT_MODE, 0, 0);
assert(mid);
hg_id_t hello_rpc_id = MARGO_REGISTER(mid, "hello", void, void, NULL);
margo_registered_disable_response(mid, hello_rpc_id, HG_TRUE);
hg_addr_t svr_addr;
ret = margo_addr_lookup(mid, argv[1], &svr_addr);
assert(ret == HG_SUCCESS);
hg_handle_t handle;
ret = margo_create(mid, svr_addr, hello_rpc_id, &handle);
assert(ret == HG_SUCCESS);
ret = margo_forward(handle, NULL);
assert(ret == HG_SUCCESS);
ret = margo_destroy(handle);
assert(ret == HG_SUCCESS);
ret = margo_addr_free(mid, svr_addr);
assert(ret == HG_SUCCESS);
margo_finalize(mid);
return 0;
}
......@@ -3,13 +3,13 @@ LDFLAGS += `pkg-config --libs margo`
all:: client server
client.o: client.c
client.o: client.c types.h
$(CC) $(CFLAGS) -c client.c
client: client.o
$(CC) client.o -o client $(LDFLAGS)
server.o: server.c
server.o: server.c types.h
$(CC) $(CFLAGS) -c server.c
server: server.o
......
#include <assert.h>
#include <stdio.h>
#include <margo.h>
#include "types.h"
int main(int argc, char** argv)
{
if(argc != 2) {
fprintf(stderr,"Usage: %s <server address>\n", argv[0]);
exit(0);
}
margo_instance_id mid = margo_init("na+sm", MARGO_CLIENT_MODE, 0, 0);
hg_id_t sum_rpc_id = MARGO_REGISTER(mid, "sum", sum_in_t, sum_out_t, NULL);
hg_addr_t svr_addr;
margo_addr_lookup(mid, argv[1], &svr_addr);
int i;
sum_in_t args;
for(i=0; i<4; i++) {
args.x = 42+i*2;
args.y = 42+i*2+1;
hg_handle_t h;
margo_create(mid, svr_addr, sum_rpc_id, &h);
margo_forward(h, &args);
sum_out_t resp;
margo_get_output(h, &resp);
printf("Got response: %d+%d = %d\n", args.x, args.y, resp.ret);
margo_free_output(h,&resp);
margo_destroy(h);
}
margo_addr_free(mid, svr_addr);
margo_finalize(mid);
return 0;
}
#include <assert.h>
#include <stdio.h>
#include <margo.h>
#include "types.h"
static const int TOTAL_RPCS = 4;
static int num_rpcs = 0;
typedef struct {
int max_rpcs;
int num_rpcs;
} server_data;
static void hello_world(hg_handle_t h);
DECLARE_MARGO_RPC_HANDLER(hello_world)
static void sum(hg_handle_t h);
DECLARE_MARGO_RPC_HANDLER(sum)
int main(int argc, char** argv)
{
margo_instance_id mid = margo_init("na+sm", MARGO_SERVER_MODE, 0, -1);
margo_instance_id mid = margo_init("na+sm", MARGO_SERVER_MODE, 0, 0);
assert(mid);
server_data svr_data = {
.max_rpcs = 4,
.num_rpcs = 0
};
hg_addr_t my_address;
margo_addr_self(mid, &my_address);
char addr_str[128];
......@@ -21,28 +29,44 @@ int main(int argc, char** argv)
margo_addr_free(mid,my_address);
printf("Server running at address %s\n", addr_str);
hg_id_t rpc_id = MARGO_REGISTER(mid, "hello", void, void, hello_world);
margo_registered_disable_response(mid, rpc_id, HG_TRUE);
hg_id_t rpc_id = MARGO_REGISTER(mid, "sum", sum_in_t, sum_out_t, sum);
margo_register_data(mid, rpc_id, &svr_data, NULL);
margo_wait_for_finalize(mid);
return 0;
}
static void hello_world(hg_handle_t h)
static void sum(hg_handle_t h)
{
hg_return_t ret;
sum_in_t in;
sum_out_t out;
margo_instance_id mid = margo_hg_handle_get_instance(h);
printf("Hello World!\n");
num_rpcs += 1;
const struct hg_info* info = margo_get_info(h);
server_data* svr_data = (server_data*)margo_registered_data(mid, info->id);
ret = margo_get_input(h, &in);
assert(ret == HG_SUCCESS);
out.ret = in.x + in.y;
printf("Computed %d + %d = %d\n",in.x,in.y,out.ret);
ret = margo_respond(h, &out);
assert(ret == HG_SUCCESS);
ret = margo_free_input(h, &in);
assert(ret == HG_SUCCESS);
ret = margo_destroy(h);
assert(ret == HG_SUCCESS);
if(num_rpcs == TOTAL_RPCS) {
svr_data->num_rpcs += 1;
if(svr_data->num_rpcs == svr_data->max_rpcs) {
margo_finalize(mid);
}
}
DEFINE_MARGO_RPC_HANDLER(hello_world)
DEFINE_MARGO_RPC_HANDLER(sum)
#ifndef PARAM_H
#define PARAM_H
#include <mercury.h>
#include <mercury_macros.h>
/* We use the Mercury macros to define the input
* and output structures along with the serialization
* functions.
*/
MERCURY_GEN_PROC(sum_in_t,
((int32_t)(x))\
((int32_t)(y)))
MERCURY_GEN_PROC(sum_out_t, ((int32_t)(ret)))
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment