楼主: bobguy
1251 1

[程序分享] direct memory access and vectorized calculation [推广有奖]

学科带头人

7%

还不是VIP/贵宾

-

威望
0
论坛币
14187 个
通用积分
28.9279
学术水平
344 点
热心指数
363 点
信用等级
228 点
经验
104882 点
帖子
1846
精华
0
在线时间
1608 小时
注册时间
2008-7-18
最后登录
2019-3-8

中级热心勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
SAS provides addrlong, peeklong functions and pokelong call routine to move/copy data from memory(in this case temporary arry) directly. The call sortn and mean function can be used as vectorized calculations. This makes the following cVaR caculation astounding fast.

Here is a link for more detailed information,

http://www2.sas.com/proceedings/sugi29/264-29.pdf


*****************************************************



719  %let nvar=50;
720
721  data simu;
722
723    array x(*) x1-x&nvar;
724
725    do i=1 to 1000000;
726       do j=1 to dim(x);
727         x(j)=rannor(-1);
728      end;
729      output;
730    end;
731
732  run;

NOTE: The data set WORK.SIMU has 1000000 observations and 52 variables.
NOTE: DATA statement used (Total process time):
      real time           6.20 seconds
      user cpu time       4.61 seconds
      system cpu time     1.06 seconds
      memory              422.90k
      OS Memory           12772.00k
      Timestamp           05/12/2014 07:49:22 PM
      Step Count                        26  Switch Count  0


733
734  data _null_;
735    set  simu nobs=nobs;
736    call symputx('nobs', nobs);
737    stop;
738    run;

NOTE: There were 1 observations read from the data set WORK.SIMU.
NOTE: DATA statement used (Total process time):
      real time           0.37 seconds
      user cpu time       0.00 seconds
      system cpu time     0.00 seconds
      memory              381.09k
      OS Memory           12772.00k
      Timestamp           05/12/2014 07:49:22 PM
      Step Count                        27  Switch Count  0


739
740   %macro cVarL(n);
741
742
743      %do i=1 %to &n;
744        %let percentile=%sysevalf((5+(&i-1)*10)/100);
745        %let idx=%sysfunc(putn((5+(&i-1)*10),z2.));
746
747        ***calculate cVar****;
748
749       max_n=int((&nobs-ceil(&percentile*&nobs)+1)/4095);
750       mod_n=mod((&nobs-ceil(&percentile*&nobs)+1),4095);
751       start_point=ceil(&percentile*&nobs);
752
753      do i=1 to max_n;
754       call pokelong(peekclong (addrlong(_y(start_point+(i-1)*4095 )) , 4095*8) ,
755                                addrlong(cVarL&idx.(1+(i-1)*4095)), 4095*8);
756      end;
757       if mod_n >0 then
758       call pokelong(peekclong (addrlong(_y(start_point+(i-1)*4095)) , mod_n*8) ,
759                                addrlong(cVarL&idx.(1+(i-1)*4095)), mod_n*8);
760
761      cVar&idx.=mean(of cVarL&idx.(*));
762      %end;
763  %mend;
764
765  options nomprint fullstimer;
766
767  data cVar;
768
769    array _x(&nvar, &nobs) _temporary_ ;
770    array _y(&nobs) _temporary_;
771
772    array cVarL05(%sysfunc(ceil(&nobs-0.05*&nobs+1))) _temporary_;
773    array cVarL15(%sysfunc(ceil(&nobs-0.15*&nobs+1))) _temporary_;
774    array cVarL25(%sysfunc(ceil(&nobs-0.25*&nobs+1))) _temporary_;
775    array cVarL35(%sysfunc(ceil(&nobs-0.35*&nobs+1))) _temporary_;
776    array cVarL45(%sysfunc(ceil(&nobs-0.45*&nobs+1))) _temporary_;
777    array cVarL55(%sysfunc(ceil(&nobs-0.55*&nobs+1))) _temporary_;
778    array cVarL65(%sysfunc(ceil(&nobs-0.65*&nobs+1))) _temporary_;
779    array cVarL75(%sysfunc(ceil(&nobs-0.75*&nobs+1))) _temporary_;
780    array cVarL85(%sysfunc(ceil(&nobs-0.85*&nobs+1))) _temporary_;
781    array cVarL95(%sysfunc(ceil(&nobs-0.95*&nobs+1))) _temporary_;
782
783
784  array cVar(10) cVar05 cVar15 cVar25 cVar35 cVar45
785                cVar55 cVar65 cVar75 cVar85 cVar95
786                ;
787
788
789
790  do until(end);
791
792  set simu end=end;
793
794    array x(*) x1-x&nvar;
795
796    n+1;
797
798    do i=1 to &nvar;
799
800       _x(i,n)=x(i);
801
802    end;
803
804  end;
805
806
807
808    do k=1 to &nvar;
809
810      ***copy all _x values to _y;
811       modname=vname (x(k));
812
813       max_n=int(&nobs/4095);
814       mod_n=mod(&nobs,4095);
815       start_point=1;
816
817
818
819      do i=1 to max_n;
820       call pokelong(peekclong (addrlong(_x(k, start_point+(i-1)*4095 )) , 4095*8) ,
821                                addrlong(_y(start_point+(i-1)*4095)), 4095*8);
822      end;
823       if mod_n >0 then
824       call pokelong(peekclong (addrlong(_x(k, start_point+(i-1)*4095)) , mod_n*8) ,
825                                addrlong(_y(start_point+(i-1)*4095)), mod_n*8);
826       call sortn(of _y(*));
827
828      %cVarL(10);
829
830       output;
831
832       end;
833
834  keep modname cVar:;
835
836  run;

NOTE: There were 1000000 observations read from the data set WORK.SIMU.
NOTE: The data set WORK.CVAR has 50 observations and 11 variables.
NOTE: DATA statement used (Total process time):
      real time           12.25 seconds
      user cpu time       11.32 seconds
      system cpu time     0.85 seconds
      memory              1060309.21k
      OS Memory           1132432.00k
      Timestamp           05/12/2014 07:49:35 PM
      Step Count                        28  Switch Count  0


837
838  proc means data=cVar;
839  var cVar:;
840  run;

NOTE: There were 50 observations read from the data set WORK.CVAR.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.01 seconds
      user cpu time       0.00 seconds
      system cpu time     0.03 seconds
      memory              6123.00k
      OS Memory           17912.00k
      Timestamp           05/12/2014 07:49:35 PM
      Step Count                        29  Switch Count  0



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Calculation ulation memory Direct Vector direct function directly provides memory

沙发
shenjdhugh 发表于 2014-5-13 08:23:33 |只看作者 |坛友微信交流群
膜拜膜拜啊!

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-25 13:38