zoukankan      html  css  js  c++  java
  • Conv2 GPU加速(有代码有图有真相)

    conv2是matlab自带的一个二维卷积的函数,使用格式为
    C = conv2(A,B,'shape')
    A和B分别是二维矩阵。详细使用见matlab帮助。
    卷积具有重要的物理意义,如滤波等过程。在物理光学中,卷积则可以表示瑞利-索末菲衍射的过程,在matlab变成中表达和计算都非常方便。

    % 本程序计算圆形高斯光束入射到位于 束腰处的 圆盘产生的衍射及伯松亮斑
    close all;clear all;
    w = 0.25;                    % 入射光束腰,束腰半径为0.25mm
    lamda = 0.632e-3;           % 入射激光波长
    k = 2*pi/lamda;             % 波数
    q = 1i*pi*w^2/lamda;        % 束腰处的q参数
    N = 500;                    % 光强采样精度
    x = linspace(-0.5,0.5,N);   % 观察光斑的坐标范围
    z = 10;                     % 衍射传播的距离
    E0 = exp(-1i*k*x.^2/2/q);   % 束腰处的光场复振幅分布
    
    [X Y] = meshgrid(x,x);      % 坐标网格化
    [Ex Ey] =meshgrid(E0,E0);   % 光场二维化
    E1 = Ex.*Ey;                % 束腰处的二维光场
    figure;imagesc(abs(E1).^2); % 圆形高斯光束
    colormap hot;axis image;
    
    Disk = ones(N);     % 定义半径为0.1mm的挡光圆盘
    for m = 1:N
        for n = 1:N
            if sqrt(x(m)^2+x(n)^2) <= 0.1
                Disk(m,n) = 0;
            end
        end
    end
    
    Ein = E1.*Disk;         % 透过圆盘的复振幅
    R = sqrt(z^2+X.^2+Y.^2);    % 衍射传播空间距离
    coef = z./(2*pi*R) .*(1i*k -1./R) .*exp(-1i*k*R);   % 瑞利索末菲衍射系数
    tic
    Eout = conv2(coef,Ein,'same');  % 衍射的二维卷积计算,得到输出复振幅
    toc
    
    Iout = abs(Eout).^2;            % 输出光强
    figure(1);imagesc(x,x,Iout);colormap hot;axis image off;
    figure(2);mesh(Iout);view(0,82);colormap hot;axis off

    Elapsed time is 7.304039 seconds.
    修改GPU 代码:

    % 本程序计算圆形高斯光束入射到位于 束腰处的 圆盘产生的衍射及伯松亮斑
    close all;clear all;
    w = 0.25;                    % 入射光束腰,束腰半径为0.25mm
    lamda = 0.632e-3;           % 入射激光波长
    k = 2*pi/lamda;             % 波数
    q = 1i*pi*w^2/lamda;        % 束腰处的q参数
    N = 500;                    % 光强采样精度
    x = linspace(-0.5,0.5,N);   % 观察光斑的坐标范围
    z = 10;                     % 衍射传播的距离
    E0 = exp(-1i*k*x.^2/2/q);   % 束腰处的光场复振幅分布
    
    x=gsingle(x);%DATA TO  GPU
    E0=gsingle(E0);
    
    [X Y] = meshgrid(x,x);      % 坐标网格化
    [Ex Ey] =meshgrid(E0,E0);   % 光场二维化
    E1 = Ex.*Ey;                % 束腰处的二维光场
    
    gfigure(1);imagesc(abs(E1).^2); % 圆形高斯光束
    
    
    % colormap hot;axis image;
    y=x;
    Disk = gones(N);     % 定义半径为0.1mm的挡光圆盘
    gfor m = 1:N
         for n = 1:N
    %         if sqrt(x(m)^2+x(n)^2) <= 0.1
                Disk(m,n) = Disk(m,n)*~(sqrt(x(m)^2+x(n)^2) <= 0.1);
    %         end
         end
    gend
    
    Ein = E1.*Disk;         % 透过圆盘的复振幅
    R = sqrt(z^2+X.^2+Y.^2);    % 衍射传播空间距离
    coef = z./(2*pi*R) .*(1i*k -1./R) .*exp(-1i*k*R);   % 瑞利索末菲衍射系数
    
    tic
    gsync;
    Eout = conv2(coef,Ein,'same');  % 衍射的二维卷积计算,得到输出复振幅
    geval(Eout);
    gsync;
    toc
    
    Iout = abs(Eout).^2;            % 输出光强
    
    Iout=double(Iout);
    x=double(x);
    figure(2);imagesc(x,x,Iout);
    colormap hot;
    axis image off;
    figure(3);mesh(Iout);
    view(0,82);colormap hot;axis off

    Elapsed time is 0.003671 seconds..


    >> ginfo
    Jacket v2.0 (build 0ae6cf5) by AccelerEyes (64-bit Windows)
    A newer version of Jacket is available: http://accelereyes.com/download_jacket
    License Type: Designated Computer (C:\Program Files\AccelerEyes\Jacket\engine\jlicense.dat)
    Addons: MGL16, JMC, SDK, DLA, SLA
    CUDA toolkit 4.0, driver 296.70
    GPU1 Tesla M2090, 5376 MB, Compute 2.0 (single,double) (in use)
    GPU2 Tesla M2090, 5376 MB, Compute 2.0 (single,double)
    Memory Usage: 5300 MB free (5376 MB total)
    >>

    在JACKET两个版本上分别测试
    >> ginfo
    Jacket v2.1 (build e2c1fa6) by AccelerEyes (64-bit Windows)
    License Type: Designated Computer (C:\Program Files\AccelerEyes\Jacket\engine\jlicense.dat)
    Addons: MGL16, JMC, SDK, DLA, SLA
    CUDA toolkit 4.1, driver 296.70
    GPU1 Tesla M2090, 5376 MB, Compute 2.0 (single,double) (in use)
    GPU2 Tesla M2090, 5376 MB, Compute 2.0 (single,double)
    Memory Usage: 5105 MB free (5376 MB total)

    Elapsed time is 0.003604 seconds.
    >> ed
    Elapsed time is 0.003579 seconds.
    >>

  • 相关阅读:
    HTML实现“摇一摇”效果,比较好的两篇文章;
    mongodb查询关于大于小于的用法;
    thenjs的应用
    js原生forEach、map与jquery的each、$.each的区别
    nodejs的url模块中的resolve()的用法总结
    2021.1.22 刷题(环形链表)
    2021.1.21 刷题(定义链表)
    2021.1.21 刷题(移除链表元素)
    2021.1.20 刷题(螺旋矩阵)
    滑动窗口-长度最小的子数组
  • 原文地址:https://www.cnblogs.com/gpus/p/2530690.html
Copyright © 2011-2022 走看看