Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / examples / msg / io / remote.c
1 /* Copyright (c) 2014. 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  * @subsection MSG_ex_resources Other resource kinds
10  * 
11  * This section contains some sparse examples of how to use the other
12  * kind of resources, such as disk or GPU. These resources are quite
13  * experimental for now, but here we go anyway.
14  * 
15  * - <b>io/remote.c</b> Example of delegated I/O operations
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include "msg/msg.h"
21 #include "surf/surf_private.h"
22
23 #define INMEGA (1024*1024)
24
25 int host(int argc, char *argv[]);
26
27 XBT_LOG_NEW_DEFAULT_CATEGORY(remote_io,
28                              "Messages specific for this io example");
29
30
31 int host(int argc, char *argv[]){
32   msg_file_t file = NULL;
33   const char* filename;
34   sg_size_t read, write;
35
36   file = MSG_file_open(argv[1], NULL);
37   filename = MSG_file_get_name(file);
38   XBT_INFO("Opened file '%s'",filename);
39   MSG_file_dump(file);
40
41   XBT_INFO("Try to read %llu from '%s'",MSG_file_get_size(file),filename);
42   read = MSG_file_read(file, MSG_file_get_size(file));
43   XBT_INFO("Have read %llu from '%s'. Offset is now at: %llu",read,filename,
44       MSG_file_tell(file));
45   XBT_INFO("Seek back to the begining of the stream...");
46   MSG_file_seek(file, 0, SEEK_SET);
47   XBT_INFO("Offset is now at: %llu", MSG_file_tell(file));
48
49   MSG_file_close(file);
50
51   if (argc > 5){
52     file = MSG_file_open(argv[2], NULL);
53     filename = MSG_file_get_name(file);
54     XBT_INFO("Opened file '%s'",filename);
55     XBT_INFO("Try to write %llu MiB to '%s'",
56         MSG_file_get_size(file)/1024,
57         filename);
58     write = MSG_file_write(file, MSG_file_get_size(file)*1024);
59     XBT_INFO("Have written %llu bytes to '%s'.",write,filename);
60
61     msg_host_t src, dest;
62     src= MSG_host_self();
63     dest = MSG_get_host_by_name(argv[3]);
64     if (atoi(argv[5])){
65       XBT_INFO("Move '%s' (of size %llu) from '%s' to '%s'", filename,
66            MSG_file_get_size(file), MSG_host_get_name(src),
67            argv[3]);
68       MSG_file_rmove(file, dest, argv[4]);
69     } else {
70       XBT_INFO("Copy '%s' (of size %llu) from '%s' to '%s'", filename,
71            MSG_file_get_size(file), MSG_host_get_name(src),
72            argv[3]);
73       MSG_file_rcopy(file, dest, argv[4]);
74     }
75   }
76
77   return 0;
78 }
79
80
81
82 int main(int argc, char **argv)
83 {
84   int res;
85   unsigned int cur;
86   xbt_dynar_t storages;
87   msg_storage_t st;
88
89   MSG_init(&argc, argv);
90   MSG_create_environment(argv[1]);
91   MSG_function_register("host", host);
92   MSG_launch_application(argv[2]);
93
94   storages = MSG_storages_as_dynar();
95   xbt_dynar_foreach(storages, cur, st){
96     XBT_INFO("Init: %llu MiB used on '%s'",
97         MSG_storage_get_used_size(st)/INMEGA,
98         MSG_storage_get_name(st));
99   }
100
101   res = MSG_main();
102
103   xbt_dynar_foreach(storages, cur, st){
104     XBT_INFO("Init: %llu MiB used on '%s'",
105         MSG_storage_get_used_size(st)/INMEGA,
106         MSG_storage_get_name(st));
107   }
108   xbt_dynar_free_container(&storages);
109
110   XBT_INFO("Simulation time %g", MSG_get_clock());
111   if (res == MSG_OK)
112     return 0;
113   else
114     return 1;
115 }