SALSA Analysis Modules
petsc.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <petscoptions.h>
00003 #include <petsc.h>
00004 #include "nmd.h"
00005 #include "anamod.h"
00006 #include "anamodsalsamodules.h"
00007 #if defined(HAVE_MYSQL)
00008 #include "mysql.h"      
00009 MYSQL *dbase;
00010 #endif
00011 
00012 #undef __FUNCT__
00013 #define __FUNCT__ "get_matrix"
00014 static PetscErrorCode get_matrix(Mat *A,PetscBool *success)
00015 {
00016   MPI_Comm comm = MPI_COMM_WORLD; char option[1000];
00017   PetscBool has; PetscViewer binary_dump; PetscErrorCode ierr;
00018 
00019   PetscFunctionBegin;
00020   *success = PETSC_FALSE;
00021 
00022   ierr = PetscOptionsGetString
00023     (PETSC_NULL,"-petsc",option,1000,&has); CHKERRQ(ierr);
00024   if (has) {
00025     PetscPrintf(comm,"reading matrix from petsc dump <%s>\n",option);
00026     ierr = PetscViewerBinaryOpen
00027       (comm,option,FILE_MODE_READ,&binary_dump); CHKERRQ(ierr);
00028     ierr = MatCreate(MPI_COMM_SELF,A); CHKERRQ(ierr);
00029     ierr = MatSetType(*A,MATSEQAIJ); CHKERRQ(ierr);
00030     ierr = MatLoad(*A,binary_dump); CHKERRQ(ierr);
00031     ierr = PetscViewerDestroy(&binary_dump); CHKERRQ(ierr);
00032     *success = PETSC_TRUE; goto exit;
00033   }
00034 
00035   ierr = PetscOptionsGetString
00036     (PETSC_NULL,"-matrixmarket",option,1000,&has); CHKERRQ(ierr);
00037   if (has) {
00038     PetscPrintf(comm,"matrix market read not yet supported\n");
00039     *success = PETSC_FALSE; goto exit;
00040   }
00041   
00042  exit:
00043   CHKMEMQ;
00044   PetscFunctionReturn(0);
00045 }
00046 
00047 #undef __FUNCT__
00048 #define __FUNCT__ "analyze_matrix"
00049 PetscErrorCode analyze_matrix(Mat A,NMD_metadata nmd)
00050 {
00051   const char *cat,**modules;
00052   AnalysisDataType *types; int imod,nmod;
00053   PetscBool success; PetscErrorCode ierr;
00054 
00055   PetscFunctionBegin;
00056   ierr = GetFirstCategory(&cat,&success); CHKERRQ(ierr);
00057   while (success) {
00058     ierr = CategoryGetModules
00059       (cat,&modules,&types,PETSC_NULL,&nmod); CHKERRQ(ierr);
00060     for (imod=0; imod<nmod; imod++) {
00061       AnalysisItem res; int reslen; PetscBool success;
00062       ierr = ComputeQuantity
00063         ((AnaModNumericalProblem)A,cat,modules[imod],
00064          &res,&reslen,&success); CHKERRQ(ierr);
00065       if (success) {
00066         AnalysisDataType type;
00067         ierr = GetDataType(cat,modules[imod],&type,PETSC_NULL); CHKERRQ(ierr);
00068         ierr = NMDSetArrayValue
00069           (nmd,cat,modules[imod],type,(void*)&res,reslen); CHKERRQ(ierr);
00070       }
00071     }
00072     ierr = GetNextCategory(&cat,&success); CHKERRQ(ierr);
00073   }
00074   PetscFunctionReturn(0);
00075 }
00076 
00077 static PetscErrorCode usage(MPI_Comm comm)
00078 {
00079   PetscErrorCode ierr;
00080   PetscFunctionBegin;
00081   ierr = PetscPrintf
00082     (comm,"-------------------------------------------------\n"); CHKERRQ(ierr);
00083   ierr = PetscPrintf
00084     (comm,"Use -petsc or -matrixmarket option to specify a matrix\n");
00085   ierr = PetscPrintf
00086     (comm,"Use -mysql to get a table definition and insert command for all computed quantities\n");
00087   PetscFunctionReturn(0);
00088 }
00089 
00090 #undef __FUNCT__
00091 #define __FUNCT__ "main"
00092 int main(int argc,char **argv)
00093 {
00094   MPI_Comm comm; Mat A; NMD_metadata nmd; PetscBool table,success,help;
00095   PetscErrorCode ierr;
00096 
00097   PetscFunctionBegin;
00098   PetscInitialize(&argc,&argv,0,0);
00099   comm = MPI_COMM_WORLD;
00100 
00101   help = PETSC_FALSE;
00102   ierr = PetscOptionsHasName(PETSC_NULL,"-h",&help); CHKERRQ(ierr);
00103   if (!help) {
00104     ierr = PetscOptionsHasName(PETSC_NULL,"-help",&help); CHKERRQ(ierr);
00105   }
00106   if (help) {
00107     ierr = usage(comm); CHKERRQ(ierr);
00108     goto petscfinalize;
00109   }
00110 
00111   ierr = AnaModInitialize(); CHKERRQ(ierr);
00112   ierr = AnaModRegisterSalsaModules(); CHKERRQ(ierr);
00113   ierr = AnaModOptionsHandling(); CHKERRQ(ierr);
00114 
00115   ierr = PetscOptionsHasName(PETSC_NULL,"-table",&table); CHKERRQ(ierr);
00116   if (table) {
00117     printf("CREATE TABLE anamod_data (\n");
00118     {
00119       int ncat, icat,iwrite=0; const char **cats;
00120       ierr = GetCategories(&ncat,&cats); CHKERRQ(ierr);
00121       for (icat=0; icat<ncat; icat++) {
00122         int ncmp,icmp; const char **cmps; AnalysisDataType *types;
00123         ierr = CategoryGetModules
00124           (cats[icat],&cmps,&types,NULL,&ncmp); CHKERRQ(ierr);
00125         for (icmp=0; icmp<ncmp; icmp++) {
00126           const char *type;
00127           if (iwrite>0) printf(",\n"); iwrite++;
00128           ierr = AnaModGetTypeMySQLName(types[icmp],&type); CHKERRQ(ierr);
00129           printf("  `%s:%s` %s",cats[icat],cmps[icmp],type);
00130           
00131         }
00132       }
00133     }
00134 #if defined(HAVE_MYSQL)
00135 /*   dbase = mysql_init(NULL); */
00136 /*   mysql_real_connect(dbase,MYSQLHOST,MYSQLUSER,MYSQLPASS,MYSQLDBASE, */
00137 /*                   0,NULL,0); */
00138 #endif
00139     printf("\n)\n");
00140   }
00141   ierr = get_matrix(&A,&success); CHKERRQ(ierr);
00142   if (!success) goto exit;
00143 
00144   ierr = NMDCreateObject(&nmd); CHKERRQ(ierr);
00145   ierr = analyze_matrix(A,nmd); CHKERRQ(ierr);
00146   ierr = NMDViewObject(nmd); CHKERRQ(ierr);
00147   ierr = NMDObjectDumpToMySQL(nmd); CHKERRQ(ierr);
00148 /*   { */
00149 /*     char *key,*val; */
00150 /*     ierr = NMDReportObject */
00151 /*       (nmd,NMDFalse,&key,&val,'\t',0,0); CHKERRQ(ierr); */
00152 /*     printf("Keys: %s\nValues: %s\n",key,val); */
00153 /*     //NMD_FREE(key); NMD_FREE(val); */
00154 /*   } */
00155   ierr = NMDDestroyObject(nmd); CHKERRQ(ierr);
00156 
00157  exit:
00158 #if defined(HAVE_MYSQL)
00159   mysql_close(dbase);
00160 #endif
00161   ierr = AnaModDeregisterSalsaModules(); CHKERRQ(ierr);
00162   ierr = AnaModFinalize(); CHKERRQ(ierr);
00163  petscfinalize:
00164   PetscFinalize();
00165   PetscFunctionReturn(0);
00166 }