bb-copy-to.c 2.66 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#include <stdio.h>
#include <assert.h>
#include <unistd.h>
Philip Carns's avatar
Philip Carns committed
10 11 12 13
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
14 15 16 17 18 19 20 21 22 23 24 25

#include "abt.h"
#include "abt-snoozer.h"
#include "bake-bulk.h"

/* client program that will shut down a BAKE bulk server. */

int main(int argc, char **argv) 
{
    int ret;
    bake_target_id_t bti;
    bake_bulk_region_id_t rid;
Philip Carns's avatar
Philip Carns committed
26 27 28
    int fd;
    struct stat statbuf;
    char* local_region;
29
 
Philip Carns's avatar
Philip Carns committed
30
    if(argc != 3)
31
    {
Philip Carns's avatar
Philip Carns committed
32 33
        fprintf(stderr, "Usage: bb-copy-to <local file> <server addr>\n");
        fprintf(stderr, "  Example: ./bb-copy-to /tmp/foo.dat tcp://localhost:1234\n");
34 35 36
        return(-1);
    }       

Philip Carns's avatar
Philip Carns committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    fd = open(argv[1], O_RDONLY);
    if(fd < 0)
    {
        perror("open");
        return(-1);
    }
    ret = fstat(fd, &statbuf);
    if(ret < 0)
    {
        perror("fstat");
        close(fd);
        return(-1);
    }

    local_region = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if(!local_region)
    {
        perror("mmap");
        close(fd);
        return(-1);
    }

59 60 61 62
    /* set up Argobots */
    ret = ABT_init(argc, argv);
    if(ret != 0)
    {
Philip Carns's avatar
Philip Carns committed
63 64
        munmap(local_region, statbuf.st_size);
        close(fd);
65 66 67 68 69 70
        fprintf(stderr, "Error: ABT_init()\n");
        return(-1);
    }
    ret = ABT_snoozer_xstream_self_set();
    if(ret != 0)
    {
Philip Carns's avatar
Philip Carns committed
71 72 73
        ABT_finalize();
        munmap(local_region, statbuf.st_size);
        close(fd);
74 75 76 77
        fprintf(stderr, "Error: ABT_snoozer_xstream_self_set()\n");
        return(-1);
    }

Philip Carns's avatar
Philip Carns committed
78
    ret = bake_probe_instance(argv[2], &bti);
79 80
    if(ret < 0)
    {
Philip Carns's avatar
Philip Carns committed
81 82 83
        ABT_finalize();
        munmap(local_region, statbuf.st_size);
        close(fd);
84 85 86 87
        fprintf(stderr, "Error: bake_probe_instance()\n");
        return(-1);
    }

Philip Carns's avatar
Philip Carns committed
88 89
    /* create region */
    ret = bake_bulk_create(bti, statbuf.st_size, &rid);
Philip Carns's avatar
Philip Carns committed
90
    if(ret != 0)
91
    {
Philip Carns's avatar
Philip Carns committed
92 93 94 95
        bake_release_instance(bti);
        ABT_finalize();
        munmap(local_region, statbuf.st_size);
        close(fd);
96 97 98 99 100 101
        fprintf(stderr, "Error: bake_bulk_create()\n");
        return(-1);
    }

    /* TODO: a way to print region id */

Philip Carns's avatar
Philip Carns committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    /* transfer data */
    ret = bake_bulk_write(
        bti,
        rid,
        0,
        local_region,
        statbuf.st_size);
    if(ret != 0)
    {
        bake_release_instance(bti);
        ABT_finalize();
        munmap(local_region, statbuf.st_size);
        close(fd);
        fprintf(stderr, "Error: bake_bulk_write()\n");
        return(-1);
    }
   
    munmap(local_region, statbuf.st_size);
    close(fd);
121 122 123 124 125 126
    bake_release_instance(bti);
    ABT_finalize();

    return(0);
}