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