Exemplo n.º 1
0
static void
umbscan(void)
{
	uchar o[2], *p;

	/*
	 * Scan the Upper Memory Blocks (0xA0000->0xF0000) for pieces
	 * which aren't used; they can be used later for devices which
	 * want to allocate some virtual address space.
	 * Check for two things:
	 * 1) device BIOS ROM. This should start with a two-byte header
	 *    of 0x55 0xAA, followed by a byte giving the size of the ROM
	 *    in 512-byte chunks. These ROM's must start on a 2KB boundary.
	 * 2) device memory. This is read-write.
	 * There are some assumptions: there's VGA memory at 0xA0000 and
	 * the VGA BIOS ROM is at 0xC0000. Also, if there's no ROM signature
	 * at 0xE0000 then the whole 64KB up to 0xF0000 is theoretically up
	 * for grabs; check anyway.
	 */
	p = KADDR(0xD0000);
	while(p < (uchar*)KADDR(0xE0000)){
		/*
		 * Check for the ROM signature, skip if valid.
		 */
		if(p[0] == 0x55 && p[1] == 0xAA){
			p += p[2]*512;
			continue;
		}

		/*
		 * Is it writeable? If yes, then stick it in
		 * the UMB device memory map. A floating bus will
		 * return 0xff, so add that to the map of the
		 * UMB space available for allocation.
		 * If it is neither of those, ignore it.
		 */
		o[0] = p[0];
		p[0] = 0xCC;
		o[1] = p[2*KB-1];
		p[2*KB-1] = 0xCC;
		if(p[0] == 0xCC && p[2*KB-1] == 0xCC){
			p[0] = o[0];
			p[2*KB-1] = o[1];
			mapfree(&rmapumbrw, PADDR(p), 2*KB);
		}
		else if(p[0] == 0xFF && p[1] == 0xFF)
			mapfree(&rmapumb, PADDR(p), 2*KB);
		p += 2*KB;
	}

	p = KADDR(0xE0000);
	if(p[0] != 0x55 || p[1] != 0xAA){
		p[0] = 0xCC;
		p[64*KB-1] = 0xCC;
		if(p[0] != 0xCC && p[64*KB-1] != 0xCC)
			mapfree(&rmapumb, PADDR(p), 64*KB);
	}

	umbexclude();
}
Exemplo n.º 2
0
static void
lowraminit(void)
{
	ulong n, pa, x;
	uchar *bda;

	/*
	 * Initialise the memory bank information for conventional memory
	 * (i.e. less than 640KB). The base is the first location after the
	 * bootstrap processor MMU information and the limit is obtained from
	 * the BIOS data area.
	 */
	x = PADDR(CPU0END);
	bda = (uchar*)KADDR(0x400);
	n = ((bda[0x14]<<8)|bda[0x13])*KB-x;
	mapfree(&rmapram, x, n);
	memset(KADDR(x), 0, n);			/* keep us honest */

	x = PADDR(PGROUND((ulong)end));
	pa = MemMin;
	if(x > pa)
		panic("kernel too big");
	mapfree(&rmapram, x, pa-x);
	memset(KADDR(x), 0, pa-x);		/* keep us honest */
}
Exemplo n.º 3
0
static void
umbscan(void)
{
	uchar *p;

	/*
	 * Scan the Upper Memory Blocks (0xA0000->0xF0000) for pieces
	 * which aren't used; they can be used later for devices which
	 * want to allocate some virtual address space.
	 * Check for two things:
	 * 1) device BIOS ROM. This should start with a two-byte header
	 *    of 0x55 0xAA, followed by a byte giving the size of the ROM
	 *    in 512-byte chunks. These ROM's must start on a 2KiB boundary.
	 * 2) device memory. This is read-write.
	 * There are some assumptions: there's VGA memory at 0xA0000 and
	 * the VGA BIOS ROM is at 0xC0000. Also, if there's no ROM signature
	 * at 0xE0000 then the whole 64KiB up to 0xF0000 is theoretically up
	 * for grabs; check anyway.
	 */
	p = KADDR(0xD0000);	/*RSC: changed from 0xC0000 */
	while(p < (uchar*)KADDR(0xE0000)){
		if (p[0] == 0x55 && p[1] == 0xAA) {
			/* Skip p[2] chunks of 512 bytes.  Test for 0x55 AA before
			     poking obtrusively, or else the Thinkpad X20 dies when
			     setting up the cardbus (PB) */
			p += p[2] * 512;
			continue;
		}

		p[0] = 0xCC;
		p[2*KiB-1] = 0xCC;
		if(p[0] != 0xCC || p[2*KiB-1] != 0xCC){
			p[0] = 0x55;
			p[1] = 0xAA;
			p[2] = 4;
			if(p[0] == 0x55 && p[1] == 0xAA){
				p += p[2]*512;
				continue;
			}
			if(p[0] == 0xFF && p[1] == 0xFF)
				mapfree(&rmapumb, PADDR(p), 2*KiB);
		}
		else
			mapfree(&rmapumbrw, PADDR(p), 2*KiB);
		p += 2*KiB;
	}

	p = KADDR(0xE0000);
	if(p[0] != 0x55 || p[1] != 0xAA){
		p[0] = 0xCC;
		p[64*KiB-1] = 0xCC;
		if(p[0] != 0xCC && p[64*KiB-1] != 0xCC)
			mapfree(&rmapumb, PADDR(p), 64*KiB);
	}
}
Exemplo n.º 4
0
Arquivo: cpm.c Projeto: 8l/inferno
void
cpminit(void)
{
	IMM *io;

	io = m->iomem;
	io->sdcr = 1;
	io->lccr &= ~1;	/* disable LCD */
	io->pcint = 0;	/* disable all port C interrupts */
	io->pcso = 0;
	io->pcdir =0;
	io->pcpar = 0;
	io->pcdat = 0;
	io->papar = 0;
	io->padir = 0;
	io->paodr = 0;
	io->padat = 0;
	io->pbpar = 0;
	io->pbdir = 0;
	io->pbodr = 0;
	io->pbdat = 0;
	eieio();

	for(io->cpcr = 0x8001; io->cpcr & 1;)	/* reset all CPM channels */
		eieio();

	mapinit(&bdmap, bdmapv, sizeof(bdmapv));
	mapfree(&bdmap, DPBASE, BDSIZE);
}
Exemplo n.º 5
0
void fb_matwrite(char *fname, int size, doublereal *mat) {
	FILE *file = fopen(fname,"wb");
	float *buffer;
	int i, items;

	if (!file) error("open failed");

#ifdef MMAP
	buffer = (float*)mapmalloc(size*sizeof(float));
#else
	buffer = (float*)malloc(size*sizeof(float));
#endif

	for (i=0 ; i<size ; i++) buffer[i] = (float)mat[i];
	
	items = (int)fwrite(buffer,sizeof(float),size,file);
	if (items != size) error("invalid number of elements");
	
#ifdef MMAP
	mapfree(buffer,size*sizeof(float));
#else
	free(buffer);
#endif

	fclose(file);
}
Exemplo n.º 6
0
/**
   Setting up aperture cordinate grid aper_locs, and amplitude map for
performance evaluation. */
APER_T * setup_aper(const PARMS_T *const parms){
    APER_T *aper = mycalloc(1,APER_T);
    tic;
    if(parms->aper.fnamp){
	info2("Reading aperture amplitude map from %s\n", parms->aper.fnamp);
	aper->ampground=mapread("%s",parms->aper.fnamp);
	if(fabs(aper->ampground->h)>1.e-14){
	    warning("ampground is not on ground, this is not tested\n");
	}else{
	    double amp_d, amp_din;
	    map_d_din(aper->ampground, &amp_d, &amp_din);
	    if(fabs(parms->aper.d - amp_d) > 1 ||
	       fabs(parms->aper.din - amp_din) > 0.5){
		if(!parms->aper.fnampuser){
		    warning2("Amplitude map does not match aperture diameter: amp.d=(%g, %g) aper.d=(%g, %g). Disabled\n", 
			     amp_d, amp_din, parms->aper.d, parms->aper.din);
		    mapfree(aper->ampground);
		    free(((PARMS_T*)parms)->aper.fnamp);
		    ((PARMS_T*)parms)->aper.fnamp=0;
		}else{
		    error("Use supplied amplitude map does not match aperture diameter: amp.d=(%g, %g) aper.d=(%g, %g).\n", 
			  amp_d, amp_din, parms->aper.d, parms->aper.din);
		}
	    }
	}
	if(fabs(parms->aper.rotdeg)>1.e-12){
	    warning("Pupil is rotated by %g deg\n",parms->aper.rotdeg);
	    const long nx=aper->ampground->nx;
	    const long ny=aper->ampground->ny;
	    dmat *B=dnew_data(nx, ny, aper->ampground->p);
	    aper->ampground->p=mycalloc(nx*ny,double);
	    dembed((dmat*)aper->ampground,B,parms->aper.rotdeg/180.*M_PI);
	    dfree(B);
	}
Exemplo n.º 7
0
Arquivo: cpm.c Projeto: 8l/inferno
void
bdfree(BD *b, int n)
{
	if(b){
		eieio();
		mapfree(&bdmap, PADDR(b), n*sizeof(BD));
	}
}
Exemplo n.º 8
0
ulong
mapalloc(RMap* rmap, ulong addr, int size, int align)
{
	Map *mp;
	ulong maddr, oaddr;

	lock(rmap);
	for(mp = rmap->map; mp->size; mp++){
		maddr = mp->addr;

		if(addr){
			/*
			 * A specific address range has been given:
			 *   if the current map entry is greater then
			 *   the address is not in the map;
			 *   if the current map entry does not overlap
			 *   the beginning of the requested range then
			 *   continue on to the next map entry;
			 *   if the current map entry does not entirely
			 *   contain the requested range then the range
			 *   is not in the map.
			 */
			if(maddr > addr)
				break;
			if(mp->size < addr - maddr)	/* maddr+mp->size < addr, but no overflow */
				continue;
			if(addr - maddr > mp->size - size)	/* addr+size > maddr+mp->size, but no overflow */
				break;
			maddr = addr;
		}

		if(align > 0)
			maddr = ((maddr+align-1)/align)*align;
		if(mp->addr+mp->size-maddr < size)
			continue;

		oaddr = mp->addr;
		mp->addr = maddr+size;
		mp->size -= maddr-oaddr+size;
		if(mp->size == 0){
			do{
				mp++;
				(mp-1)->addr = mp->addr;
			}while((mp-1)->size = mp->size);
		}

		unlock(rmap);
		if(oaddr != maddr)
			mapfree(rmap, oaddr, maddr-oaddr);

		return maddr;
	}
	unlock(rmap);

	return 0;
}
Exemplo n.º 9
0
Arquivo: memory.c Projeto: 8l/inferno
void
meminit(ulong)
{
	/* A hack to initialize unbacked physical memory.  It's assumed PCI space is assigned by 
	     the BIOS in the 0xF0000000 range and 9load never needs more than 0x2000... to run. These
	     values leave ample space for memory allocations for uninitialized PCI cards (e.g. cardbus 
	     cards).  (pb) */
	ulong maxmem = 0x40000000;

	umbscan();
	mapfree(&rmapupa, maxmem, 0x00000000-maxmem);
	if(MEMDEBUG)
		memdebug();
}
Exemplo n.º 10
0
void
mapupainit(uvlong addr, ulong size)
{
	ulong s;
	uvlong a;

	/*
	 * Careful - physical address.
	 * Boot time only to populate map.
	 */
	a = PGROUND(addr);
	s = a - addr;
	if(s >= size)
		return;
	mapfree(&rmapupa, a, size-s);
}
Exemplo n.º 11
0
Arquivo: memory.c Projeto: npe9/harvey
void
mapupainit(uint64_t addr, uint32_t size)
{
	uint32_t s;
	uint64_t a;

	/*
	 * Careful - physical address.
	 * Boot time only to populate map.
	 */
	a = PGROUND(addr);
	s = a - addr;
	if(s >= size)
		return;
	mapfree(&rmapupa, a, size-s);
}
Exemplo n.º 12
0
void
upareserve(ulong pa, int size)
{
	ulong a;
	
	a = mapalloc(&rmapupa, pa, size, 0);
	if(a != pa){
		/*
		 * This can happen when we're using the E820
		 * map, which might have already reserved some
		 * of the regions claimed by the pci devices.
		 */
	//	print("upareserve: cannot reserve pa=%#.8lux size=%d\n", pa, size);
		if(a != 0)
			mapfree(&rmapupa, a, size);
	}
}
Exemplo n.º 13
0
Arquivo: rmap.c Projeto: 8l/inferno
ulong
rmapalloc(RMap* rmap, ulong addr, int size, int align)
{
	Map *mp;
	ulong maddr, oaddr;

	lock(rmap);
	for(mp = rmap->map; mp->size; mp++){
		maddr = mp->addr;

		if(addr){
			if(maddr > addr)
				break;
			if(maddr+mp->size < addr)
				continue;
			if(addr+size > maddr+mp->size)
				break;
			maddr = addr;
		}

		if(align > 0)
			maddr = ((maddr+align-1)/align)*align;
		if(mp->addr+mp->size-maddr < size)
			continue;

		oaddr = mp->addr;
		mp->addr = maddr+size;
		mp->size -= maddr-oaddr+size;
		if(mp->size == 0){
			do{
				mp++;
				(mp-1)->addr = mp->addr;
			}while((mp-1)->size = mp->size);
		}

		unlock(rmap);
		if(oaddr != maddr)
			mapfree(rmap, oaddr, maddr-oaddr);

		return maddr;
	}
	unlock(rmap);

	return 0;
}
Exemplo n.º 14
0
void
umbrwfree(ulong addr, int size)
{
	mapfree(&rmapumbrw, PADDR(addr), size);
}
Exemplo n.º 15
0
/*
  Compute cxx on atm to compare against L2, invpsd, fractal.
*/
static void test_cxx(){
    rand_t rstat;
    int seed=4;
    double r0=0.2;
    double dx=1./4;
    long N=16;
    long nx=N;
    long ny=N;
    long nframe=40960;
    seed_rand(&rstat, seed);
    {
	dmat *cxx=dnew(N*N,N*N);
	map_t *atm=mapnew(nx+1, ny+1, dx, dx,NULL);
	for(long i=0; i<nframe; i++){
	    info("%ld of %ld\n", i, nframe);
	    for(long j=0; j<(nx+1)*(ny+1); j++){
		atm->p[j]=randn(&rstat);
	    }
	    fractal_do((dmat*)atm, dx, r0, L0, ninit);
	    dmat *sec=dsub((dmat*)atm, 0, nx, 0, ny);
	    dmat *atmvec=dref_reshape(sec, nx*ny, 1);
	    dmm(&cxx,1, atmvec,atmvec,"nt",1);
	    dfree(atmvec);
	    dfree(sec);
	}
	dscale(cxx, 1./nframe);
	writebin(cxx, "cxx_fractal");
	dfree(cxx);
	mapfree(atm);
    }
    {
	dmat *cxx=dnew(N*N,N*N);
	dmat *spect=turbpsd(nx, ny, dx, r0, 100, 0, 0.5);
	spect->p[0]=spect->p[1];
	cmat *atm=cnew(nx, ny);
	//cfft2plan(atm, -1);
	dmat *atmr=dnew(nx*ny,1);
	dmat *atmi=dnew(nx*ny,1);
	for(long ii=0; ii<nframe; ii+=2){
	    info("%ld of %ld\n", ii, nframe);
	    for(long i=0; i<atm->nx*atm->ny; i++){
		atm->p[i]=COMPLEX(randn(&rstat), randn(&rstat))*spect->p[i];
	    }
	    cfft2(atm, -1);
	    for(long i=0; i<atm->nx*atm->ny; i++){
		atmr->p[i]=creal(atm->p[i]);
		atmi->p[i]=cimag(atm->p[i]);
	    }
	    dmm(&cxx,1, atmr,atmr,"nt",1);
	    dmm(&cxx,1, atmi,atmi,"nt",1);
	}
	dscale(cxx, 1./nframe);
	writebin(cxx, "cxx_fft");
	dfree(cxx);
	dfree(atmr);
	dfree(atmi);
	cfree(atm);
    }
    loc_t *loc=mksqloc_auto(16,16,1./4,1./4);
    locwrite(loc,"loc");
    dmat *B=stfun_kolmogorov(loc, r0);
    writebin(B, "B_theory");
}
Exemplo n.º 16
0
Arquivo: memory.c Projeto: npe9/harvey
void
umbrwfree(uint32_t addr, int size)
{
	mapfree(&rmapumbrw, PADDR(addr), size);
}
Exemplo n.º 17
0
void doit(key *keys) {
	char *keyword, *value;
	
	char *data_f = NULL;
	char *act_f = NULL;
	char *weights_in_f = NULL;
	char *weights_out_f = NULL;
	char *sphere_f = NULL;
	char *bias_f = NULL;
	char *sign_f = NULL;
	
	int i, datasize, chans = 0, frames = 0, epochs = 0, datalength = 0, ncomps = 0;
	doublereal *dataA, *dataB, *weights, *sphere, *bias, *eigv;
	integer *signs;

#ifdef MPI
	int   window[NWINDOW];
	char *fnames[3];
	
	for (i=0 ; i<NWINDOW ; i++) window[i] = -1;

	fprintf(stderr,"\nICA/MPI %s\n",VER_INFO);
#else
	fprintf(stderr,"\nICA %s\n",VER_INFO);
#endif

	initdefaults();

/********************* Argument parsing and error checking ********************/
	while (keys) {
/* Extract next value from linked keys list */
		value = keys->token;
		keys = (key*)(keys->prev);
		
		if (!keys) error("even number of input arguments");
		
/* Extract next keyword from linked keys list */
		keyword = keys->token;
		keys = (key*)(keys->prev);
		lower(keyword);
		
/* Keyword: datafile */
		if (!strcmp(keyword,"datafile"))
			data_f = value;
				
/* Keyword: activationsfile */
		else if (!strcmp(keyword,"activationsfile"))
			act_f = value;
				
/* Keyword: weightsinfile */
		else if (!strcmp(keyword,"weightsinfile"))
			weights_in_f = value;
		
/* Keyword: weightsoutfile */
		else if (!strcmp(keyword,"weightsoutfile"))
			weights_out_f = value;
		
/* Keyword: spherefile */
		else if (!strcmp(keyword,"spherefile"))
			sphere_f = value;
		
/* Keyword: biasfile */
		else if (!strcmp(keyword,"biasfile"))
			bias_f = value;

/* Keyword: signfile */
		else if (!strcmp(keyword,"signfile"))
			sign_f = value;

/* Keywords: chans, chan */
		else if (!strcmp(keyword,"chans") || !strcmp(keyword,"chan")) {
			chans = atoi(value);
			if (chans < 2) error("chans value should be the number of input channels");
		}

#ifdef MPI
/* Keywords: frame, frames */
		else if (!strcmp(keyword,"frame") || !strcmp(keyword,"frames")) {
			frames = atoi(value);
			if (frames < 2) error("frames value should be the number of data points per epoch");
		}

/* Keywords: epoch, epochs */
		else if (!strcmp(keyword,"epoch") || !strcmp(keyword,"epochs")) {
			epochs = atoi(value);
			if (epochs < 2) error("epochs value should be the total number of epochs");
		}
#else
/* Keywords: frames, datalength */
		else if (!strcmp(keyword,"frames") || !strcmp(keyword,"datalength")) {
			datalength = atoi(value);
			if (datalength < 2) error("frames value should be the number of data points");
		}
#endif

/* Keyword: pca */
		else if (!strcmp(keyword,"pca")) {
			ncomps = atoi(value);
			if (ncomps != 0) {
				pcaflag = 1;
				if (ncomps < 1)
					error("pca value should be the number of principal components to retain");
			}
			else
				pcaflag = 0;
		}

/* Keyword: lrate */
		else if (!strcmp(keyword,"lrate")) {
			lrate = atof(value);
			if (lrate>MAX_LRATE || lrate<MIN_LRATE)
				error("lrate value is out of bounds");
		}

/* Keywords: block, blocksize */
		else if (!strcmp(keyword,"block") || !strcmp(keyword,"blocksize")) {
			block = atoi(value);
			if (block < 0)
				error("block size value must be positive");
		}
		
/* Keywords: stop, nochange */
		else if (!strcmp(keyword,"stop") || !strcmp(keyword,"nochange") || !strcmp(keyword,"stopping")) {
			nochange = atof(value);
			if (nochange < 0.0)
				error("stop wchange value must be positive");
		}

/* Keywords: maxsteps, steps */
		else if (!strcmp(keyword,"maxsteps") || !strcmp(keyword,"steps")) {
				maxsteps = atoi(value);
				if (maxsteps < 0)
					error("maxsteps value must be a positive integer");
		}

/* Keywords: anneal, annealstep */
		else if (!strcmp(keyword,"anneal") || !strcmp(keyword,"annealstep")) {
			annealstep = atof(value);
			if (annealstep<=0 || annealstep>1)
				error("anneal step value must be (0,1]");
		}

/* Keywords: annealdeg, degrees */
		else if (!strcmp(keyword,"annealdeg") || !strcmp(keyword,"degrees")) {
			annealdeg = atof(value);
			if (annealdeg>180 || annealdeg<0)
				error("annealdeg value is out of bounds [0,180]");
		}

/* Keyword: momentum */
		else if (!strcmp(keyword,"momentum")) {
			momentum = atof(value);
			if (momentum>1.0 || momentum<0.0)
				error("momentum value is out of bounds [0,1]");
		}

		else if (!strcmp(keyword,"sphering") || !strcmp(keyword,"sphereing") || !strcmp(keyword,"sphere")) {
			sphering = swtch(value);
			if (sphering == -1) error("sphering value must be on, off, or none");
		}

/* Keyword: bias */
		else if (!strcmp(keyword,"bias")) {
			biasflag = swtch(value);
			if (biasflag < 0) error("bias value must be on or off");
		}

/* Keywords: extended, extend */
		else if (!strcmp(keyword,"extended") || !strcmp(keyword,"extend")) {
			extblocks = atoi(value);
			extended = 1;
					
			if (extblocks == 0) extended = 0;
			else
				if (extblocks < 0) nsub = -extblocks;
		}

/* Keyword: posact */
		else if (!strcmp(keyword,"posact")) {
			posactflag = swtch(value);
			if (posactflag < 0) error("posact value must be on or off");
		}
			
/* Keyword: verbose */
		else if (!strcmp(keyword,"verbose")) {
			verbose = swtch(value);
			if (verbose < 0) error("verbose flag value must be on or off");
		}
		
#ifdef MPI
/* Keyword: framewindow */
		else if (!strcmp(keyword,"framewindow")) {
			window[FRAMEWINDOW] = atoi(value);
			if (window[FRAMEWINDOW] <= 0) error("framewindow value must be positive");
		}
/* Keyword: framestep */
		else if (!strcmp(keyword,"framestep")) {
			window[FRAMESTEP] = atoi(value);
			if (window[FRAMESTEP] <= 0) error("framestep value must be positive");
		}
/* Keyword: epochwindow */
		else if (!strcmp(keyword,"epochwindow")) {
			window[EPOCHWINDOW] = atoi(value);
			if (window[EPOCHWINDOW] <= 0) error("epochwindow value must be positive");
		}
/* Keyword: epochstep */
		else if (!strcmp(keyword,"epochstep")) {
			window[EPOCHSTEP] = atoi(value);
			if (window[EPOCHSTEP] <= 0) error("epochstep value must be positive");
		}
/* Keyword: baseline */
		else if (!strcmp(keyword,"baseline")) {
			window[BASELINE] = atoi(value);
			if (window[BASELINE] <= 0) error("Length of baseline must be positive");
		}
#endif
		else
			error("unknown flag");
	}


#ifdef MPI
	datalength = frames*epochs;

	if (window[FRAMEWINDOW] < 0) window[FRAMEWINDOW] = frames;
	if (window[FRAMESTEP]   < 0) window[FRAMESTEP]   = 1;
	if (window[EPOCHWINDOW] < 0) window[EPOCHWINDOW] = epochs;
	if (window[EPOCHSTEP]   < 0) window[EPOCHSTEP]   = 1;
	if (window[BASELINE]    < 0) window[BASELINE]    = 25;

	if (window[FRAMEWINDOW] > frames) error("window frame length must be <= frames");
	if (window[FRAMESTEP]   > frames) error("frame step size must be <= frames");
	if (window[EPOCHWINDOW] > epochs) error("window epoch length must be <= epochs");
	if (window[EPOCHSTEP]   > epochs) error("epoch step size must be <= epochs");
	if (window[BASELINE]    > frames) error("length of baseline must be <= frames");

	datasize = window[FRAMEWINDOW] * window[EPOCHWINDOW];
#else
	datasize = datalength;
#endif

	if (chans < 2) error("invalid number of channels");
	if (datasize < 3) error("invalid data length");

	if (lrate == 0.0) lrate = DEFAULT_LRATE(chans);
	if (block == 0) block = DEFAULT_BLOCK(datasize);
	if (ncomps == 0) ncomps = chans;

	if (ncomps > chans || ncomps < 1) error("invalid number of components");
	if (datasize < chans) error("data length less than data channels");
	if (block < 2) error("block size too small!");
	if (block > datasize) error("block size exceeds data length!");
	if (nsub > ncomps) error("sub-Gaussian components exceeds total number of components!");

	if (annealstep == 0.0)
		annealstep = (extended) ? DEFAULT_EXTANNEAL : DEFAULT_ANNEALSTEP;


	if (extended && extblocks>0) {
		pdfsize = MIN(pdfsize,datalength);
		if (pdfsize < MIN_PDFSIZE)
			fprintf(stderr,"ica: warning, PDF values are inexact\n");
	}


	if (data_f==NULL)
		error("input data file required");

	if (weights_out_f==NULL)
		error("output weights file required");
		
	if (sphere_f==NULL)
		error("output sphering file required");


	if (!faccess(weights_out_f))
		error("weights output file not writable");

	if (!faccess(sphere_f))
		error("sphere file not writable");


	if (act_f!=NULL && !faccess(act_f))
		error("activations file not writable");

	if (bias_f!=NULL && !faccess(bias_f))
		error("bias file not writable");

	if (sign_f!=NULL && !faccess(sign_f))
		error("sign file not writable");

/****************************** Process the data ******************************/
	if (verbose) {
#ifdef MPI
		printf("\nInput data size [%d,%d] = %d channels, %d epoch of %d frames.\n",chans,datalength,chans,epochs,frames);
#else
		printf("\nInput data size [%d,%d] = %d channels, %d frames.\n",chans,datalength,chans,datalength);
#endif
		if (pcaflag) printf("After PCA dimension reduction,\n  finding ");
		else printf("Finding ");
	
		if (!extended)
			printf("%d ICA components using logistic ICA.\n",ncomps);
		else {
			printf("%d ICA components using extended ICA.\n",ncomps);
			
			if (extblocks > 0)
				printf("PDF will be calculated initially every %d blocks using %d data points.\n",extblocks,pdfsize);
			else
				printf("PDF will not be calculated. Exactly %d sub-Gaussian components assumed.\n",nsub);
		}
		
		printf("Initial learning rate will be %g, block size %d.\n",lrate,block);
		
		if (momentum > 0.0)
			printf("Momentum will be %g.\n",momentum);
			
		printf("Learning rate will be multiplied by %g whenever angledelta >= %g deg.\n",annealstep,annealdeg);
		printf("Training will end when wchange < %g or after %d steps.\n",nochange,maxsteps);
		
		if (biasflag)
			printf("Online bias adjustment will be used.\n");
		else
			printf("Online bias adjustment will not be used.\n");
	}
	
/******************************* Allocate memory ******************************/
	if (verbose) printf("\nLoading data from %s\n",data_f);

#ifdef MMAP
	dataA = (doublereal*)mapmalloc(chans*datalength*sizeof(doublereal));
#else
	dataA = (doublereal*)malloc(chans*datalength*sizeof(doublereal));
#endif

	fb_matread(data_f,chans*datalength,dataA);

	weights = (doublereal*)malloc(ncomps*chans*sizeof(doublereal));
	if (weights_in_f!=NULL) {
		if (verbose) printf("Loading weights from %s\n",weights_in_f);
		fb_matread(weights_in_f,ncomps*ncomps,weights);
	}
	else
		zero(ncomps*chans,weights);

	sphere = (doublereal*)malloc(chans*chans*sizeof(doublereal));

	if (biasflag)
		bias = (doublereal*)malloc(ncomps*sizeof(doublereal));
	else
		bias = NULL;
	
	if (extended)
		signs = (integer*)malloc(ncomps*sizeof(integer));
	else
		signs = NULL;


/************************** Remove overall row means **************************/
	if (verbose) printf("Removing mean of each channel ...\n");
	rmmean(dataA,(integer)chans,(integer)datalength);


/**************************** Perform PCA reduction ***************************/
	if (pcaflag) {
		if (verbose) printf("Reducing the data to %d principal dimensions...\n",ncomps);

		eigv = (doublereal*)malloc(chans*chans*sizeof(doublereal));
		pca(dataA,(integer)chans,(integer)datalength,eigv);

#ifdef MMAP
		dataB = (doublereal*)mapmalloc(ncomps*datalength*sizeof(doublereal));
		pcaproj(dataA,&eigv[chans*(chans-ncomps)],(integer)ncomps,(integer)datalength,(integer)chans,dataB);
		mapfree(dataA,chans*datalength*sizeof(doublereal));
#else
		dataB = (doublereal*)malloc(ncomps*datalength*sizeof(doublereal));
		pcaproj(dataA,&eigv[chans*(chans-ncomps)],(integer)ncomps,(integer)datalength,(integer)chans,dataB);
		free(dataA);
#endif
		dataA = dataB;
	}
	else
		eigv = NULL;
	
/**************************** Apply sphering matrix ***************************/
	if (sphering == 1) {
		if (verbose) printf("Computing the sphering matrix...\n");
		do_sphere(dataA,(integer)ncomps,(integer)datalength,sphere);
		
		if (verbose) printf("Sphering the data ...\n");

#ifdef MMAP
		dataB = (doublereal*)mapmalloc(ncomps*datalength*sizeof(doublereal));
		syproj(dataA,sphere,(integer)ncomps,(integer)datalength,dataB);
		mapfree(dataA,ncomps*datalength*sizeof(doublereal));
#else
		dataB = (doublereal*)malloc(ncomps*datalength*sizeof(doublereal));
		syproj(dataA,sphere,(integer)ncomps,(integer)datalength,dataB);
		free(dataA);
#endif
		dataA = dataB;
	}
	else if (sphering == 0) {
		if (weights_in_f==NULL) {
			if (verbose) printf("Using the sphering matrix as the starting weight matrix ...\n");
			do_sphere(dataA,(integer)ncomps,(integer)datalength,weights);
		}

		if (verbose) printf("Returning the identity matrix in variable \"sphere\" ...\n");
		eye((integer)ncomps,sphere);
	}
	else if (sphering == -2) {
		if (verbose) printf("Returning the identity matrix in variable \"sphere\" ...\n");
		eye((integer)ncomps,sphere);
	}

#ifdef MPI
	fnames[0] = weights_out_f;
	fnames[1] = bias_f;
	fnames[2] = sign_f;
	mpiica(dataA,weights,sphere,eigv,(integer)chans,(integer)ncomps,(integer)frames,(integer)epochs,window,bias,signs,fnames);
#else
	runica(dataA,weights,(integer)ncomps,(integer)datalength,1,bias,signs);

/*************** Orient components toward positive activations ****************/
#ifdef MMAP
	dataB = (doublereal*)mapmalloc(ncomps*datalength*sizeof(doublereal));

	if (posactflag) posact(dataA,weights,(integer)ncomps,(integer)datalength,dataB);
	else geproj(dataA,weights,(integer)ncomps,(integer)datalength,dataB);

	mapfree(dataA,ncomps*datalength*sizeof(doublereal));
#else
	dataB = (doublereal*)malloc(ncomps*datalength*sizeof(doublereal));
		
	if (posactflag) posact(dataA,weights,(integer)ncomps,(integer)datalength,dataB);
	else geproj(dataA,weights,(integer)ncomps,(integer)datalength,dataB);

	free(dataA);
#endif
	dataA = dataB;


/******* Sort components in descending order of max projected variance ********/
	if (verbose) {
		if (pcaflag) {
			printf("Composing the eigenvector, weights, and sphere matrices\n");
			printf("  into a single rectangular weights matrix; sphere=eye(%d)\n",chans);
		}
		printf("Sorting components in descending order of mean projected variance ...\n");
	}
	
	if (eigv) {
		varsort(dataA,weights,sphere,&eigv[chans*(chans-ncomps)],bias,signs,(integer)ncomps,(integer)datalength,(integer)chans);
		eye((integer)chans,sphere);
	}
	else
		varsort(dataA,weights,sphere,NULL,bias,signs,(integer)ncomps,(integer)datalength,(integer)chans);


/**************************** Write results to disk ***************************/
	if (verbose) printf("Storing weights in %s\n",weights_out_f);
	fb_matwrite(weights_out_f,ncomps*chans,weights);
	
	if (act_f!=NULL) {
		if (verbose) printf("Storing activations in %s\n",act_f);
		fb_matwrite(act_f,ncomps*datalength,dataA);
	}
	
	if (bias_f!=NULL && bias) {
		if (verbose) printf("Storing bias vector in %s\n",bias_f);
		fb_matwrite(bias_f,ncomps,bias);
	}
	
	if (sign_f!=NULL && signs) {
		if (verbose) printf("Storing sign vector in %s\n",sign_f);
		for (i=0 ; i<ncomps ; i++) signs[i] = (signs[i]) ? (-1) : 1;
		ia_matwrite(sign_f,ncomps,signs);
	}
#endif

	if (verbose) printf("Storing sphering matrix in %s\n",sphere_f);
	fb_matwrite(sphere_f,chans*chans,sphere);

#ifdef MMAP
	if (dataA) mapfree(dataA,ncomps*datalength*sizeof(doublereal));
#else
	if (dataA) free(dataA);
#endif

	if (weights) free(weights);
	if (sphere) free(sphere);
	if (bias) free(bias);
	if (signs) free(signs);
	if (eigv) free(eigv);
}
Exemplo n.º 18
0
void
upafree(ulong pa, int size)
{
	mapfree(&rmapupa, pa, size);
}
Exemplo n.º 19
0
static void
map(ulong base, ulong len, int type)
{
	ulong e, n;
	ulong *table, flags, maxkpa;
	
	/*
	 * Split any call crossing MemMin to make below simpler.
	 */
	if(base < MemMin && len > MemMin-base){
		n = MemMin - base;
		map(base, n, type);
		map(MemMin, len-n, type);
	}
	
	/*
	 * Let lowraminit and umbscan hash out the low MemMin.
	 */
	if(base < MemMin)
		return;

	/*
	 * Any non-memory below 16*MB is used as upper mem blocks.
	 */
	if(type == MemUPA && base < 16*MB && base+len > 16*MB){
		map(base, 16*MB-base, MemUMB);
		map(16*MB, len-(16*MB-base), MemUPA);
		return;
	}
	
	/*
	 * Memory below CPU0END is reserved for the kernel
	 * and already mapped.
	 */
	if(base < PADDR(CPU0END)){
		n = PADDR(CPU0END) - base;
		if(len <= n)
			return;
		map(PADDR(CPU0END), len-n, type);
		return;
	}
	
	/*
	 * Memory between KTZERO and end is the kernel itself
	 * and is already mapped.
	 */
	if(base < PADDR(KTZERO) && base+len > PADDR(KTZERO)){
		map(base, PADDR(KTZERO)-base, type);
		return;
	}
	if(PADDR(KTZERO) < base && base < PADDR(PGROUND((ulong)end))){
		n = PADDR(PGROUND((ulong)end));
		if(len <= n)
			return;
		map(PADDR(PGROUND((ulong)end)), len-n, type);
		return;
	}
	
	/*
	 * Now we have a simple case.
	 */
	// print("map %.8lux %.8lux %d\n", base, base+len, type);
	switch(type){
	case MemRAM:
		mapfree(&rmapram, base, len);
		flags = PTEWRITE|PTEVALID;
		break;
	case MemUMB:
		mapfree(&rmapumb, base, len);
		flags = PTEWRITE|PTEUNCACHED|PTEVALID;
		break;
	case MemUPA:
		mapfree(&rmapupa, base, len);
		flags = 0;
		break;
	default:
	case MemReserved:
		flags = 0;
		break;
	}
	
	/*
	 * bottom MemMin is already mapped - just twiddle flags.
	 * (not currently used - see above)
	 */
	if(base < MemMin){
		table = KADDR(PPN(m->pdb[PDX(base)]));
		e = base+len;
		base = PPN(base);
		for(; base<e; base+=BY2PG)
			table[PTX(base)] |= flags;
		return;
	}
	
	/*
	 * Only map from KZERO to 2^32.
	 */
	if(flags){
		maxkpa = -KZERO;
		if(base >= maxkpa)
			return;
		if(len > maxkpa-base)
			len = maxkpa - base;
		pdbmap(m->pdb, base|flags, base+KZERO, len);
	}
}
Exemplo n.º 20
0
static void
ramscan(ulong maxmem)
{
	ulong *k0, kzero, map, maxkpa, maxpa, pa, *pte, *table, *va, vbase, x;
	int nvalid[NMemType];

	/*
	 * The bootstrap code has has created a prototype page
	 * table which maps the first MemMin of physical memory to KZERO.
	 * The page directory is at m->pdb and the first page of
	 * free memory is after the per-processor MMU information.
	 */
	pa = MemMin;

	/*
	 * Check if the extended memory size can be obtained from the CMOS.
	 * If it's 0 then it's either not known or >= 64MB. Always check
	 * at least 24MB in case there's a memory gap (up to 8MB) below 16MB;
	 * in this case the memory from the gap is remapped to the top of
	 * memory.
	 * The value in CMOS is supposed to be the number of KB above 1MB.
	 */
	if(maxmem == 0){
		x = (nvramread(0x18)<<8)|nvramread(0x17);
		if(x == 0 || x >= (63*KB))
			maxpa = MemMax;
		else
			maxpa = MB+x*KB;
		if(maxpa < 24*MB)
			maxpa = 24*MB;
	}else
		maxpa = maxmem;
	maxkpa = (u32int)-KZERO;	/* 2^32 - KZERO */

	/*
	 * March up memory from MemMin to maxpa 1MB at a time,
	 * mapping the first page and checking the page can
	 * be written and read correctly. The page tables are created here
	 * on the fly, allocating from low memory as necessary.
	 */
	k0 = (ulong*)KADDR(0);
	kzero = *k0;
	map = 0;
	x = 0x12345678;
	memset(nvalid, 0, sizeof(nvalid));
	
	/*
	 * Can't map memory to KADDR(pa) when we're walking because
	 * can only use KADDR for relatively low addresses.
	 * Instead, map each 4MB we scan to the virtual address range
	 * MemMin->MemMin+4MB while we are scanning.
	 */
	vbase = MemMin;
	while(pa < maxpa){
		/*
		 * Map the page. Use mapalloc(&rmapram, ...) to make
		 * the page table if necessary, it will be returned to the
		 * pool later if it isn't needed.  Map in a fixed range (the second 4M)
		 * because high physical addresses cannot be passed to KADDR.
		 */
		va = (void*)(vbase + pa%(4*MB));
		table = &m->pdb[PDX(va)];
		if(pa%(4*MB) == 0){
			if(map == 0 && (map = mapalloc(&rmapram, 0, BY2PG, BY2PG)) == 0)
				break;
			memset(KADDR(map), 0, BY2PG);
			*table = map|PTEWRITE|PTEVALID;
			memset(nvalid, 0, sizeof(nvalid));
		}
		table = KADDR(PPN(*table));
		pte = &table[PTX(va)];

		*pte = pa|PTEWRITE|PTEUNCACHED|PTEVALID;
		mmuflushtlb(PADDR(m->pdb));
		/*
		 * Write a pattern to the page and write a different
		 * pattern to a possible mirror at KZERO. If the data
		 * reads back correctly the chunk is some type of RAM (possibly
		 * a linearly-mapped VGA framebuffer, for instance...) and
		 * can be cleared and added to the memory pool. If not, the
		 * chunk is marked uncached and added to the UMB pool if <16MB
		 * or is marked invalid and added to the UPA pool.
		 */
		*va = x;
		*k0 = ~x;
		if(*va == x){
			nvalid[MemRAM] += MB/BY2PG;
			mapfree(&rmapram, pa, MB);

			do{
				*pte++ = pa|PTEWRITE|PTEVALID;
				pa += BY2PG;
			}while(pa % MB);
			mmuflushtlb(PADDR(m->pdb));
			/* memset(va, 0, MB); so damn slow to memset all of memory */
		}
		else if(pa < 16*MB){
			nvalid[MemUMB] += MB/BY2PG;
			mapfree(&rmapumb, pa, MB);

			do{
				*pte++ = pa|PTEWRITE|PTEUNCACHED|PTEVALID;
				pa += BY2PG;
			}while(pa % MB);
		}
		else{
			nvalid[MemUPA] += MB/BY2PG;
			mapfree(&rmapupa, pa, MB);

			*pte = 0;
			pa += MB;
		}
		/*
		 * Done with this 4MB chunk, review the options:
		 * 1) not physical memory and >=16MB - invalidate the PDB entry;
		 * 2) physical memory - use the 4MB page extension if possible;
		 * 3) not physical memory and <16MB - use the 4MB page extension
		 *    if possible;
		 * 4) mixed or no 4MB page extension - commit the already
		 *    initialised space for the page table.
		 */
		if(pa%(4*MB) == 0 && pa >= 32*MB && nvalid[MemUPA] == (4*MB)/BY2PG){
			/*
			 * If we encounter a 4MB chunk of missing memory
			 * at a sufficiently high offset, call it the end of
			 * memory.  Otherwise we run the risk of thinking
			 * that video memory is real RAM.
			 */
			break;
		}
		if(pa <= maxkpa && pa%(4*MB) == 0){
			table = &m->pdb[PDX(KADDR(pa - 4*MB))];
			if(nvalid[MemUPA] == (4*MB)/BY2PG)
				*table = 0;
			else if(nvalid[MemRAM] == (4*MB)/BY2PG && (m->cpuiddx & 0x08))
				*table = (pa - 4*MB)|PTESIZE|PTEWRITE|PTEVALID;
			else if(nvalid[MemUMB] == (4*MB)/BY2PG && (m->cpuiddx & 0x08))
				*table = (pa - 4*MB)|PTESIZE|PTEWRITE|PTEUNCACHED|PTEVALID;
			else{
				*table = map|PTEWRITE|PTEVALID;
				map = 0;
			}
		}
		mmuflushtlb(PADDR(m->pdb));
		x += 0x3141526;
	}
	/*
	 * If we didn't reach the end of the 4MB chunk, that part won't
	 * be mapped.  Commit the already initialised space for the page table.
	 */
	if(pa % (4*MB) && pa <= maxkpa){
		m->pdb[PDX(KADDR(pa))] = map|PTEWRITE|PTEVALID;
		map = 0;
	}
	if(map)
		mapfree(&rmapram, map, BY2PG);

	m->pdb[PDX(vbase)] = 0;
	mmuflushtlb(PADDR(m->pdb));

	mapfree(&rmapupa, pa, (u32int)-pa);
	*k0 = kzero;
}