00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <stdlib.h>
00020 #include <string.h>
00021 #include "anamod.h"
00022 #include "petsc.h"
00023
00024 static PetscTruth single_proc = PETSC_FALSE, expensive = PETSC_FALSE;
00025
00026 #undef __FUNCT__
00027 #define __FUNCT__ "AnaModOptionsHandling"
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 PetscErrorCode AnaModOptionsHandling()
00039 {
00040 PetscTruth flg; PetscErrorCode ierr;
00041 char *value;
00042 PetscFunctionBegin;
00043
00044 #define VALUELEN 150
00045 ierr = PetscMalloc(VALUELEN*sizeof(char),&value); CHKERRQ(ierr);
00046
00047 {
00048 ierr = PetscOptionsHasName(PETSC_NULL,"-anamod_force",&flg); CHKERRQ(ierr);
00049 if (flg) {
00050 ierr = PetscOptionsGetString
00051 (PETSC_NULL,"-anamod_force",value,VALUELEN,&flg); CHKERRQ(ierr);
00052 if (flg) {
00053 char *c;
00054 c = strtok(value,",");
00055 while (c) {
00056 ierr = PetscStrcmp(c,"sequential",&flg); CHKERRQ(ierr);
00057 if (flg) single_proc = PETSC_TRUE;
00058 ierr = PetscStrcmp(c,"expensive",&flg); CHKERRQ(ierr);
00059 if (flg) expensive = PETSC_TRUE;
00060 c = strtok(NULL,",");
00061 }
00062 }
00063 }
00064 }
00065
00066 {
00067 char **categories,*option; int icat,ncats;
00068 ierr = PetscMalloc(30*sizeof(char),&option); CHKERRQ(ierr);
00069 ierr = GetCategories(&categories,&ncats); CHKERRQ(ierr);
00070 for (icat=0; icat<ncats; icat++) {
00071 ierr = PetscMemzero(value,VALUELEN*sizeof(char)); CHKERRQ(ierr);
00072 ierr = PetscMemzero(option,30*sizeof(char)); CHKERRQ(ierr);
00073 sprintf(option,"-anamod_%s",categories[icat]);
00074 ierr = PetscOptionsGetString
00075 (PETSC_NULL,option,value,VALUELEN,&flg); CHKERRQ(ierr);
00076 if (flg) {
00077 PetscErrorCode (*f)(char*);
00078 ierr = GetCategoryOptionFunction(categories[icat],&f); CHKERRQ(ierr);
00079 if (f) {
00080 ierr = (*f)(value); CHKERRQ(ierr);
00081 }
00082 }
00083 }
00084 ierr = PetscFree(option); CHKERRQ(ierr);
00085 }
00086
00087 ierr = PetscFree(value); CHKERRQ(ierr);
00088
00089 PetscFunctionReturn(0);
00090 }
00091
00092 #undef __FUNCT__
00093 #define __FUNCT__ "AnaModShowOptions"
00094
00095
00096
00097
00098
00099 PetscErrorCode AnaModShowOptions(MPI_Comm comm)
00100 {
00101 PetscErrorCode ierr;
00102 PetscFunctionBegin;
00103 PetscPrintf(comm,"Available AnaMod options\n Global:\n");
00104 PetscPrintf(comm," -anamod_force sequential : do sequential computations\n even in parallel runs\n");
00105 PetscPrintf(comm," -anamod_force expensive : do certain calculations no matter how expensive\n");
00106 PetscPrintf(comm,"\n Categories and modules:\n");
00107 {
00108 char **categories,*option; int icat,ncats;
00109 ierr = PetscMalloc(30*sizeof(char),&option); CHKERRQ(ierr);
00110 ierr = GetCategories(&categories,&ncats); CHKERRQ(ierr);
00111 for (icat=0; icat<ncats; icat++) {
00112 ierr = PetscMemzero(option,30*sizeof(char)); CHKERRQ(ierr);
00113 PetscPrintf
00114 (comm," -anamod_%s : options for category %s\n",
00115 categories[icat],categories[icat]);
00116 }
00117 ierr = PetscFree(option); CHKERRQ(ierr);
00118 }
00119 PetscFunctionReturn(0);
00120 }
00121
00122 #undef __FUNCT__
00123 #define __FUNCT__ "AnaModHasForcedSequentialComputation"
00124
00125
00126
00127 PetscErrorCode AnaModHasForcedSequentialComputation(PetscTruth *flg)
00128 {
00129 PetscFunctionBegin;
00130 *flg = single_proc;
00131 PetscFunctionReturn(0);
00132 }
00133
00134 #undef __FUNCT__
00135 #define __FUNCT__ "AnaModGetSequentialMatrix"
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 PetscErrorCode AnaModGetSequentialMatrix
00154 (Mat A,Mat *Awork,PetscTruth *mem,PetscTruth *local,PetscTruth *global)
00155 {
00156 MPI_Comm comm; const MatType type; int mytid;
00157 PetscTruth flg; PetscErrorCode ierr;
00158 PetscFunctionBegin;
00159
00160 ierr = PetscObjectGetComm((PetscObject)A,&comm); CHKERRQ(ierr);
00161 MPI_Comm_rank(comm,&mytid);
00162 ierr = MatGetType(A,&type); CHKERRQ(ierr);
00163 ierr = PetscStrcmp(type,MATSEQAIJ,&flg); CHKERRQ(ierr);
00164 if (flg) {
00165 *Awork = A; *mem = PETSC_FALSE;
00166 *local = PETSC_TRUE; *global = PETSC_TRUE;
00167 } else {
00168 ierr = AnaModHasForcedSequentialComputation(&flg); CHKERRQ(ierr);
00169 if (!flg) {
00170 *Awork = NULL;
00171 *mem = PETSC_FALSE; *local = PETSC_FALSE; *global = PETSC_FALSE;
00172 PetscFunctionReturn(0);
00173 }
00174 *global = PETSC_TRUE;
00175 {
00176 Mat *Arow; IS is[1]; int N,M;
00177 ierr = MatGetSize(A,&M,&N); CHKERRQ(ierr);
00178 if (mytid==0) {
00179 ierr = ISCreateStride(MPI_COMM_SELF,M,0,1,is); CHKERRQ(ierr);
00180 *local = PETSC_TRUE;
00181 } else {
00182 ierr = ISCreateStride(MPI_COMM_SELF,0,0,1,is); CHKERRQ(ierr);
00183 *local = PETSC_FALSE;
00184 }
00185 ierr = MatGetSubMatrices
00186 (A,1,is,is,MAT_INITIAL_MATRIX,&Arow); CHKERRQ(ierr);
00187 ierr = ISDestroy(is[0]); CHKERRQ(ierr);
00188 *Awork = Arow[0]; *mem = PETSC_TRUE;
00189 }
00190 }
00191 PetscFunctionReturn(0);
00192 }
00193
00194 #undef __FUNCT__
00195 #define __FUNCT__ "AnaModHasForcedExpensiveComputation"
00196
00197
00198
00199
00200 PetscErrorCode AnaModHasForcedExpensiveComputation(PetscTruth *flg)
00201 {
00202 PetscFunctionBegin;
00203 *flg = expensive;
00204 PetscFunctionReturn(0);
00205 }