xref: /petsc/src/sys/fileio/fretrieve.c (revision 26fbe8dc1a3c99fb8dddfa572c8c6b3b4ce3ca53)
1 
2 /*
3       Code for opening and closing files.
4 */
5 #include <petscsys.h>
6 #if defined(PETSC_HAVE_PWD_H)
7 #include <pwd.h>
8 #endif
9 #include <ctype.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #if defined(PETSC_HAVE_UNISTD_H)
13 #include <unistd.h>
14 #endif
15 #if defined(PETSC_HAVE_STDLIB_H)
16 #include <stdlib.h>
17 #endif
18 #if defined(PETSC_HAVE_SYS_UTSNAME_H)
19 #include <sys/utsname.h>
20 #endif
21 #include <fcntl.h>
22 #include <time.h>
23 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
24 #include <sys/systeminfo.h>
25 #endif
26 
27 EXTERN_C_BEGIN
28 #undef __FUNCT__
29 #define __FUNCT__ "Petsc_DelTmpShared"
30 /*
31    Private routine to delete tmp/shared storage
32 
33    This is called by MPI, not by users.
34 
35    Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
36 
37 */
38 PetscMPIInt MPIAPI Petsc_DelTmpShared(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state)
39 {
40   PetscErrorCode ierr;
41 
42   PetscFunctionBegin;
43   ierr = PetscInfo1(0,"Deleting tmp/shared data in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
44   ierr = PetscFree(count_val);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
45   PetscFunctionReturn(MPI_SUCCESS);
46 }
47 EXTERN_C_END
48 
49 #undef __FUNCT__
50 #define __FUNCT__ "PetscGetTmp"
51 /*@C
52    PetscGetTmp - Gets the name of the tmp directory
53 
54    Collective on MPI_Comm
55 
56    Input Parameters:
57 +  comm - MPI_Communicator that may share /tmp
58 -  len - length of string to hold name
59 
60    Output Parameters:
61 .  dir - directory name
62 
63    Options Database Keys:
64 +    -shared_tmp
65 .    -not_shared_tmp
66 -    -tmp tmpdir
67 
68    Environmental Variables:
69 +     PETSC_SHARED_TMP
70 .     PETSC_NOT_SHARED_TMP
71 -     PETSC_TMP
72 
73    Level: developer
74 
75 
76    If the environmental variable PETSC_TMP is set it will use this directory
77   as the "/tmp" directory.
78 
79 @*/
80 PetscErrorCode  PetscGetTmp(MPI_Comm comm,char dir[],size_t len)
81 {
82   PetscErrorCode ierr;
83   PetscBool      flg;
84 
85   PetscFunctionBegin;
86   ierr = PetscOptionsGetenv(comm,"PETSC_TMP",dir,len,&flg);CHKERRQ(ierr);
87   if (!flg) {
88     ierr = PetscStrncpy(dir,"/tmp",len);CHKERRQ(ierr);
89   }
90   PetscFunctionReturn(0);
91 }
92 
93 #undef __FUNCT__
94 #define __FUNCT__ "PetscSharedTmp"
95 /*@C
96    PetscSharedTmp - Determines if all processors in a communicator share a
97          /tmp or have different ones.
98 
99    Collective on MPI_Comm
100 
101    Input Parameters:
102 .  comm - MPI_Communicator that may share /tmp
103 
104    Output Parameters:
105 .  shared - PETSC_TRUE or PETSC_FALSE
106 
107    Options Database Keys:
108 +    -shared_tmp
109 .    -not_shared_tmp
110 -    -tmp tmpdir
111 
112    Environmental Variables:
113 +     PETSC_SHARED_TMP
114 .     PETSC_NOT_SHARED_TMP
115 -     PETSC_TMP
116 
117    Level: developer
118 
119    Notes:
120    Stores the status as a MPI attribute so it does not have
121     to be redetermined each time.
122 
123       Assumes that all processors in a communicator either
124        1) have a common /tmp or
125        2) each has a separate /tmp
126       eventually we can write a fancier one that determines which processors
127       share a common /tmp.
128 
129    This will be very slow on runs with a large number of processors since
130    it requires O(p*p) file opens.
131 
132    If the environmental variable PETSC_TMP is set it will use this directory
133   as the "/tmp" directory.
134 
135 @*/
136 PetscErrorCode  PetscSharedTmp(MPI_Comm comm,PetscBool  *shared)
137 {
138   PetscErrorCode     ierr;
139   PetscMPIInt        size,rank,*tagvalp,sum,cnt,i;
140   PetscBool          flg,iflg;
141   FILE               *fd;
142   static PetscMPIInt Petsc_Tmp_keyval = MPI_KEYVAL_INVALID;
143   int                err;
144 
145   PetscFunctionBegin;
146   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
147   if (size == 1) {
148     *shared = PETSC_TRUE;
149     PetscFunctionReturn(0);
150   }
151 
152   ierr = PetscOptionsGetenv(comm,"PETSC_SHARED_TMP",PETSC_NULL,0,&flg);CHKERRQ(ierr);
153   if (flg) {
154     *shared = PETSC_TRUE;
155     PetscFunctionReturn(0);
156   }
157 
158   ierr = PetscOptionsGetenv(comm,"PETSC_NOT_SHARED_TMP",PETSC_NULL,0,&flg);CHKERRQ(ierr);
159   if (flg) {
160     *shared = PETSC_FALSE;
161     PetscFunctionReturn(0);
162   }
163 
164   if (Petsc_Tmp_keyval == MPI_KEYVAL_INVALID) {
165     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelTmpShared,&Petsc_Tmp_keyval,0);CHKERRQ(ierr);
166   }
167 
168   ierr = MPI_Attr_get(comm,Petsc_Tmp_keyval,(void**)&tagvalp,(int*)&iflg);CHKERRQ(ierr);
169   if (!iflg) {
170     char filename[PETSC_MAX_PATH_LEN],tmpname[PETSC_MAX_PATH_LEN];
171 
172     /* This communicator does not yet have a shared tmp attribute */
173     ierr = PetscMalloc(sizeof(PetscMPIInt),&tagvalp);CHKERRQ(ierr);
174     ierr = MPI_Attr_put(comm,Petsc_Tmp_keyval,tagvalp);CHKERRQ(ierr);
175 
176     ierr = PetscOptionsGetenv(comm,"PETSC_TMP",tmpname,238,&iflg);CHKERRQ(ierr);
177     if (!iflg) {
178       ierr = PetscStrcpy(filename,"/tmp");CHKERRQ(ierr);
179     } else {
180       ierr = PetscStrcpy(filename,tmpname);CHKERRQ(ierr);
181     }
182 
183     ierr = PetscStrcat(filename,"/petsctestshared");CHKERRQ(ierr);
184     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
185 
186     /* each processor creates a /tmp file and all the later ones check */
187     /* this makes sure no subset of processors is shared */
188     *shared = PETSC_FALSE;
189     for (i=0; i<size-1; i++) {
190       if (rank == i) {
191         fd = fopen(filename,"w");
192         if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open test file %s",filename);
193         err = fclose(fd);
194         if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
195       }
196       ierr = MPI_Barrier(comm);CHKERRQ(ierr);
197       if (rank >= i) {
198         fd = fopen(filename,"r");
199         if (fd) cnt = 1;
200         else cnt = 0;
201         if (fd) {
202           err = fclose(fd);
203           if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
204         }
205       } else cnt = 0;
206 
207       ierr = MPI_Allreduce(&cnt,&sum,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
208       if (rank == i) unlink(filename);
209 
210       if (sum == size) {
211         *shared = PETSC_TRUE;
212         break;
213       } else if (sum != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Subset of processes share /tmp ");
214     }
215     *tagvalp = (int)*shared;
216     ierr = PetscInfo2(0,"processors %s %s\n",(*shared) ? "share":"do NOT share",(iflg ? tmpname:"/tmp"));CHKERRQ(ierr);
217   } else *shared = (PetscBool) *tagvalp;
218   PetscFunctionReturn(0);
219 }
220 
221 #undef __FUNCT__
222 #define __FUNCT__ "PetscSharedWorkingDirectory"
223 /*@C
224    PetscSharedWorkingDirectory - Determines if all processors in a communicator share a
225          working directory or have different ones.
226 
227    Collective on MPI_Comm
228 
229    Input Parameters:
230 .  comm - MPI_Communicator that may share working directory
231 
232    Output Parameters:
233 .  shared - PETSC_TRUE or PETSC_FALSE
234 
235    Options Database Keys:
236 +    -shared_working_directory
237 .    -not_shared_working_directory
238 
239    Environmental Variables:
240 +     PETSC_SHARED_WORKING_DIRECTORY
241 .     PETSC_NOT_SHARED_WORKING_DIRECTORY
242 
243    Level: developer
244 
245    Notes:
246    Stores the status as a MPI attribute so it does not have
247     to be redetermined each time.
248 
249       Assumes that all processors in a communicator either
250        1) have a common working directory or
251        2) each has a separate working directory
252       eventually we can write a fancier one that determines which processors
253       share a common working directory.
254 
255    This will be very slow on runs with a large number of processors since
256    it requires O(p*p) file opens.
257 
258 @*/
259 PetscErrorCode  PetscSharedWorkingDirectory(MPI_Comm comm,PetscBool  *shared)
260 {
261   PetscErrorCode     ierr;
262   PetscMPIInt        size,rank,*tagvalp,sum,cnt,i;
263   PetscBool          flg,iflg;
264   FILE               *fd;
265   static PetscMPIInt Petsc_WD_keyval = MPI_KEYVAL_INVALID;
266   int                err;
267 
268   PetscFunctionBegin;
269   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
270   if (size == 1) {
271     *shared = PETSC_TRUE;
272     PetscFunctionReturn(0);
273   }
274 
275   ierr = PetscOptionsGetenv(comm,"PETSC_SHARED_WORKING_DIRECTORY",PETSC_NULL,0,&flg);CHKERRQ(ierr);
276   if (flg) {
277     *shared = PETSC_TRUE;
278     PetscFunctionReturn(0);
279   }
280 
281   ierr = PetscOptionsGetenv(comm,"PETSC_NOT_SHARED_WORKING_DIRECTORY",PETSC_NULL,0,&flg);CHKERRQ(ierr);
282   if (flg) {
283     *shared = PETSC_FALSE;
284     PetscFunctionReturn(0);
285   }
286 
287   if (Petsc_WD_keyval == MPI_KEYVAL_INVALID) {
288     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelTmpShared,&Petsc_WD_keyval,0);CHKERRQ(ierr);
289   }
290 
291   ierr = MPI_Attr_get(comm,Petsc_WD_keyval,(void**)&tagvalp,(int*)&iflg);CHKERRQ(ierr);
292   if (!iflg) {
293     char filename[PETSC_MAX_PATH_LEN];
294 
295     /* This communicator does not yet have a shared  attribute */
296     ierr = PetscMalloc(sizeof(PetscMPIInt),&tagvalp);CHKERRQ(ierr);
297     ierr = MPI_Attr_put(comm,Petsc_WD_keyval,tagvalp);CHKERRQ(ierr);
298 
299     ierr = PetscGetWorkingDirectory(filename,240);CHKERRQ(ierr);
300     ierr = PetscStrcat(filename,"/petsctestshared");CHKERRQ(ierr);
301     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
302 
303     /* each processor creates a  file and all the later ones check */
304     /* this makes sure no subset of processors is shared */
305     *shared = PETSC_FALSE;
306     for (i=0; i<size-1; i++) {
307       if (rank == i) {
308         fd = fopen(filename,"w");
309         if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open test file %s",filename);
310         err = fclose(fd);
311         if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
312       }
313       ierr = MPI_Barrier(comm);CHKERRQ(ierr);
314       if (rank >= i) {
315         fd = fopen(filename,"r");
316         if (fd) cnt = 1;
317         else cnt = 0;
318         if (fd) {
319           err = fclose(fd);
320           if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
321         }
322       } else cnt = 0;
323 
324       ierr = MPI_Allreduce(&cnt,&sum,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
325       if (rank == i) unlink(filename);
326 
327       if (sum == size) {
328         *shared = PETSC_TRUE;
329         break;
330       } else if (sum != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Subset of processes share working directory");
331     }
332     *tagvalp = (int)*shared;
333   } else *shared = (PetscBool) *tagvalp;
334   ierr = PetscInfo1(0,"processors %s working directory\n",(*shared) ? "shared" : "do NOT share");CHKERRQ(ierr);
335   PetscFunctionReturn(0);
336 }
337 
338 
339 #undef __FUNCT__
340 #define __FUNCT__ "PetscFileRetrieve"
341 /*@C
342     PetscFileRetrieve - Obtains a library from a URL or compressed
343         and copies into local disk space as uncompressed.
344 
345     Collective on MPI_Comm
346 
347     Input Parameter:
348 +   comm     - processors accessing the library
349 .   libname  - name of library, including entire URL (with or without .gz)
350 -   llen     - length of llibname
351 
352     Output Parameter:
353 +   llibname - name of local copy of library
354 -   found - if found and retrieved the file
355 
356     Level: developer
357 
358 @*/
359 PetscErrorCode  PetscFileRetrieve(MPI_Comm comm,const char libname[],char llibname[],size_t llen,PetscBool  *found)
360 {
361   char           buf[1024],tmpdir[PETSC_MAX_PATH_LEN],urlget[PETSC_MAX_PATH_LEN],*par;
362   const char     *pdir;
363   FILE           *fp;
364   PetscErrorCode ierr;
365   int            i;
366   PetscMPIInt    rank;
367   size_t         len = 0;
368   PetscBool      flg1,flg2,flg3,sharedtmp,exists;
369 #if defined(PETSC_HAVE_POPEN)
370   PetscInt       rval;
371 #endif
372 
373   PetscFunctionBegin;
374   *found = PETSC_FALSE;
375 
376   /* if file does not have an ftp:// or http:// or .gz then need not process file */
377   ierr = PetscStrstr(libname,".gz",&par);CHKERRQ(ierr);
378   if (par) {ierr = PetscStrlen(par,&len);CHKERRQ(ierr);}
379 
380   ierr = PetscStrncmp(libname,"ftp://",6,&flg1);CHKERRQ(ierr);
381   ierr = PetscStrncmp(libname,"http://",7,&flg2);CHKERRQ(ierr);
382   ierr = PetscStrncmp(libname,"file://",7,&flg3);CHKERRQ(ierr);
383   if (!flg1 && !flg2 && !flg3 && (!par || len != 3)) {
384     ierr = PetscStrncpy(llibname,libname,llen);CHKERRQ(ierr);
385     ierr = PetscTestFile(libname,'r',found);CHKERRQ(ierr);
386     if (*found) {
387       ierr = PetscInfo1(PETSC_NULL,"Found file %s\n",libname);CHKERRQ(ierr);
388     } else {
389       ierr = PetscInfo1(PETSC_NULL,"Did not find file %s\n",libname);CHKERRQ(ierr);
390     }
391     PetscFunctionReturn(0);
392   }
393 
394   /* Determine if all processors share a common /tmp */
395   ierr = PetscSharedTmp(comm,&sharedtmp);CHKERRQ(ierr);
396   ierr = PetscOptionsGetenv(comm,"PETSC_TMP",tmpdir,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
397 
398   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
399   if (!rank || !sharedtmp) {
400 
401     /* Construct the script to get URL file */
402     ierr = PetscGetPetscDir(&pdir);CHKERRQ(ierr);
403     ierr = PetscStrcpy(urlget,pdir);CHKERRQ(ierr);
404     ierr = PetscStrcat(urlget,"/bin/urlget");CHKERRQ(ierr);
405     ierr = PetscTestFile(urlget,'r',&exists);CHKERRQ(ierr);
406     if (!exists) {
407       ierr = PetscTestFile("urlget",'r',&exists);CHKERRQ(ierr);
408       if (!exists) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cannot locate PETSc script urlget in %s or current directory",urlget);
409       ierr = PetscStrcpy(urlget,"urlget");CHKERRQ(ierr);
410     }
411     ierr = PetscStrcat(urlget," ");CHKERRQ(ierr);
412 
413     /* are we using an alternative /tmp? */
414     if (flg1) {
415       ierr = PetscStrcat(urlget,"-tmp ");CHKERRQ(ierr);
416       ierr = PetscStrcat(urlget,tmpdir);CHKERRQ(ierr);
417       ierr = PetscStrcat(urlget," ");CHKERRQ(ierr);
418     }
419 
420     ierr = PetscStrcat(urlget,libname);CHKERRQ(ierr);
421     ierr = PetscStrcat(urlget," 2>&1 ");CHKERRQ(ierr);
422 
423 #if defined(PETSC_HAVE_POPEN)
424     ierr = PetscPOpen(PETSC_COMM_SELF,PETSC_NULL,urlget,"r",&fp);CHKERRQ(ierr);
425 #else
426     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
427 #endif
428     if (!fgets(buf,1024,fp)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"No output from ${PETSC_DIR}/bin/urlget in getting file %s",libname);
429     ierr = PetscInfo1(0,"Message back from urlget: %s\n",buf);CHKERRQ(ierr);
430 
431     ierr = PetscStrncmp(buf,"Error",5,&flg1);CHKERRQ(ierr);
432     ierr = PetscStrncmp(buf,"Traceback",9,&flg2);CHKERRQ(ierr);
433 #if defined(PETSC_HAVE_POPEN)
434     ierr = PetscPClose(PETSC_COMM_SELF,fp,&rval);CHKERRQ(ierr);
435 #endif
436     if (flg1 || flg2) *found = PETSC_FALSE;
437     else {
438       *found = PETSC_TRUE;
439 
440       /* Check for \n and make it 0 */
441       for (i=0; i<1024; i++) {
442         if (buf[i] == '\n') {
443           buf[i] = 0;
444           break;
445         }
446       }
447       ierr = PetscStrncpy(llibname,buf,llen);CHKERRQ(ierr);
448     }
449   }
450   if (sharedtmp) { /* send library name to all processors */
451     ierr = MPI_Bcast(found,1,MPIU_BOOL,0,comm);CHKERRQ(ierr);
452     if (*found) {
453       ierr = MPI_Bcast(llibname,llen,MPI_CHAR,0,comm);CHKERRQ(ierr);
454       ierr = MPI_Bcast(found,1,MPIU_BOOL,0,comm);CHKERRQ(ierr);
455     }
456   }
457   PetscFunctionReturn(0);
458 }
459