Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / examples / msg / io / remote.c
1 /* Copyright (c) 2014-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /** @addtogroup MSG_examples
8  * 
9  * - <b>io/remote.c</b> Example of delegated I/O operations
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include "simgrid/msg.h"
15 #include "surf/surf_private.h"
16
17 #define INMEGA (1024*1024)
18
19 int host(int argc, char *argv[]);
20
21 XBT_LOG_NEW_DEFAULT_CATEGORY(remote_io,
22                              "Messages specific for this io example");
23
24
25 int host(int argc, char *argv[]){
26   msg_file_t file = NULL;
27   const char* filename;
28   sg_size_t read, write;
29
30   file = MSG_file_open(argv[1], NULL);
31   filename = MSG_file_get_name(file);
32   XBT_INFO("Opened file '%s'",filename);
33   MSG_file_dump(file);
34
35   XBT_INFO("Try to read %llu from '%s'",MSG_file_get_size(file),filename);
36   read = MSG_file_read(file, MSG_file_get_size(file));
37   XBT_INFO("Have read %llu from '%s'. Offset is now at: %llu",read,filename,
38       MSG_file_tell(file));
39   XBT_INFO("Seek back to the begining of the stream...");
40   MSG_file_seek(file, 0, SEEK_SET);
41   XBT_INFO("Offset is now at: %llu", MSG_file_tell(file));
42
43   MSG_file_close(file);
44
45   if (argc > 5){
46     file = MSG_file_open(argv[2], NULL);
47     filename = MSG_file_get_name(file);
48     XBT_INFO("Opened file '%s'",filename);
49     XBT_INFO("Try to write %llu MiB to '%s'",
50         MSG_file_get_size(file)/1024,
51         filename);
52     write = MSG_file_write(file, MSG_file_get_size(file)*1024);
53     XBT_INFO("Have written %llu bytes to '%s'.",write,filename);
54
55     msg_host_t src, dest;
56     src= MSG_host_self();
57     dest = MSG_host_by_name(argv[3]);
58     if (atoi(argv[5])){
59       XBT_INFO("Move '%s' (of size %llu) from '%s' to '%s'", filename,
60            MSG_file_get_size(file), MSG_host_get_name(src),
61            argv[3]);
62       MSG_file_rmove(file, dest, argv[4]);
63     } else {
64       XBT_INFO("Copy '%s' (of size %llu) from '%s' to '%s'", filename,
65            MSG_file_get_size(file), MSG_host_get_name(src),
66            argv[3]);
67       MSG_file_rcopy(file, dest, argv[4]);
68       MSG_file_close(file);
69     }
70   }
71
72   return 0;
73 }
74
75
76
77 int main(int argc, char **argv)
78 {
79   int res;
80   unsigned int cur;
81   xbt_dynar_t storages;
82   msg_storage_t st;
83
84   MSG_init(&argc, argv);
85   MSG_create_environment(argv[1]);
86   MSG_function_register("host", host);
87   MSG_launch_application(argv[2]);
88
89   storages = MSG_storages_as_dynar();
90   xbt_dynar_foreach(storages, cur, st){
91     XBT_INFO("Init: %llu MiB used on '%s'",
92         MSG_storage_get_used_size(st)/INMEGA,
93         MSG_storage_get_name(st));
94   }
95
96   res = MSG_main();
97
98   xbt_dynar_foreach(storages, cur, st){
99     XBT_INFO("Init: %llu MiB used on '%s'",
100         MSG_storage_get_used_size(st)/INMEGA,
101         MSG_storage_get_name(st));
102   }
103   xbt_dynar_free_container(&storages);
104
105   XBT_INFO("Simulation time %g", MSG_get_clock());
106   if (res == MSG_OK)
107     return 0;
108   else
109     return 1;
110 }