Esempio n. 1
0
File: util.c Progetto: arsane/mp4sdk
int
RandomLong(
   int from,
   int to
)
/*++
  Purpose:

    This function returns a random integer number between (and including) the starting and
    ending values passed by parameters from and to.

  Parameters:

    from - the starting value.

    to - the ending value.

  Return value:

    The generated random number.

--*/
{
   if (to <= from)
      return from;

   return from + lrand() / (INT_MAX / (to - from + 1));
}
Esempio n. 2
0
/**
   Get normal distributed random number.
*/
double randn(rand_t *rstat){
    double x=0;
    double U;
    unsigned int l;
    int i;
    while(1){
	U = 2*randu(rstat)-1;
	l = lrand(rstat);
	i = l & LEVELS_1;/*8 bit for addressing*/
	x = U*wtab[i];
	if(fabs(U)<ktab[i]) break;
	if(i==LEVELS_1){/*tail.*/
	    double x0, y;
	    do{
		x0=log(randu(rstat))*x11;
		y=-log(randu(rstat));
	    }while(y+y<x0*x0);
	    x=x>0?(x1-x0):(x0-x1);
	    break;
	}else{
	    double yy0, yy1, y;
	    yy0=ytab[i];
	    yy1=ytab[i+1];
	    y = yy1+(yy0-yy1)*randu(rstat);
	    if(y<ff(x)) break;
	}
    }
    return x;
}
Esempio n. 3
0
File: util.c Progetto: arsane/mp4sdk
float
RandomFloat(
   float from,
   float to
)
/*++
  Purpose:

    This function returns a random floating-point number between (and including) the starting
    and ending values passed by parameters from and to.

  Parameters:

    from - the starting value.

    to - the ending value.

  Return value:

    The generated random number.

--*/
{
   if (to <= from)
      return from;

   return from + (float)lrand() / (INT_MAX / (to - from));
}
Esempio n. 4
0
FLOAT
RandomFloat(
   FLOAT from,
   FLOAT to
)
/*++
  Purpose:

    This function returns a random floating-point number between (and including) the starting
    and ending values passed by parameters from and to.

  Parameters:

    from - the starting value.

    to - the ending value.

  Return value:

    The generated random number.

--*/
{
   if (to <= from)
      return from;

   return from + lrand() % (to - from + 1);
}
Esempio n. 5
0
nrand(int n)
{
	long slop, v;

	slop = MASK % n;
	do
		v = lrand();
	while(v <= slop);
	return v % n;
}
Esempio n. 6
0
static void test_ints(){
    rand_t init;
    seed_rand(&init,1);
    const int nwfs=6;
    lrand(&init);/*atm */
    rand_t wfs_rand[nwfs];
    for(int iwfs=0; iwfs<nwfs; iwfs++){
	seed_rand(wfs_rand+iwfs,lrand(&init));
    }
    dcell *mtche=dcellread("powfs0_mtche.bin");
    int nsim=500;
    int nsa=2582;
    dmat *nea=dnew(nsim,nsa*2);
    double(*pnea)[nsim]=(void*)nea->p;
    double rne=3;
    double bkgrnd=0;
    double siglev=1000;
    for(int iwfs=0; iwfs<nwfs; iwfs++){
	for(int isim=0; isim<nsim; isim++){
	    dbg("iwfs %d isim=%d\n",iwfs,isim);
	    /*dcell *ints=dcellread("ints_%d_wfs%d.bin",isim,iwfs); */
	    dcell *ints=dcellread("ints_%d_wfs%d.bin",isim,iwfs);
	    /*dcell *i0=dcellread("powfs0_i0.bin"); */
	    dmat *im=NULL, *imy=NULL;
	    double gnf[2], gny[2];

	    for(int isa=0; isa<nsa; isa++){
		dcp(&im,ints->p[isa]);
		dscale(im,siglev);
		gnf[0]=0; gnf[1]=0;
		dmulvec(gnf, mtche->p[isa], im->p,1.);
		gny[0]=0; gny[1]=0;
		dcp(&imy, im);
		addnoise(imy, &wfs_rand[iwfs], bkgrnd, bkgrnd, 0,0,rne);
		dmulvec(gny, mtche->p[isa], imy->p,1.);
		P(pnea,isim,isa)=gny[0]-gnf[0];
		P(pnea,isim,isa+nsa)=gny[1]-gnf[1];
	    }
	}
	writebin(nea,"test_sanea_wfs%d.bin",iwfs);
    }
}
Esempio n. 7
0
File: source.c Progetto: npe9/harvey
static uint32_t
tagGen(void)
{
    uint32_t tag;

    for(;;) {
        tag = lrand();
        if(tag >= UserTag)
            break;
    }
    return tag;
}
Esempio n. 8
0
static u32int
tagGen(void)
{
	u32int tag;

	for(;;){
		tag = lrand();
		if(tag > RootTag)
			break;
	}
	return tag;
}
Esempio n. 9
0
long
lnrand(long n)
{
	long slop, v;

	if(n < 0)
		return n;
	slop = MASK % n;
	do
		v = lrand();
	while(v <= slop);
	return v % n;
}
Esempio n. 10
0
int
nrand(int n)
{
	int32_t slop, v;

	if(n < 0)
		return n;
	if(n == 1)
		return 0;
	/* and if n == 0, you deserve what you get */
	slop = MASK % n;
	do
		v = lrand();
	while(v <= slop);
	return v % n;
}
Esempio n. 11
0
Biobuf*
opencopy(char *sender)
{
    int i;
    uint32_t h;
    char buf[512];
    Biobuf *b;

    h = 0;
    while(*sender)
        h = h*257 + *sender++;
    for(i = 0; i < 50; i++) {
        h += lrand();
        sprint(buf, "%s/%lud", copydir, h);
        b = sysopen(buf, "wlc", 0600);
        if(b)
            return b;
    }
    return 0;
}
Esempio n. 12
0
    void Tick(float fSeconds)
    {
        for(UINT i=0; i<bitmapImages.Num(); i++)
            bitmapImages[i]->Tick(fSeconds);

        if(bTransitioning && bitmapImages.Num() > 1)
        {
            if(bDisableFading)
                curFadeValue = fadeTime;
            else
                curFadeValue += fSeconds;

            if(curFadeValue >= fadeTime)
            {
                curFadeValue = 0.0f;
                bTransitioning = false;
                
                if(bRandomize)
                {
                    curTexture = nextTexture;
                    while((nextTexture = lrand(bitmapImages.Num())) == curTexture);
                }
                else
                {
                    if(++curTexture == bitmapImages.Num())
                        curTexture = 0;
                    
                    nextTexture = (curTexture == bitmapImages.Num()-1) ? 0 : curTexture+1;
                }
            }
        }

        curTransitionTime += fSeconds;
        if(curTransitionTime >= transitionTime)
        {
            curTransitionTime = 0.0f;

            curFadeValue = 0.0f;
            bTransitioning = true;
        }
    }
Esempio n. 13
0
    void Tick(float fSeconds)
    {
        if(bTransitioning && textures.Num() > 1)
        {
            if(bDisableFading)
                curFadeValue = fadeTime;
            else
                curFadeValue += fSeconds;

            if(curFadeValue >= fadeTime)
            {
                curFadeValue = 0.0f;
                bTransitioning = false;
                
                if(bRandomize)
                {
                    curTexture = nextTexture;
                    while((nextTexture = lrand(textures.Num())) == curTexture);
                }
                else
                {
                    if(++curTexture == textures.Num())
                        curTexture = 0;
                    
                    nextTexture = (curTexture == textures.Num()-1) ? 0 : curTexture+1;
                }
            }
        }

        curTransitionTime += fSeconds;
        if(curTransitionTime >= transitionTime)
        {
            curTransitionTime = 0.0f;;

            curFadeValue = 0.0f;
            bTransitioning = true;
        }
    }
Esempio n. 14
0
Biobuf*
opendump(char *sender)
{
    int i;
    uint32_t h;
    char buf[512];
    Biobuf *b;
    char *cp;

    cp = ctime(time(0));
    cp[7] = 0;
    cp[10] = 0;
    if(cp[8] == ' ')
        sprint(buf, "%s/queue.dump/%s%c", SPOOL, cp+4, cp[9]);
    else
        sprint(buf, "%s/queue.dump/%s%c%c", SPOOL, cp+4, cp[8], cp[9]);
    cp = buf+strlen(buf);
    if(access(buf, 0) < 0 && sysmkdir(buf, 0777) < 0) {
        syslog(0, "smtpd", "couldn't dump mail from %s: %r", sender);
        return 0;
    }

    h = 0;
    while(*sender)
        h = h*257 + *sender++;
    for(i = 0; i < 50; i++) {
        h += lrand();
        sprint(cp, "/%lud", h);
        b = sysopen(buf, "wlc", 0644);
        if(b) {
            if(vflag)
                fprint(2, "saving in %s\n", buf);
            return b;
        }
    }
    return 0;
}
Esempio n. 15
0
int rsize()
{
    int rv = 8 << (lrand() % 24);
    rv = lrand() & (rv-1);
    return rv;
}
Esempio n. 16
0
    void UpdateSettings()
    {
        for(UINT i=0; i<bitmapImages.Num(); i++)
            delete bitmapImages[i];
        bitmapImages.Clear();

        //------------------------------------

        bool bFirst = true;

        StringList bitmapList;
        data->GetStringList(TEXT("bitmap"), bitmapList);
        for(UINT i=0; i<bitmapList.Num(); i++)
        {
            String &strBitmap = bitmapList[i];
            if(strBitmap.IsEmpty())
            {
                AppWarning(TEXT("BitmapTransitionSource::UpdateSettings: Empty path"));
                continue;
            }

            BitmapImage *bitmapImage = new BitmapImage;
            bitmapImage->SetPath(strBitmap);
            bitmapImage->EnableFileMonitor(false);
            bitmapImage->Init();

            if(bFirst)
            {
                fullSize = bitmapImage->GetSize();
                baseAspect = double(fullSize.x)/double(fullSize.y);
                bFirst = false;
            }

            bitmapImages << bitmapImage;
        }

        //------------------------------------

        transitionTime = data->GetFloat(TEXT("transitionTime"));
        if(transitionTime < MIN_TRANSITION_TIME)
            transitionTime = MIN_TRANSITION_TIME;
        else if(transitionTime > MAX_TRANSITION_TIME)
            transitionTime = MAX_TRANSITION_TIME;

        //------------------------------------

        bFadeInOnly = data->GetInt(TEXT("fadeInOnly"), 1) != 0;
        bDisableFading = data->GetInt(TEXT("disableFading")) != 0;
        bRandomize = data->GetInt(TEXT("randomize")) != 0;

        //------------------------------------

        curTransitionTime = 0.0f;
        curTexture = 0;

        if(bRandomize)
        {
            srand( (unsigned)time( NULL ) );
            if(bitmapImages.Num() > 1)
            {
                curTexture = lrand(bitmapImages.Num());
                while((nextTexture = lrand(bitmapImages.Num())) == curTexture);
            }
        }
        else
            nextTexture = (curTexture == bitmapImages.Num()-1) ? 0 : curTexture+1;

        bTransitioning = false;
        curFadeValue = 0.0f;
    }
Esempio n. 17
0
File: rand.c Progetto: npe9/harvey
int
rand(void)
{
	return lrand()%(RAND_MAX+1);
}
Esempio n. 18
0
double
frand(void)
{

	return lrand() * FRACT;
}
Esempio n. 19
0
void
prng(uchar *p, int n)
{
    while(n-- > 0)
        *p++ = lrand();
}
Esempio n. 20
0
void maos_isim(int isim){
    const PARMS_T *parms=global->parms;
    RECON_T *recon=global->recon;
    SIM_T   *simu =global->simu;
    int iseed=global->iseed;
    int simstart=parms->sim.start;
    int simend=parms->sim.end;
    if(isim==simstart+1){//skip slow first step.
	tk_atm=myclockd();
    }
    if(isim+2+parms->sim.dtrat_hi>=simend){
	draw_single=0;
    }
    double ck_0=myclockd();
    simu->isim=isim;
    simu->status->isim=isim;
    sim_update_etf(simu);
    if(parms->atm.frozenflow){
#if USE_CUDA
	if(parms->gpu.evl || parms->gpu.wfs){
	    /*may need to copy another part */
	    gpu_atm2gpu(simu->atm, simu->atmscale, parms, iseed, isim);
	}
#endif
    }else{
	//Do not put this one inside parallel 
	genatm(simu);
	/*re-seed the atmosphere in case atm is loaded from shm/file */
	seed_rand(simu->atm_rand, lrand(simu->init_rand));
    }
    OMPTASK_SINGLE{
	if(parms->sim.dmproj){
	    /* teporarily disable FR.M so that Mfun is used.*/
	    cell *FRM=recon->FR.M; recon->FR.M=NULL; 
	    muv_solve(&simu->dmproj, &recon->FL, &recon->FR, NULL);
	    recon->FR.M=FRM;/*set FR.M back*/
	    if(parms->save.dm){
		zfarr_dcell(simu->save->dmproj, simu->isim, simu->dmproj);
	    }
	    if(!parms->fit.square){
		/* Embed DM commands to a square array for fast ray tracing */
		for(int idm=0; idm<parms->ndm; idm++){
		    loc_embed(simu->dmprojsq->p[idm], recon->aloc->p[idm], simu->dmproj->p[idm]->p);
		}
	    }
#if USE_CUDA
	    if(parms->gpu.evl || parms->gpu.wfs){
		gpu_dmproj2gpu(simu->dmprojsq);
	    }
#endif
	}
	save_dmreal(simu);
	extern int NO_RECON, NO_WFS, NO_EVL;
	if(PARALLEL){
	    /*
	      We do the big loop in parallel to make better use the
	      CPUs. Notice that the reconstructor is working on grad from
	      last time step so that there is no confliction in data access.
	    */
	    /*when we want to apply idealngs correction, wfsgrad need to wait for perfevl. */
	    long group=0;
	    if(parms->gpu.evl && !NO_EVL){
		//Queue tasks on GPU, no stream sync is done
		QUEUE_THREAD(group, simu->perf_evl_pre, 0);
	    }
	    if(!parms->tomo.ahst_idealngs && parms->gpu.wfs && !NO_WFS){
		//task for each wfs
		QUEUE_THREAD(group, simu->wfs_grad_pre, 0);
	    }
	    if(!NO_RECON){
		//don't put this first. It has cpu overhead in computing gradol
		QUEUE(group, reconstruct, simu, 1, 0);
	    }
	    if(!NO_EVL){
		if(parms->gpu.evl){
		    //wait for GPU tasks to be queued before calling sync
		    WAIT(group);
		}
		QUEUE(group, perfevl, simu, 1, 0);
	    }
	    if(!NO_WFS){
		if(parms->tomo.ahst_idealngs || (parms->gpu.wfs && !parms->gpu.evl)){
		    //in ahst_idealngs mode, weight for perfevl to finish.
		    //otherwise, wait for GPU tasks to be queued before calling sync
		    WAIT(group);
		}
		QUEUE(group, wfsgrad, simu, 1, 0);
	    }
	    if(!NO_RECON){
		//wait for all tasks to finish before modifying dmreal
		WAIT(group);
		shift_grad(simu);/*before filter() */
		filter_dm(simu);/*updates dmreal, so has to be after prefevl/wfsgrad is done. */
	    }
	    WAIT(group);
	}else{/*do the big loop in serial mode. */
	    if(parms->sim.closeloop){
		if(!NO_EVL) perfevl(simu);/*before wfsgrad so we can apply ideal NGS modes */
		if(!NO_WFS) wfsgrad(simu);/*output grads to gradcl, gradol */
		if(!NO_RECON) {
		    reconstruct(simu);/*uses grads from gradlast cl, gradlast ol. */
		    shift_grad(simu);
		    filter_dm(simu);
		}
	    }else{/*in OL mode,  */
		if(!NO_WFS) wfsgrad(simu);
		if(!NO_RECON) {
		    shift_grad(simu);
		    reconstruct(simu);
		    filter_dm(simu);
		}
		if(!NO_EVL) perfevl(simu);
	    }
	}
    }
    double ck_end=myclockd();
    long steps_done=iseed*(simend-simstart)+(isim+1-simstart);
    long steps_rest=parms->sim.nseed*(simend-simstart)-steps_done;
    if(isim!=simstart){
	simu->status->rest=(long)((ck_end-tk_0-(tk_atm-tk_1)*(iseed+1))/steps_done*steps_rest
				  +(tk_atm-tk_1)*(parms->sim.nseed-iseed-1));
	simu->status->mean=(ck_end-tk_atm)/(double)(isim-simstart);
    }
    simu->status->laps=(long)(ck_end-tk_0);
    simu->status->tot  =ck_end-ck_0;
    simu->status->wfs  =simu->tk_wfs;
    simu->status->recon=simu->tk_recon;
    simu->status->other=simu->tk_cache;
    simu->status->eval =simu->tk_eval;
    simu->status->scale=1;
    if(simu->timing){
	simu->timing->p[isim*simu->timing->nx]=get_job_mem();
	simu->timing->p[isim*simu->timing->nx+1]=simu->status->tot;
	simu->timing->p[isim*simu->timing->nx+2]=simu->status->wfs;
	simu->timing->p[isim*simu->timing->nx+3]=simu->status->recon;
	simu->timing->p[isim*simu->timing->nx+4]=simu->status->eval;
    }
    double this_time=myclockd();
    if(this_time>simu->last_report_time+1 || isim+1==simend || parms->sim.pause){
	/*we don't print out or report too frequently. */
	simu->last_report_time=this_time;
#if defined(__linux__) || defined(__APPLE__)
	scheduler_report(simu->status);
#endif
	print_progress(simu);
    }
}
Esempio n. 21
0
File: rand.c Progetto: npe9/harvey
int
rand(void)
{

	return lrand() & 0x7fff;
}
Esempio n. 22
0
    void UpdateSettings()
    {
        for(UINT i=0; i<textures.Num(); i++)
            delete textures[i];
        textures.Clear();

        //------------------------------------

        bool bFirst = true;

        StringList bitmapList;
        data->GetStringList(TEXT("bitmap"), bitmapList);
        for(UINT i=0; i<bitmapList.Num(); i++)
        {
            String &strBitmap = bitmapList[i];
            if(strBitmap.IsEmpty())
            {
                AppWarning(TEXT("BitmapTransitionSource::UpdateSettings: Empty path"));
                continue;
            }

            Texture *texture = GS->CreateTextureFromFile(strBitmap, TRUE);
            if(!texture)
            {
                AppWarning(TEXT("BitmapTransitionSource::UpdateSettings: could not create texture '%s'"), strBitmap.Array());
                continue;
            }

            if(bFirst)
            {
                fullSize.x = float(texture->Width());
                fullSize.y = float(texture->Height());
                baseAspect = double(fullSize.x)/double(fullSize.y);
                bFirst = false;
            }

            textures << texture;
        }

        if(textures.Num() == 0)
            CreateErrorTexture();

        //------------------------------------

        transitionTime = data->GetFloat(TEXT("transitionTime"));
        if(transitionTime < MIN_TRANSITION_TIME)
            transitionTime = MIN_TRANSITION_TIME;
        else if(transitionTime > MAX_TRANSITION_TIME)
            transitionTime = MAX_TRANSITION_TIME;

        //------------------------------------

        bFadeInOnly = data->GetInt(TEXT("fadeInOnly")) != 0;
        bDisableFading = data->GetInt(TEXT("disableFading")) != 0;
        bRandomize = data->GetInt(TEXT("randomize")) != 0;

        //------------------------------------

        curTransitionTime = 0.0f;
        curTexture = 0;

        if(bRandomize)
        {
            srand( (unsigned)time( NULL ) );
            if(textures.Num() > 1)
            {
                curTexture = lrand(textures.Num());
                while((nextTexture = lrand(textures.Num())) == curTexture);
            }
        }
        else
            nextTexture = (curTexture == textures.Num()-1) ? 0 : curTexture+1;

        bTransitioning = false;
        curFadeValue = 0.0f;
    }