3. Open Mp (Open Multiprocessing)
• API for shared Memory Model Programming
•Compiler Directives
•Runtime Library Routines
•Environmental Variables
•Thread Based: OpenMP programs accomplish parallelism exclusively
through the use of threads
•Fork- Join Model
•Since 1997(version 1)
•Latest 2013(version 4)
•Advantages
•Standardization and portability
•Ease of Use
4. Data Environment :
•Controlling thread data.
•Controls the execution of program.
Changing Storage
Attributes
•Private
•First private
•Last private
•Shared
•Default
•threadprivate
•Copy in
5. Private Construct:
Createsprivate copy of variablefor eachthread.
Uninitializedvalue.
int main()
{
Int tmp = 0;
#pragma omp parallel for private(tmp)
for (int j=0; j<100;j++)
tmp += j;
printf(“%dn”, tmp);
}
6. First -Private Construct:
Creates Private copy of variable for each thread.
Intialized with global value.
int main()
{
int tmp = 0;
#pragma omp parallel for firstprivate(tmp)
for (int j=0; j<100;j++)
tmp += j; printf(“%dn”, tmp);
printf(“%dn”, tmp);
}
7. Lastprivate Construct:
copies value of last iterationto global scope.
– It ensures that the last valueof a data object listedis accessibleafterthe corresponding
construct has completed execution.
int main()
{ int a, j;
#pragma omp parallelfor private(j) lastprivate(a)
for (j=0; j<5;j++);
{ a = j + 2;
printf(“Thread%d has a value of a = %d for j = %dn”, omp_get_thread_num(), a, j);
}
printf(“valueof a afterparallel= %dn”, a);
}
8. Thread private:
•Threadprivate makes global data private to a thread.
– C/C++: only applies file scope and static variables, static class
members
•Threadrpivate preserves global scope within each thread.
•Threadprivate variables can be initialized using copyin clause or at
time of definition
Syntax
>>-THREADPRIVATE--(--threadprivate_entity_list--)--------------><
where threadprivate_entity_list is:
>>-+-variable_name---------+----------------------------------->< '-/
common_block_name /-'
common_block_nameis the name of a common block to be made
private to a thread.variable_nameis the name of a variable to be made
private to a thread.
9. Copyin clause:
•Copyin allows to copy the master thread’s threadprivate variables to
corresponding threadprivate variables of the other threads.
int global[100];
#pragma omp threadprivate(global)
int main()
{ for(int i= 0; i<100;i++) global[i] = i+2; // initialize data
#pragma omp parallel copyin(global)
{ /// parallel region, each thread gets a copy of global, with initialized value
}
}