Ejemplo n.º 1
0
Archivo: buffer.c Proyecto: 4ad/sam
void
Binsert(Buffer *b, String *s, Posn p0)
{
	if(b->c2>b->disc->nrunes || b->c1>b->disc->nrunes)
		panic("binsert cache");
	if(p0<0)
		panic("Binsert p0<0");
	if(s->n == 0)
		return;
	if(incache(b, p0, p0) && b->cache.n+s->n<=STRSIZE){
		Strinsert(&b->cache, s, p0-b->c1);
		b->dirty = TRUE;
		if(b->cache.n > BLOCKSIZE*2){
			b->nrunes += s->n;
			Bflush(b);
			/* try to leave some cache around p0 */
			if(p0 >= b->c1+BLOCKSIZE){
				/* first BLOCKSIZE can go */
				Strdelete(&b->cache, 0, BLOCKSIZE);
				b->c1 += BLOCKSIZE;
			}else if(p0 <= b->c2-BLOCKSIZE){
				/* last BLOCKSIZE can go */
				b->cache.n -= BLOCKSIZE;
				b->c2 -= BLOCKSIZE;
			}else{
				/* too hard; negate the cache and pick up next time */
				Strzero(&b->cache);
				b->c1 = b->c2 = 0;
			}
			return;
		}
	}else{
		Bflush(b);
		if(s->n >= BLOCKSIZE/2){
			b->cache.n = 0;
			b->c1 = b->c2 = 0;
			Dinsert(b->disc, s->s, s->n, p0);
		}else{
			int m;
			Posn minp;
			if(b->nrunes-p0 > BLOCKSIZE/2)
				m = BLOCKSIZE/2;
			else
				m = b->nrunes-p0;
			minp = p0-BLOCKSIZE/2;
			if(minp < 0)
				minp = 0;
			m += p0-minp;
			Strinsure(&b->cache, m);
			if(Dread(b->disc, b->cache.s, m, minp)!=m)
				panic("Bread");
			b->cache.n = m;
			b->c1 = minp;
			b->c2 = minp+m;
			Strinsert(&b->cache, s, p0-b->c1);
			b->dirty = TRUE;
		}
	}
	b->nrunes += s->n;
}
Ejemplo n.º 2
0
Archivo: buffer.c Proyecto: 4ad/sam
int
Bread(Buffer *b, Rune *addr, int n, Posn p0)
{
	int m;

	if(b->c2>b->disc->nrunes || b->c1>b->disc->nrunes)
		panic("bread cache");
	if(p0 < 0)
		panic("Bread p0<0");
	if(p0+n > b->nrunes){
		n = b->nrunes-p0;
		if(n < 0)
			panic("Bread<0");
	}
	if(!incache(b, p0, p0+n)){
		Bflush(b);
		if(n>=BLOCKSIZE/2)
			return Dread(b->disc, addr, n, p0);
		else{
			Posn minp;
			if(b->nrunes-p0>BLOCKSIZE/2)
				m = BLOCKSIZE/2;
			else
				m = b->nrunes-p0;
			if(m<n)
				m = n;
			minp = p0-BLOCKSIZE/2;
			if(minp<0)
				minp = 0;
			m += p0-minp;
			Strinsure(&b->cache, m);
			if(Dread(b->disc, b->cache.s, m, minp)!=m)
				panic("Bread");
			b->cache.n = m;
			b->c1 = minp;
			b->c2 = minp+m;
			b->dirty = FALSE;
		}
	}
	memmove(addr, &b->cache.s[p0-b->c1], n*RUNESIZE);
	return n;
}
 static int
process(Engine *ep, char *parname, char *resname)
{
	FILE *f;
	FileInfo fi;
	char *s;
	int *asv, asvkind[3], i, j, k, m1, n1, n2, nd, *p, rc;
	mxArray *X;
	real *x;

	rc = 1;
        if (!strcmp(parname,"setup") && 
            !strcmp(resname,"workspace"))
           { 
             printf("pre-processing with setupworkspace.m\n");
             fflush(stdout);
             engEvalString(ep, "setupworkspace");
	     return 0;
           }
	if (!(fi.f = fopen(parname,"r"))) {
		BadOpen("parameters_file", parname);
		goto done1;
		}
	fi.fname = s;
	fi.lineno = 0;

	n1 = Iread(&fi, "variables");
	X = mxCreateDoubleMatrix(1, n1, mxREAL);
	x = (real*)mxGetPr(X);

	for(i = 0; i < n1; i++)
		x[i] = Dread(&fi);

	m1 = Iread(&fi, "functions");

	asvkind[0] = asvkind[1] = asvkind[2] = 0;
	asv = (int*)Malloc((n1+m1)*sizeof(int));
	p = asv + m1;
	for(i = 0; i < m1; i++) {
		asv[i] = j = Iread(&fi,0);
		for(k = 0; k < 3 && j; k++, j >>= 1)
			if (j & 1)
				++asvkind[k];
		}
	nd = Iread(&fi, "derivative_variables");
	for(i = 0; i < nd; i++)
		p[i] = Iread(&fi, 0) - 1;
	fclose(fi.f);
	f = fopen(resname, "w");
	if (!f) {
		BadOpen("results_file", resname);
		mxDestroyArray(X);
		goto done1;
		}
	engPutVariable(ep, "x", X);
	mxDestroyArray(X);

	X = mxCreateDoubleMatrix(1, 3, mxREAL);
	x = (real*)mxGetPr(X);
	for(i = 0; i < 3; i++)
		x[i] = asvkind[i];
	engPutVariable(ep, "asv", X);
	mxDestroyArray(X);

	if (asvkind[0]) {
		engEvalString(ep, "eval(FunctionEvaluation);");
		//engEvalString(ep, "y = f(x);");
		X = engGetVariable(ep,"y");
		if (!X) {
			Squawk("MATLAB didn't return y = f(x)\n");
			goto done;
			}
		x = (real*)mxGetPr(X);
		for(i = 0; i < m1; i++)
			if (asv[i] & 1)
				fprintf(f, " %.17g\n", x[i]);
		mxDestroyArray(X);
		}
	if (asvkind[1]) {
		engEvalString(ep, "eval(GradientEvaluation);");
		//engEvalString(ep, "yp = fp(x);");
		X = engGetVariable(ep,"yp");
		if (!X) {
			Squawk("MATLAB didn't return yp = fp(x)\n");
			goto done;
			}
		x = (real*)mxGetPr(X);
		for(i = 0; i < m1; i++, x += n1)
			if (asv[i] & 2)
				grdout(f, n1, p, x);
		mxDestroyArray(X);
		}
	if (asvkind[2]) {
		engEvalString(ep, "eval(HessianEvaluation);");
		//engEvalString(ep, "ypp = fpp(x);");
		X = engGetVariable(ep,"ypp");
		if (!X) {
			Squawk("MATLAB didn't return ypp = fpp(x)\n");
			goto done;
			}
		x = (real*)mxGetPr(X);
		n2 = n1*n1;
		for(i = 0; i < m1; i++, x += n2)
			if (asv[i] & 4)
				hesout(f, n1, p, x);
		mxDestroyArray(X);
		}
	rc = 0;
 done:
	fclose(f);
 done1:
	free(asv);
	return rc;
	}