7
data heap,stack /2*40000/
9
c*** Intitialize a message passing library
17
call ga_initialize() ! initialize GAs
18
if (.not. ma_init(MT_DBL, heap, stack)) ! initialize MA
19
$ call ga_error("ma init failed",heap+stack)
21
call trace_init(10000) ! initialize trace
23
call iterate() ! do the work
25
call trace_end(ga_nodeid()) ! end trace
27
call ga_terminate() ! terminate GAs
39
#include "include/finclude/petsc.h"
40
#include "include/finclude/vec.h"
41
#include "include/finclude/mat.h"
42
#include "include/finclude/pc.h"
43
#include "include/finclude/ksp.h"
44
#include "include/finclude/sles.h"
45
#include "include/finclude/sys.h"
47
#include "mafdecls.fh"
51
integer i, j, II, JJ, ierr, m, n
54
integer its, Istart, Iend, flg
56
Scalar v, one, neg_one
65
double precision buf_v(1)
70
c$$$ double precision tol
72
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
73
! Beginning of program
74
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
75
c*** check parallel environment
84
c*** create global arrays: g_x - approx. solution
85
if (.not. ga_create(MT_DBL, m*n, 1, 'x', 1, 1, g_x))
86
$ call ga_error(' ga_create failed ',0)
88
c*** initial guess for x -- zero
94
c$$$ call ga_put(g_x,1,m*n,1,1,buf,ld)
97
call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
99
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
100
! Compute the matrix and right-hand-side vector that define
101
! the linear system, Ax = b.
102
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
105
call MatCreate(PETSC_COMM_WORLD,m*n,m*n,A,ierr)
106
call MatGetOwnershipRange(A,Istart,Iend,ierr)
114
call MatSetValues(A,1,II,1,JJ,v,ADD_VALUES,ierr)
118
call MatSetValues(A,1,II,1,JJ,v,ADD_VALUES,ierr)
122
call MatSetValues(A,1,II,1,JJ,v,ADD_VALUES,ierr)
126
call MatSetValues(A,1,II,1,JJ,v,ADD_VALUES,ierr)
129
call MatSetValues(A,1,II,1,II,v,ADD_VALUES,ierr)
132
call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)
133
call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)
136
call VecCreateMPI(PETSC_COMM_WORLD,PETSC_DECIDE,m*n,u,ierr)
137
call VecSetFromOptions(u,ierr)
138
call VecDuplicate(u,b,ierr)
139
call VecDuplicate(b,x,ierr)
141
c*** u is the exact solution
142
call VecSet(one,u,ierr)
143
if (me .eq. 0) print *, 'Exact solution:'
144
call VecView(u, VIEWER_STDOUT_WORLD, ierr)
146
c*** b is the right hand side
147
call MatMult(A,u,b,ierr)
149
c*** Manage to make connection of ga to petsc: g_x -> x
150
call VecGetOwnershipRange(x,Istart,Iend,ierr)
151
call VecGetArray(x,buf_v,idx,ierr)
153
call ga_get(g_x,Istart+1,Iend,1,1,buf_v(idx+1),ld)
155
call VecRestoreArray(x,buf_v,idx,ierr)
157
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
158
! Create the linear solver and set various options
159
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
161
call SLESCreate(PETSC_COMM_WORLD,sles,ierr)
163
call SLESSetOperators(sles,A,A,DIFFERENT_NONZERO_PATTERN,
166
c$$$ call SLESGetPC(sles,pc,ierr)
167
c$$$ ptype = PCJACOBI
168
c$$$ call PCSetType(pc,ptype,ierr)
169
c$$$ call SLESGetKSP(sles,ksp,ierr)
171
c$$$ call KSPSetTolerances(ksp,tol,PETSC_DEFAULT_DOUBLE_PRECISION,
172
c$$$ & PETSC_DEFAULT_DOUBLE_PRECISION,PETSC_DEFAULT_INTEGER,ierr)
174
call SLESSetFromOptions(sles,ierr)
176
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
177
! Solve the linear system
178
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
180
call SLESSolve(sles,b,x,its,ierr)
183
c*** write the approx solution back to ga
184
call VecGetArray(x,buf_v,idx,ierr)
186
call ga_put(g_x,Istart+1,Iend,1,1,buf_v(idx+1),ld)
188
call VecRestoreArray(x,buf_v,idx,ierr)
190
if (me .eq. 0) print *, 'Approx solution:'
193
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
194
! Check solution and clean up
195
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
199
call VecAXPY(neg_one,u,x,ierr)
200
call VecNorm(x,NORM_2,norm,ierr)
202
if (norm .gt. 1.e-12) then
203
write(6,100) norm, its
208
100 format('Norm of error ',e10.4,' iterations ',i5)
209
110 format('Norm of error < 1.e-12, iterations ',i5)
212
call SLESDestroy(sles,ierr)
213
call VecDestroy(u,ierr)
214
call VecDestroy(x,ierr)
215
call VecDestroy(b,ierr)
216
call MatDestroy(A,ierr)
218
call PetscFinalize(ierr)
220
if(.not. ga_destroy(g_x)) call ga_error('invalid handle ?',0)