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;
1339 slepc 8
PetscEvent SVD_SetUp = 0, SVD_Solve = 0, SVD_Dense = 0;
1249 slepc 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);
1339 slepc 42
  ierr = PetscLogEventRegister(&SVD_Dense,"SVDDense",SVD_COOKIE);CHKERRQ(ierr);
1249 slepc 43
  /* Process info exclusions */
44
  ierr = PetscOptionsGetString(PETSC_NULL, "-log_info_exclude", logList, 256, &opt);CHKERRQ(ierr);
45
  if (opt) {
46
    ierr = PetscStrstr(logList, "svd", &className);CHKERRQ(ierr);
47
    if (className) {
48
      ierr = PetscInfoDeactivateClass(SVD_COOKIE);CHKERRQ(ierr);
49
    }
50
  }
51
  /* Process summary exclusions */
52
  ierr = PetscOptionsGetString(PETSC_NULL, "-log_summary_exclude", logList, 256, &opt);CHKERRQ(ierr);
53
  if (opt) {
54
    ierr = PetscStrstr(logList, "svd", &className);CHKERRQ(ierr);
55
    if (className) {
56
      ierr = PetscLogEventDeactivateClass(SVD_COOKIE);CHKERRQ(ierr);
57
    }
58
  }
59
  PetscFunctionReturn(0);
60
}
61
 
62
#undef __FUNCT__  
63
#define __FUNCT__ "SVDView"
64
/*@C
65
   SVDView - Prints the SVD data structure.
66
 
67
   Collective on SVD
68
 
69
   Input Parameters:
1260 slepc 70
+  svd - the singular value solver context
1249 slepc 71
-  viewer - optional visualization context
72
 
73
   Options Database Key:
74
.  -svd_view -  Calls SVDView() at end of SVDSolve()
75
 
76
   Note:
77
   The available visualization contexts include
78
+     PETSC_VIEWER_STDOUT_SELF - standard output (default)
79
-     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
80
         output where only the first processor opens
81
         the file.  All other processors send their
82
         data to the first processor to print.
83
 
84
   The user can open an alternative visualization context with
85
   PetscViewerASCIIOpen() - output to a specified file.
86
 
87
   Level: beginner
88
 
89
.seealso: STView(), PetscViewerASCIIOpen()
90
@*/
91
PetscErrorCode SVDView(SVD svd,PetscViewer viewer)
92
{
93
  PetscErrorCode ierr;
94
  const char     *type;
95
  PetscTruth     isascii;
96
 
97
  PetscFunctionBegin;
98
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
99
  if (!viewer) viewer = PETSC_VIEWER_STDOUT_(svd->comm);
100
  PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
101
  PetscCheckSameComm(svd,1,viewer,2);
102
 
103
  ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
104
  if (isascii) {
105
    ierr = PetscViewerASCIIPrintf(viewer,"SVD Object:\n");CHKERRQ(ierr);
106
    ierr = SVDGetType(svd,&type);CHKERRQ(ierr);
107
    if (type) {
1257 slepc 108
      ierr = PetscViewerASCIIPrintf(viewer,"  method: %s\n",type);CHKERRQ(ierr);
1249 slepc 109
    } else {
110
      ierr = PetscViewerASCIIPrintf(viewer,"  method: not yet set\n");CHKERRQ(ierr);
111
    }
1268 slepc 112
    switch (svd->transmode) {
113
      case SVD_TRANSPOSE_EXPLICIT:
114
        ierr = PetscViewerASCIIPrintf(viewer,"  transpose mode: explicit\n");CHKERRQ(ierr);
115
        break;
1283 slepc 116
      case SVD_TRANSPOSE_IMPLICIT:
117
        ierr = PetscViewerASCIIPrintf(viewer,"  transpose mode: implicit\n");CHKERRQ(ierr);
1268 slepc 118
        break;
119
      default:
120
        ierr = PetscViewerASCIIPrintf(viewer,"  transpose mode: not yet set\n");CHKERRQ(ierr);
121
    }
1283 slepc 122
    if (svd->which == SVD_LARGEST) {
123
      ierr = PetscViewerASCIIPrintf(viewer,"  selected portion of the spectrum: largest\n");CHKERRQ(ierr);
124
    } else {
125
      ierr = PetscViewerASCIIPrintf(viewer,"  selected portion of the spectrum: smallest\n");CHKERRQ(ierr);
126
    }  
127
    ierr = PetscViewerASCIIPrintf(viewer,"  number of singular values (nsv): %d\n",svd->nsv);CHKERRQ(ierr);
128
    ierr = PetscViewerASCIIPrintf(viewer,"  number of column vectors (ncv): %d\n",svd->ncv);CHKERRQ(ierr);
129
    ierr = PetscViewerASCIIPrintf(viewer,"  maximum number of iterations: %d\n",svd->max_it);
130
    ierr = PetscViewerASCIIPrintf(viewer,"  tolerance: %g\n",svd->tol);CHKERRQ(ierr);
1249 slepc 131
    if (svd->ops->view) {
132
      ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
133
      ierr = (*svd->ops->view)(svd,viewer);CHKERRQ(ierr);
134
      ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
135
    }
1327 slepc 136
    ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
137
    ierr = IPView(svd->ip,viewer);CHKERRQ(ierr);
138
    ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1249 slepc 139
  } else {
140
    if (svd->ops->view) {
141
      ierr = (*svd->ops->view)(svd,viewer);CHKERRQ(ierr);
142
    }
143
  }
144
  PetscFunctionReturn(0);
145
}
146
 
147
#undef __FUNCT__  
148
#define __FUNCT__ "SVDPublish_Petsc"
149
static PetscErrorCode SVDPublish_Petsc(PetscObject object)
150
{
151
  PetscFunctionBegin;
152
  PetscFunctionReturn(0);
153
}
154
 
155
#undef __FUNCT__  
156
#define __FUNCT__ "SVDCreate"
157
/*@C
158
   SVDCreate - Creates the default SVD context.
159
 
160
   Collective on MPI_Comm
161
 
162
   Input Parameter:
163
.  comm - MPI communicator
164
 
165
   Output Parameter:
166
.  svd - location to put the SVD context
167
 
168
   Note:
169
   The default SVD type is SVDEIGENSOLVER
170
 
171
   Level: beginner
172
 
173
.seealso: SVDSetUp(), SVDSolve(), SVDDestroy(), SVD
174
@*/
175
PetscErrorCode SVDCreate(MPI_Comm comm,SVD *outsvd)
176
{
177
  PetscErrorCode ierr;
178
  SVD            svd;
179
 
180
  PetscFunctionBegin;
181
  PetscValidPointer(outsvd,2);
182
 
183
  PetscHeaderCreate(svd,_p_SVD,struct _SVDOps,SVD_COOKIE,-1,"SVD",comm,SVDDestroy,SVDView);
184
  *outsvd = svd;
185
 
186
  svd->bops->publish   = SVDPublish_Petsc;
187
  ierr = PetscMemzero(svd->ops,sizeof(struct _SVDOps));CHKERRQ(ierr);
188
 
189
  svd->type_name   = PETSC_NULL;
1314 slepc 190
  svd->OP          = PETSC_NULL;
1249 slepc 191
  svd->A           = PETSC_NULL;
1255 slepc 192
  svd->AT          = PETSC_NULL;
1327 slepc 193
  svd->transmode   = (SVDTransposeMode)PETSC_DECIDE;
1249 slepc 194
  svd->sigma       = PETSC_NULL;
1251 slepc 195
  svd->U           = PETSC_NULL;
196
  svd->V           = PETSC_NULL;
1272 slepc 197
  svd->vec_initial = PETSC_NULL;
198
  svd->which       = SVD_LARGEST;
1263 slepc 199
  svd->n           = 0;
1283 slepc 200
  svd->nconv       = 0;
1272 slepc 201
  svd->nsv         = 1;    
202
  svd->ncv         = PETSC_DECIDE;    
1283 slepc 203
  svd->its         = 0;
204
  svd->max_it      = PETSC_DECIDE;  
1272 slepc 205
  svd->tol         = 1e-7;    
1288 slepc 206
  svd->errest      = PETSC_NULL;
1249 slepc 207
  svd->data        = PETSC_NULL;
208
  svd->setupcalled = 0;
1283 slepc 209
  svd->reason      = SVD_CONVERGED_ITERATING;
1288 slepc 210
  svd->numbermonitors = 0;
1305 slepc 211
  svd->matvecs = 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
}
1345 slepc 418
 
419
#undef __FUNCT__  
420
#define __FUNCT__ "SVDSetIP"
421
/*@
1349 slepc 422
   SVDSetIP - Associates an inner product object to the
1345 slepc 423
   singular value solver.
424
 
425
   Collective on SVD
426
 
427
   Input Parameters:
428
+  svd - singular value solver context obtained from SVDCreate()
429
-  ip  - the inner product object
430
 
431
   Note:
432
   Use SVDGetIP() to retrieve the inner product context (for example,
433
   to free it at the end of the computations).
434
 
435
   Level: advanced
436
 
437
.seealso: SVDGetIP()
438
@*/
439
PetscErrorCode SVDSetIP(SVD svd,IP ip)
440
{
441
  PetscErrorCode ierr;
442
 
443
  PetscFunctionBegin;
444
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
445
  PetscValidHeaderSpecific(ip,IP_COOKIE,2);
446
  PetscCheckSameComm(svd,1,ip,2);
447
  ierr = PetscObjectReference((PetscObject)ip);CHKERRQ(ierr);
448
  ierr = IPDestroy(svd->ip); CHKERRQ(ierr);
449
  svd->ip = ip;
450
  PetscFunctionReturn(0);
451
}
452
 
453
#undef __FUNCT__  
454
#define __FUNCT__ "SVDGetIP"
455
/*@C
456
   SVDGetIP - Obtain the inner product object associated
457
   to the singular value solver object.
458
 
459
   Not Collective
460
 
461
   Input Parameters:
462
.  svd - singular value solver context obtained from SVDCreate()
463
 
464
   Output Parameter:
465
.  ip - inner product context
466
 
1349 slepc 467
   Level: advanced
1345 slepc 468
 
469
.seealso: SVDSetIP()
470
@*/
471
PetscErrorCode SVDGetIP(SVD svd,IP *ip)
472
{
473
  PetscFunctionBegin;
474
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
475
  PetscValidPointer(ip,2);
476
  *ip = svd->ip;
477
  PetscFunctionReturn(0);
478
}