PROGRAM parallel_01 USE omp_lib IMPLICIT NONE INTEGER :: i,j INTEGER(4) :: time_begin, time_end, time_rate REAL, DIMENSION(1:50,1:50) :: f, g REAL :: k WRITE(*,*) '開始進行串行計算' !>@ 1、經過串行計算得到兩個矩陣的初始化計算 CALL system_clock(time_begin,time_rate) DO i = 1, 50 DO j = 1, 50 f(i,j) = i*j k = k + 1 END DO END DO DO i = 1, 50 DO j = 1, 50 g(i,j) = i*j + 1 k = k + 1 END DO END DO CALL system_clock(time_end,time_rate) WRITE(*,*) 'The value of k after serial computing is: ', k WRITE(*,*) 'The time wasted on serial computing is: ',(time_end - time_begin)/time_rate WRITE(*,*) WRITE(*,*) WRITE(*,*) '開始進行第一類串行計算—SECTIONS' !>@ 2、經過塊並行計算得到兩個矩陣的初始化計算 k = 0 ! 從新初始化k的值 CALL system_clock(time_begin,time_rate) CALL omp_set_num_threads(2) !$omp parallel !$omp sections private(i,j,k) !$omp section DO i = 1, 50 DO j = 1, 50 f(i,j) = i*j k = k + 1 END DO END DO WRITE(*,*) 'The value of k after parallel computing is: ', k,', and it comes from the thread of ',omp_get_thread_num() !$omp section DO i = 1, 50 DO j = 1, 50 g(i,j) = i*j + 1 k = k + 1 END DO END DO WRITE(*,*) 'The value of k after parallel computing is: ', k,', and it comes from the thread of ',omp_get_thread_num() !$omp end sections !$omp end parallel CALL system_clock(time_end,time_rate) WRITE(*,*) 'The time wasted on the first class parallel computing is: ',(time_end - time_begin)/time_rate WRITE(*,*) WRITE(*,*) WRITE(*,*) '開始進行第二類並行計算—DO' !>@ 3、經過DO循環實現兩個矩陣的初始化計算 k = 0 ! 從新初始化k的值 CALL system_clock(time_begin,time_rate) !$omp parallel private(k,j,i) !$omp do DO i = 1, 50 DO j = 1, 50 f(i,j) = i*j k = k + 1 ! 去掉註釋後,可現實每一次循環所在的線程ID ! WRITE(*,*) 'The value of k after parallel computing is: ', k,', and it comes from the thread of ',omp_get_thread_num() END DO END DO !$omp end do !$omp end parallel !$omp parallel private(k,j,i) !$omp do DO i = 1, 50 DO j = 1, 50 g(i,j) = i*j k = k + 1 ! WRITE(*,*) 'The value of k after parallel computing is: ', k,', and it comes from the thread of ',omp_get_thread_num() END DO END DO !$omp end do !$omp end parallel CALL system_clock(time_end,time_rate) WRITE(*,*) 'The time wasted on the first class parallel computing is: ',(time_end - time_begin)/time_rate END PROGRAM --------------------- 本文來自 B325帥貓-量子前沿技術研究所 的CSDN 博客 ,全文地址請點擊:https://blog.csdn.net/dengm155/article/details/78837408?utm_source=copy