Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid into...
[simgrid.git] / examples / msg / io / file.c
1 /* Copyright (c) 2008, 2009, 2010. 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 "./doc/simgrid/examples/platforms/g5k.xml"
19 #define FILENAME2 "./doc/simgrid/examples/platforms/One_cluster_no_backbone.xml"
20 #define FILENAME3 "./doc/simgrid/examples/platforms/g5k_cabinets.xml"
21 #define FILENAME4 "./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   s_msg_stat_t stat;
37   void *ptr = NULL;
38   char* mount = bprintf("/home");
39   double read,write;
40
41   if(!strcmp(MSG_process_get_name(MSG_process_self()),"0"))
42     file = MSG_file_open(mount,FILENAME1,"rw");
43   else if(!strcmp(MSG_process_get_name(MSG_process_self()),"1"))
44     file = MSG_file_open(mount,FILENAME2,"rw");
45   else if(!strcmp(MSG_process_get_name(MSG_process_self()),"2"))
46     file = MSG_file_open(mount,FILENAME3,"rw");
47   else if(!strcmp(MSG_process_get_name(MSG_process_self()),"3"))
48     file = MSG_file_open(mount,FILENAME4,"rw");
49   else xbt_die("FILENAME NOT DEFINED %s",MSG_process_get_name(MSG_process_self()));
50
51   XBT_INFO("\tOpen file '%s'",file->name);
52
53   read = MSG_file_read(ptr,10000000,sizeof(char*),file);     // Read for 10Mo
54   XBT_INFO("\tHave read    %8.1f on %s",read,file->name);
55
56   write = MSG_file_write(ptr,100000,sizeof(char*),file);  // Write for 100Ko
57   XBT_INFO("\tHave written %8.1f on %s",write,file->name);
58
59   read = MSG_file_read(ptr,10000000,sizeof(char*),file);     // Read for 10Mo
60   XBT_INFO("\tHave read    %8.1f on %s",read,file->name);
61
62   MSG_file_stat(file,&stat);
63   XBT_INFO("\tFile stat %s Size %.1f",file->name,stat.size);
64   MSG_file_free_stat(&stat);
65
66   XBT_INFO("\tClose file '%s'",file->name);
67   MSG_file_close(file);
68
69   free(mount);
70   return 0;
71 }
72
73 int main(int argc, char **argv)
74 {
75   int i,res;
76   MSG_init(&argc, argv);
77   MSG_create_environment(argv[1]);
78   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
79   MSG_function_register("host", host);
80   unsigned long nb_hosts = xbt_dynar_length(hosts);
81   XBT_INFO("Number of host '%lu'",nb_hosts);
82   for(i = 0 ; i<nb_hosts; i++)
83   {
84     char* name_host = bprintf("%d",i);
85     MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,i,msg_host_t) );
86     free(name_host);
87   }
88   xbt_dynar_free(&hosts);
89
90   res = MSG_main();
91   XBT_INFO("Simulation time %g", MSG_get_clock());
92   if (res == MSG_OK)
93     return 0;
94   else
95     return 1;
96
97 }