コード例 #1
0
ファイル: sparse.cpp プロジェクト: 4Liamk/KFusion
SparseMatrix * loadSparseMatrix(char * filename, int pad)
{
	char input[128];
	strcpy(input,filename);
	strcat(input,".bin");
	printf("loading binary: %s\n", input);
	SparseMatrix * m = bmpaddedread(input,pad);
	if(m == NULL)
	{
		strcpy(input,filename);
		strcat(input,".txt");
		printf("binary not found, loading text and then saving as binary for next time: %s\n",input);
		m = mread(input);
		strcphttps://mail.google.com/mail/u/0/?ui=2&shva=1#inboxy(input,filename);
		strcat(input,".bin");
		bmwrite(input,m);
	}
	assert(m);
	int i;
	for(i = 0; i < 10; i++)
	{
		printf("%d, %e, %d\n",m->cpu_cols[i],m->cpu_vals[i],m->cpu_index[i]);
	}
	int result;
	//make the gpu side data structure
	m->gpu_cols = clCreateBuffer(context,CL_MEM_READ_WRITE, sizeof(int)*m->colsLength,NULL,&result);
	m->gpu_vals = clCreateBuffer(context,CL_MEM_READ_WRITE, sizeof(TYPE)*m->colsLength,NULL,&result);
	m->gpu_index = clCreateBuffer(context,CL_MEM_READ_WRITE, sizeof(int)*m->indexLength,NULL,&result);
	m->locality = 0;
	return m;
}
コード例 #2
0
ファイル: su3d2par.c プロジェクト: JOravetz/SeisUnix
int
main(int argc, char **argv)
{
	
	float **data;
        char **hdrdata;
	float ***workm;
	
        int ival2;              /* int value of key2                */
        int ival3;              /* int value of key3                */
        Value val2;      	/* ... its value                        */
        Value val3;      	/* ... its value                        */
	int index2;
	int index3;
	
	int n1,n2,n3;
	int i1,i2,i3;
        char *key2=NULL;      /* header key word from segy.h    */
        char *type2=NULL;     /* ... its type                   */
        char *key3=NULL;      /* header key word from segy.h    */
        char *type3=NULL;     /* ... its type                   */
	
	int lins;
	int line;
	int nl;
	int dir=2;
	int il;
	int su;
	float cdp;
	float t;
	float dt;
	
        /* Initialize */
        initargs(argc, argv);
        requestdoc(1);
       	
       MUSTGETPARINT("n2", &n2);
       MUSTGETPARINT("n3", &n3);
       
       /* get key2*/
        if (!getparstring("key2", &key2))  key2 = "fldr"; 
        type2 = hdtype(key2);
        index2 = getindex(key2);
	
       /* get key3*/
        if (!getparstring("key3", &key3))  key3 = "tracf"; 
        type3 = hdtype(key3);
        index3 = getindex(key3);

        if (!getparint("lins", &lins))  lins = 1;
	if(lins<1) err(" lins must be larger than 0");
        if (!getparint("line", &line)) line = n2; 
         if (!getparint("su", &su)) su = 1; 
      
	/* Get info from first trace */
        if (!gettr(&tr))  err ("can't get first trace");
        n1 = tr.ns;
	
	if (!getparfloat("dt", &dt))	dt = ((float) tr.dt)/1000000.0;
	if (!dt) {
		dt = .01;
		warn("dt not set, assumed to be .01");
	}



	data = bmalloc(n1*sizeof(float),n2,n3);
		
	hdrdata = bmalloc(HDRBYTES,n2,n3);
	
        do {
		gethval(&tr, index2, &val2);
		ival2 = vtoi(type2,val2);
		gethval(&tr, index3, &val3);
		ival3 = vtoi(type3,val3);

		if(ival2>n2 || ival2<0 ) err(" Array in dimension 2 out of bound\n");
		if(ival3>n3 || ival3<0 ) err(" Array in dimension 3 out of bound\n");
		
		
      		bmwrite(data,1,ival2-1,ival3-1,1,tr.data);
		bmwrite(hdrdata,1,ival2-1,ival3-1,1,&tr);
		
	} while (gettr(&tr));
	
	
	
	nl=line-lins+1;
	if(dir==2) {
		/* n2 direction */
		workm = alloc3float(n1,n3,nl);
		for(il=lins-1;il<nl;il++) {
			for(i3=0;i3<n3;i3++) bmread(data,1,lins+il-1,i3,1,workm[il][i3]);
		}
		
		if(su==1) {	
			fprintf(stdout,"cdp=");
			for(il=lins-1;il<nl;il++) {
				for(i3=0;i3<n3;i3++) fprintf(stdout,"%d,",(lins+il)*1000+i3+1);
			}
		
			for(il=lins-1;il<nl;il++) {
				for(i3=0;i3<n3;i3++){
					fprintf(stdout,"\ntnmo=");
					for(i1=0;i1<n1;i1++) fprintf(stdout,"%.3f,",dt*i1);
					fprintf(stdout,"\nvnmo=");
					for(i1=0;i1<n1;i1++)
						fprintf(stdout,"%.3f,",workm[il][i3][i1]);
				}
		
			}
		} else {
			for(il=lins-1;il<nl;il++) {
				for(i3=0;i3<n3;i3++) {
					 cdp=(lins+il)*1000+i3+1;
					 fwrite(&cdp,sizeof(float),1,stdout);
				}
			}
		
			for(il=lins-1;il<nl;il++) {
				for(i3=0;i3<n3;i3++){
					for(i1=0;i1<n1;i1++) {
						t=tr.d1*i1;
						fwrite(&t,sizeof(float),1,stdout);
					}
					for(i1=0;i1<n1;i1++)
						fwrite(&workm[il][i3][i1],sizeof(float),1,stdout);
				}
		
			}
		}
			free3float(workm);	
	}
	
	bmfree(data);
	bmfree(hdrdata);	
	return EXIT_SUCCESS;
}
コード例 #3
0
ファイル: su3dkmig_scale.c プロジェクト: JOravetz/SeisUnix
int main( int argc, char *argv[] )
{
        int ntr=0;                /* number of traces                     */
        int ntrv=0;               /* number of traces                     */
	int ns=0;
	int nsv=0;
	float dt;
	float dtv;
	
	cwp_String fs;
	cwp_String fv;
	FILE *fps;
	FILE *fpv;
	FILE *headerfp;
		
	float *data;		/* data matrix of the migration volume */
	float *vel;		/* velocity matrix */
	float *velfi;		/* velocity function interpolated to ns values*/
	float *velf;		/* velocity function */
	float *vdt;
	float *ddt;
	float *ap;		/* array of apperture values in m */
	float apr;		/* array of apperture values in m */
	int *apt=NULL;		/* array of apperture time limits in mig. gath*/
	float   r;		/* maximum radius with a given apperture */
	float ir2;		/* r/d2 */
	float ir3;		/* r/d3 */
	float d2;		/* spatial sampling int. in dir 2. */
	float d3;		/* spatial sampling int. in dir 3. */
	float **mgd=NULL;	/* migration gather data */
	float *migt;		/* migrated data trace */
	int **mgdnz=NULL;		/* migration gather data non zero samples*/
	float dm;		/* migration gather spatial sample int. */
	int im;			/* number of traces in migration gather */
	int *mtnz;		/* migrated trace data non zero smaples */
	char **dummyi;		/* index array that the trace contains zeros only */
	float fac;		/* velocity scale factor */
	int sphr;		/* spherical divergence flag */
	int imt;		/* mute time sample of trace */
	float tmp;
	int imoff;
	int **igtr=NULL;
	int nigtr;
	int n2;
	int n3;

	int verbose;
	
	/* phase shift filter stuff */
        float power;            /* power of i omega applied to data     */
        float amp;              /* amplitude associated with the power  */
        float arg;              /* argument of power                    */
        float phasefac;         /* phase factor                         */
        float phase;            /* phase shift = phasefac*PI            */
        complex exparg;         /* cexp(I arg)                          */
        register float *rt;     /* real trace                           */
        register complex *ct;   /* complex transformed trace            */
        complex *filt;          /* complex power                        */
        float omega;            /* circular frequency                   */
        float domega;           /* circular frequency spacing (from dt) */
        float sign;             /* sign in front of i*omega default -1  */
        int nfft;               /* number of points in nfft             */
        int nf;                 /* number of frequencies (incl Nyq)     */
        float onfft;            /* 1 / nfft                             */
        size_t nzeros;          /* number of padded zeroes in bytes     */
	
	initargs(argc, argv);
   	requestdoc(1);
	
        MUSTGETPARSTRING("fs",&fs);
        MUSTGETPARSTRING("fv",&fv);
        MUSTGETPARINT("n2",&n2);
        MUSTGETPARINT("n3",&n3);
        MUSTGETPARFLOAT("d2",&d2);
        MUSTGETPARFLOAT("d3",&d3);
	
	if (!getparfloat("dm", &dm))	dm=(d2+d3)/2.0;
	
	/* open datafile */
        fps = efopen(fs,"r");
	fpv = efopen(fv,"r");
	
	/* Open tmpfile for headers */
	headerfp = etmpfile();

	/* get information from the first data trace */
	ntr = fgettra(fps,&tr,0);
	if(n2*n3!=ntr) err(" Number of traces in file %d not equal to n2*n3 %d \n",
			     ntr,n2*n3);
	ns=tr.ns;
	if (!getparfloat("dt", &dt))	dt = ((float) tr.dt)/1000000.0;
	if (!dt) {
		dt = .002;
		warn("dt not set, assumed to be .002");
	}

	/* get information from the first velocity trace */
	ntrv = fgettra(fpv,&trv,0);
	if(ntrv!=ntr) err(" Number of traces in velocity file %d differ from %d \n",
			     ntrv,ntr);
	nsv=trv.ns;
	if (!getparfloat("dtv", &dtv))	dtv = ((float) trv.dt)/1000000.0;
	if (!dtv) {
		dtv = .002;
		warn("dtv not set, assumed to be .002 for velocity");
	}
	
	if (!getparfloat("fac", &fac))	fac=2.0;
	if (!getparint("verbose", &verbose))	verbose=0;
	if (!getparint("sphr", &sphr))	sphr=0;
	
	if (!getparfloat("apr", &apr))	apr=75;
	apr*=3.141592653/180;

	/* allocate arrays */
	data = bmalloc(sizeof(float),ns,ntr);
	vel = bmalloc(sizeof(float),nsv,ntr);
	velf = ealloc1float(nsv); 
	velfi = ealloc1float(ns);
	migt = ealloc1float(ns);
	vdt = ealloc1float(nsv);
	ddt = ealloc1float(ns);
	ap = ealloc1float(ns);
	mtnz = ealloc1int(ns);
	dummyi = (char **) ealloc2(n2,n3,sizeof(char));
	
	/* Times to do interpolation of velocity from sparse sampling */
	/* to fine sampling of the data */
	{ register int it;
		for(it=0;it<nsv;it++) vdt[it]=it*dtv;
		for(it=0;it<ns;it++)  ddt[it]=it*dt;
	}
	
	/* Read traces into data */
        /* Store headers in tmpfile */
        ntr=0;
	erewind(fps);
	erewind(fpv);
		
	{ register int i2,i3;
	for(i3=0;i3<n3;i3++) 
		for(i2=0;i2<n2;i2++) {
			fgettr(fps,&tr);
			fgettr(fpv,&trv);
			if(tr.trid > 2) dummyi[i3][i2]=1;
			else dummyi[i3][i2]=0;	
			efwrite(&tr, 1, HDRBYTES, headerfp);
		 	bmwrite(data,1,0,i3*n2+i2,ns,tr.data);
		 	bmwrite(vel,1,0,i3*n2+i2,nsv,trv.data);
		}
	erewind(headerfp);

	/* set up the phase filter */
	power = 1.0;sign = 1.0;phasefac = 0.5;
	phase = phasefac * PI;
         
	/* Set up for fft */
        nfft = npfaro(ns, LOOKFAC * ns);
        if (nfft >= SU_NFLTS || nfft >= PFA_MAX)
                err("Padded nt=%d -- too big", nfft);

        nf = nfft/2 + 1;
        onfft = 1.0 / nfft;
        nzeros = (nfft - ns) * FSIZE;
        domega = TWOPI * onfft / dt;
        
	/* Allocate fft arrays */
        rt   = ealloc1float(nfft);
        ct   = ealloc1complex(nf);
        filt = ealloc1complex(nf);
        
	/* Set up args for complex power evaluation */
        arg = sign * PIBY2 * power + phase;
        exparg = cexp(crmul(I, arg));
        {       
		register int i;
                for (i = 0 ; i < nf; ++i) {

                        omega = i * domega;
		
		        /* kludge to handle omega=0 case for power < 0 */
                        if (power < 0 && i == 0) omega = FLT_MAX;

                        /* calculate filter */
                        amp = pow(omega, power) * onfft;
			filt[i] = crmul(exparg, amp);
                }
        }
	
	/* set up constants for migration */ 
	if(verbose) fprintf(stderr," Setting up constants....\n");
	r=0;
	for(i3=0;i3<n3;i3++) 
	    for(i2=0;i2<n2;i2++) {
		if(dummyi[i3][i2] < 1) {
			
			/* get the velocity function */
			bmread(vel,1,0,i3*n2+i2,nsv,velf);
			
			/* linear interpolation from nsv to ns values */  
			intlin(nsv,vdt,velf,velf[0],velf[nsv-1],ns,ddt,velfi);
			
			/* Apply scale factor to velocity */
			{ register int it;
				for(it=0;it<ns;it++) velfi[it] *=fac;
			}
			
			/* compute maximum radius from apperture and velocity */
			{ register int it;
				for(it=0;it<ns;it++) 
				ap[it] = ddt[it]*velfi[it]*tan(apr)/2.0;
			}
			tmp = ap[isamax(ns,ap,1)];
			if(tmp>r) r=tmp;
		}
	}
	r=MIN(r,sqrt(SQR((n2-1)*d2)+SQR((n3-1)*d3)));
	ir2 =  (int)(2*r/d2)+1;
	ir3 =  (int)(2*r/d3)+1;
	im = (int)(r/dm)+1;
		
	/*  allocate migration gather */
	mgd = ealloc2float(ns,im);
	mgdnz = ealloc2int(ns,im);
	apt = ealloc1int(im);
	/* set up the stencil for selecting traces */
	igtr = ealloc2int(ir2*ir3,2);
	stncl(r, d2, d3,igtr,&nigtr);
	
	if(verbose) {
		fprintf(stderr," Maximum radius %f\n",r);
		fprintf(stderr," Maximum offset %f\n",
			sqrt(SQR((n2-1)*d2)+SQR((n3-1)*d3)));
	}

	/* main processing loop */
	for(i3=0;i3<n3;i3++) 
	    for(i2=0;i2<n2;i2++) {
		memset( (void *) tr.data, (int) '\0',ns*FSIZE);
		if(dummyi[i3][i2] < 1) {
			memset( (void *) mgd[0], (int) '\0',ns*im*FSIZE);
			memset( (void *) mgdnz[0], (int) '\0',ns*im*ISIZE);
			/* get the velocity function */
			bmread(vel,1,0,i3*n2+i2,nsv,velf);
		
			/* linear interpolation from nsv to ns values */  
			intlin(nsv,vdt,velf,velf[0],velf[nsv-1],ns,ddt,velfi);
		
			/* Apply scale factor to velocity */
			{ register int it;
				for(it=0;it<ns;it++) velfi[it] *=fac;
			}

			/* create the migration gather */
			{ register int itr,ist2,ist3;
				for(itr=0;itr<nigtr;itr++) {
					ist2=i2+igtr[0][itr];
					ist3=i3+igtr[1][itr];
					if(ist2 >= 0 && ist2 <n2) 
						if(ist3 >= 0 && ist3 <n3) {
							if(dummyi[ist3][ist2] <1) {
								imoff = (int) ( 
								sqrt(SQR(igtr[0][itr]*d2)
							     	    +SQR(igtr[1][itr]*d3))/dm+0.5);
								bmread(data,1,0,ist3*n2+ist2,ns,tr.data);
								imoff=MIN(imoff,im-1);
								{ register int it;									
									/* get the mute time for this 
									  offset, apperture and velocity */
									xindex(ns,ap,imoff*dm,&imt);
									for(it=imt;it<ns;it++)
										if(tr.data[it]!=0) {
											mgd[imoff][it]+=tr.data[it];
											mgdnz[imoff][it]+=1;
									}	
								}
							}
						}
				}
			}

			/* normalize the gather */
				{ register int ix,it;
				for(ix=0;ix<im;ix++)
					for(it=0;it<ns;it++) 
						if(mgdnz[ix][it] > 1) mgd[ix][it] /=(float) mgdnz[ix][it];
			}
			memset( (void *) tr.data, (int) '\0',ns*FSIZE);
			memset( (void *) mtnz, (int) '\0',ns*ISIZE);
		
			/* do a knmo */
			{ register int ix,it;
				for(ix=0;ix<im;ix++) {
					/* get the mute time for this 
					offset, apperture and velocity */
					xindex(ns,ap,ix*dm,&imt);
					knmo(mgd[ix],migt,ns,velfi,0,ix*dm,dt,imt,sphr);
					/* stack the gather */
						for(it=0;it<ns;it++) { 
						if(migt[it]!=0.0) { 
								tr.data[it] += migt[it];
								mtnz[it]++;
						}
/*						tr.data[it] += mgd[ix][it]; */
					}
				}

			}
			{ register int it;
				for(it=0;it<ns;it++) 
					if(mtnz[it]>1) tr.data[it] /=(float)mtnz[it];
			}
		
			/*Do the phase filtering before the trace is released*/
                	/* Load trace into rt (zero-padded) */
               		memcpy( (void *) rt, (const void *) tr.data, ns*FSIZE);
               		memset((void *) (rt + ns), (int) '\0', nzeros);

         		pfarc(1, nfft, rt, ct);
        		{ register int i;
        			for (i = 0; i < nf; ++i)  ct[i] = cmul(ct[i], filt[i]);
        		}
         		pfacr(-1, nfft, ct, rt);
     			memcpy( (void *) tr.data, (const void *) rt, ns*FSIZE);
			
		} /* end of dummy if */
		/* spit out the gather */
		efread(&tr, 1, HDRBYTES, headerfp);
		puttr(&tr);
		if(verbose) fprintf(stderr," %d %d\n",i2,i3);
	    }   /* end of i2 loop */
	}	/* end of i3 loop */
	/* This should be the last thing */
	efclose(headerfp);
	/* Free memory */
	free2int(igtr);
	free2float(mgd);
	free2int(mgdnz);
	free1int(apt);
	bmfree(data);
	bmfree(vel);
	free1float(velfi);
	free1float(velf);
	free1float(ddt);
	free1float(vdt);
	free1float(ap);
	free1int(mtnz);
	free1float(migt);
	free1float(rt);
	free1complex(ct);
	free1complex(filt);
	free2((void **) dummyi);
	
	return EXIT_SUCCESS;
}