|
初級者向けParact BASICによるマルチスレッド化プログラム
まだ不慣れなので、間違えていたら教えてください。
自然数1億から双子素数を数えるプログラム
!双子素数6n-1 6n+1 Parallel activity Ver.2
DECLARE STRUCTURE abc1: 1 OF NUMERIC
DECLARE STRUCTURE abc2: NUMERIC(1230)
DECLARE STRUCTURE abc3: NUMERIC(16666666)
DECLARE SHARED sha OF abc2
DECLARE SHARED shb OF abc3
DECLARE MESSAGE AMACHANN OF abc1
Paract Par1
OPTION ARITHMETIC NATIVE
DECLARE NUMERIC zv
DECLARE NUMERIC A(1230) !素数
LET t0=TIME
LET k=100000000
LET k2=5761455
!エラトステネスの篩
LET Fu=10007
LET Fm=1230
DIM P(Fu)
MAT P=ZER
LET I=2
LET H=1
LET A(H)=I
FOR I=3 TO SQR(Fu) STEP 2
IF P(I)=0 THEN
FOR J=I*I TO Fu STEP I
LET P(J)=1
NEXT J
END IF
NEXT I
FOR I=3 TO Fu STEP 2
IF P(I)=0 THEN
LET H=H+1
LET A(H)=I
END IF
NEXT I
PUT TO sha FROM A
START par2
START par3
RECEIVE FROM AMACHANN TO zv
PRINT "pi2(x)";zv+1
LET TM=TIME-t0
PRINT USING"###.###":TM;
PRINT "秒"
END PARACT
paract par2
OPTION ARITHMETIC NATIVE
DECLARE NUMERIC A(1230) !素数
DECLARE NUMERIC AA(16666666)
GET from sha TO A
LET Q=6
LET k7=100000000 !篩の計算範囲
LET k5=INT(k7/Q)
FOR n=3 TO 1229
LET Pu=A(n)
IF MOD(Pu+1,Q)=0 THEN !(6*n-1)
LET ru=(Pu+1)/Q
FOR i=1 TO k5
IF Pu*i+ru>k5 THEN EXIT FOR
LET AA(Pu*i+ru)=1
NEXT i
END IF
IF MOD(Pu-1,Q)=0 THEN
LET ru=(Pu-1)/Q
FOR i=1 TO k5
IF Pu*i-ru>k5 THEN EXIT FOR
LET AA(Pu*i-ru)=1
NEXT i
END IF
NEXT n
put TO shb from AA
END PARACT
Paract Par3
DECLARE NUMERIC zv
DECLARE NUMERIC A(1230) !素数
DECLARE NUMERIC AA(16666666)
GET FROM sha TO A
LET Q=6
LET k7=100000000 !篩の計算範囲
LET k5=INT(k7/Q)
DIM Av(k5)
MAT Av = ZER !(6*n+1)
FOR n=3 TO 1229
LET Pu=A(n)
IF MOD(Pu+1,Q)=0 THEN !(6*n+1)
LET ru=(Pu+1)/Q
FOR i=1 TO k5
IF Pu*i-ru>k5 THEN EXIT FOR
LET Av(Pu*i-ru)=1
NEXT i
END IF
IF MOD(Pu-1,Q)=0 THEN
LET ru=(Pu-1)/Q
FOR i=1 TO k5
IF Pu*i+ru>k5 THEN EXIT FOR
LET Av(Pu*i+ru)=1
NEXT i
END IF
NEXT n
GET from shb TO AA
MAT Av=AA+Av
FOR n=1 TO k5
IF 6*n+1>k7 THEN EXIT FOR
IF Av(n)=0 THEN
LET zv=zv+1
END IF
NEXT n
send TO AMACHANN from zv
END PARACT
---------------------------------------
当初の狙いは、sample program を手本に
!SHAREDポート経由のデータ転送
DECLARE STRUCTURE struct2: NUMERIC(10)
DECLARE shared sha OF struct2
DECLARE SHARED shb OF struct2
paract p1
DECLARE NUMERIC m(10)
start p2
FOR i=1 TO 9 STEP 2
LET m(i)=i^2
NEXT i
put TO sha from m
start p3
END PARACT
PARACT p2
DECLARE NUMERIC W(10)
FOR i=2 TO 10 STEP 2
LET W(i)=i^2
NEXT i
put TO shb from W
END PARACT
paract p3
DECLARE NUMERIC m(10)
DECLARE NUMERIC W(10)
GET from sha TO m
GET from shb TO W
MAT m=m+w
FOR i=1 TO 10
PRINT m(i)
NEXT i
END paract
----------------------------
SHAREDポート経由のデータ転送は、配列を大きくすると難しい。
http://blogs.yahoo.co.jp/donald_stinger
|
|