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);
1302 slepc 130
    ierr = IPView(svd->ip,viewer);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
    }
136
  } else {
137
    if (svd->ops->view) {
138
      ierr = (*svd->ops->view)(svd,viewer);CHKERRQ(ierr);
139
    }
140
  }
141
  PetscFunctionReturn(0);
142
}
143
 
144
#undef __FUNCT__  
145
#define __FUNCT__ "SVDPublish_Petsc"
146
static PetscErrorCode SVDPublish_Petsc(PetscObject object)
147
{
148
  PetscFunctionBegin;
149
  PetscFunctionReturn(0);
150
}
151
 
152
#undef __FUNCT__  
153
#define __FUNCT__ "SVDCreate"
154
/*@C
155
   SVDCreate - Creates the default SVD context.
156
 
157
   Collective on MPI_Comm
158
 
159
   Input Parameter:
160
.  comm - MPI communicator
161
 
162
   Output Parameter:
163
.  svd - location to put the SVD context
164
 
165
   Note:
166
   The default SVD type is SVDEIGENSOLVER
167
 
168
   Level: beginner
169
 
170
.seealso: SVDSetUp(), SVDSolve(), SVDDestroy(), SVD
171
@*/
172
PetscErrorCode SVDCreate(MPI_Comm comm,SVD *outsvd)
173
{
174
  PetscErrorCode ierr;
175
  SVD            svd;
176
 
177
  PetscFunctionBegin;
178
  PetscValidPointer(outsvd,2);
179
 
180
  PetscHeaderCreate(svd,_p_SVD,struct _SVDOps,SVD_COOKIE,-1,"SVD",comm,SVDDestroy,SVDView);
181
  *outsvd = svd;
182
 
183
  svd->bops->publish   = SVDPublish_Petsc;
184
  ierr = PetscMemzero(svd->ops,sizeof(struct _SVDOps));CHKERRQ(ierr);
185
 
186
  svd->type_name   = PETSC_NULL;
1314 slepc 187
  svd->OP          = PETSC_NULL;
1249 slepc 188
  svd->A           = PETSC_NULL;
1255 slepc 189
  svd->AT          = PETSC_NULL;
1283 slepc 190
  svd->transmode   = PETSC_DECIDE;
1249 slepc 191
  svd->sigma       = PETSC_NULL;
1251 slepc 192
  svd->U           = PETSC_NULL;
193
  svd->V           = PETSC_NULL;
1272 slepc 194
  svd->vec_initial = PETSC_NULL;
195
  svd->which       = SVD_LARGEST;
1263 slepc 196
  svd->n           = 0;
1283 slepc 197
  svd->nconv       = 0;
1272 slepc 198
  svd->nsv         = 1;    
199
  svd->ncv         = PETSC_DECIDE;    
1283 slepc 200
  svd->its         = 0;
201
  svd->max_it      = PETSC_DECIDE;  
1272 slepc 202
  svd->tol         = 1e-7;    
1288 slepc 203
  svd->errest      = PETSC_NULL;
1249 slepc 204
  svd->data        = PETSC_NULL;
205
  svd->setupcalled = 0;
1283 slepc 206
  svd->reason      = SVD_CONVERGED_ITERATING;
1288 slepc 207
  svd->numbermonitors = 0;
1305 slepc 208
  svd->matvecs = 0;
209
  svd->dots = 0;
1249 slepc 210
 
1302 slepc 211
  ierr = IPCreate(comm,&svd->ip);CHKERRQ(ierr);
1316 slepc 212
  ierr = IPSetOptionsPrefix(svd->ip,svd->prefix);
213
  ierr = IPAppendOptionsPrefix(svd->ip,"svd_");
1302 slepc 214
  PetscLogObjectParent(svd,svd->ip);
215
 
1249 slepc 216
  ierr = PetscPublishAll(svd);CHKERRQ(ierr);
217
  PetscFunctionReturn(0);
218
}
219
 
220
#undef __FUNCT__  
221
#define __FUNCT__ "SVDDestroy"
222
/*@
223
   SVDDestroy - Destroys the SVD context.
224
 
225
   Collective on SVD
226
 
227
   Input Parameter:
228
.  svd - eigensolver context obtained from SVDCreate()
229
 
230
   Level: beginner
231
 
232
.seealso: SVDCreate(), SVDSetUp(), SVDSolve()
233
@*/
234
PetscErrorCode SVDDestroy(SVD svd)
235
{
236
  PetscErrorCode ierr;
1251 slepc 237
  int            i;
238
 
1249 slepc 239
  PetscFunctionBegin;
240
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
241
  if (--svd->refct > 0) PetscFunctionReturn(0);
242
 
243
  /* if memory was published with AMS then destroy it */
244
  ierr = PetscObjectDepublish(svd);CHKERRQ(ierr);
245
 
246
  if (svd->ops->destroy) {
247
    ierr = (*svd->ops->destroy)(svd); CHKERRQ(ierr);
248
  }
1251 slepc 249
 
1314 slepc 250
  if (svd->OP) { ierr = MatDestroy(svd->OP);CHKERRQ(ierr); }
1283 slepc 251
  if (svd->A) { ierr = MatDestroy(svd->A);CHKERRQ(ierr); }
252
  if (svd->AT) { ierr = MatDestroy(svd->AT);CHKERRQ(ierr); }
1263 slepc 253
  if (svd->n) {
254
    ierr = PetscFree(svd->sigma);CHKERRQ(ierr);
1288 slepc 255
    ierr = PetscFree(svd->errest);CHKERRQ(ierr);
1314 slepc 256
    if (svd->U) {
257
      for (i=0;i<svd->n;i++) {
258
        ierr = VecDestroy(svd->U[i]); CHKERRQ(ierr);
259
      }
260
      ierr = PetscFree(svd->U);CHKERRQ(ierr);
1251 slepc 261
    }
1263 slepc 262
    for (i=0;i<svd->n;i++) {
1251 slepc 263
      ierr = VecDestroy(svd->V[i]);CHKERRQ(ierr);
264
    }
265
    ierr = PetscFree(svd->V);CHKERRQ(ierr);
1249 slepc 266
  }
1283 slepc 267
  if (svd->vec_initial) { ierr = VecDestroy(svd->vec_initial);CHKERRQ(ierr); }
268
  if (svd->data) { ierr = PetscFree(svd->data);CHKERRQ(ierr); }
1288 slepc 269
  ierr = SVDClearMonitor(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__  
278
#define __FUNCT__ "SVDSetType"
279
/*@C
280
   SVDSetType - Selects the particular solver to be used in the SVD object.
281
 
282
   Collective on SVD
283
 
284
   Input Parameters:
1260 slepc 285
+  svd      - the singular value solver context
1249 slepc 286
-  type     - a known method
287
 
288
   Options Database Key:
289
.  -svd_type <method> - Sets the method; use -help for a list
290
    of available methods
291
 
292
   Notes:  
293
   See "slepc/include/slepcsvd.h" for available methods. The default
294
   is SVDEIGENSOLVER.
295
 
296
   Normally, it is best to use the SVDSetFromOptions() command and
297
   then set the SVD type from the options database rather than by using
298
   this routine.  Using the options database provides the user with
299
   maximum flexibility in evaluating the different available methods.
300
   The SVDSetType() routine is provided for those situations where it
301
   is necessary to set the iterative solver independently of the command
302
   line or options database.
303
 
304
   Level: intermediate
305
 
306
.seealso: SVDType
307
@*/
308
PetscErrorCode SVDSetType(SVD svd,SVDType type)
309
{
310
  PetscErrorCode ierr,(*r)(SVD);
311
  PetscTruth match;
312
 
313
  PetscFunctionBegin;
314
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
315
  PetscValidCharPointer(type,2);
316
 
317
  ierr = PetscTypeCompare((PetscObject)svd,type,&match);CHKERRQ(ierr);
318
  if (match) PetscFunctionReturn(0);
319
 
320
  if (svd->data) {
321
    /* destroy the old private SVD context */
322
    ierr = (*svd->ops->destroy)(svd); CHKERRQ(ierr);
323
    svd->data = 0;
324
  }
325
 
326
  ierr = PetscFListFind(svd->comm,SVDList,type,(void (**)(void)) &r);CHKERRQ(ierr);
327
 
328
  if (!r) SETERRQ1(1,"Unknown SVD type given: %s",type);
329
 
330
  svd->setupcalled = 0;
331
  ierr = PetscMemzero(svd->ops,sizeof(struct _SVDOps));CHKERRQ(ierr);
332
  ierr = (*r)(svd); CHKERRQ(ierr);
333
 
334
  ierr = PetscObjectChangeTypeName((PetscObject)svd,type);CHKERRQ(ierr);
335
  PetscFunctionReturn(0);
336
}
337
 
338
#undef __FUNCT__  
339
#define __FUNCT__ "SVDGetType"
340
/*@C
341
   SVDGetType - Gets the SVD type as a string from the SVD object.
342
 
343
   Not Collective
344
 
345
   Input Parameter:
1260 slepc 346
.  svd - the singular value solver context
1249 slepc 347
 
348
   Output Parameter:
349
.  name - name of SVD method
350
 
351
   Level: intermediate
352
 
353
.seealso: SVDSetType()
354
@*/
355
PetscErrorCode SVDGetType(SVD svd,SVDType *type)
356
{
357
  PetscFunctionBegin;
358
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
359
  *type = svd->type_name;
360
  PetscFunctionReturn(0);
361
}
362
 
363
/*MC
1260 slepc 364
   SVDRegisterDynamic - Adds a method to the singular value solver package.
1249 slepc 365
 
366
   Synopsis:
367
   SVDRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(SVD))
368
 
369
   Not Collective
370
 
371
   Input Parameters:
372
+  name_solver - name of a new user-defined solver
373
.  path - path (either absolute or relative) the library containing this solver
374
.  name_create - name of routine to create the solver context
375
-  routine_create - routine to create the solver context
376
 
377
   Notes:
378
   SVDRegisterDynamic() may be called multiple times to add several user-defined solvers.
379
 
380
   If dynamic libraries are used, then the fourth input argument (routine_create)
381
   is ignored.
382
 
383
   Sample usage:
384
.vb
385
   SVDRegisterDynamic("my_solver",/home/username/my_lib/lib/libO/solaris/mylib.a,
386
               "MySolverCreate",MySolverCreate);
387
.ve
388
 
389
   Then, your solver can be chosen with the procedural interface via
390
$     SVDSetType(svd,"my_solver")
391
   or at runtime via the option
392
$     -svd_type my_solver
393
 
394
   Level: advanced
395
 
396
   Environmental variables such as ${PETSC_ARCH}, ${SLEPC_DIR},
397
   and others of the form ${any_environmental_variable} occuring in pathname will be
398
   replaced with appropriate values.
399
 
400
.seealso: SVDRegisterAll()
401
 
402
M*/
403
 
404
#undef __FUNCT__  
405
#define __FUNCT__ "SVDRegister"
406
PetscErrorCode SVDRegister(const char *sname,const char *path,const char *name,int (*function)(SVD))
407
{
408
  PetscErrorCode ierr;
409
  char           fullname[256];
410
 
411
  PetscFunctionBegin;
412
  ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
413
  ierr = PetscFListAdd(&SVDList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
414
  PetscFunctionReturn(0);
415
}