@2012,eko didik menjadwalkan for dan lisensi...
TRANSCRIPT
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP Tasks
Lisensi
Pemrograman OpenMP (3)Kuliah#9 TSK617 Pengolahan Paralel - TA 2011/2012
Eko Didik Widianto
Teknik Sistem Komputer - Universitas Diponegoro
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP Tasks
Lisensi
Tentang Kuliah #9 Pemrograman OpenMP
I Akan dibahas tentang pemrograman paralel dengan OpenMPmenggunakan kompiler directive
I Arsitektur memori: shared (SMP, symmetricmulti-processor)
I Model programming: threadI Pokok Bahasan: (kuliah #8 akan membahas item yang
ditebalkan)
1. Pengantar OpenMP2. Membuat Thread3. Sinkronisasi dengan critical, atomic4. Loop dan Worksharing5. Sinkronisasi dengan barrier, single, master, ordered6. Environment Data7. Menjadwalkan for dan section8. Task di OpenMP 3.0
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP Tasks
Lisensi
Kompetensi Dasar
I Setelah mempelajari bab ini, mahasiswa akan mampu:1. [C2] Mahasiswa memahami konsep pemrograman paralel
menggunakan OpenMP2. [C3] Mahasiswa akan mampu membuat program paralel
dari program serial menggunakan compiler-directive danpustaka-pustaka OpenMP
3. [C5] Mahasiswa akan mampu memprogram suatu aplikasikomputasi matrik menggunakan OpenMP serta menghitungfaktor speedupnya
I LinkI Website: http://didik.blog.undip.ac.id/2012/02/25/
kuliah-tsk-617-pengolahan-paralel-2011/I Email: [email protected]
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP Tasks
Lisensi
Acknowledment
I Materi dan gambar didapat dari:I Tim Mattson, Larry Meadows (2008): “A Hands-on
Introduction to OpenMP“I Website: http://openmp.org/wp/resources/I OmpSCR: OpenMP Source Code Repository
(http://sourceforge.net/projects/ompscr/)
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP Tasks
Lisensi
Bahasan
Menjadwalkan For dan SectionSections Worksharing ConstructSchedule Clause
OpenMP TasksOpenMP 3.0 and TasksOpenMP TasksTask ConstructTask ExampleTask and Thread SwitchingProfilingResources
Lisensi
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSectionSections WorksharingConstruct
Schedule Clause
OpenMP Tasks
Lisensi
Bahasan
Menjadwalkan For dan SectionSections Worksharing ConstructSchedule Clause
OpenMP TasksOpenMP 3.0 and TasksOpenMP TasksTask ConstructTask ExampleTask and Thread SwitchingProfilingResources
Lisensi
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSectionSections WorksharingConstruct
Schedule Clause
OpenMP Tasks
Lisensi
Sections Worksharing Construct
I The Sections worksharing construct gives a differentstructured block to each thread
#pragma omp parallel{#pragma omp sections{
#pragma omp sectionX_calculation();
#pragma omp sectionY_calculation();
#pragma omp sectionZ_calculation();
}
}
I By default, there is a barrier at the end of the “ompsections”. Use the “nowait” clause to turn off the barrier
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSectionSections WorksharingConstruct
Schedule Clause
OpenMP Tasks
Lisensi
Sections Example
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSectionSections WorksharingConstruct
Schedule Clause
OpenMP Tasks
Lisensi
Bahasan
Menjadwalkan For dan SectionSections Worksharing ConstructSchedule Clause
OpenMP TasksOpenMP 3.0 and TasksOpenMP TasksTask ConstructTask ExampleTask and Thread SwitchingProfilingResources
Lisensi
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSectionSections WorksharingConstruct
Schedule Clause
OpenMP Tasks
Lisensi
Schedule Clause in Loop Worksharing for
I The schedule clause affects how loop iterations aremapped onto threads
I schedule(static [,chunk])Deal-out blocks of iterations of size “chunk” to each thread.
I schedule(dynamic[,chunk])Each thread grabs “chunk” iterations off a queue until alliterations have been handled.
I schedule(guided[,chunk])Threads dynamically grab blocks of iterations. The size ofthe block starts large and shrinks down to size “chunk” asthe calculation proceeds.
I schedule(runtime)Schedule and chunk size taken from the OMP_SCHEDULEenvironment variable (or the runtime library)
I for OpenMP 3.0
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSectionSections WorksharingConstruct
Schedule Clause
OpenMP Tasks
Lisensi
Static, Dynamic and Guided ScheduleClauses
ScheduleClause
When To Use Notes
STATIC Pre-determined andpredictable by the
programmer
Least work at runtime:scheduling done at
compile-time
DYNAMIC Unpredictable, highly variablework per iteration
Most work at runtime:complex scheduling logic
used at run-time
GUIDED Special case of dynamic toreduce scheduling overhead
Contoh: Linked List
I Program Serial: linked.cI Menampilkan urutan bilangan fibonacci:
F0 F0 F0 F0 F0 F0 F0 F0 F0 F0 F0 F0 F0 ...0 1 1 2 3 5 8 13 21 34 55 89 144 ...
Fn = Fn−1 + Fn−2 dengan nilai seed: F0 = 0, F1 = 1I menggunakan linked list
int fib(int n) {
int x, y;
if (n < 2) {
return (n);
} else {
x = fib(n - 1);
y = fib(n - 2);
return (x + y);
}
}Pascal Trianglea
aSumber: www.wikipedia.org
Linked List (cont)
I Buat program paralelnya (linked_omp25.c)I Ubah loop while menjadi for
I jumlah iterasi dihitung terlebih dahulu
p = init_list(p);head = p;
while (p != NULL) {processwork(p);p = p->next;
}
p = init_list(p);head = p;while (p != NULL) {p = p->next;count++;
}p = head;for(i=0; i<count; i++) {parr[i] = p;p = p->next;
}#pragma omp parallel{#pragma omp singleprintf(" %d threads \n",omp_get_num_threads());#pragma omp forfor(i=0; i<count; i++)processwork(parr[i]);
}
Latihan: Perkalian Matrik
I Program serial perkalian matriks: matmul.cI Buat program paralelnya
I Optimasi program dengan menjadwalkan loop
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Bahasan
Menjadwalkan For dan SectionSections Worksharing ConstructSchedule Clause
OpenMP TasksOpenMP 3.0 and TasksOpenMP TasksTask ConstructTask ExampleTask and Thread SwitchingProfilingResources
Lisensi
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
OpenMP Release History
I 2011: Release OpenMP 3.1 (http://www.openmp.org/mp-documents/OpenMP3.1.pdf)
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Bahasan
Menjadwalkan For dan SectionSections Worksharing ConstructSchedule Clause
OpenMP TasksOpenMP 3.0 and TasksOpenMP TasksTask ConstructTask ExampleTask and Thread SwitchingProfilingResources
Lisensi
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
OpenMP Tasks
I Tasking is added since OpenMP 3.0I A task has
I Code to executeI A data environment (it owns its data)I An assigned thread that executes the code and uses the
data
I Two activities: packaging and executionI Each encountering thread packages a new instance of a
task (code and data)I Some thread in the team executes the task
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Definitions
I Task constructI task directive plus structured block
I TaskI the package of code and instructions for allocating dataI created when a thread encounters a task construct
I Task regionI the dynamic sequence of instructions produced by the
execution of a task by a thread
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Tasks and OpenMP
I Tasks have been fully integrated into OpenMPI Key concept: OpenMP has always had tasks, we just
never called them that.I Thread encountering parallel construct packages up a set of
implicit tasks, one per threadI Team of threads is createdI Each thread in team is assigned to one of the tasks (and
tied to it)I Barrier holds original master thread until all implicit tasks
are finished
I We have simply added a way to create a task explicitlyfor the team to execute.
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Bahasan
Menjadwalkan For dan SectionSections Worksharing ConstructSchedule Clause
OpenMP TasksOpenMP 3.0 and TasksOpenMP TasksTask ConstructTask ExampleTask and Thread SwitchingProfilingResources
Lisensi
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Task Construct
#pragma omp task [clause[[,]clause] ...]
structured-block
I where clause can be one of:if (expression)
I When the if clause expression evaluates to false, an
undeferred task is generated, and the encountering
thread must suspend the current task region, for
which execution cannot be resumed until the generated
task is completed
untiedI If present, any thread in the team can resume the
task region after a suspension
shared (list)
private (list)
firstprivate (list)
default( shared | none )
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
When/where are tasks complete?
I At thread barriers, explicit or implicitI applies to all tasks generated in the current parallel region
up to the barrierI matches user expectation
I At task barriersI i.e. Wait until all tasks defined in the current task have
completed.
#pragma omp taskwait
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Bahasan
Menjadwalkan For dan SectionSections Worksharing ConstructSchedule Clause
OpenMP TasksOpenMP 3.0 and TasksOpenMP TasksTask ConstructTask ExampleTask and Thread SwitchingProfilingResources
Lisensi
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Task ExampleParallel Pointer Chasing using Tasks
#pragma omp parallel{#pragma omp single private(p){p = listhead ;
while (p) {#pragma omp task // p is �rstprivate inside this task
process (p)p=next (p) ;
}}
}
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Task ExampleParallel Pointer Chasing on Multiple Lists using Tasks
#pragma omp parallel{#pragma omp for private(p)for ( int i =0; i <numlists ; i++) {p = listheads [ i ] ;while (p ) {#pragma omp taskprocess (p)p=next (p ) ;}
}
}
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Bahasan
Menjadwalkan For dan SectionSections Worksharing ConstructSchedule Clause
OpenMP TasksOpenMP 3.0 and TasksOpenMP TasksTask ConstructTask ExampleTask and Thread SwitchingProfilingResources
Lisensi
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Task Switching
I When a thread encounters a task scheduling point, it isallowed to suspend the current task and execute another(called task switching)
I Generating task will have to suspend for a while
I It can then return to the original task and resume
#pragma omp single
{
for (i=0; i<ONEZILLION; i++)
#pragma omp task
process(item[i]);
}
I With task switching, the executing thread can:I execute an already generated task (draining the “task pool”)I dive into the encountered task (could be very
cache-friendly)
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Thread Switching
#pragma omp single
{
#pragma omp task untied
for (i=0; i<ONEZILLION; i++)
#pragma omp task
process(item[i]);
}
I Generating task is suspended and executing threadswitches to a long task
I With thread switching, the generating task can be resumedby a different thread
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Bahasan
Menjadwalkan For dan SectionSections Worksharing ConstructSchedule Clause
OpenMP TasksOpenMP 3.0 and TasksOpenMP TasksTask ConstructTask ExampleTask and Thread SwitchingProfilingResources
Lisensi
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Profiling
I Bagaimana melakukan profiling kode paralel?
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Inserting Profiling Code
I Use OpenMP function omp_get_wtime() to get timestamp
#include <omp.h>#de�ne ORDER 1000double start, end;....start = omp_get_wtime();<paralel code>end = omp_get_wtime();printf("Compute Time: %f seconds\n", end - start);dN = (double)ORDER;m�ops = 2.0 * dN * dN * dN/(1000000.0* run_time);printf(" Order %d multiplication at %f m�ops\n", OR-DER, m�ops);
...
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
Bahasan
Menjadwalkan For dan SectionSections Worksharing ConstructSchedule Clause
OpenMP TasksOpenMP 3.0 and TasksOpenMP TasksTask ConstructTask ExampleTask and Thread SwitchingProfilingResources
Lisensi
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
OpenMP Resources
I OmpSCR: OpenMP Source Code Repository(http://sourceforge.net/projects/ompscr/)
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP TasksOpenMP 3.0 and Tasks
OpenMP Tasks
Task Construct
Task Example
Task and Thread Switching
Profiling
Resources
Lisensi
OpenMP On the Real World
I Christian Terboven, Dieter an Mey, “OpenMP in the RealWorld”
PemrogramanOpenMP (3)
@2012,Eko DidikWidianto
Menjadwalkan For danSection
OpenMP Tasks
Lisensi
Lisensi
Creative Common Attribution-ShareAlike 3.0 Unported (CCBY-SA 3.0)
I Anda bebas:I untuk Membagikan — untuk menyalin, mendistribusikan,
dan menyebarkan karya, danI untuk Remix — untuk mengadaptasikan karya
I Di bawah persyaratan berikut:I Atribusi — Anda harus memberikan atribusi karya sesuai
dengan cara-cara yang diminta oleh pembuat karyatersebut atau pihak yang mengeluarkan lisensi.
I Pembagian Serupa — Jika Anda mengubah, menambah,atau membuat karya lain menggunakan karya ini, Andahanya boleh menyebarkan karya tersebut hanya denganlisensi yang sama, serupa, atau kompatibel.
I Lihat: Creative Commons Attribution-ShareAlike 3.0Unported License