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.
1376 slepc 3
 
4
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5
      SLEPc - Scalable Library for Eigenvalue Problem Computations
6
      Copyright (c) 2002-2007, Universidad Politecnica de Valencia, Spain
7
 
8
      This file is part of SLEPc. See the README file for conditions of use
9
      and additional information.
10
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1249 slepc 11
*/
1376 slepc 12
 
1249 slepc 13
#include "src/svd/svdimpl.h"      /*I "slepcsvd.h" I*/
14
 
15
PetscFList SVDList = 0;
16
PetscCookie SVD_COOKIE = 0;
1493 slepc 17
PetscLogEvent SVD_SetUp = 0, SVD_Solve = 0, SVD_Dense = 0;
1249 slepc 18
 
19
#undef __FUNCT__  
20
#define __FUNCT__ "SVDInitializePackage"
21
/*@C
22
  SVDInitializePackage - This function initializes everything in the SVD package. It is called
23
  from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to SVDCreate()
24
  when using static libraries.
25
 
26
  Input Parameter:
27
  path - The dynamic library path, or PETSC_NULL
28
 
29
  Level: developer
30
 
31
.seealso: SlepcInitialize()
32
@*/
33
PetscErrorCode SVDInitializePackage(char *path)
34
{
35
  static PetscTruth initialized = PETSC_FALSE;
36
  char              logList[256];
37
  char              *className;
38
  PetscTruth        opt;
39
  PetscErrorCode    ierr;
40
 
41
  PetscFunctionBegin;
42
  if (initialized) PetscFunctionReturn(0);
43
  initialized = PETSC_TRUE;
44
  /* Register Classes */
1493 slepc 45
  ierr = PetscCookieRegister("Singular Value Solver",&SVD_COOKIE);CHKERRQ(ierr);
1249 slepc 46
  /* Register Constructors */
47
  ierr = SVDRegisterAll(path);CHKERRQ(ierr);
48
  /* Register Events */
1493 slepc 49
  ierr = PetscLogEventRegister("SVDSetUp",SVD_COOKIE,&SVD_SetUp);CHKERRQ(ierr);
50
  ierr = PetscLogEventRegister("SVDSolve",SVD_COOKIE,&SVD_Solve);CHKERRQ(ierr);
51
  ierr = PetscLogEventRegister("SVDDense",SVD_COOKIE,&SVD_Dense);CHKERRQ(ierr);
1249 slepc 52
  /* Process info exclusions */
53
  ierr = PetscOptionsGetString(PETSC_NULL, "-log_info_exclude", logList, 256, &opt);CHKERRQ(ierr);
54
  if (opt) {
55
    ierr = PetscStrstr(logList, "svd", &className);CHKERRQ(ierr);
56
    if (className) {
57
      ierr = PetscInfoDeactivateClass(SVD_COOKIE);CHKERRQ(ierr);
58
    }
59
  }
60
  /* Process summary exclusions */
61
  ierr = PetscOptionsGetString(PETSC_NULL, "-log_summary_exclude", logList, 256, &opt);CHKERRQ(ierr);
62
  if (opt) {
63
    ierr = PetscStrstr(logList, "svd", &className);CHKERRQ(ierr);
64
    if (className) {
65
      ierr = PetscLogEventDeactivateClass(SVD_COOKIE);CHKERRQ(ierr);
66
    }
67
  }
68
  PetscFunctionReturn(0);
69
}
70
 
71
#undef __FUNCT__  
72
#define __FUNCT__ "SVDView"
73
/*@C
74
   SVDView - Prints the SVD data structure.
75
 
76
   Collective on SVD
77
 
78
   Input Parameters:
1260 slepc 79
+  svd - the singular value solver context
1249 slepc 80
-  viewer - optional visualization context
81
 
82
   Options Database Key:
83
.  -svd_view -  Calls SVDView() at end of SVDSolve()
84
 
85
   Note:
86
   The available visualization contexts include
87
+     PETSC_VIEWER_STDOUT_SELF - standard output (default)
88
-     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
89
         output where only the first processor opens
90
         the file.  All other processors send their
91
         data to the first processor to print.
92
 
93
   The user can open an alternative visualization context with
94
   PetscViewerASCIIOpen() - output to a specified file.
95
 
96
   Level: beginner
97
 
98
.seealso: STView(), PetscViewerASCIIOpen()
99
@*/
100
PetscErrorCode SVDView(SVD svd,PetscViewer viewer)
101
{
102
  PetscErrorCode ierr;
103
  const char     *type;
104
  PetscTruth     isascii;
105
 
106
  PetscFunctionBegin;
107
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
1422 slepc 108
  if (!viewer) viewer = PETSC_VIEWER_STDOUT_(((PetscObject)svd)->comm);
1249 slepc 109
  PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
110
  PetscCheckSameComm(svd,1,viewer,2);
111
 
112
  ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
113
  if (isascii) {
114
    ierr = PetscViewerASCIIPrintf(viewer,"SVD Object:\n");CHKERRQ(ierr);
115
    ierr = SVDGetType(svd,&type);CHKERRQ(ierr);
116
    if (type) {
1257 slepc 117
      ierr = PetscViewerASCIIPrintf(viewer,"  method: %s\n",type);CHKERRQ(ierr);
1249 slepc 118
    } else {
119
      ierr = PetscViewerASCIIPrintf(viewer,"  method: not yet set\n");CHKERRQ(ierr);
120
    }
1268 slepc 121
    switch (svd->transmode) {
122
      case SVD_TRANSPOSE_EXPLICIT:
123
        ierr = PetscViewerASCIIPrintf(viewer,"  transpose mode: explicit\n");CHKERRQ(ierr);
124
        break;
1283 slepc 125
      case SVD_TRANSPOSE_IMPLICIT:
126
        ierr = PetscViewerASCIIPrintf(viewer,"  transpose mode: implicit\n");CHKERRQ(ierr);
1268 slepc 127
        break;
128
      default:
129
        ierr = PetscViewerASCIIPrintf(viewer,"  transpose mode: not yet set\n");CHKERRQ(ierr);
130
    }
1283 slepc 131
    if (svd->which == SVD_LARGEST) {
132
      ierr = PetscViewerASCIIPrintf(viewer,"  selected portion of the spectrum: largest\n");CHKERRQ(ierr);
133
    } else {
134
      ierr = PetscViewerASCIIPrintf(viewer,"  selected portion of the spectrum: smallest\n");CHKERRQ(ierr);
135
    }  
136
    ierr = PetscViewerASCIIPrintf(viewer,"  number of singular values (nsv): %d\n",svd->nsv);CHKERRQ(ierr);
137
    ierr = PetscViewerASCIIPrintf(viewer,"  number of column vectors (ncv): %d\n",svd->ncv);CHKERRQ(ierr);
138
    ierr = PetscViewerASCIIPrintf(viewer,"  maximum number of iterations: %d\n",svd->max_it);
139
    ierr = PetscViewerASCIIPrintf(viewer,"  tolerance: %g\n",svd->tol);CHKERRQ(ierr);
1249 slepc 140
    if (svd->ops->view) {
141
      ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
142
      ierr = (*svd->ops->view)(svd,viewer);CHKERRQ(ierr);
143
      ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
144
    }
1327 slepc 145
    ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
146
    ierr = IPView(svd->ip,viewer);CHKERRQ(ierr);
147
    ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1249 slepc 148
  } else {
149
    if (svd->ops->view) {
150
      ierr = (*svd->ops->view)(svd,viewer);CHKERRQ(ierr);
151
    }
152
  }
153
  PetscFunctionReturn(0);
154
}
155
 
156
#undef __FUNCT__  
157
#define __FUNCT__ "SVDCreate"
158
/*@C
159
   SVDCreate - Creates the default SVD context.
160
 
161
   Collective on MPI_Comm
162
 
163
   Input Parameter:
164
.  comm - MPI communicator
165
 
166
   Output Parameter:
167
.  svd - location to put the SVD context
168
 
169
   Note:
1405 slepc 170
   The default SVD type is SVDCROSS
1249 slepc 171
 
172
   Level: beginner
173
 
174
.seealso: SVDSetUp(), SVDSolve(), SVDDestroy(), SVD
175
@*/
176
PetscErrorCode SVDCreate(MPI_Comm comm,SVD *outsvd)
177
{
178
  PetscErrorCode ierr;
179
  SVD            svd;
180
 
181
  PetscFunctionBegin;
182
  PetscValidPointer(outsvd,2);
183
 
1454 slepc 184
  ierr = PetscHeaderCreate(svd,_p_SVD,struct _SVDOps,SVD_COOKIE,-1,"SVD",comm,SVDDestroy,SVDView);CHKERRQ(ierr);
1249 slepc 185
  *outsvd = svd;
186
 
187
  ierr = PetscMemzero(svd->ops,sizeof(struct _SVDOps));CHKERRQ(ierr);
188
 
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;
1249 slepc 211
 
1302 slepc 212
  ierr = IPCreate(comm,&svd->ip);CHKERRQ(ierr);
1422 slepc 213
  ierr = IPSetOptionsPrefix(svd->ip,((PetscObject)svd)->prefix);
1316 slepc 214
  ierr = IPAppendOptionsPrefix(svd->ip,"svd_");
1302 slepc 215
  PetscLogObjectParent(svd,svd->ip);
216
 
1249 slepc 217
  ierr = PetscPublishAll(svd);CHKERRQ(ierr);
218
  PetscFunctionReturn(0);
219
}
220
 
221
#undef __FUNCT__  
222
#define __FUNCT__ "SVDDestroy"
223
/*@
224
   SVDDestroy - Destroys the SVD context.
225
 
226
   Collective on SVD
227
 
228
   Input Parameter:
1320 slepc 229
.  svd - singular value solver context obtained from SVDCreate()
1249 slepc 230
 
231
   Level: beginner
232
 
233
.seealso: SVDCreate(), SVDSetUp(), SVDSolve()
234
@*/
235
PetscErrorCode SVDDestroy(SVD svd)
236
{
237
  PetscErrorCode ierr;
1251 slepc 238
  int            i;
239
 
1249 slepc 240
  PetscFunctionBegin;
241
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
1422 slepc 242
  if (--((PetscObject)svd)->refct > 0) PetscFunctionReturn(0);
1249 slepc 243
 
244
  /* if memory was published with AMS then destroy it */
245
  ierr = PetscObjectDepublish(svd);CHKERRQ(ierr);
246
 
247
  if (svd->ops->destroy) {
248
    ierr = (*svd->ops->destroy)(svd); CHKERRQ(ierr);
249
  }
1251 slepc 250
 
1314 slepc 251
  if (svd->OP) { ierr = MatDestroy(svd->OP);CHKERRQ(ierr); }
1283 slepc 252
  if (svd->A) { ierr = MatDestroy(svd->A);CHKERRQ(ierr); }
253
  if (svd->AT) { ierr = MatDestroy(svd->AT);CHKERRQ(ierr); }
1263 slepc 254
  if (svd->n) {
255
    ierr = PetscFree(svd->sigma);CHKERRQ(ierr);
1288 slepc 256
    ierr = PetscFree(svd->errest);CHKERRQ(ierr);
1314 slepc 257
    if (svd->U) {
258
      for (i=0;i<svd->n;i++) {
259
        ierr = VecDestroy(svd->U[i]); CHKERRQ(ierr);
260
      }
261
      ierr = PetscFree(svd->U);CHKERRQ(ierr);
1251 slepc 262
    }
1263 slepc 263
    for (i=0;i<svd->n;i++) {
1251 slepc 264
      ierr = VecDestroy(svd->V[i]);CHKERRQ(ierr);
265
    }
266
    ierr = PetscFree(svd->V);CHKERRQ(ierr);
1249 slepc 267
  }
1283 slepc 268
  if (svd->vec_initial) { ierr = VecDestroy(svd->vec_initial);CHKERRQ(ierr); }
1331 slepc 269
  ierr = SVDMonitorCancel(svd);CHKERRQ(ierr);
1249 slepc 270
 
1302 slepc 271
  ierr = IPDestroy(svd->ip);CHKERRQ(ierr);
272
 
1249 slepc 273
  PetscHeaderDestroy(svd);
274
  PetscFunctionReturn(0);
275
}
276
 
277
#undef __FUNCT__  
1391 slepc 278
#define __FUNCT__ "SVDDestroy_Default"
279
PetscErrorCode SVDDestroy_Default(SVD svd)
280
{
281
  PetscErrorCode ierr;
282
 
283
  PetscFunctionBegin;
284
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
285
  ierr = PetscFree(svd->data);CHKERRQ(ierr);
286
  PetscFunctionReturn(0);
287
}
288
 
289
#undef __FUNCT__  
1249 slepc 290
#define __FUNCT__ "SVDSetType"
291
/*@C
292
   SVDSetType - Selects the particular solver to be used in the SVD object.
293
 
294
   Collective on SVD
295
 
296
   Input Parameters:
1260 slepc 297
+  svd      - the singular value solver context
1249 slepc 298
-  type     - a known method
299
 
300
   Options Database Key:
301
.  -svd_type <method> - Sets the method; use -help for a list
302
    of available methods
303
 
304
   Notes:  
305
   See "slepc/include/slepcsvd.h" for available methods. The default
1404 slepc 306
   is SVDCROSS.
1249 slepc 307
 
308
   Normally, it is best to use the SVDSetFromOptions() command and
309
   then set the SVD type from the options database rather than by using
310
   this routine.  Using the options database provides the user with
311
   maximum flexibility in evaluating the different available methods.
312
   The SVDSetType() routine is provided for those situations where it
313
   is necessary to set the iterative solver independently of the command
314
   line or options database.
315
 
316
   Level: intermediate
317
 
318
.seealso: SVDType
319
@*/
1502 slepc 320
PetscErrorCode SVDSetType(SVD svd,const SVDType type)
1249 slepc 321
{
322
  PetscErrorCode ierr,(*r)(SVD);
323
  PetscTruth match;
324
 
325
  PetscFunctionBegin;
326
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
327
  PetscValidCharPointer(type,2);
328
 
329
  ierr = PetscTypeCompare((PetscObject)svd,type,&match);CHKERRQ(ierr);
330
  if (match) PetscFunctionReturn(0);
331
 
332
  if (svd->data) {
333
    /* destroy the old private SVD context */
334
    ierr = (*svd->ops->destroy)(svd); CHKERRQ(ierr);
335
    svd->data = 0;
336
  }
337
 
1422 slepc 338
  ierr = PetscFListFind(SVDList,((PetscObject)svd)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
1249 slepc 339
 
340
  if (!r) SETERRQ1(1,"Unknown SVD type given: %s",type);
341
 
342
  svd->setupcalled = 0;
343
  ierr = PetscMemzero(svd->ops,sizeof(struct _SVDOps));CHKERRQ(ierr);
344
  ierr = (*r)(svd); CHKERRQ(ierr);
345
 
346
  ierr = PetscObjectChangeTypeName((PetscObject)svd,type);CHKERRQ(ierr);
347
  PetscFunctionReturn(0);
348
}
349
 
350
#undef __FUNCT__  
351
#define __FUNCT__ "SVDGetType"
352
/*@C
353
   SVDGetType - Gets the SVD type as a string from the SVD object.
354
 
355
   Not Collective
356
 
357
   Input Parameter:
1260 slepc 358
.  svd - the singular value solver context
1249 slepc 359
 
360
   Output Parameter:
361
.  name - name of SVD method
362
 
363
   Level: intermediate
364
 
365
.seealso: SVDSetType()
366
@*/
1501 slepc 367
PetscErrorCode SVDGetType(SVD svd,const SVDType *type)
1249 slepc 368
{
369
  PetscFunctionBegin;
370
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
1501 slepc 371
  PetscValidPointer(type,2);
1422 slepc 372
  *type = ((PetscObject)svd)->type_name;
1249 slepc 373
  PetscFunctionReturn(0);
374
}
375
 
376
/*MC
1260 slepc 377
   SVDRegisterDynamic - Adds a method to the singular value solver package.
1249 slepc 378
 
379
   Synopsis:
380
   SVDRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(SVD))
381
 
382
   Not Collective
383
 
384
   Input Parameters:
385
+  name_solver - name of a new user-defined solver
386
.  path - path (either absolute or relative) the library containing this solver
387
.  name_create - name of routine to create the solver context
388
-  routine_create - routine to create the solver context
389
 
390
   Notes:
391
   SVDRegisterDynamic() may be called multiple times to add several user-defined solvers.
392
 
393
   If dynamic libraries are used, then the fourth input argument (routine_create)
394
   is ignored.
395
 
396
   Sample usage:
397
.vb
398
   SVDRegisterDynamic("my_solver",/home/username/my_lib/lib/libO/solaris/mylib.a,
399
               "MySolverCreate",MySolverCreate);
400
.ve
401
 
402
   Then, your solver can be chosen with the procedural interface via
403
$     SVDSetType(svd,"my_solver")
404
   or at runtime via the option
405
$     -svd_type my_solver
406
 
407
   Level: advanced
408
 
409
   Environmental variables such as ${PETSC_ARCH}, ${SLEPC_DIR},
410
   and others of the form ${any_environmental_variable} occuring in pathname will be
411
   replaced with appropriate values.
412
 
1389 slepc 413
.seealso: SVDRegisterDestroy(), SVDRegisterAll()
1249 slepc 414
 
415
M*/
416
 
417
#undef __FUNCT__  
418
#define __FUNCT__ "SVDRegister"
1389 slepc 419
/*@C
420
  SVDRegister - See SVDRegisterDynamic()
421
 
422
  Level: advanced
423
@*/
1249 slepc 424
PetscErrorCode SVDRegister(const char *sname,const char *path,const char *name,int (*function)(SVD))
425
{
426
  PetscErrorCode ierr;
427
  char           fullname[256];
428
 
429
  PetscFunctionBegin;
430
  ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
431
  ierr = PetscFListAdd(&SVDList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
432
  PetscFunctionReturn(0);
433
}
1345 slepc 434
 
435
#undef __FUNCT__  
1389 slepc 436
#define __FUNCT__ "SVDRegisterDestroy"
437
/*@
438
   SVDRegisterDestroy - Frees the list of SVD methods that were
439
   registered by SVDRegisterDynamic().
440
 
441
   Not Collective
442
 
443
   Level: advanced
444
 
445
.seealso: SVDRegisterDynamic(), SVDRegisterAll()
446
@*/
447
PetscErrorCode SVDRegisterDestroy(void)
448
{
449
  PetscErrorCode ierr;
450
 
451
  PetscFunctionBegin;
452
  ierr = PetscFListDestroy(&SVDList);CHKERRQ(ierr);
453
  ierr = SVDRegisterAll(PETSC_NULL);CHKERRQ(ierr);
454
  PetscFunctionReturn(0);
455
}
456
 
457
#undef __FUNCT__  
1345 slepc 458
#define __FUNCT__ "SVDSetIP"
459
/*@
1349 slepc 460
   SVDSetIP - Associates an inner product object to the
1345 slepc 461
   singular value solver.
462
 
463
   Collective on SVD
464
 
465
   Input Parameters:
466
+  svd - singular value solver context obtained from SVDCreate()
467
-  ip  - the inner product object
468
 
469
   Note:
470
   Use SVDGetIP() to retrieve the inner product context (for example,
471
   to free it at the end of the computations).
472
 
473
   Level: advanced
474
 
475
.seealso: SVDGetIP()
476
@*/
477
PetscErrorCode SVDSetIP(SVD svd,IP ip)
478
{
479
  PetscErrorCode ierr;
480
 
481
  PetscFunctionBegin;
482
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
483
  PetscValidHeaderSpecific(ip,IP_COOKIE,2);
484
  PetscCheckSameComm(svd,1,ip,2);
485
  ierr = PetscObjectReference((PetscObject)ip);CHKERRQ(ierr);
486
  ierr = IPDestroy(svd->ip); CHKERRQ(ierr);
487
  svd->ip = ip;
488
  PetscFunctionReturn(0);
489
}
490
 
491
#undef __FUNCT__  
492
#define __FUNCT__ "SVDGetIP"
493
/*@C
494
   SVDGetIP - Obtain the inner product object associated
495
   to the singular value solver object.
496
 
497
   Not Collective
498
 
499
   Input Parameters:
500
.  svd - singular value solver context obtained from SVDCreate()
501
 
502
   Output Parameter:
503
.  ip - inner product context
504
 
1349 slepc 505
   Level: advanced
1345 slepc 506
 
507
.seealso: SVDSetIP()
508
@*/
509
PetscErrorCode SVDGetIP(SVD svd,IP *ip)
510
{
511
  PetscFunctionBegin;
512
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
513
  PetscValidPointer(ip,2);
514
  *ip = svd->ip;
515
  PetscFunctionReturn(0);
516
}