Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge remote-tracking branch 'origin/master'
[simgrid.git] / examples / smpi / MM / param.c
1 /*!
2  * get the parameter specific to the process from a file
3  *
4  */
5
6 #include "topo.h"
7 #include "param.h"
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <mpi.h>
12 #include "xbt/log.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(MM_param,
14                              "Messages specific for this msg example");
15
16 int match(const char *s, char p);
17 char** get_list_param(const char* input);
18 int match(const char *s, char p)
19 {
20   int c = 0;
21   while (*s != '\0') {
22     if (strncmp(s++, &p, 1)) continue;
23     c++;
24   }
25   return c;
26 }
27
28 char** get_list_param(const char* input){
29   if(input==NULL) return NULL;
30   char *line = strdup(input);
31   int size = match(line, ' ');
32   char **list_param = malloc(size*sizeof(char*));
33   char *pch = strtok (line," \t\n");
34   int i = 0;
35   while (pch != NULL)
36   {
37     if(strcmp(pch,"") != 0 ) {
38       list_param[i] = pch;
39       i++;
40     }
41     pch = strtok (NULL, " \t\n");
42   }
43   return list_param;
44 }
45
46 char** get_conf(MPI_Comm comm, const char * filename, int mynoderank)
47 {
48   if(filename == NULL) return NULL;
49   if(mynoderank == -1){
50     MPI_Comm comm_node;
51     split_comm_intra_node(comm, &comm_node, -1);
52     MPI_Comm_rank ( comm_node, &mynoderank );
53     MPI_Comm_free(&comm_node);
54   }
55   char name[MPI_MAX_PROCESSOR_NAME];
56   int len;
57   MPI_Get_processor_name(name, &len);
58   FILE* conf;
59   conf = fopen(filename, "r");
60   if (conf == NULL) {
61     XBT_DEBUG(
62               "Try to open the configuration file %s\n", filename);
63     perror("fopen");
64     return NULL;
65   }
66   char *machine_name = NULL;
67   size_t number = 0;
68   int err = 1;
69   int index = -1;
70
71
72   /* Try to find the line correponding to this processor */
73   while((err = getdelim(&machine_name, &number, ' ', conf)) != -1) {
74     while(err == 1){
75       err = getdelim(&machine_name, &number, ' ', conf);
76     }
77     if(err == -1) break;
78     XBT_DEBUG(
79               "read: %s cmp to %s\n", machine_name, name);
80     /* the first element is in machine_name
81        it's normally a processor name */
82     /* remove the delimiter before doing the comparison*/
83     machine_name[strlen(machine_name)-1] = 0;
84
85     if(strcmp(machine_name,name) == 0){
86       /* the machine name match */
87
88       /* try to get for which process with the index*/
89       char* char_index=NULL;
90       err = getdelim(&char_index, &number, ' ', conf);
91       while(err == 1){
92         err = getdelim(&char_index, &number, ' ', conf);
93       }
94       if(err == -1) break;
95
96       index=atoi(char_index);
97       XBT_DEBUG(
98                 "read: %d cmp to %d\n",
99                 index, mynoderank);
100
101       if(index == mynoderank){
102         /* we have found the good line
103          * we rebuild the line to get every information*/
104         char* line = NULL;
105         number = 0;
106         if (getline(&line,&number,conf) == -1)
107           xbt_die("Cannot get line");
108         char* line1 = NULL;
109         asprintf(&line1,"%s %s %s",name,char_index,line);
110         return get_list_param(line1);
111       }
112     }
113
114     /*prepare for the next line*/
115     free(machine_name);
116     machine_name = NULL;
117     number = 0;
118     err = getline(&machine_name, &number,  conf);
119     if (err >= 1) {
120       free(machine_name);
121       machine_name = NULL;
122       number = 0;
123     }
124
125   }
126   XBT_DEBUG(
127             "No configuration for %s %d\n", name, mynoderank );
128   return NULL;
129 }
130
131
132 char*** get_conf_all(char * filename, int * nb_process){
133   if(filename == NULL) return NULL;
134
135   char *** all_conf = NULL;
136   FILE* conf;
137   int nb_line = 0;
138   char *line = NULL;
139   size_t linecap = 0;
140   ssize_t linelen;
141
142   conf = fopen(filename, "r");
143   if (conf == NULL) {
144     XBT_DEBUG(
145               "Try to open the configuration file %s\n", filename);
146     perror("fopen");
147     return NULL;
148   }
149
150   while ((linelen = getline(&line, &linecap, conf)) > 0)
151     nb_line++;
152   fclose(conf);
153   conf = fopen(filename, "r");
154
155   all_conf = malloc(sizeof(char**) * nb_line);
156   /* Try to find the line correponding to this processor */
157   nb_line = 0;
158   while ((linelen = getline(&line, &linecap, conf)) > 0){
159     if (strcmp(line,"") == 0) continue; //Skip blank line
160     if (line[0] == '#') continue; //Skip comment line
161     all_conf[nb_line] = get_list_param(line);
162     nb_line++;
163   }
164   if(nb_process != NULL) *nb_process = nb_line;
165   return all_conf;
166 }
167
168
169 void print_conf(MPI_Comm comm, int rank, FILE* file, char * default_options){
170   char name[MPI_MAX_PROCESSOR_NAME];
171   int len;
172   MPI_Get_processor_name(name, &len);
173   if(file == NULL) file = stdout;
174
175   MPI_Comm comm_node;
176   split_comm_intra_node(comm, &comm_node, 0);
177
178   char ** names = get_names(comm);
179   int* index = get_index( comm,  comm_node);
180   MPI_Comm_free(&comm_node);
181   int size;
182   MPI_Comm_size(comm, &size);
183   int i=0;
184   if(rank == 0){
185     fprintf(file, "#processor_name index USER ARGS (like the cpu binding ...)\n");
186     for(i = 0; i < size; i++){
187       if(default_options != NULL)
188         fprintf(file, "%s %d %s\n", names[i],index[i],default_options);
189       else
190         fprintf(file, "%s %d\n", names[i],index[i]);
191     }
192   }
193   free(names);
194   free(index);
195   return;
196 }