next up previous contents
Next: Solution Up: Data Distribution Previous: Solution

 

Array Masked Array Assignment

Using an allocatable array, an array constructor and the WHERE statement, write an HPF program implement the following algorithm for finding prime numbers:

  1. define a vector, Prime, of size n,
  2. initialise Prime such that Prime(i) = i for i = 1,n
  3. set i = 2
  4. for all tex2html_wrap_inline2886 , ( tex2html_wrap_inline2888 ) if Prime(j) is exactly divisible by i then set Prime(j) = 0, [hint: use the MOD (remainder) intrinsic in conjunction with a WHERE statement.]
  5. increment i,
  6. if i equals n then exit
  7. if Prime(i) is zero then goto step 5
  8. goto step 4

Print out all non-zero entries of the vector (the prime numbers).

Hint: the WHERE statement is an array assignment statement and not a control construct therefore it cannot contain a PRINT statement. The PACK intrinsic can accept an array argument and a conformable MASK and will return a 1D vector of all the elements of the array where the corresponding mask elements are .TRUE..

  Print*, PACK(Array,Mask)

Choose a suitable distribution for the Prime vector; look at the element reference patterns in order to make your decision.




next up previous contents
Next: Solution Up: Data Distribution Previous: Solution

Adam Marshall ©University of Liverpool, 1996
Fri Dec 6 14:10:26 GMT 1996