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