- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
[cpp]#pragma omp parallel for firstprivate(accessor) for (int i = 0; i < trip_count; ++i) { work(accessor.GetWorkData(i)); }[/cpp]
링크가 복사됨
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
[cpp]#includeNote that you need to put an empty pair of parenthesis after the holder name to cause it to yield its value. (A new syntax for holders and reducers is in the works that will make them look like pointers rather than like function objects.)
cilk::holderaccessor_holder; cilk_for (int = 0; i < trip_count; ++i) { work(accessor_holder()); // separate view for each iteration
} [/cpp]
To get the effect of firstprivate, assign the holder a specific value at the start of each iteration:
[cpp]cilk::holderTo get the effect of lastprivate, specify the "keep_last" policy for the holder. This policy causes the holder to retain its "last" value (i.e., the last value that would be set if the program were run serially) at the end of the parallel region:accessor_holder(accessor); // Initial value
cilk_for (int = 0; i < trip_count; ++i)
{
accessor_holder() = accessor; // Initialized to value of accessor at each iteration
work(accessor_holder());
} [/cpp]
[cpp]cilk::holderHolders can do a lot more than this, however. Arch Robison wrote an excellent blog entry about holders (including how to create your own holder), although it was written before holders were added as a standard component in the Cilk Plus library. Documentation for holders is available in theIntel C++ Compiler XE 12.1 User and Reference Guides under "Key Features|Intel Cilk Plus|Holders".accessor_holder;
cilk_for (int = 0; i < trip_count; ++i) { accessor_holder() = work(); // Modify a different view in each iteration
} accessor = accessor_holder(); // get value of "last" view[/cpp]
At present, holders are available only in C++. A syntax for using holder in C is still under development.
Let us know how they work for you.
-Pablo
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
Hi,
I am getting the following error when I tried to do this.
error: missing template arguments before ‘accessor_holder
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
Another way to do private variables in a cilk_for loop is simply to declare them inside the loop body. Then they are allocated on the stack inside the lambda function created out of the loop body and automatically private. The equivalent of a FIRSTPRIVATE variable would be something like
[cpp]
myclass init_value;
cilk_for(int i = 0; i < trip_count; i++)
{
myclass private_value(init_value);
work(private_value);
}
[/cpp]
Since init_value is never modified during the paralllel code, using it to initialize private_value is not a race.
- Barry
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
When I last tried it, compiling with icc didn't give a warning about the shared induction variable implied by
cilk_for(i=0; ...
while a warning was issued by icpc.
It's definitely important to compile in C99 or C++ mode so as to have a private induction variable, so as not to limit scaling to larger numbers of workers.
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
Cilkscreen (or Amplifier) should tell you about this, as well as any other races in your code. Anyone who writes parallel code should run a race detection religiously. If you haven't checked for races recently, you probably have them.
- Barry
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
It might be worth noting that the code environment used in these forum posts sometimes has an ugly habit of swallowing text between "<" and ">" arguments. Notice that the name of the include file in #include < ... > has disappeared. Are you missing the object type in your declaration of a holder?
cilk::holder<ObjectType> accessor_holder(accessor);
Anyway, I have also attached a simple but complete test program that uses a holder, that may be of some use to you.
Cheers,
Jim
