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