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 / file.c
1 /* Copyright (c) 2008-2010, 2012-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/file.c</b> Example with the disk resource
16  */
17
18 #define FILENAME1 "/home/doc/simgrid/examples/platforms/g5k.xml"
19 #define FILENAME2 "c:\\Windows\\setupact.log"
20 #define FILENAME3 "/home/doc/simgrid/examples/platforms/g5k_cabinets.xml"
21 #define FILENAME4 "/home/doc/simgrid/examples/platforms/nancy.xml"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "msg/msg.h"
26 #include "surf/surf_private.h"
27
28 int host(int argc, char *argv[]);
29
30 XBT_LOG_NEW_DEFAULT_CATEGORY(io_file,
31                              "Messages specific for this io example");
32
33 int host(int argc, char *argv[])
34 {
35   msg_file_t file = NULL;
36   sg_size_t read,write;
37
38   if(!strcmp(MSG_process_get_name(MSG_process_self()),"0")){
39     file = MSG_file_open(FILENAME1, NULL);
40     MSG_file_dump(file);
41   } else if(!strcmp(MSG_process_get_name(MSG_process_self()),"1")) {
42     file = MSG_file_open(FILENAME2, NULL);
43   } else if(!strcmp(MSG_process_get_name(MSG_process_self()),"2")){
44     file = MSG_file_open(FILENAME3, NULL);
45   } else if(!strcmp(MSG_process_get_name(MSG_process_self()),"3"))
46     file = MSG_file_open(FILENAME4, NULL);
47   else xbt_die("FILENAME NOT DEFINED %s",MSG_process_get_name(MSG_process_self()));
48
49   const char* filename = MSG_file_get_name(file);
50   XBT_INFO("\tOpen file '%s'",filename);
51
52   read = MSG_file_read(file, 10000000);     // Read for 10MB
53   XBT_INFO("\tHave read    %llu on %s",read,filename);
54
55   write = MSG_file_write(file, 100000);  // Write for 100KB
56   XBT_INFO("\tHave written %llu on %s",write,filename);
57
58   read = MSG_file_read(file, 110000);     // Read for 110KB
59   XBT_INFO("\tHave read    %llu on %s (of size %llu)",read,filename,
60       MSG_file_get_size(file));
61
62   XBT_INFO("\tClose file '%s'",filename);
63   MSG_file_close(file);
64
65
66   return 0;
67 }
68
69 int main(int argc, char **argv)
70 {
71   int i,res;
72   MSG_init(&argc, argv);
73   MSG_create_environment(argv[1]);
74   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
75   MSG_function_register("host", host);
76   unsigned long nb_hosts = xbt_dynar_length(hosts);
77   XBT_INFO("Number of host '%lu'",nb_hosts);
78   for(i = 0 ; i<nb_hosts; i++)
79   {
80     char* name_host = bprintf("%d",i);
81     MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,i,msg_host_t) );
82     free(name_host);
83   }
84   xbt_dynar_free(&hosts);
85
86   res = MSG_main();
87   XBT_INFO("Simulation time %g", MSG_get_clock());
88   if (res == MSG_OK)
89     return 0;
90   else
91     return 1;
92
93 }