Subversion Repositories slepc-dev

Rev

Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2544 eromero 1
/*
2
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3
   SLEPc - Scalable Library for Eigenvalue Problem Computations
4
   Copyright (c) 2002-2010, Universidad Politecnica de Valencia, Spain
5
 
6
   This file is part of SLEPc.
7
 
8
   SLEPc is free software: you can redistribute it and/or modify it under  the
9
   terms of version 3 of the GNU Lesser General Public License as published by
10
   the Free Software Foundation.
11
 
12
   SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY
13
   WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS
14
   FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for
15
   more details.
16
 
17
   You  should have received a copy of the GNU Lesser General  Public  License
18
   along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
19
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20
*/
21
 
22
static char help[] = "Tests matdense interface.\n\n";
23
 
24
#include <slepcmatdense.h>
25
 
26
#undef __FUNCT__
27
#define __FUNCT__ "main"
28
int main( int argc, char **argv )
29
{
2560 eromero 30
  MatDense       A,B,C;        /* matrices */
2544 eromero 31
  PetscScalar    *v;
32
  PetscInt       n=45,m,i,j,its,M,reps=1;
33
  PetscBool      flag;
2560 eromero 34
  Vec            *vecs0,*vecs1,t,*vecso;
35
  PetscLogStage  saxpy,smult,supdate,supdateo;
2544 eromero 36
  PetscErrorCode ierr;
37
 
38
  SlepcInitialize(&argc,&argv,(char*)0,help);
39
  ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr);
40
  ierr = PetscOptionsGetInt(PETSC_NULL,"-m",&m,&flag);CHKERRQ(ierr);
41
  if(!flag) m=n;
42
  M = PetscMax(m,n);
43
  ierr = PetscOptionsGetInt(PETSC_NULL,"-r",&reps,&flag);CHKERRQ(ierr);
44
  ierr = PetscPrintf(PETSC_COMM_WORLD,"\nDense matrices of size %Dx%D\n\n",m,n);CHKERRQ(ierr);
45
 
46
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2560 eromero 47
     Initialize vectors
48
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
49
  ierr = VecCreate(PETSC_COMM_WORLD,&t);CHKERRQ(ierr);
50
  ierr = VecSetSizes(t,PETSC_DECIDE,m);CHKERRQ(ierr);
51
  ierr = VecSetFromOptions(t);CHKERRQ(ierr);
52
  ierr = VecDuplicateVecs(t,n,&vecso);CHKERRQ(ierr);
53
  ierr = SlepcVecSetTemplate(t);CHKERRQ(ierr);
54
  ierr = VecDuplicateVecs(t,n,&vecs0);CHKERRQ(ierr);
55
  ierr = VecDuplicateVecs(t,n,&vecs1);CHKERRQ(ierr);
56
  ierr = VecGetLocalSize(t,&m);CHKERRQ(ierr);
57
  ierr = VecDestroy(&t);CHKERRQ(ierr);
58
  for (i=0;i<n;i++) { ierr = VecSetRandom(vecs0[i],PETSC_NULL);CHKERRQ(ierr); }
59
  for (i=0;i<n;i++) { ierr = VecSetRandom(vecs1[i],PETSC_NULL);CHKERRQ(ierr); }
60
  for (i=0;i<n;i++) { ierr = VecSetRandom(vecso[i],PETSC_NULL);CHKERRQ(ierr); }
61
 
62
 
63
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2544 eromero 64
     Create matrices
65
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
66
 
67
  ierr = MatDenseCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
68
  ierr = MatDenseSetMaxSizes(A,m,n);CHKERRQ(ierr);
69
  ierr = MatDenseSetSizes(A,0,0,m,n);CHKERRQ(ierr);
70
  ierr = MatDenseSetFromOptions(A);CHKERRQ(ierr);
71
  ierr = MatDenseSetUpPreallocation(A);CHKERRQ(ierr);
72
 
73
  ierr = MatDenseDuplicate(A,MATDENSE_DO_NOT_COPY_VALUES,&B);CHKERRQ(ierr);
74
  ierr = MatDenseSetFromOptions(B);CHKERRQ(ierr);
75
  ierr = MatDenseSetUpPreallocation(B);CHKERRQ(ierr);
76
 
77
  ierr = MatDenseCreate(PETSC_COMM_WORLD,&C);CHKERRQ(ierr);
78
  ierr = MatDenseSetMaxSizes(C,M,M);CHKERRQ(ierr);
79
  ierr = MatDenseSetFromOptions(C);CHKERRQ(ierr);
80
  ierr = MatDenseSetUpPreallocation(C);CHKERRQ(ierr);
81
 
82
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
83
     Initialize matrices
84
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
85
 
86
  ierr = MatDenseGetArray(A,&v);CHKERRQ(ierr);
87
  for (j=0; j<n; j++) {
88
    for (i=0; i<m; i++) {
89
      v[m*j+i] = 1.0;
90
    }
91
  }
92
  ierr = MatDenseRestoreArray(A,&v);CHKERRQ(ierr);
93
 
94
  ierr = MatDenseGetArray(B,&v);CHKERRQ(ierr);
95
  for (j=0; j<n; j++) {
96
    for (i=0; i<m; i++) {
97
      v[m*j+i] = i & 1 ? 1.0 : -1.0;
98
    }
99
  }
100
  ierr = MatDenseRestoreArray(B,&v);CHKERRQ(ierr);
101
 
102
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
103
     Test operations
104
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
105
 
106
  ierr = PetscLogStageRegister("AXPY",&saxpy);CHKERRQ(ierr);
107
  ierr = PetscLogStagePush(saxpy);CHKERRQ(ierr);
108
  for (i=0; i<reps; i++) { ierr = MatDenseAXPY(B,1.0,A);CHKERRQ(ierr); }
109
  for (i=0; i<reps; i++) {
110
    for (j=0; j<n; j++) { ierr = VecAXPY(vecs0[j],1.0,vecs1[j]);CHKERRQ(ierr); }
111
  }
112
  PetscLogStagePop();
113
 
114
  ierr = PetscLogStageRegister("MatMult",&smult);CHKERRQ(ierr);
115
  ierr = PetscLogStagePush(smult);CHKERRQ(ierr);
116
  ierr = MatDenseSetSizes(C,0,0,n,n);CHKERRQ(ierr);
117
  for (i=0; i<reps; i++) { ierr = MatDenseMatMult(C,1.0,0.0,A,PETSC_TRUE,B,PETSC_FALSE);CHKERRQ(ierr); }
118
  ierr = MatDenseGetArray(C,&v);CHKERRQ(ierr);
119
  for (i=0; i<reps; i++) {
120
    for (j=0; j<n; j++) { ierr = VecMDot(vecs0[j],n,vecs1,&v[n*j]);CHKERRQ(ierr); }
121
  }
122
  ierr = MatDenseRestoreArray(C,&v);CHKERRQ(ierr);
123
  PetscLogStagePop();
124
 
125
  ierr = PetscLogStageRegister("Update",&supdate);CHKERRQ(ierr);
126
  ierr = PetscLogStagePush(supdate);CHKERRQ(ierr);
127
  ierr = MatDenseSetSizes(C,0,0,n,n);CHKERRQ(ierr);
128
  ierr = MatDenseGetArray(C,&v);CHKERRQ(ierr);
129
  for (i=0; i<reps; i++) {
130
    ierr = SlepcUpdateVectors(n,vecs0,0,n,v,m,PETSC_FALSE);CHKERRQ(ierr);
2560 eromero 131
    for (j=0; j<n; j++) { ierr = VecAXPY(vecs0[j],1.0,vecs1[j]);CHKERRQ(ierr); }
2544 eromero 132
  }
133
  ierr = MatDenseRestoreArray(C,&v);CHKERRQ(ierr);
134
  PetscLogStagePop();
135
 
2560 eromero 136
  ierr = PetscLogStageRegister("Update Orig.",&supdateo);CHKERRQ(ierr);
137
  ierr = PetscLogStagePush(supdateo);CHKERRQ(ierr);
138
  ierr = MatDenseSetSizes(C,0,0,n,n);CHKERRQ(ierr);
139
  ierr = MatDenseGetArray(C,&v);CHKERRQ(ierr);
140
  for (i=0; i<reps; i++) {
141
    ierr = SlepcUpdateVectors(n,vecso,0,n,v,m,PETSC_FALSE);CHKERRQ(ierr);
142
    for (j=0; j<n; j++) { ierr = VecAXPY(vecso[j],1.0,vecs1[j]);CHKERRQ(ierr); }
143
  }
144
  ierr = MatDenseRestoreArray(C,&v);CHKERRQ(ierr);
145
  PetscLogStagePop();
146
 
2544 eromero 147
  ierr = MatDenseDestroy(&A);CHKERRQ(ierr);
148
  ierr = MatDenseDestroy(&B);CHKERRQ(ierr);
149
  ierr = MatDenseDestroy(&C);CHKERRQ(ierr);
150
  ierr = VecDestroyVecs(n,&vecs0);CHKERRQ(ierr);
151
  ierr = VecDestroyVecs(n,&vecs1);CHKERRQ(ierr);
2560 eromero 152
  ierr = VecDestroyVecs(n,&vecso);CHKERRQ(ierr);
2544 eromero 153
  ierr = SlepcFinalize();CHKERRQ(ierr);
154
  return 0;
155
}
156