zoukankan      html  css  js  c++  java
  • Fortran+ OpenMP实现实例

    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 
  • 相关阅读:
    依次逐个亮灯并且每次只能亮一个灯的跑马灯程序
    逐个点亮LED灯,再逐个熄灭LED灯的跑马灯程序---基于74HC595移位锁存器,程序框架用switch语句
    把74HC595驱动程序翻译成类似单片机IO口直接驱动的方式
    两片联级74HC595驱动16个LED灯的基本驱动程序
    树莓派
    Linux I2C驱动
    转:使用 /proc 文件系统来访问 Linux 内核的内容
    转: 使用 /sys 文件系统访问 Linux 内核
    树梅派 -- 通过/sys读写ADC芯片 pcf8591
    树莓派 -- oled 续(2) python
  • 原文地址:https://www.cnblogs.com/China3S/p/9713381.html
Copyright © 2011-2022 走看看