Logo AND Algorithmique Numérique Distribuée

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