17d0a6c19SBarry Smith 2e5c89e4eSSatish Balay /* 3e5c89e4eSSatish Balay Code for opening and closing files. 4e5c89e4eSSatish Balay */ 5c6db04a5SJed Brown #include <petscsys.h> 6e5c89e4eSSatish Balay #if defined(PETSC_HAVE_PWD_H) 7e5c89e4eSSatish Balay #include <pwd.h> 8e5c89e4eSSatish Balay #endif 9e5c89e4eSSatish Balay #include <ctype.h> 10e5c89e4eSSatish Balay #include <sys/stat.h> 11e5c89e4eSSatish Balay #if defined(PETSC_HAVE_UNISTD_H) 12e5c89e4eSSatish Balay #include <unistd.h> 13e5c89e4eSSatish Balay #endif 14e5c89e4eSSatish Balay #if defined(PETSC_HAVE_SYS_UTSNAME_H) 15e5c89e4eSSatish Balay #include <sys/utsname.h> 16e5c89e4eSSatish Balay #endif 17e5c89e4eSSatish Balay #include <fcntl.h> 18e5c89e4eSSatish Balay #include <time.h> 19e5c89e4eSSatish Balay #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H) 20e5c89e4eSSatish Balay #include <sys/systeminfo.h> 21e5c89e4eSSatish Balay #endif 22e5c89e4eSSatish Balay 23480cf27aSJed Brown /* 24480cf27aSJed Brown Private routine to delete tmp/shared storage 25480cf27aSJed Brown 26480cf27aSJed Brown This is called by MPI, not by users. 27480cf27aSJed Brown 2812801b39SBarry Smith Note: this is declared extern "C" because it is passed to MPI_Comm_create_keyval() 29480cf27aSJed Brown 30480cf27aSJed Brown */ 318cc058d9SJed Brown PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelTmpShared(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state) 32480cf27aSJed Brown { 33480cf27aSJed Brown PetscErrorCode ierr; 34480cf27aSJed Brown 35480cf27aSJed Brown PetscFunctionBegin; 3602c9f0b5SLisandro Dalcin ierr = PetscInfo1(NULL,"Deleting tmp/shared data in an MPI_Comm %ld\n",(long)comm);CHKERRMPI(ierr); 3712801b39SBarry Smith ierr = PetscFree(count_val);CHKERRMPI(ierr); 38480cf27aSJed Brown PetscFunctionReturn(MPI_SUCCESS); 39480cf27aSJed Brown } 40e5c89e4eSSatish Balay 41e5c89e4eSSatish Balay /*@C 42e5c89e4eSSatish Balay PetscGetTmp - Gets the name of the tmp directory 43e5c89e4eSSatish Balay 44d083f849SBarry Smith Collective 45e5c89e4eSSatish Balay 46e5c89e4eSSatish Balay Input Parameters: 47e5c89e4eSSatish Balay + comm - MPI_Communicator that may share /tmp 48e5c89e4eSSatish Balay - len - length of string to hold name 49e5c89e4eSSatish Balay 50e5c89e4eSSatish Balay Output Parameters: 51e5c89e4eSSatish Balay . dir - directory name 52e5c89e4eSSatish Balay 53e5c89e4eSSatish Balay Options Database Keys: 5410699b91SBarry Smith + -shared_tmp - indicates the directory is shared among the MPI ranks 5510699b91SBarry Smith . -not_shared_tmp - indicates the directory is not shared among the MPI ranks 5610699b91SBarry Smith - -tmp tmpdir - name of the directory you wish to use as /tmp 57e5c89e4eSSatish Balay 58e5c89e4eSSatish Balay Environmental Variables: 5910699b91SBarry Smith + PETSC_SHARED_TMP - indicates the directory is shared among the MPI ranks 6010699b91SBarry Smith . PETSC_NOT_SHARED_TMP - indicates the directory is not shared among the MPI ranks 6110699b91SBarry Smith - PETSC_TMP - name of the directory you wish to use as /tmp 62e5c89e4eSSatish Balay 63e5c89e4eSSatish Balay Level: developer 64e5c89e4eSSatish Balay 65e5c89e4eSSatish Balay @*/ 667087cfbeSBarry Smith PetscErrorCode PetscGetTmp(MPI_Comm comm,char dir[],size_t len) 67e5c89e4eSSatish Balay { 68e5c89e4eSSatish Balay PetscErrorCode ierr; 69ace3abfcSBarry Smith PetscBool flg; 70e5c89e4eSSatish Balay 71e5c89e4eSSatish Balay PetscFunctionBegin; 72e5c89e4eSSatish Balay ierr = PetscOptionsGetenv(comm,"PETSC_TMP",dir,len,&flg);CHKERRQ(ierr); 73e5c89e4eSSatish Balay if (!flg) { 74e5c89e4eSSatish Balay ierr = PetscStrncpy(dir,"/tmp",len);CHKERRQ(ierr); 75e5c89e4eSSatish Balay } 76e5c89e4eSSatish Balay PetscFunctionReturn(0); 77e5c89e4eSSatish Balay } 78e5c89e4eSSatish Balay 79e5c89e4eSSatish Balay /*@C 80e5c89e4eSSatish Balay PetscSharedTmp - Determines if all processors in a communicator share a 81e5c89e4eSSatish Balay /tmp or have different ones. 82e5c89e4eSSatish Balay 83d083f849SBarry Smith Collective 84e5c89e4eSSatish Balay 85e5c89e4eSSatish Balay Input Parameters: 86e5c89e4eSSatish Balay . comm - MPI_Communicator that may share /tmp 87e5c89e4eSSatish Balay 88e5c89e4eSSatish Balay Output Parameters: 89e5c89e4eSSatish Balay . shared - PETSC_TRUE or PETSC_FALSE 90e5c89e4eSSatish Balay 91e5c89e4eSSatish Balay Options Database Keys: 9210699b91SBarry Smith + -shared_tmp - indicates the directory is shared among the MPI ranks 9310699b91SBarry Smith . -not_shared_tmp - indicates the directory is not shared among the MPI ranks 9410699b91SBarry Smith - -tmp tmpdir - name of the directory you wish to use as /tmp 95e5c89e4eSSatish Balay 96e5c89e4eSSatish Balay Environmental Variables: 9710699b91SBarry Smith + PETSC_SHARED_TMP - indicates the directory is shared among the MPI ranks 9810699b91SBarry Smith . PETSC_NOT_SHARED_TMP - indicates the directory is not shared among the MPI ranks 9910699b91SBarry Smith - PETSC_TMP - name of the directory you wish to use as /tmp 100e5c89e4eSSatish Balay 101e5c89e4eSSatish Balay Level: developer 102e5c89e4eSSatish Balay 103e5c89e4eSSatish Balay Notes: 104e5c89e4eSSatish Balay Stores the status as a MPI attribute so it does not have 105e5c89e4eSSatish Balay to be redetermined each time. 106e5c89e4eSSatish Balay 107e5c89e4eSSatish Balay Assumes that all processors in a communicator either 108e5c89e4eSSatish Balay 1) have a common /tmp or 109a8c7a070SBarry Smith 2) each has a separate /tmp 110e5c89e4eSSatish Balay eventually we can write a fancier one that determines which processors 111e5c89e4eSSatish Balay share a common /tmp. 112e5c89e4eSSatish Balay 113e5c89e4eSSatish Balay This will be very slow on runs with a large number of processors since 114e5c89e4eSSatish Balay it requires O(p*p) file opens. 115e5c89e4eSSatish Balay 116e5c89e4eSSatish Balay If the environmental variable PETSC_TMP is set it will use this directory 117e5c89e4eSSatish Balay as the "/tmp" directory. 118e5c89e4eSSatish Balay 119e5c89e4eSSatish Balay @*/ 1207087cfbeSBarry Smith PetscErrorCode PetscSharedTmp(MPI_Comm comm,PetscBool *shared) 121e5c89e4eSSatish Balay { 122e5c89e4eSSatish Balay PetscErrorCode ierr; 123e5c89e4eSSatish Balay PetscMPIInt size,rank,*tagvalp,sum,cnt,i; 124ace3abfcSBarry Smith PetscBool flg,iflg; 125e5c89e4eSSatish Balay FILE *fd; 126e5c89e4eSSatish Balay static PetscMPIInt Petsc_Tmp_keyval = MPI_KEYVAL_INVALID; 127ed9cf6e9SBarry Smith int err; 128e5c89e4eSSatish Balay 129e5c89e4eSSatish Balay PetscFunctionBegin; 130ffc4695bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr); 131e5c89e4eSSatish Balay if (size == 1) { 132e5c89e4eSSatish Balay *shared = PETSC_TRUE; 133e5c89e4eSSatish Balay PetscFunctionReturn(0); 134e5c89e4eSSatish Balay } 135e5c89e4eSSatish Balay 1360298fd71SBarry Smith ierr = PetscOptionsGetenv(comm,"PETSC_SHARED_TMP",NULL,0,&flg);CHKERRQ(ierr); 137e5c89e4eSSatish Balay if (flg) { 138e5c89e4eSSatish Balay *shared = PETSC_TRUE; 139e5c89e4eSSatish Balay PetscFunctionReturn(0); 140e5c89e4eSSatish Balay } 141e5c89e4eSSatish Balay 1420298fd71SBarry Smith ierr = PetscOptionsGetenv(comm,"PETSC_NOT_SHARED_TMP",NULL,0,&flg);CHKERRQ(ierr); 143e5c89e4eSSatish Balay if (flg) { 144e5c89e4eSSatish Balay *shared = PETSC_FALSE; 145e5c89e4eSSatish Balay PetscFunctionReturn(0); 146e5c89e4eSSatish Balay } 147e5c89e4eSSatish Balay 148e5c89e4eSSatish Balay if (Petsc_Tmp_keyval == MPI_KEYVAL_INVALID) { 149ffc4695bSBarry Smith ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,Petsc_DelTmpShared,&Petsc_Tmp_keyval,NULL);CHKERRMPI(ierr); 150e5c89e4eSSatish Balay } 151e5c89e4eSSatish Balay 152ffc4695bSBarry Smith ierr = MPI_Comm_get_attr(comm,Petsc_Tmp_keyval,(void**)&tagvalp,(int*)&iflg);CHKERRMPI(ierr); 153e5c89e4eSSatish Balay if (!iflg) { 154e5c89e4eSSatish Balay char filename[PETSC_MAX_PATH_LEN],tmpname[PETSC_MAX_PATH_LEN]; 155e5c89e4eSSatish Balay 156e5c89e4eSSatish Balay /* This communicator does not yet have a shared tmp attribute */ 157854ce69bSBarry Smith ierr = PetscMalloc1(1,&tagvalp);CHKERRQ(ierr); 158ffc4695bSBarry Smith ierr = MPI_Comm_set_attr(comm,Petsc_Tmp_keyval,tagvalp);CHKERRMPI(ierr); 159e5c89e4eSSatish Balay 160e5c89e4eSSatish Balay ierr = PetscOptionsGetenv(comm,"PETSC_TMP",tmpname,238,&iflg);CHKERRQ(ierr); 161e5c89e4eSSatish Balay if (!iflg) { 162e5c89e4eSSatish Balay ierr = PetscStrcpy(filename,"/tmp");CHKERRQ(ierr); 163e5c89e4eSSatish Balay } else { 164e5c89e4eSSatish Balay ierr = PetscStrcpy(filename,tmpname);CHKERRQ(ierr); 165e5c89e4eSSatish Balay } 166e5c89e4eSSatish Balay 167e5c89e4eSSatish Balay ierr = PetscStrcat(filename,"/petsctestshared");CHKERRQ(ierr); 168ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 169e5c89e4eSSatish Balay 170e5c89e4eSSatish Balay /* each processor creates a /tmp file and all the later ones check */ 171e5c89e4eSSatish Balay /* this makes sure no subset of processors is shared */ 172e5c89e4eSSatish Balay *shared = PETSC_FALSE; 173e5c89e4eSSatish Balay for (i=0; i<size-1; i++) { 174e5c89e4eSSatish Balay if (rank == i) { 175e5c89e4eSSatish Balay fd = fopen(filename,"w"); 176eb3354f8SBarry Smith if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open test file %s",filename); 177ed9cf6e9SBarry Smith err = fclose(fd); 178e32f2f54SBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 179e5c89e4eSSatish Balay } 180ffc4695bSBarry Smith ierr = MPI_Barrier(comm);CHKERRMPI(ierr); 181e5c89e4eSSatish Balay if (rank >= i) { 182e5c89e4eSSatish Balay fd = fopen(filename,"r"); 183a297a907SKarl Rupp if (fd) cnt = 1; 184a297a907SKarl Rupp else cnt = 0; 185e5c89e4eSSatish Balay if (fd) { 186ed9cf6e9SBarry Smith err = fclose(fd); 187e32f2f54SBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 188e5c89e4eSSatish Balay } 189a297a907SKarl Rupp } else cnt = 0; 190a297a907SKarl Rupp 191820f2d46SBarry Smith ierr = MPIU_Allreduce(&cnt,&sum,1,MPI_INT,MPI_SUM,comm);CHKERRMPI(ierr); 192a297a907SKarl Rupp if (rank == i) unlink(filename); 193e5c89e4eSSatish Balay 194e5c89e4eSSatish Balay if (sum == size) { 195e5c89e4eSSatish Balay *shared = PETSC_TRUE; 196e5c89e4eSSatish Balay break; 197eb3354f8SBarry Smith } else if (sum != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Subset of processes share /tmp "); 198e5c89e4eSSatish Balay } 199e5c89e4eSSatish Balay *tagvalp = (int)*shared; 20002c9f0b5SLisandro Dalcin ierr = PetscInfo2(NULL,"processors %s %s\n",(*shared) ? "share":"do NOT share",(iflg ? tmpname:"/tmp"));CHKERRQ(ierr); 201a297a907SKarl Rupp } else *shared = (PetscBool) *tagvalp; 202e5c89e4eSSatish Balay PetscFunctionReturn(0); 203e5c89e4eSSatish Balay } 204e5c89e4eSSatish Balay 205e5c89e4eSSatish Balay /*@C 206e5c89e4eSSatish Balay PetscSharedWorkingDirectory - Determines if all processors in a communicator share a 207e5c89e4eSSatish Balay working directory or have different ones. 208e5c89e4eSSatish Balay 209d083f849SBarry Smith Collective 210e5c89e4eSSatish Balay 211e5c89e4eSSatish Balay Input Parameters: 212e5c89e4eSSatish Balay . comm - MPI_Communicator that may share working directory 213e5c89e4eSSatish Balay 214e5c89e4eSSatish Balay Output Parameters: 215e5c89e4eSSatish Balay . shared - PETSC_TRUE or PETSC_FALSE 216e5c89e4eSSatish Balay 217e5c89e4eSSatish Balay Options Database Keys: 21810699b91SBarry Smith + -shared_working_directory - indicates the directory is shared among the MPI ranks 21910699b91SBarry Smith - -not_shared_working_directory - indicates the directory is shared among the MPI ranks 220e5c89e4eSSatish Balay 221e5c89e4eSSatish Balay Environmental Variables: 22210699b91SBarry Smith + PETSC_SHARED_WORKING_DIRECTORY - indicates the directory is shared among the MPI ranks 2233222ab0cSSatish Balay - PETSC_NOT_SHARED_WORKING_DIRECTORY - indicates the directory is shared among the MPI ranks 224e5c89e4eSSatish Balay 225e5c89e4eSSatish Balay Level: developer 226e5c89e4eSSatish Balay 227e5c89e4eSSatish Balay Notes: 228e5c89e4eSSatish Balay Stores the status as a MPI attribute so it does not have 229e5c89e4eSSatish Balay to be redetermined each time. 230e5c89e4eSSatish Balay 231e5c89e4eSSatish Balay Assumes that all processors in a communicator either 232e5c89e4eSSatish Balay 1) have a common working directory or 233a8c7a070SBarry Smith 2) each has a separate working directory 234e5c89e4eSSatish Balay eventually we can write a fancier one that determines which processors 235e5c89e4eSSatish Balay share a common working directory. 236e5c89e4eSSatish Balay 237e5c89e4eSSatish Balay This will be very slow on runs with a large number of processors since 238e5c89e4eSSatish Balay it requires O(p*p) file opens. 239e5c89e4eSSatish Balay 240e5c89e4eSSatish Balay @*/ 2417087cfbeSBarry Smith PetscErrorCode PetscSharedWorkingDirectory(MPI_Comm comm,PetscBool *shared) 242e5c89e4eSSatish Balay { 243e5c89e4eSSatish Balay PetscErrorCode ierr; 244e5c89e4eSSatish Balay PetscMPIInt size,rank,*tagvalp,sum,cnt,i; 245ace3abfcSBarry Smith PetscBool flg,iflg; 246e5c89e4eSSatish Balay FILE *fd; 247e5c89e4eSSatish Balay static PetscMPIInt Petsc_WD_keyval = MPI_KEYVAL_INVALID; 248ed9cf6e9SBarry Smith int err; 249e5c89e4eSSatish Balay 250e5c89e4eSSatish Balay PetscFunctionBegin; 251ffc4695bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr); 252e5c89e4eSSatish Balay if (size == 1) { 253e5c89e4eSSatish Balay *shared = PETSC_TRUE; 254e5c89e4eSSatish Balay PetscFunctionReturn(0); 255e5c89e4eSSatish Balay } 256e5c89e4eSSatish Balay 2570298fd71SBarry Smith ierr = PetscOptionsGetenv(comm,"PETSC_SHARED_WORKING_DIRECTORY",NULL,0,&flg);CHKERRQ(ierr); 258e5c89e4eSSatish Balay if (flg) { 259e5c89e4eSSatish Balay *shared = PETSC_TRUE; 260e5c89e4eSSatish Balay PetscFunctionReturn(0); 261e5c89e4eSSatish Balay } 262e5c89e4eSSatish Balay 2630298fd71SBarry Smith ierr = PetscOptionsGetenv(comm,"PETSC_NOT_SHARED_WORKING_DIRECTORY",NULL,0,&flg);CHKERRQ(ierr); 264e5c89e4eSSatish Balay if (flg) { 265e5c89e4eSSatish Balay *shared = PETSC_FALSE; 266e5c89e4eSSatish Balay PetscFunctionReturn(0); 267e5c89e4eSSatish Balay } 268e5c89e4eSSatish Balay 269e5c89e4eSSatish Balay if (Petsc_WD_keyval == MPI_KEYVAL_INVALID) { 270ffc4695bSBarry Smith ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,Petsc_DelTmpShared,&Petsc_WD_keyval,NULL);CHKERRMPI(ierr); 271e5c89e4eSSatish Balay } 272e5c89e4eSSatish Balay 273ffc4695bSBarry Smith ierr = MPI_Comm_get_attr(comm,Petsc_WD_keyval,(void**)&tagvalp,(int*)&iflg);CHKERRMPI(ierr); 274e5c89e4eSSatish Balay if (!iflg) { 275e5c89e4eSSatish Balay char filename[PETSC_MAX_PATH_LEN]; 276e5c89e4eSSatish Balay 277e5c89e4eSSatish Balay /* This communicator does not yet have a shared attribute */ 278854ce69bSBarry Smith ierr = PetscMalloc1(1,&tagvalp);CHKERRQ(ierr); 279ffc4695bSBarry Smith ierr = MPI_Comm_set_attr(comm,Petsc_WD_keyval,tagvalp);CHKERRMPI(ierr); 280e5c89e4eSSatish Balay 281e5c89e4eSSatish Balay ierr = PetscGetWorkingDirectory(filename,240);CHKERRQ(ierr); 282e5c89e4eSSatish Balay ierr = PetscStrcat(filename,"/petsctestshared");CHKERRQ(ierr); 283ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 284e5c89e4eSSatish Balay 285e5c89e4eSSatish Balay /* each processor creates a file and all the later ones check */ 286e5c89e4eSSatish Balay /* this makes sure no subset of processors is shared */ 287e5c89e4eSSatish Balay *shared = PETSC_FALSE; 288e5c89e4eSSatish Balay for (i=0; i<size-1; i++) { 289e5c89e4eSSatish Balay if (rank == i) { 290e5c89e4eSSatish Balay fd = fopen(filename,"w"); 291e32f2f54SBarry Smith if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open test file %s",filename); 292ed9cf6e9SBarry Smith err = fclose(fd); 293e32f2f54SBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 294e5c89e4eSSatish Balay } 295ffc4695bSBarry Smith ierr = MPI_Barrier(comm);CHKERRMPI(ierr); 296e5c89e4eSSatish Balay if (rank >= i) { 297e5c89e4eSSatish Balay fd = fopen(filename,"r"); 298a297a907SKarl Rupp if (fd) cnt = 1; 299a297a907SKarl Rupp else cnt = 0; 300e5c89e4eSSatish Balay if (fd) { 301ed9cf6e9SBarry Smith err = fclose(fd); 302e32f2f54SBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 303e5c89e4eSSatish Balay } 304a297a907SKarl Rupp } else cnt = 0; 305a297a907SKarl Rupp 306820f2d46SBarry Smith ierr = MPIU_Allreduce(&cnt,&sum,1,MPI_INT,MPI_SUM,comm);CHKERRMPI(ierr); 307a297a907SKarl Rupp if (rank == i) unlink(filename); 308e5c89e4eSSatish Balay 309e5c89e4eSSatish Balay if (sum == size) { 310e5c89e4eSSatish Balay *shared = PETSC_TRUE; 311e5c89e4eSSatish Balay break; 312eb3354f8SBarry Smith } else if (sum != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Subset of processes share working directory"); 313e5c89e4eSSatish Balay } 314e5c89e4eSSatish Balay *tagvalp = (int)*shared; 315a297a907SKarl Rupp } else *shared = (PetscBool) *tagvalp; 31602c9f0b5SLisandro Dalcin ierr = PetscInfo1(NULL,"processors %s working directory\n",(*shared) ? "shared" : "do NOT share");CHKERRQ(ierr); 317e5c89e4eSSatish Balay PetscFunctionReturn(0); 318e5c89e4eSSatish Balay } 319e5c89e4eSSatish Balay 320e5c89e4eSSatish Balay /*@C 3210c4f890aSBarry Smith PetscFileRetrieve - Obtains a file from a URL or compressed 322e5c89e4eSSatish Balay and copies into local disk space as uncompressed. 323e5c89e4eSSatish Balay 324d083f849SBarry Smith Collective 325e5c89e4eSSatish Balay 326*d8d19677SJose E. Roman Input Parameters: 3270c4f890aSBarry Smith + comm - processors accessing the file 3280c4f890aSBarry Smith . url - name of file, including entire URL (with or without .gz) 3290c4f890aSBarry Smith - llen - length of localname 330e5c89e4eSSatish Balay 331*d8d19677SJose E. Roman Output Parameters: 33208fb59bfSBarry Smith + localname - name of local copy of file - valid on only process zero 33308fb59bfSBarry Smith - found - if found or retrieved the file - valid on all processes 334e5c89e4eSSatish Balay 33595452b02SPatrick Sanan Notes: 33695452b02SPatrick Sanan if the file already exists local this function just returns without downloading it. 337e5c89e4eSSatish Balay 3380c4f890aSBarry Smith Level: intermediate 339e5c89e4eSSatish Balay @*/ 3400c4f890aSBarry Smith PetscErrorCode PetscFileRetrieve(MPI_Comm comm,const char url[],char localname[],size_t llen,PetscBool *found) 341e5c89e4eSSatish Balay { 34208fb59bfSBarry Smith char buffer[PETSC_MAX_PATH_LEN],*par,*tlocalname,name[PETSC_MAX_PATH_LEN]; 343e5c89e4eSSatish Balay FILE *fp; 344e5c89e4eSSatish Balay PetscErrorCode ierr; 345e5c89e4eSSatish Balay PetscMPIInt rank; 346e5c89e4eSSatish Balay size_t len = 0; 34708fb59bfSBarry Smith PetscBool flg1,flg2,flg3,flg4,download,compressed = PETSC_FALSE; 348e5c89e4eSSatish Balay 349e5c89e4eSSatish Balay PetscFunctionBegin; 350ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 35108fb59bfSBarry Smith if (!rank) { 352e5c89e4eSSatish Balay *found = PETSC_FALSE; 353e5c89e4eSSatish Balay 3540c4f890aSBarry Smith ierr = PetscStrstr(url,".gz",&par);CHKERRQ(ierr); 35508fb59bfSBarry Smith if (par) { 35608fb59bfSBarry Smith ierr = PetscStrlen(par,&len);CHKERRQ(ierr); 35708fb59bfSBarry Smith if (len == 3) compressed = PETSC_TRUE; 35808fb59bfSBarry Smith } 359e5c89e4eSSatish Balay 3600c4f890aSBarry Smith ierr = PetscStrncmp(url,"ftp://",6,&flg1);CHKERRQ(ierr); 3610c4f890aSBarry Smith ierr = PetscStrncmp(url,"http://",7,&flg2);CHKERRQ(ierr); 3620c4f890aSBarry Smith ierr = PetscStrncmp(url,"file://",7,&flg3);CHKERRQ(ierr); 36308fb59bfSBarry Smith ierr = PetscStrncmp(url,"https://",8,&flg4);CHKERRQ(ierr); 364a4772d12SBarry Smith download = (PetscBool) (flg1 || flg2 || flg3 || flg4); 36508fb59bfSBarry Smith 36608fb59bfSBarry Smith if (!download && !compressed) { 3670c4f890aSBarry Smith ierr = PetscStrncpy(localname,url,llen);CHKERRQ(ierr); 3680c4f890aSBarry Smith ierr = PetscTestFile(url,'r',found);CHKERRQ(ierr); 369487e5849SBarry Smith if (*found) { 3700c4f890aSBarry Smith ierr = PetscInfo1(NULL,"Found file %s\n",url);CHKERRQ(ierr); 371487e5849SBarry Smith } else { 3720c4f890aSBarry Smith ierr = PetscInfo1(NULL,"Did not find file %s\n",url);CHKERRQ(ierr); 373487e5849SBarry Smith } 37408fb59bfSBarry Smith goto done; 375734f99bcSBarry Smith } 376734f99bcSBarry Smith 37705698389SBarry Smith /* look for uncompressed file in requested directory */ 37805698389SBarry Smith if (compressed) { 37905698389SBarry Smith ierr = PetscStrncpy(localname,url,llen);CHKERRQ(ierr); 38005698389SBarry Smith ierr = PetscStrstr(localname,".gz",&par);CHKERRQ(ierr); 38105698389SBarry Smith *par = 0; /* remove .gz extension */ 38205698389SBarry Smith ierr = PetscTestFile(localname,'r',found);CHKERRQ(ierr); 38305698389SBarry Smith if (*found) goto done; 38405698389SBarry Smith } 38505698389SBarry Smith 38605698389SBarry Smith /* look for file in current directory */ 3870c4f890aSBarry Smith ierr = PetscStrrchr(url,'/',&tlocalname);CHKERRQ(ierr); 3880c4f890aSBarry Smith ierr = PetscStrncpy(localname,tlocalname,llen);CHKERRQ(ierr); 38908fb59bfSBarry Smith if (compressed) { 39008fb59bfSBarry Smith ierr = PetscStrstr(localname,".gz",&par);CHKERRQ(ierr); 39108fb59bfSBarry Smith *par = 0; /* remove .gz extension */ 39208fb59bfSBarry Smith } 3930c4f890aSBarry Smith ierr = PetscTestFile(localname,'r',found);CHKERRQ(ierr); 39408fb59bfSBarry Smith if (*found) goto done; 395e5c89e4eSSatish Balay 39608fb59bfSBarry Smith if (download) { 39708fb59bfSBarry Smith /* local file is not already here so use curl to get it */ 39808fb59bfSBarry Smith ierr = PetscStrncpy(localname,tlocalname,llen);CHKERRQ(ierr); 399e8eec0a8SKarl Rupp ierr = PetscStrcpy(buffer,"curl --fail --silent --show-error ");CHKERRQ(ierr); 40008fb59bfSBarry Smith ierr = PetscStrcat(buffer,url);CHKERRQ(ierr); 40108fb59bfSBarry Smith ierr = PetscStrcat(buffer," > ");CHKERRQ(ierr); 40208fb59bfSBarry Smith ierr = PetscStrcat(buffer,localname);CHKERRQ(ierr); 403e5c89e4eSSatish Balay #if defined(PETSC_HAVE_POPEN) 40408fb59bfSBarry Smith ierr = PetscPOpen(PETSC_COMM_SELF,NULL,buffer,"r",&fp);CHKERRQ(ierr); 405016831caSBarry Smith ierr = PetscPClose(PETSC_COMM_SELF,fp);CHKERRQ(ierr); 406e5c89e4eSSatish Balay #else 407e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 408e5c89e4eSSatish Balay #endif 4090c4f890aSBarry Smith ierr = PetscTestFile(localname,'r',found);CHKERRQ(ierr); 4106e3a5469SBarry Smith if (*found) { 4116e3a5469SBarry Smith FILE *fd; 4126e3a5469SBarry Smith char buf[1024],*str,*substring; 4136e3a5469SBarry Smith 4146e3a5469SBarry Smith /* check if the file didn't exist so it downloaded an HTML message instead */ 4156e3a5469SBarry Smith fd = fopen(localname,"r"); 4166e3a5469SBarry Smith if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"PetscTestFile() indicates %s exists but fopen() cannot open it",localname); 4176e3a5469SBarry Smith str = fgets(buf,sizeof(buf)-1,fd); 4186e3a5469SBarry Smith while (str) { 4196e3a5469SBarry Smith ierr = PetscStrstr(buf,"<!DOCTYPE html>",&substring);CHKERRQ(ierr); 4206e3a5469SBarry Smith if (substring) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unable to download %s it does not appear to exist at this URL, dummy HTML file was downloaded",url); 4216e3a5469SBarry Smith ierr = PetscStrstr(buf,"Not Found",&substring);CHKERRQ(ierr); 4226e3a5469SBarry Smith if (substring) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unable to download %s it does not appear to exist at this URL, dummy HTML file was downloaded",url); 4236e3a5469SBarry Smith str = fgets(buf,sizeof(buf)-1,fd); 4246e3a5469SBarry Smith } 4256e3a5469SBarry Smith fclose(fd); 4266e3a5469SBarry Smith } 42708fb59bfSBarry Smith } else if (compressed) { 42808fb59bfSBarry Smith ierr = PetscTestFile(url,'r',found);CHKERRQ(ierr); 42908fb59bfSBarry Smith if (!*found) goto done; 43008fb59bfSBarry Smith ierr = PetscStrncpy(localname,url,llen);CHKERRQ(ierr); 43108fb59bfSBarry Smith } 43208fb59bfSBarry Smith if (compressed) { 43308fb59bfSBarry Smith ierr = PetscStrrchr(localname,'/',&tlocalname);CHKERRQ(ierr); 43408fb59bfSBarry Smith ierr = PetscStrncpy(name,tlocalname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 43508fb59bfSBarry Smith ierr = PetscStrstr(name,".gz",&par);CHKERRQ(ierr); 43608fb59bfSBarry Smith *par = 0; /* remove .gz extension */ 43708fb59bfSBarry Smith /* uncompress file */ 43808fb59bfSBarry Smith ierr = PetscStrcpy(buffer,"gzip -c -d ");CHKERRQ(ierr); 43908fb59bfSBarry Smith ierr = PetscStrcat(buffer,localname);CHKERRQ(ierr); 44008fb59bfSBarry Smith ierr = PetscStrcat(buffer," > ");CHKERRQ(ierr); 44108fb59bfSBarry Smith ierr = PetscStrcat(buffer,name);CHKERRQ(ierr); 44208fb59bfSBarry Smith #if defined(PETSC_HAVE_POPEN) 44308fb59bfSBarry Smith ierr = PetscPOpen(PETSC_COMM_SELF,NULL,buffer,"r",&fp);CHKERRQ(ierr); 444016831caSBarry Smith ierr = PetscPClose(PETSC_COMM_SELF,fp);CHKERRQ(ierr); 44508fb59bfSBarry Smith #else 44608fb59bfSBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 44708fb59bfSBarry Smith #endif 44808fb59bfSBarry Smith ierr = PetscStrncpy(localname,name,llen);CHKERRQ(ierr); 44908fb59bfSBarry Smith ierr = PetscTestFile(localname,'r',found);CHKERRQ(ierr); 450e5c89e4eSSatish Balay } 451e5c89e4eSSatish Balay } 452955d42a0SBarry Smith done: 453ffc4695bSBarry Smith ierr = MPI_Bcast(found,1,MPIU_BOOL,0,comm);CHKERRMPI(ierr); 454ffc4695bSBarry Smith ierr = MPI_Bcast(localname, llen, MPI_CHAR, 0, comm);CHKERRMPI(ierr); 455e5c89e4eSSatish Balay PetscFunctionReturn(0); 456e5c89e4eSSatish Balay } 457