bake-bulk-client.c 15.8 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1
2
3
4
5
6
/*
 * (C) 2016 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

7
#include <assert.h>
8
#include <bake-bulk-client.h>
Philip Carns's avatar
Philip Carns committed
9
#include <margo.h>
Philip Carns's avatar
Philip Carns committed
10
#include "uthash.h"
11
#include "bake-bulk-rpc.h"
Philip Carns's avatar
Philip Carns committed
12

13
/* Refers to a single Margo initialization, for now this is shared by
14
15
16
 * all remote targets.  In the future we probably need to support multiple in
 * case we run atop more than one transport at a time.
 */
17
struct bake_margo_instance
18
19
{
    margo_instance_id mid;  
20

21
    hg_id_t bake_bulk_probe_id;
22
23
    hg_id_t bake_bulk_shutdown_id; 
    hg_id_t bake_bulk_create_id;
Philip Carns's avatar
Philip Carns committed
24
    hg_id_t bake_bulk_eager_write_id;
Philip Carns's avatar
Philip Carns committed
25
    hg_id_t bake_bulk_eager_read_id;
26
27
28
29
    hg_id_t bake_bulk_write_id;
    hg_id_t bake_bulk_persist_id;
    hg_id_t bake_bulk_get_size_id;
    hg_id_t bake_bulk_read_id;
Philip Carns's avatar
Philip Carns committed
30
    hg_id_t bake_bulk_noop_id;
31
32
33
};

/* Refers to an instance connected to a specific target */
Philip Carns's avatar
Philip Carns committed
34
35
struct bake_instance
{
36
37
38
    bake_target_id_t bti;   /* persistent identifier for this target */
    hg_addr_t dest;         /* resolved Mercury address */
    UT_hash_handle hh;
Philip Carns's avatar
Philip Carns committed
39
40
};

41
42
struct bake_instance *instance_hash = NULL;

Philip Carns's avatar
Philip Carns committed
43

44
struct bake_margo_instance g_margo_inst = {
45
46
    .mid = MARGO_INSTANCE_NULL,
};
Philip Carns's avatar
Philip Carns committed
47

Philip Carns's avatar
Philip Carns committed
48
49
50
51
52
53
static int bake_bulk_eager_read(
    bake_target_id_t bti,
    bake_bulk_region_id_t rid,
    uint64_t region_offset,
    void *buf,
    uint64_t buf_size);
Shane Snyder's avatar
Shane Snyder committed
54

55
56
57
58
/* XXX calling this function again just overwrites the previous global mid...
 * need to be smarter if we truly want to support multiple client-side mids
 */
static int bake_margo_instance_init(margo_instance_id mid)
Philip Carns's avatar
Philip Carns committed
59
{
60
    g_margo_inst.mid = mid;
Philip Carns's avatar
Philip Carns committed
61
62

    /* register RPCs */
63
64
    g_margo_inst.bake_bulk_probe_id = 
        MARGO_REGISTER(g_margo_inst.mid, 
65
66
        "bake_bulk_probe_rpc", void, bake_bulk_probe_out_t, 
        NULL);
67
68
    g_margo_inst.bake_bulk_shutdown_id = 
        MARGO_REGISTER(g_margo_inst.mid, 
Philip Carns's avatar
Philip Carns committed
69
70
        "bake_bulk_shutdown_rpc", void, void, 
        NULL);
71
72
    g_margo_inst.bake_bulk_create_id = 
        MARGO_REGISTER(g_margo_inst.mid, 
73
74
75
76
        "bake_bulk_create_rpc", 
        bake_bulk_create_in_t,
        bake_bulk_create_out_t,
        NULL);
77
78
    g_margo_inst.bake_bulk_write_id = 
        MARGO_REGISTER(g_margo_inst.mid, 
79
80
81
82
        "bake_bulk_write_rpc", 
        bake_bulk_write_in_t,
        bake_bulk_write_out_t,
        NULL);
83
84
    g_margo_inst.bake_bulk_eager_write_id = 
        MARGO_REGISTER(g_margo_inst.mid, 
Philip Carns's avatar
Philip Carns committed
85
86
87
88
        "bake_bulk_eager_write_rpc", 
        bake_bulk_eager_write_in_t,
        bake_bulk_eager_write_out_t,
        NULL);
89
90
    g_margo_inst.bake_bulk_eager_read_id = 
        MARGO_REGISTER(g_margo_inst.mid, 
Philip Carns's avatar
Philip Carns committed
91
92
93
94
        "bake_bulk_eager_read_rpc", 
        bake_bulk_eager_read_in_t,
        bake_bulk_eager_read_out_t,
        NULL);
95
96
    g_margo_inst.bake_bulk_persist_id = 
        MARGO_REGISTER(g_margo_inst.mid, 
Philip Carns's avatar
Philip Carns committed
97
98
99
100
        "bake_bulk_persist_rpc", 
        bake_bulk_persist_in_t,
        bake_bulk_persist_out_t,
        NULL);
101
102
    g_margo_inst.bake_bulk_get_size_id = 
        MARGO_REGISTER(g_margo_inst.mid, 
103
104
105
106
        "bake_bulk_get_size_rpc", 
        bake_bulk_get_size_in_t,
        bake_bulk_get_size_out_t,
        NULL);
107
108
    g_margo_inst.bake_bulk_read_id = 
        MARGO_REGISTER(g_margo_inst.mid, 
109
110
111
112
        "bake_bulk_read_rpc", 
        bake_bulk_read_in_t,
        bake_bulk_read_out_t,
        NULL);
113
114
    g_margo_inst.bake_bulk_noop_id = 
        MARGO_REGISTER(g_margo_inst.mid, 
Philip Carns's avatar
Philip Carns committed
115
116
117
118
        "bake_bulk_noop_rpc", 
        void,
        void,
        NULL);
Philip Carns's avatar
Philip Carns committed
119

120
121
122
123
    return(0);
}

int bake_probe_instance(
124
125
    margo_instance_id mid,
    hg_addr_t dest_addr,
126
127
128
129
    bake_target_id_t *bti)
{
    hg_return_t hret;
    int ret;
130
131
    bake_bulk_probe_out_t out;
    hg_handle_t handle;
132
    struct bake_instance *new_instance;
133

134
    ret = bake_margo_instance_init(mid);
135
136
    if(ret < 0)
        return(ret);
Philip Carns's avatar
Philip Carns committed
137

138
139
140
141
    new_instance = calloc(1, sizeof(*new_instance));
    if(!new_instance)
        return(-1);

142
    hret = margo_addr_dup(g_margo_inst.mid, dest_addr, &new_instance->dest);
Philip Carns's avatar
Philip Carns committed
143
144
    if(hret != HG_SUCCESS)
    {
145
        free(new_instance);
Philip Carns's avatar
Philip Carns committed
146
147
148
        return(-1);
    }

149
    /* create handle */
150
151
    hret = margo_create(g_margo_inst.mid, new_instance->dest, 
        g_margo_inst.bake_bulk_probe_id, &handle);
152
153
    if(hret != HG_SUCCESS)
    {
154
        margo_addr_free(g_margo_inst.mid, new_instance->dest);
155
        free(new_instance);
156
157
158
        return(-1);
    }

159
    hret = margo_forward(handle, NULL);
160
161
    if(hret != HG_SUCCESS)
    {
162
        margo_destroy(handle);
163
164
        margo_addr_free(g_margo_inst.mid, new_instance->dest);
        free(new_instance);
165
166
167
        return(-1);
    }

168
    hret = margo_get_output(handle, &out);
169
170
    if(hret != HG_SUCCESS)
    {
171
        margo_destroy(handle);
172
173
        margo_addr_free(g_margo_inst.mid, new_instance->dest);
        free(new_instance);
174
175
176
177
178
        return(-1);
    }

    ret = out.ret;
    *bti = out.bti;
179
    new_instance->bti = out.bti;
180

181
182
    margo_free_output(handle, &out);
    margo_destroy(handle);
183

184
185
    if(ret != 0)
    {
186
        margo_addr_free(g_margo_inst.mid, new_instance->dest);
187
188
189
190
191
192
193
194
        free(new_instance);
    }
    else
    {
        /* TODO: safety check that it isn't already there.  Here or earlier? */
        HASH_ADD(hh, instance_hash, bti, sizeof(new_instance->bti), new_instance);
    }

195
    return(ret);
Philip Carns's avatar
Philip Carns committed
196
197
198
199
200
}
  
void bake_release_instance(
    bake_target_id_t bti)
{
201
202
203
204
205
206
207
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return;
    
    HASH_DELETE(hh, instance_hash, instance);
208
    margo_addr_free(g_margo_inst.mid, instance->dest);
209
210
    free(instance);

Philip Carns's avatar
Philip Carns committed
211
212
213
214
215
216
    return;
}

int bake_shutdown_service(bake_target_id_t bti)
{
    hg_return_t hret;
217
    struct bake_instance *instance = NULL;
Shane Snyder's avatar
Shane Snyder committed
218
    hg_handle_t handle;
219
220
221
222

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);
Philip Carns's avatar
Philip Carns committed
223

Shane Snyder's avatar
Shane Snyder committed
224
225
226
227
    hret = margo_create(g_margo_inst.mid, instance->dest, 
        g_margo_inst.bake_bulk_shutdown_id, &handle);
    if(hret != HG_SUCCESS)
        return(-1);
Philip Carns's avatar
Philip Carns committed
228

Shane Snyder's avatar
Shane Snyder committed
229
    hret = margo_forward(handle, NULL);
Philip Carns's avatar
Philip Carns committed
230
231
    if(hret != HG_SUCCESS)
    {
Shane Snyder's avatar
Shane Snyder committed
232
        margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
233
234
235
        return(-1);
    }

Shane Snyder's avatar
Shane Snyder committed
236
    margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
237
238
    return(0);
}
Philip Carns's avatar
Philip Carns committed
239

Philip Carns's avatar
Philip Carns committed
240
static int bake_bulk_eager_write(
Philip Carns's avatar
Philip Carns committed
241
242
243
244
245
246
247
    bake_target_id_t bti,
    bake_bulk_region_id_t rid,
    uint64_t region_offset,
    void const *buf,
    uint64_t buf_size)
{
    hg_return_t hret;
Shane Snyder's avatar
Shane Snyder committed
248
    hg_handle_t handle;
Philip Carns's avatar
Philip Carns committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
    bake_bulk_eager_write_in_t in;
    bake_bulk_eager_write_out_t out;
    int ret;
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);

    in.bti = bti;
    in.rid = rid;
    in.region_offset = region_offset;
    in.size = buf_size;
    in.buffer = (char*)buf;
263
  
Shane Snyder's avatar
Shane Snyder committed
264
265
266
267
    hret = margo_create(g_margo_inst.mid, instance->dest, 
        g_margo_inst.bake_bulk_eager_write_id, &handle);
    if(hret != HG_SUCCESS)
        return(-1);
Philip Carns's avatar
Philip Carns committed
268

Shane Snyder's avatar
Shane Snyder committed
269
    hret = margo_forward(handle, &in);
Philip Carns's avatar
Philip Carns committed
270
271
    if(hret != HG_SUCCESS)
    {
Shane Snyder's avatar
Shane Snyder committed
272
        margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
273
274
275
        return(-1);
    }

Shane Snyder's avatar
Shane Snyder committed
276
    hret = margo_get_output(handle, &out);
Philip Carns's avatar
Philip Carns committed
277
278
    if(hret != HG_SUCCESS)
    {
Shane Snyder's avatar
Shane Snyder committed
279
        margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
280
281
282
283
284
        return(-1);
    }
    
    ret = out.ret;

Shane Snyder's avatar
Shane Snyder committed
285
286
    margo_free_output(handle, &out);
    margo_destroy(handle);
287

Philip Carns's avatar
Philip Carns committed
288
289
290
    return(ret);
}

Philip Carns's avatar
Philip Carns committed
291
#define BAKE_BULK_EAGER_LIMIT 2048
Philip Carns's avatar
Philip Carns committed
292

Philip Carns's avatar
Philip Carns committed
293
294
295
296
297
298
299
int bake_bulk_write(
    bake_target_id_t bti,
    bake_bulk_region_id_t rid,
    uint64_t region_offset,
    void const *buf,
    uint64_t buf_size)
{
Philip Carns's avatar
Philip Carns committed
300
    hg_return_t hret;
Shane Snyder's avatar
Shane Snyder committed
301
    hg_handle_t handle;
Philip Carns's avatar
Philip Carns committed
302
303
304
    bake_bulk_write_in_t in;
    bake_bulk_write_out_t out;
    int ret;
305
306
    struct bake_instance *instance = NULL;

Philip Carns's avatar
Philip Carns committed
307
308
309
    if(buf_size <= BAKE_BULK_EAGER_LIMIT)
        return(bake_bulk_eager_write(bti, rid, region_offset, buf, buf_size));

310
311
312
    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);
Philip Carns's avatar
Philip Carns committed
313
314
315
316

    in.bti = bti;
    in.rid = rid;
    in.region_offset = region_offset;
317
318
319
    in.bulk_offset = 0;
    in.bulk_size = buf_size;
    in.remote_addr_str = NULL; /* set remote_addr to NULL to disable proxy write */
Philip Carns's avatar
Philip Carns committed
320

321
    hret = margo_bulk_create(g_margo_inst.mid, 1, (void**)(&buf), &buf_size, 
Philip Carns's avatar
Philip Carns committed
322
        HG_BULK_READ_ONLY, &in.bulk_handle);
Shane Snyder's avatar
Shane Snyder committed
323
324
325
326
327
    if(hret != HG_SUCCESS)
        return(-1);
   
    hret = margo_create(g_margo_inst.mid, instance->dest, 
        g_margo_inst.bake_bulk_write_id, &handle);
Philip Carns's avatar
Philip Carns committed
328
329
    if(hret != HG_SUCCESS)
    {
Shane Snyder's avatar
Shane Snyder committed
330
        margo_bulk_free(in.bulk_handle);
Philip Carns's avatar
Philip Carns committed
331
332
333
        return(-1);
    }

Shane Snyder's avatar
Shane Snyder committed
334
    hret = margo_forward(handle, &in);
Philip Carns's avatar
Philip Carns committed
335
336
    if(hret != HG_SUCCESS)
    {
337
        margo_bulk_free(in.bulk_handle);
Shane Snyder's avatar
Shane Snyder committed
338
        margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
339
340
341
        return(-1);
    }

Shane Snyder's avatar
Shane Snyder committed
342
    hret = margo_get_output(handle, &out);
Philip Carns's avatar
Philip Carns committed
343
344
    if(hret != HG_SUCCESS)
    {
345
        margo_bulk_free(in.bulk_handle);
Shane Snyder's avatar
Shane Snyder committed
346
        margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
347
348
349
350
351
        return(-1);
    }
    
    ret = out.ret;

Shane Snyder's avatar
Shane Snyder committed
352
    margo_free_output(handle, &out);
353
    margo_bulk_free(in.bulk_handle);
Shane Snyder's avatar
Shane Snyder committed
354
    margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
355
    return(ret);
Philip Carns's avatar
Philip Carns committed
356
357
}

358
359
360
361
362
363
int bake_bulk_proxy_write(
    bake_target_id_t bti,
    bake_bulk_region_id_t rid,
    uint64_t region_offset,
    hg_bulk_t remote_bulk,
    uint64_t remote_offset,
364
    const char* remote_addr,
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
    uint64_t size)
{
    hg_return_t hret;
    hg_handle_t handle;
    bake_bulk_write_in_t in;
    bake_bulk_write_out_t out;
    struct bake_instance *instance = NULL;
    int ret;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);

    in.bti = bti;
    in.rid = rid;
    in.region_offset = region_offset;
    in.bulk_handle = remote_bulk;
    in.bulk_offset = remote_offset;
    in.bulk_size = size;
384
    in.remote_addr_str = (char*)remote_addr;
Shane Snyder's avatar
Shane Snyder committed
385

386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
    hret = margo_create(g_margo_inst.mid, instance->dest,
        g_margo_inst.bake_bulk_write_id, &handle);
    if(hret != HG_SUCCESS)
        return(-1);

    hret = margo_forward(handle, &in);
    if(hret != HG_SUCCESS)
    {   
        margo_destroy(handle);
        return(-1);
    }
    
    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS)
    {   
        margo_destroy(handle);
        return(-1);
    }

    ret = out.ret;

    margo_free_output(handle, &out);
    margo_destroy(handle);
    return(ret);
}

Philip Carns's avatar
Philip Carns committed
412
413
414
415
416
int bake_bulk_create(
    bake_target_id_t bti,
    uint64_t region_size,
    bake_bulk_region_id_t *rid)
{
417
    hg_return_t hret;
Shane Snyder's avatar
Shane Snyder committed
418
    hg_handle_t handle;
419
420
421
    bake_bulk_create_in_t in;
    bake_bulk_create_out_t out;
    int ret;
422
423
424
425
426
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);
427

Philip Carns's avatar
Philip Carns committed
428
429
430
    in.bti = bti;
    in.region_size = region_size;

Shane Snyder's avatar
Shane Snyder committed
431
432
433
434
    hret = margo_create(g_margo_inst.mid, instance->dest,
        g_margo_inst.bake_bulk_create_id, &handle);
    if(hret != HG_SUCCESS)
        return(-1);
435

Shane Snyder's avatar
Shane Snyder committed
436
    hret = margo_forward(handle, &in);
437
438
    if(hret != HG_SUCCESS)
    {
Shane Snyder's avatar
Shane Snyder committed
439
        margo_destroy(handle);
440
441
        return(-1);
    }
Philip Carns's avatar
Philip Carns committed
442

Shane Snyder's avatar
Shane Snyder committed
443
    hret = margo_get_output(handle, &out);
444
445
    if(hret != HG_SUCCESS)
    {
Shane Snyder's avatar
Shane Snyder committed
446
        margo_destroy(handle);
447
448
449
450
451
452
        return(-1);
    }

    ret = out.ret;
    *rid = out.rid;

Shane Snyder's avatar
Shane Snyder committed
453
454
    margo_free_output(handle, &out);
    margo_destroy(handle);
455
    return(ret);
Philip Carns's avatar
Philip Carns committed
456
457
458
}


Philip Carns's avatar
Philip Carns committed
459
460
461
462
int bake_bulk_persist(
    bake_target_id_t bti,
    bake_bulk_region_id_t rid)
{
Philip Carns's avatar
Philip Carns committed
463
    hg_return_t hret;
Shane Snyder's avatar
Shane Snyder committed
464
    hg_handle_t handle;
Philip Carns's avatar
Philip Carns committed
465
466
467
    bake_bulk_persist_in_t in;
    bake_bulk_persist_out_t out;
    int ret;
468
469
470
471
472
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);
Philip Carns's avatar
Philip Carns committed
473
474
475
476

    in.bti = bti;
    in.rid = rid;

Shane Snyder's avatar
Shane Snyder committed
477
478
479
480
    hret = margo_create(g_margo_inst.mid, instance->dest,
        g_margo_inst.bake_bulk_persist_id, &handle);
    if(hret != HG_SUCCESS)
        return(-1);
Philip Carns's avatar
Philip Carns committed
481

Shane Snyder's avatar
Shane Snyder committed
482
    hret = margo_forward(handle, &in);
Philip Carns's avatar
Philip Carns committed
483
484
    if(hret != HG_SUCCESS)
    {
Shane Snyder's avatar
Shane Snyder committed
485
        margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
486
487
488
        return(-1);
    }

Shane Snyder's avatar
Shane Snyder committed
489
    hret = margo_get_output(handle, &out);
Philip Carns's avatar
Philip Carns committed
490
491
    if(hret != HG_SUCCESS)
    {
Shane Snyder's avatar
Shane Snyder committed
492
        margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
493
494
495
496
497
        return(-1);
    }

    ret = out.ret;

Shane Snyder's avatar
Shane Snyder committed
498
499
    margo_free_output(handle, &out);
    margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
500
    return(ret);
Philip Carns's avatar
Philip Carns committed
501
}
Philip Carns's avatar
Philip Carns committed
502

503
504
505
506
507
508
int bake_bulk_get_size(
    bake_target_id_t bti,
    bake_bulk_region_id_t rid,
    uint64_t *region_size)
{
    hg_return_t hret;
Shane Snyder's avatar
Shane Snyder committed
509
    hg_handle_t handle;
510
511
512
    bake_bulk_get_size_in_t in;
    bake_bulk_get_size_out_t out;
    int ret;
513
514
515
516
517
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);
518
519
520
521

    in.bti = bti;
    in.rid = rid;

Shane Snyder's avatar
Shane Snyder committed
522
523
524
525
    hret = margo_create(g_margo_inst.mid, instance->dest,
        g_margo_inst.bake_bulk_get_size_id, &handle);
    if(hret != HG_SUCCESS)
        return(-1);
526

Shane Snyder's avatar
Shane Snyder committed
527
    hret = margo_forward(handle, &in);
528
529
    if(hret != HG_SUCCESS)
    {
Shane Snyder's avatar
Shane Snyder committed
530
        margo_destroy(handle);
531
532
533
        return(-1);
    }

Shane Snyder's avatar
Shane Snyder committed
534
    hret = margo_get_output(handle, &out);
535
536
    if(hret != HG_SUCCESS)
    {
Shane Snyder's avatar
Shane Snyder committed
537
        margo_destroy(handle);
538
539
540
541
542
543
        return(-1);
    }

    ret = out.ret;
    *region_size = out.size;

Shane Snyder's avatar
Shane Snyder committed
544
545
    margo_free_output(handle, &out);
    margo_destroy(handle);
546
547
548
    return(ret);
}

Philip Carns's avatar
Philip Carns committed
549
550
551
552
int bake_bulk_noop(
    bake_target_id_t bti)
{
    hg_return_t hret;
Shane Snyder's avatar
Shane Snyder committed
553
    hg_handle_t handle;
Philip Carns's avatar
Philip Carns committed
554
555
556
557
558
559
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);

Shane Snyder's avatar
Shane Snyder committed
560
561
562
563
    hret = margo_create(g_margo_inst.mid, instance->dest,
        g_margo_inst.bake_bulk_noop_id, &handle);
    if(hret != HG_SUCCESS)
        return(-1);
Philip Carns's avatar
Philip Carns committed
564

Shane Snyder's avatar
Shane Snyder committed
565
    hret = margo_forward(handle, NULL);
Philip Carns's avatar
Philip Carns committed
566
567
    if(hret != HG_SUCCESS)
    {
Shane Snyder's avatar
Shane Snyder committed
568
        margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
569
570
571
        return(-1);
    }

Shane Snyder's avatar
Shane Snyder committed
572
    margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
573
574
575
    return(0);
}

Philip Carns's avatar
Philip Carns committed
576
577
578
579
580
581
582
int bake_bulk_read(
    bake_target_id_t bti,
    bake_bulk_region_id_t rid,
    uint64_t region_offset,
    void *buf,
    uint64_t buf_size)
{
583
    hg_return_t hret;
Shane Snyder's avatar
Shane Snyder committed
584
    hg_handle_t handle;
585
586
587
    bake_bulk_read_in_t in;
    bake_bulk_read_out_t out;
    int ret;
588
589
    struct bake_instance *instance = NULL;

Philip Carns's avatar
Philip Carns committed
590
591
592
    if(buf_size <= BAKE_BULK_EAGER_LIMIT)
        return(bake_bulk_eager_read(bti, rid, region_offset, buf, buf_size));

593
594
595
    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);
596
597
598
599
600

    in.bti = bti;
    in.rid = rid;
    in.region_offset = region_offset;

601
    hret = margo_bulk_create(g_margo_inst.mid, 1, (void**)(&buf), &buf_size, 
602
        HG_BULK_WRITE_ONLY, &in.bulk_handle);
Shane Snyder's avatar
Shane Snyder committed
603
604
605
606
607
    if(hret != HG_SUCCESS)
        return(-1);
   
    hret = margo_create(g_margo_inst.mid, instance->dest,
        g_margo_inst.bake_bulk_read_id, &handle);
608
609
    if(hret != HG_SUCCESS)
    {
Shane Snyder's avatar
Shane Snyder committed
610
        margo_bulk_free(in.bulk_handle);
611
612
613
        return(-1);
    }

Shane Snyder's avatar
Shane Snyder committed
614
    hret = margo_forward(handle, &in);
615
616
    if(hret != HG_SUCCESS)
    {
617
        margo_bulk_free(in.bulk_handle);
Shane Snyder's avatar
Shane Snyder committed
618
        margo_destroy(handle);
619
620
621
        return(-1);
    }

Shane Snyder's avatar
Shane Snyder committed
622
    hret = margo_get_output(handle, &out);
623
624
    if(hret != HG_SUCCESS)
    {
625
        margo_bulk_free(in.bulk_handle);
Shane Snyder's avatar
Shane Snyder committed
626
        margo_destroy(handle);
627
628
629
630
631
        return(-1);
    }
    
    ret = out.ret;

Shane Snyder's avatar
Shane Snyder committed
632
    margo_free_output(handle, &out);
633
    margo_bulk_free(in.bulk_handle);
Shane Snyder's avatar
Shane Snyder committed
634
    margo_destroy(handle);
635
    return(ret);
Philip Carns's avatar
Philip Carns committed
636
637
638
}


Philip Carns's avatar
Philip Carns committed
639
640
641
642
643
644
645
646
static int bake_bulk_eager_read(
    bake_target_id_t bti,
    bake_bulk_region_id_t rid,
    uint64_t region_offset,
    void *buf,
    uint64_t buf_size)
{
    hg_return_t hret;
Shane Snyder's avatar
Shane Snyder committed
647
    hg_handle_t handle;
Philip Carns's avatar
Philip Carns committed
648
649
650
651
652
653
654
655
656
657
658
659
660
661
    bake_bulk_eager_read_in_t in;
    bake_bulk_eager_read_out_t out;
    int ret;
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);

    in.bti = bti;
    in.rid = rid;
    in.region_offset = region_offset;
    in.size = buf_size;

Shane Snyder's avatar
Shane Snyder committed
662
663
664
665
666
667
    hret = margo_create(g_margo_inst.mid, instance->dest,
        g_margo_inst.bake_bulk_eager_read_id, &handle);
    if(hret != HG_SUCCESS)
        return(-1);
  
    hret = margo_forward(handle, &in);
Philip Carns's avatar
Philip Carns committed
668
669
    if(hret != HG_SUCCESS)
    {
Shane Snyder's avatar
Shane Snyder committed
670
        margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
671
672
673
        return(-1);
    }

Shane Snyder's avatar
Shane Snyder committed
674
    hret = margo_get_output(handle, &out);
Philip Carns's avatar
Philip Carns committed
675
676
    if(hret != HG_SUCCESS)
    {
Shane Snyder's avatar
Shane Snyder committed
677
        margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
678
679
680
681
682
683
684
        return(-1);
    }
    
    ret = out.ret;
    if(ret == 0)
        memcpy(buf, out.buffer, out.size);

Shane Snyder's avatar
Shane Snyder committed
685
686
    margo_free_output(handle, &out);
    margo_destroy(handle);
Philip Carns's avatar
Philip Carns committed
687
688
689
    return(ret);
}