Subversion Repositories slepc-dev

Rev

Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1249 slepc 1
/*
2
     The basic SVD routines, Create, View, etc. are here.
3
*/
4
#include "src/svd/svdimpl.h"      /*I "slepcsvd.h" I*/
5
 
6
PetscFList SVDList = 0;
7
PetscCookie SVD_COOKIE = 0;
8
PetscEvent SVD_SetUp = 0, SVD_Solve = 0;
9
 
10
#undef __FUNCT__  
11
#define __FUNCT__ "SVDInitializePackage"
12
/*@C
13
  SVDInitializePackage - This function initializes everything in the SVD package. It is called
14
  from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to SVDCreate()
15
  when using static libraries.
16
 
17
  Input Parameter:
18
  path - The dynamic library path, or PETSC_NULL
19
 
20
  Level: developer
21
 
22
.seealso: SlepcInitialize()
23
@*/
24
PetscErrorCode SVDInitializePackage(char *path)
25
{
26
  static PetscTruth initialized = PETSC_FALSE;
27
  char              logList[256];
28
  char              *className;
29
  PetscTruth        opt;
30
  PetscErrorCode    ierr;
31
 
32
  PetscFunctionBegin;
33
  if (initialized) PetscFunctionReturn(0);
34
  initialized = PETSC_TRUE;
35
  /* Register Classes */
36
  ierr = PetscLogClassRegister(&SVD_COOKIE,"Singular Value Solver");CHKERRQ(ierr);
37
  /* Register Constructors */
38
  ierr = SVDRegisterAll(path);CHKERRQ(ierr);
39
  /* Register Events */
40
  ierr = PetscLogEventRegister(&SVD_SetUp,"SVDSetUp",SVD_COOKIE);CHKERRQ(ierr);
41
  ierr = PetscLogEventRegister(&SVD_Solve,"SVDSolve",SVD_COOKIE);CHKERRQ(ierr);
42
  /* Process info exclusions */
43
  ierr = PetscOptionsGetString(PETSC_NULL, "-log_info_exclude", logList, 256, &opt);CHKERRQ(ierr);
44
  if (opt) {
45
    ierr = PetscStrstr(logList, "svd", &className);CHKERRQ(ierr);
46
    if (className) {
47
      ierr = PetscInfoDeactivateClass(SVD_COOKIE);CHKERRQ(ierr);
48
    }
49
  }
50
  /* Process summary exclusions */
51
  ierr = PetscOptionsGetString(PETSC_NULL, "-log_summary_exclude", logList, 256, &opt);CHKERRQ(ierr);
52
  if (opt) {
53
    ierr = PetscStrstr(logList, "svd", &className);CHKERRQ(ierr);
54
    if (className) {
55
      ierr = PetscLogEventDeactivateClass(SVD_COOKIE);CHKERRQ(ierr);
56
    }
57
  }
58
  PetscFunctionReturn(0);
59
}
60
 
61
#undef __FUNCT__  
62
#define __FUNCT__ "SVDView"
63
/*@C
64
   SVDView - Prints the SVD data structure.
65
 
66
   Collective on SVD
67
 
68
   Input Parameters:
1260 slepc 69
+  svd - the singular value solver context
1249 slepc 70
-  viewer - optional visualization context
71
 
72
   Options Database Key:
73
.  -svd_view -  Calls SVDView() at end of SVDSolve()
74
 
75
   Note:
76
   The available visualization contexts include
77
+     PETSC_VIEWER_STDOUT_SELF - standard output (default)
78
-     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
79
         output where only the first processor opens
80
         the file.  All other processors send their
81
         data to the first processor to print.
82
 
83
   The user can open an alternative visualization context with
84
   PetscViewerASCIIOpen() - output to a specified file.
85
 
86
   Level: beginner
87
 
88
.seealso: STView(), PetscViewerASCIIOpen()
89
@*/
90
PetscErrorCode SVDView(SVD svd,PetscViewer viewer)
91
{
92
  PetscErrorCode ierr;
93
  const char     *type;
94
  PetscTruth     isascii;
95
 
96
  PetscFunctionBegin;
97
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
98
  if (!viewer) viewer = PETSC_VIEWER_STDOUT_(svd->comm);
99
  PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
100
  PetscCheckSameComm(svd,1,viewer,2);
101
 
102
  ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
103
  if (isascii) {
104
    ierr = PetscViewerASCIIPrintf(viewer,"SVD Object:\n");CHKERRQ(ierr);
105
    ierr = SVDGetType(svd,&type);CHKERRQ(ierr);
106
    if (type) {
1257 slepc 107
      ierr = PetscViewerASCIIPrintf(viewer,"  method: %s\n",type);CHKERRQ(ierr);
1249 slepc 108
    } else {
109
      ierr = PetscViewerASCIIPrintf(viewer,"  method: not yet set\n");CHKERRQ(ierr);
110
    }
1268 slepc 111
    switch (svd->transmode) {
112
      case SVD_TRANSPOSE_EXPLICIT:
113
        ierr = PetscViewerASCIIPrintf(viewer,"  transpose mode: explicit\n");CHKERRQ(ierr);
114
        break;
1283 slepc 115
      case SVD_TRANSPOSE_IMPLICIT:
116
        ierr = PetscViewerASCIIPrintf(viewer,"  transpose mode: implicit\n");CHKERRQ(ierr);
1268 slepc 117
        break;
118
      default:
119
        ierr = PetscViewerASCIIPrintf(viewer,"  transpose mode: not yet set\n");CHKERRQ(ierr);
120
    }
1283 slepc 121
    if (svd->which == SVD_LARGEST) {
122
      ierr = PetscViewerASCIIPrintf(viewer,"  selected portion of the spectrum: largest\n");CHKERRQ(ierr);
123
    } else {
124
      ierr = PetscViewerASCIIPrintf(viewer,"  selected portion of the spectrum: smallest\n");CHKERRQ(ierr);
125
    }  
126
    ierr = PetscViewerASCIIPrintf(viewer,"  number of singular values (nsv): %d\n",svd->nsv);CHKERRQ(ierr);
127
    ierr = PetscViewerASCIIPrintf(viewer,"  number of column vectors (ncv): %d\n",svd->ncv);CHKERRQ(ierr);
128
    ierr = PetscViewerASCIIPrintf(viewer,"  maximum number of iterations: %d\n",svd->max_it);
129
    ierr = PetscViewerASCIIPrintf(viewer,"  tolerance: %g\n",svd->tol);CHKERRQ(ierr);
1249 slepc 130
    if (svd->ops->view) {
131
      ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
132
      ierr = (*svd->ops->view)(svd,viewer);CHKERRQ(ierr);
133
      ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
134
    }
1327 slepc 135
    ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
136
    ierr = IPView(svd->ip,viewer);CHKERRQ(ierr);
137
    ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1249 slepc 138
  } else {
139
    if (svd->ops->view) {
140
      ierr = (*svd->ops->view)(svd,viewer);CHKERRQ(ierr);
141
    }
142
  }
143
  PetscFunctionReturn(0);
144
}
145
 
146
#undef __FUNCT__  
147
#define __FUNCT__ "SVDPublish_Petsc"
148
static PetscErrorCode SVDPublish_Petsc(PetscObject object)
149
{
150
  PetscFunctionBegin;
151
  PetscFunctionReturn(0);
152
}
153
 
154
#undef __FUNCT__  
155
#define __FUNCT__ "SVDCreate"
156
/*@C
157
   SVDCreate - Creates the default SVD context.
158
 
159
   Collective on MPI_Comm
160
 
161
   Input Parameter:
162
.  comm - MPI communicator
163
 
164
   Output Parameter:
165
.  svd - location to put the SVD context
166
 
167
   Note:
168
   The default SVD type is SVDEIGENSOLVER
169
 
170
   Level: beginner
171
 
172
.seealso: SVDSetUp(), SVDSolve(), SVDDestroy(), SVD
173
@*/
174
PetscErrorCode SVDCreate(MPI_Comm comm,SVD *outsvd)
175
{
176
  PetscErrorCode ierr;
177
  SVD            svd;
178
 
179
  PetscFunctionBegin;
180
  PetscValidPointer(outsvd,2);
181
 
182
  PetscHeaderCreate(svd,_p_SVD,struct _SVDOps,SVD_COOKIE,-1,"SVD",comm,SVDDestroy,SVDView);
183
  *outsvd = svd;
184
 
185
  svd->bops->publish   = SVDPublish_Petsc;
186
  ierr = PetscMemzero(svd->ops,sizeof(struct _SVDOps));CHKERRQ(ierr);
187
 
188
  svd->type_name   = PETSC_NULL;
1314 slepc 189
  svd->OP          = PETSC_NULL;
1249 slepc 190
  svd->A           = PETSC_NULL;
1255 slepc 191
  svd->AT          = PETSC_NULL;
1327 slepc 192
  svd->transmode   = (SVDTransposeMode)PETSC_DECIDE;
1249 slepc 193
  svd->sigma       = PETSC_NULL;
1251 slepc 194
  svd->U           = PETSC_NULL;
195
  svd->V           = PETSC_NULL;
1272 slepc 196
  svd->vec_initial = PETSC_NULL;
197
  svd->which       = SVD_LARGEST;
1263 slepc 198
  svd->n           = 0;
1283 slepc 199
  svd->nconv       = 0;
1272 slepc 200
  svd->nsv         = 1;    
201
  svd->ncv         = PETSC_DECIDE;    
1283 slepc 202
  svd->its         = 0;
203
  svd->max_it      = PETSC_DECIDE;  
1272 slepc 204
  svd->tol         = 1e-7;    
1288 slepc 205
  svd->errest      = PETSC_NULL;
1249 slepc 206
  svd->data        = PETSC_NULL;
207
  svd->setupcalled = 0;
1283 slepc 208
  svd->reason      = SVD_CONVERGED_ITERATING;
1288 slepc 209
  svd->numbermonitors = 0;
1305 slepc 210
  svd->matvecs = 0;
211
  svd->dots = 0;
1249 slepc 212
 
1302 slepc 213
  ierr = IPCreate(comm,&svd->ip);CHKERRQ(ierr);
1316 slepc 214
  ierr = IPSetOptionsPrefix(svd->ip,svd->prefix);
215
  ierr = IPAppendOptionsPrefix(svd->ip,"svd_");
1302 slepc 216
  PetscLogObjectParent(svd,svd->ip);
217
 
1249 slepc 218
  ierr = PetscPublishAll(svd);CHKERRQ(ierr);
219
  PetscFunctionReturn(0);
220
}
221
 
222
#undef __FUNCT__  
223
#define __FUNCT__ "SVDDestroy"
224
/*@
225
   SVDDestroy - Destroys the SVD context.
226
 
227
   Collective on SVD
228
 
229
   Input Parameter:
1320 slepc 230
.  svd - singular value solver context obtained from SVDCreate()
1249 slepc 231
 
232
   Level: beginner
233
 
234
.seealso: SVDCreate(), SVDSetUp(), SVDSolve()
235
@*/
236
PetscErrorCode SVDDestroy(SVD svd)
237
{
238
  PetscErrorCode ierr;
1251 slepc 239
  int            i;
240
 
1249 slepc 241
  PetscFunctionBegin;
242
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
243
  if (--svd->refct > 0) PetscFunctionReturn(0);
244
 
245
  /* if memory was published with AMS then destroy it */
246
  ierr = PetscObjectDepublish(svd);CHKERRQ(ierr);
247
 
248
  if (svd->ops->destroy) {
249
    ierr = (*svd->ops->destroy)(svd); CHKERRQ(ierr);
250
  }
1251 slepc 251
 
1314 slepc 252
  if (svd->OP) { ierr = MatDestroy(svd->OP);CHKERRQ(ierr); }
1283 slepc 253
  if (svd->A) { ierr = MatDestroy(svd->A);CHKERRQ(ierr); }
254
  if (svd->AT) { ierr = MatDestroy(svd->AT);CHKERRQ(ierr); }
1263 slepc 255
  if (svd->n) {
256
    ierr = PetscFree(svd->sigma);CHKERRQ(ierr);
1288 slepc 257
    ierr = PetscFree(svd->errest);CHKERRQ(ierr);
1314 slepc 258
    if (svd->U) {
259
      for (i=0;i<svd->n;i++) {
260
        ierr = VecDestroy(svd->U[i]); CHKERRQ(ierr);
261
      }
262
      ierr = PetscFree(svd->U);CHKERRQ(ierr);
1251 slepc 263
    }
1263 slepc 264
    for (i=0;i<svd->n;i++) {
1251 slepc 265
      ierr = VecDestroy(svd->V[i]);CHKERRQ(ierr);
266
    }
267
    ierr = PetscFree(svd->V);CHKERRQ(ierr);
1249 slepc 268
  }
1283 slepc 269
  if (svd->vec_initial) { ierr = VecDestroy(svd->vec_initial);CHKERRQ(ierr); }
270
  if (svd->data) { ierr = PetscFree(svd->data);CHKERRQ(ierr); }
1331 slepc 271
  ierr = SVDMonitorCancel(svd);CHKERRQ(ierr);
1249 slepc 272
 
1302 slepc 273
  ierr = IPDestroy(svd->ip);CHKERRQ(ierr);
274
 
1249 slepc 275
  PetscHeaderDestroy(svd);
276
  PetscFunctionReturn(0);
277
}
278
 
279
#undef __FUNCT__  
280
#define __FUNCT__ "SVDSetType"
281
/*@C
282
   SVDSetType - Selects the particular solver to be used in the SVD object.
283
 
284
   Collective on SVD
285
 
286
   Input Parameters:
1260 slepc 287
+  svd      - the singular value solver context
1249 slepc 288
-  type     - a known method
289
 
290
   Options Database Key:
291
.  -svd_type <method> - Sets the method; use -help for a list
292
    of available methods
293
 
294
   Notes:  
295
   See "slepc/include/slepcsvd.h" for available methods. The default
296
   is SVDEIGENSOLVER.
297
 
298
   Normally, it is best to use the SVDSetFromOptions() command and
299
   then set the SVD type from the options database rather than by using
300
   this routine.  Using the options database provides the user with
301
   maximum flexibility in evaluating the different available methods.
302
   The SVDSetType() routine is provided for those situations where it
303
   is necessary to set the iterative solver independently of the command
304
   line or options database.
305
 
306
   Level: intermediate
307
 
308
.seealso: SVDType
309
@*/
310
PetscErrorCode SVDSetType(SVD svd,SVDType type)
311
{
312
  PetscErrorCode ierr,(*r)(SVD);
313
  PetscTruth match;
314
 
315
  PetscFunctionBegin;
316
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
317
  PetscValidCharPointer(type,2);
318
 
319
  ierr = PetscTypeCompare((PetscObject)svd,type,&match);CHKERRQ(ierr);
320
  if (match) PetscFunctionReturn(0);
321
 
322
  if (svd->data) {
323
    /* destroy the old private SVD context */
324
    ierr = (*svd->ops->destroy)(svd); CHKERRQ(ierr);
325
    svd->data = 0;
326
  }
327
 
328
  ierr = PetscFListFind(svd->comm,SVDList,type,(void (**)(void)) &r);CHKERRQ(ierr);
329
 
330
  if (!r) SETERRQ1(1,"Unknown SVD type given: %s",type);
331
 
332
  svd->setupcalled = 0;
333
  ierr = PetscMemzero(svd->ops,sizeof(struct _SVDOps));CHKERRQ(ierr);
334
  ierr = (*r)(svd); CHKERRQ(ierr);
335
 
336
  ierr = PetscObjectChangeTypeName((PetscObject)svd,type);CHKERRQ(ierr);
337
  PetscFunctionReturn(0);
338
}
339
 
340
#undef __FUNCT__  
341
#define __FUNCT__ "SVDGetType"
342
/*@C
343
   SVDGetType - Gets the SVD type as a string from the SVD object.
344
 
345
   Not Collective
346
 
347
   Input Parameter:
1260 slepc 348
.  svd - the singular value solver context
1249 slepc 349
 
350
   Output Parameter:
351
.  name - name of SVD method
352
 
353
   Level: intermediate
354
 
355
.seealso: SVDSetType()
356
@*/
357
PetscErrorCode SVDGetType(SVD svd,SVDType *type)
358
{
359
  PetscFunctionBegin;
360
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
361
  *type = svd->type_name;
362
  PetscFunctionReturn(0);
363
}
364
 
365
/*MC
1260 slepc 366
   SVDRegisterDynamic - Adds a method to the singular value solver package.
1249 slepc 367
 
368
   Synopsis:
369
   SVDRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(SVD))
370
 
371
   Not Collective
372
 
373
   Input Parameters:
374
+  name_solver - name of a new user-defined solver
375
.  path - path (either absolute or relative) the library containing this solver
376
.  name_create - name of routine to create the solver context
377
-  routine_create - routine to create the solver context
378
 
379
   Notes:
380
   SVDRegisterDynamic() may be called multiple times to add several user-defined solvers.
381
 
382
   If dynamic libraries are used, then the fourth input argument (routine_create)
383
   is ignored.
384
 
385
   Sample usage:
386
.vb
387
   SVDRegisterDynamic("my_solver",/home/username/my_lib/lib/libO/solaris/mylib.a,
388
               "MySolverCreate",MySolverCreate);
389
.ve
390
 
391
   Then, your solver can be chosen with the procedural interface via
392
$     SVDSetType(svd,"my_solver")
393
   or at runtime via the option
394
$     -svd_type my_solver
395
 
396
   Level: advanced
397
 
398
   Environmental variables such as ${PETSC_ARCH}, ${SLEPC_DIR},
399
   and others of the form ${any_environmental_variable} occuring in pathname will be
400
   replaced with appropriate values.
401
 
402
.seealso: SVDRegisterAll()
403
 
404
M*/
405
 
406
#undef __FUNCT__  
407
#define __FUNCT__ "SVDRegister"
408
PetscErrorCode SVDRegister(const char *sname,const char *path,const char *name,int (*function)(SVD))
409
{
410
  PetscErrorCode ierr;
411
  char           fullname[256];
412
 
413
  PetscFunctionBegin;
414
  ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
415
  ierr = PetscFListAdd(&SVDList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
416
  PetscFunctionReturn(0);
417
}