In OpenMP, if I didn't initialize an array, the result will be wrong.

In OpenMP, if I didn't initialize an array, the result will be wrong.

First the code. Througn judging a large number a prime, I test the OpenMP Parallel.

program main
    
    use portlib
    implicit none
    integer :: i, j
    integer :: x(10000) = 0  ! #1
    integer :: a(10000) = (/(i,i=100000001,100010000)/)
    real :: Elapsed_time
    
    Elapsed_time = TIMEF()
    !$omp parallel do
        do i = 1, 10000
            x(i) = is_prime(a(i))
        end do
    !$omp end parallel do
    Elapsed_time = TIMEF()
    write(*,*) ' Elapsed_time = ', Elapsed_time
    write(*,*) 'end'
contains
    function is_prime(num) result(prime)
    
        integer, intent(in) :: num
        integer :: prime
        integer :: i
        
        prime = num
        do i=2,int(num/2)
            if(mod(num,i)==0) then
                prime = 0
                exit
            end if
        end do
        
    end function is_prime
    
end program main

Second, In line #1, now i initialize the x(10000) to 0, if i didn't do this, the result of x will be totally wrong, which you can see in the monitor. If I dellete all the OpenMP Directive and disable the setting in the proerties, even if i didn't initialize the x array ,the result will be correct.

I understand the share memory mechanism of the OpenMP, But i cannot understand these from this mechanism. Anyone can help me , thank you!

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

In your code, the initialization of x to 0 is not required as all values in x are written by the do loop (at least they ought to be written). Your listed code above should run without error.

The Intel compiler, with full optimizations, aggressivly removes code that generates values not used. As a test for this situation, following "Elapsed_time = TIMEF()", insert some code to use x. Note, in your actual program you would use values calculated. However for this test program, insert something to use x, such as:

do i=1,10000
if(x(i) .lt. 0) write(*,*) i, x
end do

For information that you can provide to Intel support, modify your above code to initialize x array to -1. Run your test to see if you have any -1's, invalid primes, or missed primes.

www.quickthreadprogramming.com

thank you jimdempseyatthecove. I test what you said, you are right. Oh your name is tooooooooooooooooo long :P

But i still have a question. I check the properties of the project. the optimization is disable(/Od), why the intel compile still do the optimization for me ?

What is being done can be called elimination of dead code. Dead code historically has been defined as code in program that could never run (no flow into section of code). Intel (and others) have expanded the scope of the dead code elimination process to include computational results never used (and the code that produced the unused results). There may be an Intel internal definition (name) of this process.

Users typically run into this situation when they construct a synthetic benchmark that fills in a results array that is not subsequently used, then report exceptionally good scaling of their code. Only to be embarrassed later when someone points out the folly of the test.

Jim Dempsey
(I forgot to sign the earlier post)

www.quickthreadprogramming.com

Leave a Comment

Please sign in to add a comment. Not a member? Join today