Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[smpi,example] an example of matrix multiplication with non contignous memory
[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 {
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, 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         getline(&line,&number,conf);
107         char* line1 = NULL;
108         asprintf(&line1,"%s %s %s",name,char_index,line);
109         return get_list_param(line1);
110       }
111     }
112
113     /*prepare for the next line*/
114     free(machine_name);
115     machine_name = NULL;
116     number = 0;
117     err = getline(&machine_name, &number,  conf);
118     if (err >= 1) {
119       free(machine_name);
120       machine_name = NULL;
121       number = 0;
122     }
123
124   }
125   XBT_DEBUG(
126             "No configuration for %s %d\n", name, mynoderank );
127   return NULL;
128 }
129
130
131 char*** get_conf_all(char * filename, int * nb_process){
132   if(filename == NULL) return NULL;
133
134   char *** all_conf = NULL;
135   FILE* conf;
136   int nb_line = 0;
137   char *line = NULL;
138   size_t linecap = 0;
139   ssize_t linelen;
140
141   conf = fopen(filename, "r");
142   if (conf == NULL) {
143     XBT_DEBUG(
144               "Try to open the configuration file %s\n", filename);
145     perror("fopen");
146     return NULL;
147   }
148
149   while ((linelen = getline(&line, &linecap, conf)) > 0)
150     nb_line++;
151   fclose(conf);
152   conf = fopen(filename, "r");
153
154   all_conf = malloc(sizeof(char**) * nb_line);
155   /* Try to find the line correponding to this processor */
156   nb_line = 0;
157   while ((linelen = getline(&line, &linecap, conf)) > 0){
158     if (strcmp(line,"") == 0) continue; //Skip blank line
159     if (line[0] == '#') continue; //Skip comment line
160     all_conf[nb_line] = get_list_param(line);
161     nb_line++;
162   }
163   if(nb_process != NULL) *nb_process = nb_line;
164   return all_conf;
165 }
166
167
168 void print_conf(MPI_Comm comm, int rank, FILE* file, char * default_options){
169   char name[MPI_MAX_PROCESSOR_NAME];
170   int len;
171   MPI_Get_processor_name(name, &len);
172   if(file == NULL) file = stdout;
173   if(default_options == NULL) default_options = "";
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       fprintf(file, "%s %d %s\n", names[i],index[i],default_options);
188     }
189   }
190   free(names);
191   free(index);
192   return;
193 }