コード例 #1
0
void  Map_And_Multiply()
{
        int Row,Col,k,Index,Block_Index;
        int fd=open("./MatrixC",O_RDWR);
        if(fd==-1)
        {
                perror("File not opened ");
                exit(1);

        }

        bufferA=(double*)malloc(sizeof(double)*Block_Of_Row*Total_Row);
        bufferB=(double*)malloc(sizeof(double)*Block_Of_Row*Total_Col);
        bufferC=(double*)malloc(sizeof(double)*Block_Of_Row*Block_Of_Row);

        for(Row=0;Row<Total_Row;Row=Row+Block_Of_Row)
        {

                for(Col=0;Col<Total_Col;Col=Col+Block_Of_Row)
                {

                        int indexA=0,indexB=0;
                        for(Block_Index=0;Block_Index<Block_Of_Row;Block_Index++)
                        {
                                for(k=0;k<(Total_Col/Chunk_Size);k++)
                                {

                                        MatrixA=Map_FILE_To_Memory("./MatrixA",1,Row+Block_Index,k);
                                        MatrixB=Map_FILE_To_Memory("./MatrixB",1,Col+Block_Index,k);
                                        for(Index=0;Index<Chunk_Size;Index++)
                                        {
                                                bufferA[indexA++]=MatrixA[Index];
                                                bufferB[indexB++]=MatrixB[Index];

                                        }

                                        munmap(MatrixB,Chunk_Size*sizeof(double));
                                        munmap(MatrixA,Chunk_Size*sizeof(double));
                                 }
                        }
 domult(Row,Col);
                        int p,q;
                        double Buf;
                        for(p=0;p<Block_Of_Row;p++)
                        {
                                lseek(fd,(Row+p)*Total_Row*sizeof(double)+(Col*sizeof(double)),SEEK_SET);
                                for(q=0;q<Block_Of_Row;q++)
                                {
                                        Buf=bufferC[p*Block_Of_Row+q];
                                        write(fd,&Buf,sizeof(double));

                                }
                        }
                }
        }

        close(fd);
}
コード例 #2
0
ファイル: smartrel.c プロジェクト: b1234561/EIG
int main(int argc, char **argv)
{

  char **eglist ;
  int numeg ;
  int i, j, k, pos; 
  int *vv ;
  SNP *cupt, *cupt2 ;
  Indiv *indx ;
  double y1, y2, y ;

  int n0, n1, nkill ;

  int nindiv = 0 ;
  int nignore, numrisks = 1 ;
  SNP **xsnplist  ;
  Indiv **xindlist ;
  int *xindex ;
  int nrows, ncols, m ;
  double *XTX, *cc, *evecs, *ww ;
  double *lambda ;
  double *tvecs ;
  int weightmode = NO ;
  int t ;
  double *xmean, *xfancy ;
  double *ldmat = NULL, *ldmat2 = NULL;
  double *ldvv = NULL, *ldvv2 = NULL, *vv2 = NULL ;
  int chrom,  numclear ;
  double gdis ;
  int outliter, numoutiter, *badlist, nbad ;
  int a, b, n ;
  FILE *outlfile ;
  

  int xblock, blocksize=10000 ;   
  double *tblock ;  

  OUTLINFO *outpt ;
  int *idperm, *vecind ;   // for sort

  readcommands(argc, argv) ;
  printf("## smartrel version: %s\n", WVERSION) ;
  packmode = YES ;
  setomode(&outputmode, omode) ;

  if (parname == NULL) return 0 ;
  if (xchrom == (numchrom+1)) noxdata = NO ;

  if (fstonly) { 
   printf("fstonly\n") ;
   numeigs = 0 ; 
   numoutliter = 0 ;
   numoutiter = 0 ;
   outputname = NULL ;
   snpeigname = NULL ;
  }

  if (fancynorm) printf("norm used\n\n") ;
  else printf("no norm used\n\n") ;

  nostatslim = MAX(nostatslim, 3) ;

  outlfile = ofile = stdout; 

  if (outputname != NULL)  openit(outputname, &ofile, "w") ;
  if (outliername != NULL) openit(outliername, &outlfile, "w") ;
  if (fstdetailsname != NULL) openit(fstdetailsname, &fstdetails, "w") ;

  numsnps = 
    getsnps(snpname, &snpmarkers, 0.0, badsnpname, &nignore, numrisks) ;

  numindivs = getindivs(indivname, &indivmarkers) ;
  k = getgenos(genotypename, snpmarkers, indivmarkers, 
    numsnps, numindivs, nignore) ;


  if (poplistname != NULL) 
  { 
    ZALLOC(eglist, numindivs, char *) ; 
    numeg = loadlist(eglist, poplistname) ;
    seteglist(indivmarkers, numindivs, poplistname);
  }
  else
  {
    setstatus(indivmarkers, numindivs, NULL) ;
    ZALLOC(eglist, MAXPOPS, char *) ;
    numeg = makeeglist(eglist, MAXPOPS, indivmarkers, numindivs) ;
  }
  for (i=0; i<numeg; i++) 
  {  
    /* printf("%3d %s\n",i, eglist[i]) ; */
  }

  nindiv=0 ;
  for (i=0; i<numindivs; i++) 
  {
    indx = indivmarkers[i] ;
    if(indx -> affstatus == YES) ++nindiv  ;
  }

  for (i=0; i<numsnps; i++)  
  {  
    cupt = snpmarkers[i] ; 
    chrom = cupt -> chrom ;
    if ((noxdata) && (chrom == (numchrom+1))) cupt-> ignore = YES ;
    if (chrom == 0) cupt -> ignore = YES ;
    if (chrom > (numchrom+1)) cupt -> ignore = YES ;
  }
  for (i=0; i<numsnps; i++)  
  {
    cupt = snpmarkers[i] ; 
    pos = nnint(cupt -> physpos) ;
    if ((xchrom>0) && (cupt -> chrom != xchrom)) cupt -> ignore = YES ;
    if ((xchrom > 0) && (pos < lopos)) cupt -> ignore = YES ;
    if ((xchrom > 0) && (pos > hipos)) cupt -> ignore = YES ;
    if (cupt -> ignore) continue ;
    if (numvalidgtx(indivmarkers, cupt, YES) <= 1) 
    { 
      printf("nodata: %20s\n", cupt -> ID) ;
      cupt -> ignore = YES ;
    }
  }

  if (killr2) {
   nkill = killhir2(snpmarkers, numsnps, numindivs, r2physlim, r2genlim, r2thresh) ;
   if (nkill>0) printf("killhir2.  number of snps killed: %d\n", nkill) ;
  }

  ZALLOC(vv, numindivs, int) ;
  numvalidgtallind(vv, snpmarkers, numsnps,  numindivs) ; 
  for (i=0; i<numindivs; ++i)  { 
  if (vv[i] == 0) {
    indx = indivmarkers[i] ;
    indx -> ignore = YES ; 
   }
  }
  free(vv) ;

  numsnps = rmsnps(snpmarkers, numsnps, NULL) ;  //  rid ignorable snps

   
  if (missingmode) 
  {
    setmiss(snpmarkers, numsnps) ;
    fancynorm = NO ;
  }

  if  (weightname != NULL)   
  {  
    weightmode = YES ;
    getweights(weightname, snpmarkers, numsnps) ;
  }
  if (ldregress>0) 
  {  
    ZALLOC(ldvv,  ldregress*numindivs, double) ;
    ZALLOC(ldvv2,  ldregress*numindivs, double) ;
    ZALLOC(vv2,  numindivs, double) ;
    ZALLOC(ldmat,  ldregress*ldregress, double) ;
    ZALLOC(ldmat2,  ldregress*ldregress, double) ;
    setidmat(ldmat, ldregress) ;         
    vst(ldmat, ldmat, 1.0e-6, ldregress*ldregress) ;
  }

  ZALLOC(xindex, numindivs, int) ;
  ZALLOC(xindlist, numindivs, Indiv *) ;
  ZALLOC(xsnplist, numsnps, SNP *) ;

  if (popsizelimit > 0) 
  {  
    setplimit(indivmarkers, numindivs, eglist, numeg, popsizelimit) ; 
  }

  nrows = loadindx(xindlist, xindex, indivmarkers, numindivs) ;
  ncols = loadsnpx(xsnplist, snpmarkers, numsnps, indivmarkers) ;
  printf("number of samples used: %d number of snps used: %d\n", nrows, ncols) ;

/**
  cupt = xsnplist[0] ;
  for (j=0; j<nrows; ++j) {  
   k = xindex[j] ;
   g = getgtypes(cupt, k) ;
   indx = indivmarkers[k] ;
   t = indxindex(eglist, numeg, indx -> egroup) ;
   printf("yy1 %20s %20s %20s %d %d %d\n", cupt ->ID, indx -> ID, indx -> egroup, j, k, g) ;
  }
  printf("yya: ") ; printimat(xindex, 1, nrows) ;
  printf("zzindxa:  %s\n", indivmarkers[230] -> egroup) ;
*/

  /* printf("## nrows: %d  ncols  %d\n", nrows, ncols) ; */
  ZALLOC(xmean, ncols, double) ;
  ZALLOC(xfancy, ncols, double) ;
  ZALLOC(XTX, nrows*nrows, double) ;
  ZALLOC(evecs, nrows*nrows, double) ;
  ZALLOC(tvecs, nrows*nrows, double) ;
  ZALLOC(lambda, nrows, double) ;
  ZALLOC(cc, nrows, double) ;
  ZALLOC(ww, nrows, double) ;
  ZALLOC(badlist, nrows, int) ;

  blocksize = MIN(blocksize, ncols) ; 
  ZALLOC(tblock, nrows*blocksize, double) ;

  // xfancy is multiplier for column xmean is mean to take off
  // badlist is list of rows to delete (outlier removal) 

  numoutiter = 1 ;  

  if (numoutliter>=1) 
  {
    numoutiter = numoutliter+1 ;
    ZALLOC(outinfo, nrows,  OUTLINFO *) ;  
    for (k=0; k<nrows; k++) 
    {  
      ZALLOC(outinfo[k], 1, OUTLINFO) ;
    }
    /* fprintf(outlfile, "##%18s %4s %6s %9s\n", "ID", "iter","eigvec", "score") ; */
  }

  for (outliter = 1; outliter <= numoutiter ; ++outliter)  {
    if (fstonly) { 
     setidmat(XTX, nrows) ;
     vclear(lambda, 1.0, nrows) ;
     break ;
    }
    if (outliter>1) {
     ncols = loadsnpx(xsnplist, snpmarkers, numsnps, indivmarkers) ;
    }
    vzero(XTX, nrows*nrows) ;
    vzero(tblock, nrows*blocksize) ;
    xblock = 0 ; 

    vzero(xmean, ncols) ;
    vclear(xfancy, 1.0, ncols) ;

    for (i=0; i<ncols; i++) 
    { 
      cupt = xsnplist[i] ;
      chrom = cupt -> chrom ;
      getcolxz(cc, cupt, xindex, nrows, i, xmean, xfancy, &n0, &n1) ;
      t = MIN(n0, n1) ; 

      if (t <= minallelecnt)  {  
       cupt -> ignore = YES ;
       vzero(cc, nrows) ; 
      }

      if (weightmode) 
      {
        vst(cc, cc, xsnplist[i] -> weight, nrows) ;
      }
      if (ldregress>0) 
      {  
        numclear = 0 ;
        for (k=1; k<= ldregress; ++k)  
        {  
          j = i-k ;  
          if (j<0) 
          { 
            numclear = ldregress-k+1 ; 
            break ;
          }
          cupt2 = xsnplist[j] ;  
          if (cupt2 -> chrom != chrom) gdis = ldlimit + 1.0 ; 
          else gdis = cupt -> genpos - cupt2 -> genpos ;
          if (gdis>=ldlimit) 
          {   
            numclear = ldregress-k+1 ; 
            break ;
          }
        }
        if (numclear>0) clearld(ldmat, ldvv, ldregress, nrows, numclear) ; 
        ldreg(ldmat, ldmat2, cc, vv2, ldvv, ldvv2, ldregress, nrows) ;
        copyarr(ldmat2, ldmat, ldregress*ldregress) ;
        copyarr(vv2, cc, nrows) ;
        copyarr(ldvv2, ldvv, ldregress*nrows) ;
      }
      copyarr(cc, tblock+xblock*nrows, nrows) ;
      ++xblock ; 

/** this is the key code to parallelize */
      if (xblock==blocksize) 
      {  
        domult(tvecs, tblock, xblock, nrows) ;
        vvp(XTX, XTX, tvecs, nrows*nrows) ;
        xblock = 0 ;
        vzero(tblock, nrows*blocksize) ;
      }
    }

    if (xblock>0) 
    { 
     domult(tvecs, tblock, xblock, nrows) ;
     vvp(XTX, XTX, tvecs, nrows*nrows) ;
    }
    symit(XTX, nrows) ;

    /**
    a = 0; b=0 ;
    printf("zz1 %12.6f ", XTX[a*nrows+b]) ;
    a = nrows-1; b=nrows-1 ;
    printf(" %12.6f %15.9g\n", XTX[a*nrows+b], asum(XTX, nrows*nrows)) ;
    */

    if (verbose) 
    {
      printdiag(XTX, nrows) ;
    }

    y = trace(XTX, nrows) / (double) (nrows-1) ;
    if (isnan(y)) fatalx("bad XTX matrix\n") ;
    /* printf("trace:  %9.3f\n", y) ; */
    if (y<=0.0) fatalx("XTX has zero trace (perhaps no data)\n") ;
    vst(XTX, XTX, 1.0/y, nrows * nrows) ;
/// mean eigenvalue is 1
    eigvecs(XTX, lambda, evecs, nrows) ;
// eigenvalues are in decreasing order 

    if (outliter > numoutliter) break ;  
    // last pass skips outliers 
    numoutleigs = MIN(numoutleigs, nrows-1) ;
    nbad = ridoutlier(evecs, nrows, numoutleigs, outlthresh, badlist, outinfo) ;
    if (nbad == 0) break ; 
    for (i=0; i<nbad; i++) 
    {  
      j = badlist[i] ;
      indx = xindlist[j] ;
      outpt = outinfo[j] ;
      fprintf(outlfile, "REMOVED outlier %s iter %d evec %d sigmage %.3f\n", indx -> ID, outliter, outpt -> vecno, outpt -> score) ;
      indx -> ignore = YES ;
    }
    nrows = loadindx(xindlist, xindex, indivmarkers, numindivs) ;
    printf("number of samples after outlier removal: %d\n", nrows) ;
  }

  if (outliername != NULL) fclose(outlfile) ;

  m = numgtz(lambda, nrows)  ;
  /* printf("matrix rank: %d\n", m) ; */
  if (m==0) fatalx("no data\n") ;

/** smartrel code */
  for (i=0; i<numeigs; i++) {  
   y = sqrt(lambda[i]) ;
   vst(ww, evecs+i*nrows, y, nrows) ;               
   subouter(XTX, ww, nrows) ;
  }
  free(tvecs) ; 

  n = 0 ;
  ZALLOC(vecind, nrows*nrows/2, int) ; 
  for (i=0; i<nrows; i++) { 
   for (j=i+1; j<nrows; j++) { 
    k = i*nrows + j ; 
    y1 = XTX[i*nrows+i] ;
    y2 = XTX[j*nrows+j] ;
    y = XTX[k]/sqrt(y1*y2) ;
    y += 1/(double)(nrows-1);
    if (y<relthresh) continue ;
    vecind[n] = k ; 
    evecs[n] = -y ;
    ++n ;
   }
  }
  free(XTX) ; 
  if (n==0) { 
   printf("## nothing above relthresh!\n") ;
   printf("##end of smartrel run\n") ;
   return 0 ;
  }
  ZALLOC(idperm, n, int) ; 
  sortit(evecs, idperm, n) ;
  for (i=0; i<n; i++) {  
   j = idperm[i] ;
   k = vecind[j] ;
   a = k/nrows ; 
   b = k%nrows ;
   printf("rel: %20s ",  xindlist[a] ->ID) ;
   printf("%20s ",  xindlist[b] ->ID) ;
   printf(" %9.3f", -evecs[i]) ;
   printnl() ;
  }
  
  printf("##end of smartrel run\n") ;
  return 0 ;
}
コード例 #3
0
void  map_Matrix()
{

int i,j,k,index,l;
		int fd=open("./MatrixC",O_RDWR);
			if(fd==-1)
				{


					perror("File not opened ");
					exit(1);

				}

bufferA=(int*)malloc(sizeof(int)*4*nRows);
bufferB=(int*)malloc(sizeof(int)*4*nCols);
bufferC=(int*)malloc(sizeof(int)*4*4);

			for(i=0;i<(nRows);i=i+4)
				{
					//printf(" %d \n ",i+1);
					for(j=0;j<(nCols);j=j+4)
						{
							//printf("\n ------------- i    and   j   vale is %d %d\n",i,j);
							//if(j%(chunk_size/4)==0)
									//{
										//MatrixC=map_matrix_chunk2("./MatrixC",2,i,j%(chunk_size/4));
										////memset(MatrixC,0,chunk_size);
										//index=0;
									//}
int indexA=0,indexB=0;
                                                            for(l=0;l<4;l++)
									for(k=0;k<(nCols*4/chunk_size);k++)
										{int a;
											MatrixA=map_matrix_chunk2("./MatrixA",1,i+l,k);
											MatrixB=map_matrix_chunk("./MatrixB",1,j+l,k);
											for(a=0;a<chunk_size/4;a++)
												{
												bufferA[indexA++]=MatrixA[a];
											        bufferB[indexB++]=MatrixB[a];
				
												}
											//MatrixC[index]+=chunk_by_chunk_multiply(k);
											munmap(MatrixB,chunk_size);
											munmap(MatrixA,chunk_size);
										}
											//index++;

									/*if((j+1)%(chunk_size/4)==0)
										{

                                                                                    	int loc=0,w,buf=0;
											for(loc=0;loc<chunk_size/4;loc++)
											{     buf=MatrixC[loc];
//printf("%d ",buf);
 												w=write(fd,&buf,4);
											}
										munmap(MatrixC,chunk_size);

										}*/

domult(bufferA,bufferB,bufferC);
						 }
                     		}

close(fd);
}
コード例 #4
0
ファイル: test.c プロジェクト: zouf/dynfixed
int main(int argc, char** argv)
{
  if( argc == 4 )
  {
    float a = atof(argv[2]);
    float b = atof(argv[3]);
    float resf;
    setPrecision(atoi(argv[1]));
    //allowDynamic = 0;
    printf("vals %f %f\n", a, b);
    fixed_point_t fa = convert_in(a);
    fixed_point_t fb = convert_in(b);
    fixed_point_t res;
    printfp(fa, "first");
    printfp(fb, "second");
    res = doadd(fa, fb);
    resf = convert_out(res);
    printf("add ours %f orig %Lf\n", resf, res.orig);

    res = dosub(fa, fb);
    resf = convert_out(res);
    printf("sub ours %f orig %Lf\n", (resf), res.orig);

    res = domult(fa, fb);
    resf = convert_out(res);
    printf("mult ours %f orig %Lf\n", (resf), res.orig);

    res = dofloor(fa);
    resf = convert_out(res);
    printf("floor ours %f orig %Lf\n", (resf), res.orig);

    return 0;
  }

  float l=3.0;//2.987654;
  float r = 2.01;
  int PRECISION = 30;
  setPrecision(PRECISION);
  fixed_point_t lc = convert_in(l);
  fixed_point_t rc= convert_in(r);
  allowDynamic = 1;
  int i;
  fixed_point_t res = doadd(lc,rc);
  long double check = l+r;
  for(i = 0; i < 2; i++)
  {
    check = check*check;
    res = domult(res,res);
  }
  res = dosub(res,convert_in(10.0));
  check -=10.0;
 // printf("Check against %f\n", check);
 // printfp(res,"restest");
 // printfp(lc,"L ");
  //printfp(rc,"R ");
//  long double lconv = convert_out(lc);
 // long double rconv = convert_out(rc);
//  printf("lconv is %Lf rconv is %Lf\n", lconv,rconv);
  long double result = convert_out(res);
  printf("Final val is %Lf. Should be %Lf\n", result,check);


  printf("\n Checking very small numbers times whole numbers\n");
  fixed_point_t small = convert_in(1e-12);
  fixed_point_t one = convert_in(1);
  fixed_point_t res2 = domult(small, one);

  printfp( small, "small");
  printfp( one, "one");
  printfp( res2, "res2");

return 0;
}