/*@C PetscGetFullPath - Given a filename, returns the fully qualified file name. Not Collective Input Parameters: + path - pathname to qualify . fullpath - pointer to buffer to hold full pathname - flen - size of fullpath Level: developer Concepts: full path Concepts: path^full .seealso: PetscGetRelativePath() @*/ PetscErrorCode PetscGetFullPath(const char path[],char fullpath[],size_t flen) { PetscErrorCode ierr; size_t ln; PetscBool flg; PetscFunctionBegin; if (path[0] == '/') { ierr = PetscStrncmp("/tmp_mnt/",path,9,&flg);CHKERRQ(ierr); if (flg) {ierr = PetscStrncpy(fullpath,path + 8,flen);CHKERRQ(ierr);} else {ierr = PetscStrncpy(fullpath,path,flen);CHKERRQ(ierr);} fullpath[flen-1] = 0; PetscFunctionReturn(0); } ierr = PetscStrncpy(fullpath,path,flen);CHKERRQ(ierr); fullpath[flen-1] = 0; /* Remove the various "special" forms (~username/ and ~/) */ if (fullpath[0] == '~') { char tmppath[PETSC_MAX_PATH_LEN],*rest; if (fullpath[1] == '/') { ierr = PetscGetHomeDirectory(tmppath,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); rest = fullpath + 2; } else { #if defined(PETSC_HAVE_PWD_H) struct passwd *pwde; char *p,*name; /* Find username */ name = fullpath + 1; p = name; while (*p && *p != '/') p++; *p = 0; rest = p + 1; pwde = getpwnam(name); if (!pwde) PetscFunctionReturn(0); ierr = PetscStrcpy(tmppath,pwde->pw_dir);CHKERRQ(ierr); #else PetscFunctionReturn(0); #endif } ierr = PetscStrlen(tmppath,&ln);CHKERRQ(ierr); if (tmppath[ln-1] != '/') {ierr = PetscStrcat(tmppath+ln-1,"/");CHKERRQ(ierr);} ierr = PetscStrcat(tmppath,rest);CHKERRQ(ierr); ierr = PetscStrncpy(fullpath,tmppath,flen);CHKERRQ(ierr); fullpath[flen-1] = 0; } else { ierr = PetscGetWorkingDirectory(fullpath,flen);CHKERRQ(ierr); ierr = PetscStrlen(fullpath,&ln);CHKERRQ(ierr); ierr = PetscStrncpy(fullpath+ln,"/",flen - ln);CHKERRQ(ierr); fullpath[flen-1] = 0; ierr = PetscStrlen(fullpath,&ln);CHKERRQ(ierr); if (path[0] == '.' && path[1] == '/') { ierr = PetscStrncat(fullpath,path+2,flen - ln - 1);CHKERRQ(ierr); } else { ierr = PetscStrncat(fullpath,path,flen - ln - 1);CHKERRQ(ierr); } fullpath[flen-1] = 0; } /* Remove the automounter part of the path */ ierr = PetscStrncmp(fullpath,"/tmp_mnt/",9,&flg);CHKERRQ(ierr); if (flg) { char tmppath[PETSC_MAX_PATH_LEN]; ierr = PetscStrcpy(tmppath,fullpath + 8);CHKERRQ(ierr); ierr = PetscStrcpy(fullpath,tmppath);CHKERRQ(ierr); } /* We could try to handle things like the removal of .. etc */ PetscFunctionReturn(0); }
int main( int argc, char **argv ) { Mat hmat,smat; PetscInt n=0,row,col, nev; PetscScalar number; PetscViewer view; PetscBool flag; int ierr,i; char path[PETSC_MAX_PATH_LEN]="", temporary[PETSC_MAX_PATH_LEN]=""; char infilename[PETSC_MAX_PATH_LEN]="", outfilename[PETSC_MAX_PATH_LEN]=""; SlepcInitialize(&argc,&argv,(char*)0,help); ierr = PetscOptionsGetString(PETSC_NULL,"-in",infilename,PETSC_MAX_PATH_LEN-1,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetString(PETSC_NULL,"-out",outfilename,PETSC_MAX_PATH_LEN-1,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr); ierr = PetscGetWorkingDirectory(path,PETSC_MAX_PATH_LEN); if ( strcmp(infilename, "") == 0 || strcmp(outfilename, "") == 0 || n == 0 ){ SETERRQ(PETSC_COMM_WORLD,1,"Error in parameterlist.\nExample call: ./convertToPETScBin -in matrix.hmat -out matrix.dat -n 1912"); } // number = 2.0 + 3.0 * PETSC_i; // PetscPrintf(PETSC_COMM_WORLD,"number: %g i%g\n", number); // std::cout << number PetscPrintf(PETSC_COMM_WORLD,"processing %s into %s\n", infilename, outfilename); ierr = MatCreateSeqAIJ(PETSC_COMM_WORLD,n,n,n,PETSC_NULL,&hmat); FILE *file; double real, imag; PetscScalar complex; PetscScalar myreal; strcat(temporary, path); strcat(temporary, "/"); strcat(temporary, infilename); strcpy(infilename, temporary); ierr = PetscFOpen(PETSC_COMM_SELF,infilename,"r",&file);CHKERRQ(ierr); while ( fscanf(file,"%d %d %le %le\n",&row,&col,(double*)&real, (double*)&imag) != EOF ){ row = row - 1; col = col -1; // adjustment if ( row == col ){ complex = real /*+ imag*PETSC_i*/; ierr = MatSetValues(hmat,1,&row,1,&col,&complex,INSERT_VALUES);CHKERRQ(ierr); }else{ complex = real /*+ imag*PETSC_i */; ierr = MatSetValues(hmat,1,&row,1,&col,&complex,INSERT_VALUES);CHKERRQ(ierr); complex = real /*- imag*PETSC_i */; ierr = MatSetValues(hmat,1,&col,1,&row,&complex,INSERT_VALUES);CHKERRQ(ierr); } } fclose(file); ierr = MatAssemblyBegin(hmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(hmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatIsHermitian(hmat,0.00000000000001,&flag); CHKERRQ(ierr); if ( !flag ){ SETERRQ(PETSC_COMM_WORLD,1,"Error. Output matrix is not hermitian."); } // writing strcpy(temporary,""); strcat(temporary, path); strcat(temporary, "/"); strcat(temporary, outfilename); strcpy(outfilename, temporary); ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,outfilename,FILE_MODE_WRITE,&view);CHKERRQ(ierr); ierr = MatView(hmat,view);CHKERRQ(ierr); ierr = PetscViewersDestroy(&view); ierr = SlepcFinalize();CHKERRQ(ierr); return 0; }
int main( int argc, char **argv ) { int ierr,i,j; char infilename[PETSC_MAX_PATH_LEN]="", temporary[PETSC_MAX_PATH_LEN]="", path[PETSC_MAX_PATH_LEN]=""; PetscViewer viewer; PetscBool isTail=PETSC_FALSE,isRight=PETSC_FALSE; Mat mat; PetscInt idxn[DIM], idxm[DIM], m, n, windowX=0, windowY=0; PetscScalar values[DIM*DIM]; SlepcInitialize(&argc,&argv,(char*)0,help); ierr = PetscOptionsGetString(PETSC_NULL,"-file",infilename,PETSC_MAX_PATH_LEN-1,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(PETSC_NULL, "-tail", &isTail, NULL); ierr = PetscOptionsGetBool(PETSC_NULL, "-right", &isRight, NULL); ierr = PetscGetWorkingDirectory(path,PETSC_MAX_PATH_LEN); // concat filename strcat(temporary, path); strcat(temporary, "/"); strcat(temporary, infilename); strcpy(infilename, temporary); if ( strcmp(infilename, "") == 0 ){ SETERRQ(PETSC_COMM_WORLD,1,"Specifiy the filename!\nExample call: ./inspectBinary -file matrix.dat"); } ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,infilename,FILE_MODE_READ,&viewer);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&mat);CHKERRQ(ierr); ierr = MatSetFromOptions(mat);CHKERRQ(ierr); ierr = MatLoad(mat,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); MatGetSize(mat,&m,&n); if ( isTail == PETSC_TRUE ){ windowY = m - DIM; } if ( isRight == PETSC_TRUE ){ windowX = n - DIM; } for ( i = 0 ; i < DIM ; ++i ){ idxm[i] = i + windowY; idxn[i] = i + windowX; } MatGetValues(mat,DIM,idxm,DIM,idxn,values); ierr = PetscPrintf(PETSC_COMM_WORLD,"Dimension: %d rows x %d columns:\n", m, n);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"row index idxm:");CHKERRQ(ierr); for ( i = 0 ; i < DIM ; ++i ){ ierr = PetscPrintf(PETSC_COMM_WORLD,"%d, ", idxm[i]);CHKERRQ(ierr); } ierr = PetscPrintf(PETSC_COMM_WORLD,"\ncol index idxn:");CHKERRQ(ierr); for ( i = 0 ; i < DIM ; ++i ){ ierr = PetscPrintf(PETSC_COMM_WORLD,"%d, ", idxn[i]);CHKERRQ(ierr); } ierr = PetscPrintf(PETSC_COMM_WORLD,"\n\n\n");CHKERRQ(ierr); for ( i = 0 ; i < DIM ; ++i ){ for ( j = 0 ; j < DIM ; ++j ){ ierr = PetscPrintf(PETSC_COMM_WORLD,"(%2.10e/ %2.10e) ", PetscRealPart(values[i*DIM + j]), PetscImaginaryPart(values[i*DIM + j]) );CHKERRQ(ierr); } ierr = PetscPrintf(PETSC_COMM_WORLD,"\n");CHKERRQ(ierr); } ierr = SlepcFinalize();CHKERRQ(ierr); return 0; }
/*@C PetscSharedWorkingDirectory - Determines if all processors in a communicator share a working directory or have different ones. Collective on MPI_Comm Input Parameters: . comm - MPI_Communicator that may share working directory Output Parameters: . shared - PETSC_TRUE or PETSC_FALSE Options Database Keys: + -shared_working_directory . -not_shared_working_directory Environmental Variables: + PETSC_SHARED_WORKING_DIRECTORY . PETSC_NOT_SHARED_WORKING_DIRECTORY Level: developer Notes: Stores the status as a MPI attribute so it does not have to be redetermined each time. Assumes that all processors in a communicator either 1) have a common working directory or 2) each has a separate working directory eventually we can write a fancier one that determines which processors share a common working directory. This will be very slow on runs with a large number of processors since it requires O(p*p) file opens. @*/ PetscErrorCode PetscSharedWorkingDirectory(MPI_Comm comm,PetscBool *shared) { PetscErrorCode ierr; PetscMPIInt size,rank,*tagvalp,sum,cnt,i; PetscBool flg,iflg; FILE *fd; static PetscMPIInt Petsc_WD_keyval = MPI_KEYVAL_INVALID; int err; PetscFunctionBegin; ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); if (size == 1) { *shared = PETSC_TRUE; PetscFunctionReturn(0); } ierr = PetscOptionsGetenv(comm,"PETSC_SHARED_WORKING_DIRECTORY",NULL,0,&flg);CHKERRQ(ierr); if (flg) { *shared = PETSC_TRUE; PetscFunctionReturn(0); } ierr = PetscOptionsGetenv(comm,"PETSC_NOT_SHARED_WORKING_DIRECTORY",NULL,0,&flg);CHKERRQ(ierr); if (flg) { *shared = PETSC_FALSE; PetscFunctionReturn(0); } if (Petsc_WD_keyval == MPI_KEYVAL_INVALID) { ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelTmpShared,&Petsc_WD_keyval,0);CHKERRQ(ierr); } ierr = MPI_Attr_get(comm,Petsc_WD_keyval,(void**)&tagvalp,(int*)&iflg);CHKERRQ(ierr); if (!iflg) { char filename[PETSC_MAX_PATH_LEN]; /* This communicator does not yet have a shared attribute */ ierr = PetscMalloc1(1,&tagvalp);CHKERRQ(ierr); ierr = MPI_Attr_put(comm,Petsc_WD_keyval,tagvalp);CHKERRQ(ierr); ierr = PetscGetWorkingDirectory(filename,240);CHKERRQ(ierr); ierr = PetscStrcat(filename,"/petsctestshared");CHKERRQ(ierr); ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); /* each processor creates a file and all the later ones check */ /* this makes sure no subset of processors is shared */ *shared = PETSC_FALSE; for (i=0; i<size-1; i++) { if (rank == i) { fd = fopen(filename,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open test file %s",filename); err = fclose(fd); if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); } ierr = MPI_Barrier(comm);CHKERRQ(ierr); if (rank >= i) { fd = fopen(filename,"r"); if (fd) cnt = 1; else cnt = 0; if (fd) { err = fclose(fd); if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); } } else cnt = 0; ierr = MPIU_Allreduce(&cnt,&sum,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr); if (rank == i) unlink(filename); if (sum == size) { *shared = PETSC_TRUE; break; } else if (sum != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Subset of processes share working directory"); } *tagvalp = (int)*shared; } else *shared = (PetscBool) *tagvalp; ierr = PetscInfo1(0,"processors %s working directory\n",(*shared) ? "shared" : "do NOT share");CHKERRQ(ierr); PetscFunctionReturn(0); }