rtsIDe (1158448), страница 24
Текст из файла (страница 24)
integer*4 tint4 (2)
integer*8 tint8 (2)
C Initialization of arrays with addresses of adjacent elements of the same type
faddr(1) = getai (tint(1))
naddr(1) = getai (tint(2))
faddr(2) = getal (tlog(1))
naddr(2) = getal (tlog(2))
faddr(3) = getaf (tfloat(1))
naddr(3) = getaf (tfloat(2))
faddr(4) = getad (tdouble(1))
naddr(4) = getad (tdouble(2))
faddr(5) = getach (tchar(1))
naddr(5) = getach (tchar(2))
faddr(6) = getash (tint2(1))
naddr(6) = getash (tint2(2))
faddr(7) = getai (tint4(1))
naddr(7) = getai (tint4(2))
faddr(8) = getai (tint8(1))
naddr(8) = getai (tint8(2))
C Initialization of array with supposed sizes of checked types
tlen(1) = 4
tlen(2) = 4
tlen(3) = 4
tlen(4) = 8
tlen(5) = 1
tlen(6) = 2
tlen(7) = 4
tlen(8) = 8
C Initialization of arrays with numbers of checked types
tcode(1) = 1
tcode(2) = 1
tcode(3) = 3
tcode(4) = 4
tcode(5) = 5
tcode(6) = 6
tcode(7) = 1
tcode(8) = 2
C Checking data types
call tpcntr (8, faddr, naddr, tlen, tcode)
C Run-Time System initialization
dvm = linit (0)
19Examples of programs using Run-Time System functions
19.1Solution of Laplas equation by Jacobi method
The usage of main functions of Run-Time System is shown on the example of numerical solution of two-dimensional Laplas equation in specified rectangular area with specified boundary conditions (Dirixle task)
| 2U | 2U |
Three-point approximation of partial derivatives of the equation (1) on squared grid using five-point pattern
(i-1,j) , (i+1,j) , (i,j) , (i,j-1) , (i,j+1)
gives differential equation ("cross" scheme), reduced to the equation
ui,j = (ui+1,j + ui-1,j + ui,j+1 + ui,j-1)/4 (i,j=1, ... , k-2) (2) ,
where:
| k | | a number of grid points of each dimension; |
| ui,j | | a value of grid function in a note (i,j). |
The values of the grid function
u0,j , uk-1,j , ui,0 , ui,k-1 (i,j=0, ... , k-1)
are determined by boundary conditions.
In the programs below (Fortran and C) the solution of the equation (1) is seached by formulas (2) using method of sequential approximations. The iterational proccess is completed if maximal diviation of the grid function ui,j on all knots for to sequential iterations will become less, then a value of variable maxeps. The initial values of the grid functoin are ui,j = 1+i+j (i,j = 1, … , k-2). The boundary conditions are zero: u0,j=0, uk-1,j=0, ui,0=0 , ui,k-1=0 (i,j = 0, ... , k-1). The solution is searched on square [0:7, 0:7].
PROGRAM IN FORTRAM LANGUAGE
program cross
integer linit, lexit, getam, getps, crtamv, distr, crtda, align,
+ crtpl, dvmadr, mappl, endpl, exfrst, imlast, dopl,
+ tstio, crtrg, crtred, insred, strtrd, waitrd,
+ crtshg, inssh, strtsh, waitsh, delrg, delshg
real bptr(1)
integer dvm
integer amref, psref, mvref, plref, rgref, redref, shgref
integer amdim(2), disaxs(2), dispar(2)
integer shwdth(2), axis(2), coeff(2), const(2)
integer lvaddr(2), lvtype(2), iiniti(2), ilasti(2), istep(2)
integer oiniti(2), olasti(2), ostep(2)
C A number of grid points by each dimension and
C maximal number of iterations
parameter (k = 8, itmax = 20)
real eps, maxeps
C Array header with previous values of the grid function
integer ahdr(3)
C Array header with next values of the grid function
integer bhdr(3)
maxeps = 0.5e-7
C Run-Time System initialisation
dvm = linit (0)
C Creating abstract machine representation
C and mapping it onto processor subsystem
amref = getam ()
psref = getps (amref)
amdim(1) = k
amdim(2) = k
mvref = crtamv (amref, 2, amdim,0)
disaxs(1) = 1
disaxs(2) = 2
dispar(1) = 0
dispar(2) = 0
dvm = distr (mvref, psref, 2, disaxs, dispar)
C Creating and mapping arrays
C with the values of the grid function
shwdth(1) = 1
shwdth(2) = 1
dvm = crtda (ahdr, 1, bptr, 2, 4, amdim, 0, 0, shwdth, shwdth)
dvm = crtda (bhdr, 1, bptr, 2, 4, amdim, 0, 0, shwdth, shwdth)
axis(1) = 1
axis(2) = 2
coeff(1) = 1
coeff(2) = 1
const(1) = 0
const(2) = 0
dvm = align (ahdr, mvref, axis, coeff, const)
dvm = align (bhdr, mvref, axis, coeff, const)
C Parallel loop of initializing arrays
C with values of the grid function
C (parallel loop with the base array ahdr)
plref = crtpl (2)
lvaddr(1) = dvmadr (j)
lvaddr(2) = dvmadr (i)
lvtype(1) = 1
lvtype(2) = 1
iiniti(1) = 0
iiniti(2) = 0
ilasti(1) = k - 1
ilasti(2) = k - 1
istep(1) = 1
istep(2) = 1
dvm = mappl (plref, ahdr, axis, coeff, const, lvaddr, lvtype,
+ iiniti, ilasti, istep, oiniti, olasti, ostep)
4 if (dopl (plref) .eq. 0) goto 5
do 1 j = oiniti(1), olasti(1), ostep(1)
do 1 i = oiniti(2), olasti(2), ostep(2)
bptr( ahdr(3) + 1 + i + ahdr(2) * j ) = 0.
bptr( bhdr(3) + 1 + i + bhdr(2) * j ) = 1. + i + j
1 continue
goto 4
5 dvm = endpl (plref)
C Creating reduction variable and reduction group
C to calculate maximal deviation of the grid function for
C two sequential iterations
redref = crtred ( 3, eps, 3, 1, 0, 0, 0)
rgref = crtrg (0, 0)
dvm = insred (rgref, redref, 0)
C Creating shadow edge group for renewing shadow edges
shgref = crtshg (0)
dvm = inssh (shgref, ahdr, shwdth, shwdth, 0)
C MAIN ITERATION LOOP
do 2 it = 1,itmax
C Parallel loop to calculate maximal deviation
C of the grid function in variable eps
C (parallel loop with base array ahdr)
plref = crtpl (2)
iiniti(1) = 1
iiniti(2) = 1
ilasti(1) = k - 2
ilasti(2) = k - 2
dvm = mappl (plref, ahdr, axis, coeff, const, lvaddr, lvtype,
+ iiniti, ilasti, istep, oiniti, olasti, ostep)
eps = 0.
6 if (dopl (plref) .eq. 0) goto 8
do 7 j = oiniti(1), olasti(1), ostep(1)
do 7 i = oiniti(2), olasti(2), ostep(2)
eps = max(eps, abs(bptr( bhdr(3)+1+i+bhdr(2)*j )
+ - bptr( ahdr(3)+1+i+ahdr(2)*j )))
bptr( ahdr(3) + 1 + i + ahdr(2) * j ) =
+ bptr( bhdr(3) + 1 + i + bhdr(2) * j )
7 continue
goto 6
8 dvm = endpl (plref)
C Calculation of reductional maximum
dvm = strtrd (rgref)
dvm = waitrd (rgref)
C Shadow edge exchange
dvm = strtsh (shgref)
dvm = waitsh (shgref)
C Parallel loop to calculate new values
C of the grid function in array bhdr
C (parallel loop with base array bhdr)
plref = crtpl (2)
dvm = mappl ( plref, bhdr, axis, coeff, const, lvaddr, lvtype,
+ iiniti, ilasti, istep, oiniti, olasti, ostep)
9 if (dopl (plref) .eq. 0) goto 11
do 10 j = oiniti(1), olasti(1), ostep(1)
do 10 i = oiniti(2), olasti(2), ostep(2)
bptr(bhdr(3) + 1 + i + bhdr(2) * j) =
+ ( bptr(ahdr(3) + 1 + (i - 1) + ahdr(2) * j) +
+ bptr(ahdr(3) + 1 + i + ahdr(2) * (j - 1)) +
+ bptr(ahdr(3) + 1 + (i + 1) + ahdr(2) * j) +
+ bptr(ahdr(3) + 1 + i + ahdr(2) * (j + 1)) ) / 4.
10 continue
goto 9
11 dvm = endpl (plref)
C Printing current deviation of the grid function
if (tstio ()) print *,'IT = ',it,' EPS = ',eps
C Exit, if specified precision is achieved
if (eps .lt. maxeps) goto 3
2 continue
C END OF MAIN ITERATION LOOP
C End of dealing with Run-Time System
3 dvm = lexit (0)
end
PROGRAM IN C LANGUAGE
#include "dvmlib.h"
int main(int argc, char *árgv[])
{
long InitPar = 0, UserRes = 0, ExtHdr = 0;
long StaticMV = 0, StaticDA = 0, ReDistrDA = 0, StaticRV = 0,
DelRed = 0, StaticRG = 0, StaticShG = 0, FullShd = 0;
long i, j, it, Rank2 = 2, Long0 = 0, TypeSize = sizeof(float);
long RedFunc = rf_MAX, RVType = rt_FLOAT, RedArrayLength = 1;
AMRef amref;
PSRef psref;
AMViewRef mvref;
LoopRef plref;
RedGroupRef rgref;
RedRef redref;
ShadowGroupRef shgref;
AddrType lvaddr[2];
long lvtype[2] = {0,0};
long amdim[2], disaxs[2], dispar[2];
long shwdth[2], axis[2], coeff[2], cnst[2];
long iiniti[2], ilasti[2], istep[2];
long oiniti[2], olasti[2], ostep[2];
DVMFILE *OutFile;
float eps; /* current precision of calculation */
long k = 8; /* a number of grid points by every dimension */
float maxeps = 0.5e-2f; /* requied precision of calculation */
long itmax = 200; /* maximal number of iterations */
long Ahdr[3]; /* header of array with previous
values of the grid function */
long Bhdr[3]; /* header of array with next
values of the grid function */
rtl_init(InitPar, argc, argv); /* Run-Time System initialization */
/* Creating abstract machine representation
and its mapping on processor subsystem */
amref = getam_ ();
psref = getps_ (&amref);
amdim[0] = k;
amdim[1] = k;
mvref = crtamv_ (&amref, &Rank2, amdim, &StaticMV);
disaxs[0] = 1;
disaxs[1] = 2;
dispar[0] = 0;
dispar[1] = 0;
distr_ (&mvref, &psref, &Rank2, disaxs, dispar);
/* Creation and distribution of arrays
with values of the grid function */
shwdth[0] = 1;
shwdth[1] = 1;
/* Array with previous values of the grig function */
crtda_ (Ahdr, &ExtHdr, NULL, &Rank2, &TypeSize, amdim,
&StaticDA, &ReDistrDA,shwdth, shwdth);
/* Array with next values of grid function */
crtda_ (Bhdr, &ExtHdr, NULL, &Rank2, &TypeSize, amdim,
&StaticDA, &ReDistrDA, shwdth, shwdth);
axis[0] = 1;
axis[1] = 2;
coeff[0] = 1;
coeff[1] = 1;
cnst[0] = 0;
cnst[1] = 0;
align_ (Ahdr,&mvref,axis,coeff,cnst);
align_ (Bhdr,&mvref,axis,coeff,cnst);
/* Parallel loop of initializing arrays
with values of the grid function
(parallel loop with base array Ahdr) */
plref = crtpl_ (&Rank2);
lvaddr[0] = (AddrType)&j;
lvaddr[1] = (AddrType)&i;
iiniti[0] = 0;
iiniti[1] = 0;
ilasti[0] = k - 1;
ilasti[1] = k - 1;
istep[0] = 1;
istep[1] = 1;
mappl_ (&plref, (PatternRef *)Ahdr, axis, coeff, cnst, lvaddr,
lvtype, iiniti, ilasti, istep, oiniti, olasti, ostep);
while (dopl_ (&plref))
for ( j = oiniti[0]; j <= olasti[0]; j += ostep[0] )
for ( i = oiniti[1]; i <= olasti[1]; i += ostep[1] )
{ DAElm2(Ahdr, float, j, i) = 0.;
DAElm2(Bhdr, float, j, i) = 1.f + i + j;
}
endpl_ (&plref);
/* Creating reduction variable and reduction group
to calculate maximal deviation of the grid function for
two sequential iterations */
redref = crtred_ (&RedFunc, &eps, &RVType, &RedArrayLength,
NULL, &Long0, &StaticRV);
rgref = crtrg_ (&StaticRG, &DelRed);
insred_ (&rgref, &redref, NULL);
/* Creating shadow edge group for reîewing shadow edges */
shgref = crtshg_ (&StaticShG);
inssh_ (&shgref, Ahdr, shwdth, shwdth, &FullShd);
/* MAIN ITERATION LOOP */















