- 论坛徽章:
- 0
|
本帖最后由 kingwmj 于 2012-08-08 16:02 编辑
写了一个程序,因为太慢,所以改用多进程,但是与我的原来的不分进程的程序结果不一致,最大的可能是多个进程互相修改某个变量.但我没有找到,求高手给看一下.- sub step_3_jaccard_distance_cal(){
- #格式:+1 P08758 Human 0 0 0 0 0 0 0 0 0 0 0 0 0 1
- my $in_file=shift;
- my $in= new IO::File($in_file) or die $!;
- my $out_file=shift;
- my $out = new IO::File(">$out_file") or die $!;
- my $len=0;
- seek $in,0,0;
- print "distance of r and s\n";
- my @matrix=<$in>;
- $len=my @list=split/\s+/,$matrix[0];
- print "$len\n";
- #以下为多进程----------------------------------------------------------------------------------------
- my @child;
- $|++;
- print "good\n";
- # my $R;
- my $num_proc = 0;
- ## == number of collected ==
- my $num_collect = 0;
- my $collect;
- ## == get the child signal ==
- $SIG{CHLD} = sub { $num_proc--};
-
-
- for(my $r=0;$r<=$len-4;$r++){ #
- #my @sum=();
- #$sum[$r]=0;
- print "r=$r\n";
- my $pid=fork(); #分多进程
- push(@child,$pid);
- if(!defined($pid)){
- print "Error in fork: $!";
- exit 1;
- }
- if($pid==0){ #子程序
- my @sum=();
- $sum[$r]=0;
-
- for(my $s=0;$s<=$len-4;$s++){
- print "s= $s\n";
- my $l_up; #这个是hs_up共有多少个键 #这个是分子
- my $l_dn; #这个是hs_dn共有多少个键 #这个是分母
- my %hs_up=();
- my %hs_dn=();
- my @d=();
- foreach my $line (@matrix){
- my @data=split/\s+/,$line;
- if(($data[3+$r]!=$data[3+$s]) and (($data[3+$r]==1) or ($data[3+$s]==1))) { #注意,这里是从3开始,把输入文件整理成前三列是标记
- $hs_up{$data[1]}=2; #这里data[1] 是UNIPROT ID
- }
- if(($data[3+$r]==1) or ($data[3+$s]==1)){
- $hs_dn{$data[1]}=2;
- }
- }
- $l_up=keys %hs_up; #
- $l_dn=keys %hs_dn;
- if($l_dn==0){
- print "r=$r, s=$s\n";
- die ; #
-
- }
- else{
- $d[$r][$s]=$l_up/$l_dn;
- #print $out "$d[$r][$s] "
- }
- $sum[$r]+=$d[$r][$s];
- #print $out "$sum[$r] ";
- }
- print $out "$sum[$r] ";
- exit 0; #very important. 运行到他,这个子进程就结束了.所以要放到子进程的最后.
- }
- $num_proc ++;
- ## == if need to collect zombies ==
- if (($r-$num_proc-$num_collect) > 0) {
- while (($collect = waitpid(-1, WNOHANG)) > 0) {
- $num_collect ++;
- }
- }
- while($num_proc > 1000){ # 控制子进程数
- sleep(3);
- print "stop\n";
- }
-
-
- }
- print $out "\n";
- foreach (@child){ # 回收所有进程
- waitpid($_, 0);
- }
- $in->close;
- $out->close;
- }
复制代码 |
|