Trình bày: Nguyễn Quang Thái Lớp K22-CKT OpenMP là gì? • OpenMP ( Open Multi-Processing) (1997 ) • là một môi trường API (Application Programming Interface) để viết ứng dụng dạng chia sẻ bộ nhớ (shared memory) cho C/C++ và Fortran. • Sử dụng các chỉ thị trực tiếp trong chương trình; 1. Các chỉ thị biên dịch (Compiler Directives) 2. Các thư viện runtime (Runtime Library Routines) 3. Các biến môi trường (Emviroment Variables) . • Có thể viết theo kiểu tuần tự Cài đặt trên windown • Cài đặt Intel Visual Studio 2010 • Cài đặt nhân Fortran Intel Fotran • Kích hoạt chức năng tính toán song song – Với Visual C++: Project -> projecties ->C/C++ ->Language- >OpenMP Support mở lên (tham số /openmp) – Với Visual Fortran làm tương tự thay vì C/C++ Fortran • Với Ubuntu cần include thêm thư viện của OpenMP biên dịch : gcc -fopenmp test.c -o testOMP chạy : ./testOMP Cú pháp lập trình và ví dụ Với C++
Chương trình tuần tự Chương trình song song hóa
Cú pháp lập trình và ví dụ Với Fortran program main use omp_lib …. double precision, allocatable,dimension(:) :: fx double precision :: f …. do … allocate(fx(n)) …. !$omp parallel private ( i , x ) shared ( h , fx, n ) !$omp do do i = 1,n write(*,*) i x = h * ( DBLE (i) - 0.5) fx(i)= f(x) end do !$omp end do !$omp end parallel …. deallocate(fx) ….. end program Cú pháp lập trình và ví dụ Với Fortran program main use omp_lib …. double precision, allocatable,dimension(:) :: fx double precision :: f …. do … allocate(fx(n)) …. !$omp parallel private ( i , x ) shared ( h , fx, n ) !$omp do do i = 1,n write(*,*) i x = h * ( DBLE (i) - 0.5) fx(i)= f(x) end do !$omp end do !$omp end parallel …. private ( i , x ) – Chỉ thị biến I và x được địa phương hóa với mỗi core deallocate(fx) shared ( h , fx, n ) – chỉ thị chia sẻ giá trị h,fx,n ….. end program Các chỉ thị của OpenMP parallel …. end parallel: Vùng được song song hóa program main use omp_lib …. double precision, allocatable,dimension(:) :: fx double precision :: f …. do … allocate(fx(n)) …. !$omp parallel private ( i , x ) shared ( h , fx, n ) !$omp do do i = 1,n write(*,*) i x = h * ( DBLE (i) - 0.5) fx(i)= f(x) end do Private: Cá nhân hóa biến cho luồng !$omp end do !$omp end parallel Shared: Xác định biến được chia sẻ …. cho các luồng deallocate(fx) Luồng chủ sẽ tính toán giá trị cuối ….. cùng end program Các chỉ thị của OpenMP • do and end do: Đảm bảo các luồng không xử lý vòng lặp thừa program main use omp_lib …. double precision, allocatable,dimension(:) :: fx double precision :: f …. do … allocate(fx(n)) …. !$omp parallel private ( i , x ) shared ( h , fx, n ) ! $omp do do i = 1,n write(*,*) i x = h * ( DBLE (i) - 0.5) fx(i)= f(x) end do ! $omp end do !$omp end parallel …. deallocate(fx) ….. Các chỉ thị của OpenMP Chỉ thị critical …. end critical Đảm bảo tính tuần tự của code Các chỉ thị của OpenMP Reduction clause Tương tự với reduction trong MPI Các chỉ thị của OpenMP single and end single Cú pháp lập trình và ví dụ Với Fortran program main use omp_lib …. double precision, allocatable,dimension(:) :: fx double precision :: f …. do … allocate(fx(n)) …. !$omp parallel private ( i , x ) shared ( h , fx, n ) !$omp do do i = 1,n write(*,*) i x = h * ( DBLE (i) - 0.5) fx(i)= f(x) end do !$omp end do !$omp end parallel …. deallocate(fx) ….. end program