openmp firstprivate
От: barmale-y  
Дата: 09.09.10 19:50
Оценка:
Почему firstprivate не работает и не всегда инициализирует переменную внутри параллельного потока, значение из последовательного?
#include <iostream>
#include <omp.h>

main(){
    int x,i,id;
    x=1;
    #pragma omp parallel sections  firstprivate(x)
    {   
        #pragma omp  section 
        {
           id = omp_get_thread_num();
           std::cout << "Thread id = " << id << std::endl;
           std::cout << "start x = " << x << std::endl;
           x=x+id;
           std::cout << "comput. x = x + id = " << x << std::endl;
        }

        #pragma omp  section 
        {
           id = omp_get_thread_num();
           std::cout << "Thread id = " << id << std::endl;
           std::cout << "start x = " << x << std::endl;
           x=x+id;
           std::cout << "comput. x = x + id = " << x << std::endl;
        }
    }
}


$ c++ -fopenmp firstpr.cc -o firstpr && ./firstpr
Thread id = 2
start x = 1
comput. x = x + id = 3
Thread id = 2
start x = 3
comput. x = x + id = 5

$ c++ -fopenmp firstpr.cc -o firstpr && ./firstpr
Thread id = 1
start x = 1
comput. x = x + id = 2
Thread id = 1
start x = 1
comput. x = x + id = 2


второй случай отработал вроде нормально, а вот первый ?

здесь:

The FIRSTPRIVATE clause combines the behavior of the PRIVATE clause with automatic initialization of the variables in its list

Listed variables are initialized according to the value of their original objects prior to entry into the parallel or work-sharing construct.

Re: openmp firstprivate
От: barmale-y  
Дата: 09.09.10 20:17
Оценка:
BY>
BY>$ c++ -fopenmp firstpr.cc -o firstpr && ./firstpr
BY>Thread id = 2
BY>start x = 1
BY>comput. x = x + id = 3
BY>Thread id = 2
BY>start x = 3
BY>comput. x = x + id = 5

BY>$ c++ -fopenmp firstpr.cc -o firstpr && ./firstpr
BY>Thread id = 1
BY>start x = 1
BY>comput. x = x + id = 2
BY>Thread id = 1
BY>start x = 1
BY>comput. x = x + id = 2
BY>




Проблема с sections. ОС просто иногда запускает одну нить, почемуто

если нити разные, то все ok
$ c++ -fopenmp firstpr.cc -o firstpr && ./firstpr
Thread id = Thread id = 32
start x = 1
comput. x = x + id = 3

start x = 1
comput. x = x + id = 4



$ c++ -fopenmp firstpr.cc -o firstpr && ./firstpr
Thread id = 3
Thread id = 1start x = 1
start x = 1
comput. x = x + id = 2

comput. x = x + id = 4
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.