Exemple #1
0
int snapshot_write_next(struct snapshot_handle *handle, size_t count)
{
	static struct chain_allocator ca;
	int error = 0;

	/* Check if we have already loaded the entire image */
	if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
		return 0;

	if (!buffer) {
		/* This makes the buffer be freed by swsusp_free() */
		buffer = alloc_image_page(GFP_ATOMIC, PG_ANY);
		if (!buffer)
			return -ENOMEM;
	}
	if (!handle->offset)
		handle->buffer = buffer;
	handle->sync_read = 1;
	if (handle->prev < handle->cur) {
		if (handle->prev == 0) {
			error = load_header(buffer);
			if (error)
				return error;

			error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
			if (error)
				return error;

		} else if (handle->prev <= nr_meta_pages) {
			unpack_orig_pfns(buffer, &copy_bm);
			if (handle->prev == nr_meta_pages) {
				error = prepare_image(&orig_bm, &copy_bm);
				if (error)
					return error;

				chain_init(&ca, GFP_ATOMIC, PG_SAFE);
				memory_bm_position_reset(&orig_bm);
				restore_pblist = NULL;
				handle->buffer = get_buffer(&orig_bm, &ca);
				handle->sync_read = 0;
				if (!handle->buffer)
					return -ENOMEM;
			}
		} else {
			handle->buffer = get_buffer(&orig_bm, &ca);
			handle->sync_read = 0;
		}
		handle->prev = handle->cur;
	}
	handle->buf_offset = handle->cur_offset;
	if (handle->cur_offset + count >= PAGE_SIZE) {
		count = PAGE_SIZE - handle->cur_offset;
		handle->cur_offset = 0;
		handle->cur++;
	} else {
		handle->cur_offset += count;
	}
	handle->offset += count;
	return count;
}
void addChainOrder(enum trident code[X][X], int x, int y, _Bool do_chain){
	/*TODO: code may be prepcode*/
	int i, j, k;
	if(do_chain){
		code[x][y]=Filled;
		chain[chaincont].x=x;
		chain[chaincont].y=y;
		tate[x]++;
		yoko[y]++;
	}
	if(yoko[y]==X-1){
		i=findFinalPlace(code, True, y);
		code[i][y]=AutoFilled;
		chain[chaincont].toafill[chain[chaincont].toafillcont].x=i;
		chain[chaincont].toafill[chain[chaincont].toafillcont].y=y;
		for(j=k=0; j<X; j++){
			if(j==i)
				continue;
			chain[chaincont].toafillroad[chain[chaincont].	\
							toafillcont][k].x=j;
			chain[chaincont].toafillroad[chain[chaincont].	\
							toafillcont][k].y=y;
			k++;
		}
		chain[chaincont].toafillcont++;
		tate[i]++;
		yoko[y]++;
		/*TODO: don't need to check yoko in this case!*/
		addChainOrder(code, i, y, False);
	}
	if(tate[x]==X-1){
		i=findFinalPlace(code, False, x);
		code[x][i]=AutoFilled;
		chain[chaincont].toafill[chain[chaincont].toafillcont].x=x;
		chain[chaincont].toafill[chain[chaincont].toafillcont].y=i;
		for(j=k=0; j<X; j++){
			if(j==i)
				continue;
			chain[chaincont].toafillroad[chain[chaincont].	\
							toafillcont][k].x=x;
			chain[chaincont].toafillroad[chain[chaincont].	\
							toafillcont][k].y=j;
			k++;
		}
		chain[chaincont].toafillcont++;
		tate[x]++;
		yoko[i]++;
		/*TODO: don't need to check tate in this case!*/
		addChainOrder(code, x, i, False);
	}
	if(do_chain){
		chaincont++;
		chain_init(chain+chaincont);
	}
	return;
}
Exemple #3
0
bsh_command_chain_t *build_chain_from_str(char *line) {
	bsh_command_chain_t *chain = chain_init();
	bsh_command_chain_t *current = chain;
	bsh_command_chain_t *b = current;
	char *linep = line;

MORE_INPUT:
	
	switch(parser_read(current, &line)) {
		case CHAIN_WANT_PROCESS_PIPE:
			//printf("CHAIN_WANT_PROCESS_PIPE:\n");
			current->next = chain_init();
			current = current->next;
			current->op = PIPE_STDIN;
			goto MORE_INPUT;
			break;

		case CHAIN_DONE:
			//printf("CHAIN_DONE:\n");
			return chain;

		case CONTEXT_NOCONTEXT:
			/* cleanup elements which have been given invalid arguments, like pipes without args */
			for(current = chain; current != NULL; current = current->next) {
				if(current->command == NULL)
					if(current == chain) return NULL;
					else{
						chain_free(current);
						free(current);
						b->next = NULL;
					}

				b = current;
			}
			return chain;

		default:
			errx(-1, "error!");

	}

	return chain;
}
Exemple #4
0
static int
memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
{
	struct chain_allocator ca;
	struct zone *zone;
	struct zone_bitmap *zone_bm;
	struct bm_block *bb;
	unsigned int nr;

	chain_init(&ca, gfp_mask, safe_needed);

	/* Compute the number of zones */
	nr = 0;
	for_each_zone(zone)
		if (populated_zone(zone))
			nr++;

	/* Allocate the list of zones bitmap objects */
	zone_bm = create_zone_bm_list(nr, &ca);
	bm->zone_bm_list = zone_bm;
	if (!zone_bm) {
		chain_free(&ca, PG_UNSAFE_CLEAR);
		return -ENOMEM;
	}

	/* Initialize the zone bitmap objects */
	for_each_zone(zone) {
		unsigned long pfn;

		if (!populated_zone(zone))
			continue;

		zone_bm->start_pfn = zone->zone_start_pfn;
		zone_bm->end_pfn = zone->zone_start_pfn + zone->spanned_pages;
		/* Allocate the list of bitmap block objects */
		nr = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
		bb = create_bm_block_list(nr, &ca);
		zone_bm->bm_blocks = bb;
		zone_bm->cur_block = bb;
		if (!bb)
			goto Free;

		nr = zone->spanned_pages;
		pfn = zone->zone_start_pfn;
		/* Initialize the bitmap block objects */
		while (bb) {
			unsigned long *ptr;

			ptr = get_image_page(gfp_mask, safe_needed);
			bb->data = ptr;
			if (!ptr)
				goto Free;

			bb->start_pfn = pfn;
			if (nr >= BM_BITS_PER_BLOCK) {
				pfn += BM_BITS_PER_BLOCK;
				bb->size = BM_CHUNKS_PER_BLOCK;
				nr -= BM_BITS_PER_BLOCK;
			} else {
				/* This is executed only once in the loop */
				pfn += nr;
				bb->size = DIV_ROUND_UP(nr, BM_BITS_PER_CHUNK);
			}
			bb->end_pfn = pfn;
			bb = bb->next;
		}
		zone_bm = zone_bm->next;
	}
	bm->p_list = ca.chain;
	memory_bm_position_reset(bm);
	return 0;

 Free:
	bm->p_list = ca.chain;
	memory_bm_free(bm, PG_UNSAFE_CLEAR);
	return -ENOMEM;
}
Exemple #5
0
int main(int argc, char** argv)
{
	char *bugname, *rname_init, *rname_data;
	int nupdate, nburnin,niteration;
	int thin=1;
	int nchain=1;
	int nmonitor=0;
	char* monitor_list[100];
	CHAIN chain;
	int i,j;
	int ret;	
	int n;
    //SAMPLERLIST* slist = c->model->samplers;
    NODE* node;
	NODE* monitor_nodes[100];
	FILE* fp;

#if __VC
	_fullpath(bin_path, argv[0], PATH_MAX); 
#endif

	strcpy(bin_path, argv[0]);
	for( i = strlen(bin_path)-1 ; i >= 0 ; i-- )
	{
#ifdef __VC
		if( bin_path[i] == '\\' ){
#else
		if( bin_path[i] == '/' ){
#endif
			bin_path[i] = '\0';
			break;
		}
	}
	printf(bin_path);
		

	getcwd(boot_path, PATH_MAX);

	if( argc <= 3 )
	{
		printf("usage: magro [bugs_file] [iteration] [burin-in] <options>\n");
		printf("options: -init=[R-init-file]\n");
		printf("         -data=[R-data-file]\n");
		printf("         -c=[number of chains]\n");
		printf("         -m=[monitoring variable]\n");
		printf("         -thin=[thinning interval]\n");
		printf("         -codegen\n");
		printf("         -st\n");
		printf("         -v\n");
		printf("bugs_file ... the model definition file.\n");
		printf("iteration ... the number of MCMC iterations.\n");
		printf("burn-in ... the number of MCMC burn-in periods.\n");
		printf("-init ... set the initialize parameters file. \n");
		printf("-data ... set the data file.\n");
		printf("-c ... set the number of chains. if you set more than ONE, you SHOULD NOT set -st option.\n");
		printf("-st ... disable multi threading support.\n");
		printf("-codegen ... generate human readable code set.\n");
		printf("-m ... set the monitoring values. (donot set array values)\n");
		printf("-v ... set verbose mode. \n");
		exit(1);
	}

	bugname = argv[1];
	
	if( sscanf(argv[2], "%d", &niteration) != 1 )
	{
		printf("iteration count must be integer.\n");
		exit(2);
	}

	if( sscanf(argv[3], "%d", &nburnin) != 1 )
	{
		printf("burn-in count must be integer.\n");
		exit(3);
	}

	if( niteration < nburnin )
	{
		printf("itertaion[%d] muse be more than burn-in[%d]\n", niteration, nburnin);
		exit(4);
	}
	nupdate = niteration - nburnin;

	rname_data = NULL;
	rname_init = NULL;

	for( i = 1 ; i < argc ; i++ )
	{
		if( argv[i][0] == '-' )
		{
			if( strncmp(argv[i], "-init=", 6) == 0 )
			{
				rname_init = &argv[i][6];
			}
			else if( strncmp(argv[i], "-data=", 6) == 0 )
			{
				rname_data = &argv[i][6];
			}
			else if( strcmp(argv[i], "-st") == 0 )
			{
				mode_thread = 0;
			}
			else if( strcmp(argv[i], "-codegen") == 0 )
			{
				mode_codegen = 1;
			}
			else if( strcmp(argv[i], "-v") == 0 )
			{
				mode_verbose = 1;
			}
			else if( strncmp(argv[i], "-thin=", 6) == 0 )
			{
				if( sscanf(&argv[i][6], "%d", &thin) != 1 )
				{
					printf("option '-thin': value must be integer.\n");
					exit(11);
				}
				if( thin < 1 )
				{
					printf("option '-thin': value must be greater than one.\n");
					exit(12);
				}
			}
			else if( strncmp(argv[i], "-c=", 3) == 0 )
			{
				if( sscanf(&argv[i][3], "%d", &nchain) != 1 )
				{
					printf("option '-c': value must be integer.\n");
					exit(13);
				}
				if( nchain < 1 || nchain>=CHAIN_MAX)
				{
					printf("option '-c': value must be greater than 0 and less than %d.\n", CHAIN_MAX-1);
					exit(14);
				}
			}
			else if( strncmp(argv[i], "-v=", 3) == 0 )
			{
				if( sscanf(&argv[i][3], "%d", &mode_verbose) != 1 )
				{
					printf("option '-v': value must be integer.\n");
					exit(13);
				}
				if( mode_verbose < 1 )
				{
					printf("option '-v': value must be greater than one.");
					exit(14);
				}
				
			}
			else if( strncmp(argv[i], "-m=", 3) == 0 )
			{
				char* p = &argv[i][3];
				int l = strlen(p);
				char* buff = p;//GC_MALLOC(sizeof(char) * l);
				int j,k,n;
				n = 0;
				for( j = 0 ; j < l ; j++ )
				{
					if( p[j] == ',' ) n++;
				}
				nmonitor = n+1;
				n = 0;
				k = 0;
				monitor_list[n++] = p;
				for( j = 0 ; j < l ; j++ )
				{
					if( buff[j] == ',' )
					{
						buff[j] = '\0';
						monitor_list[n++] = buff+j+1;
					}
				}
				buff[l] = '\0';	
			}
		}
	}

	GC_INIT();

	chain_init(&chain, nchain);
	
	printf("reading model in %s ... ", bugname);
	ret = chain_loadmodel(&chain, bugname);
	if( ret == -1 )
	{
		printf("fail to open the file.\n");
		exit(1);
	}
	if( ret > 0 )
	{
		printf("%d errors found.\n", ret);
		exit(-1);
	}	

	if( rname_data != NULL )
	{
        printf("reading data in %s ... ", rname_data);
		ret = chain_loaddata(&chain, rname_data);
		if( ret != -1 )
        {
            if( ret > 0 )
			{
				printf("%d errors found.\n", ret);
                return -1;
            }
            printf("success\n");
        }
		else
		{
			printf("fail to open the file.\n");
			exit(1);	
		}
	}

	printf("compiling ... ");
	chain_compile(&chain);
	printf("success\n");

	if( mode_verbose > 1 )
	{
		for( i = 0 ; i < nchain ; i++ )
		{
			printf("chain #%d\n", i+1);
			printf("  number of samplers = %d\n", chain.core[i].compiler->model->samplers->count);
			printf("  number of nodes of the graph = %d\n", chain.core[i].compiler->graph->count);
		}
	}

	if( rname_init != NULL )
	{
		printf("reading parameter file in %s ... ", rname_init);
		ret = chain_loadinit(&chain, rname_init);
		if( ret == -1 ) 
		{
			printf("faile to open the file\n");
			exit(1);
		}
		if( ret > 0 )
		{
			printf("%d errors found.\n", ret);
			return -1;
		}
		printf("success\n");
	}

	chain_initialize(&chain);

	for ( j = 0 ; j < nchain ; j++ )
	{
		chain.core[j].nmonitor = nmonitor;
		for( i = 0 ; i < nmonitor ; i++ )
		{
			CHAINCORE* core = &chain.core[j];
			node = nodedic_findnode_byliteral(core->compiler->model->relations, monitor_list[i]);
			core->monitor_nodes[i] = node;
			if( node == NULL )
			{
				printf("cannot find monitoring variable named '%s'\n", monitor_list[i]);
				exit(99);
			}
		}	
	}

	if( mode_codegen == 0 )
	{
		int ind=0;

		for( i = 0 ; i < nchain ; i++ )
		{
			int monitor_size = (int)((double)nupdate-1 / thin) + 1;
			double* monitor_buff = GC_MALLOC(sizeof(double) * monitor_size * nmonitor);
//			int monitor_count = 0;
			CHAINCORE* core = &chain.core[i];
	
			core->monitor_buff = monitor_buff;
			core->monitor_counter = 0;
			core->nthin = thin;
		}

		printf("burn-in\n");
		printf("--------------------------------------------------| %d\n", nburnin);
		chain_update(&chain, nburnin, 0);
		printf("* 100%%\n");

		ind = 0;
		printf("updates\n");
		printf("--------------------------------------------------| %d\n", nupdate);
		chain_update(&chain, nupdate, 1);
		printf("* 100%%\n");

		//	printf("*** DUMP MONITORTED VALUES***\n");
		saveCODA(&chain, nmonitor, monitor_list);	
/*		
		for( j = 0 ; j < nchain ; j++ )
		{
			CHAINCORE* core = &chain.core[j];
			double* monitor_buff = core->monitor_buff;
			int monitor_count = core->monitor_counter;
			for( n = 0 ; n < nmonitor; n++ )
			{
				printf("%s:\n", monitor_list[n]);
				for( i = 0 ; i < monitor_count; i++ )
				{
					printf("%d\t%f\n", thin*i, monitor_buff[i*nmonitor+n]);
				}
			}
		}
*/
	}
	else if( mode_codegen == 1 )
	{
		DIR* dir = opendir(CODEGEN_DIRNAME);
		if( dir == NULL )
		{
#ifndef __VC
			if( mkdir(CODEGEN_DIRNAME, S_IRWXU | S_IRWXG | S_IRWXO) != 0 )
#else
			if( mkdir(CODEGEN_DIRNAME, 0) != 0 )
#endif
			{
				printf("failed to make directory named '%s'\n", CODEGEN_DIRNAME);
				exit(90);
			}
		}
		else closedir(dir);

		chdir("codegen");

		printf("writing sampler.hdf ... ");
		fp = fopen("sampler.hdf", "w");
		if( fp != NULL )
		{
			chain_savehdf(&chain, fp, monitor_list, nmonitor, nburnin, nupdate, thin);
			fclose(fp);
			printf("OK\n");

			printf("reading sampler.hdf ... ");
			fp = fopen("sampler.hdf", "r");
			if( fp != NULL )
			{
				int sz;
				char *buf;
				char* templatedir;
				char cmdbuf[1024];
				HDF* hdf;

				fseek(fp, 0, SEEK_END);
				sz = ftell(fp);
				fseek(fp, 0, SEEK_SET);
				
				buf = (char*)GC_MALLOC_ATOMIC(sz);
				fread(buf, 1, sz, fp);
				fclose(fp); 
				printf("OK\n");

				hdf_init(&hdf);
				printf("parsing hdf ... ");
				hdf_read_string(hdf, buf);
				printf("OK\n");

				templatedir = gettemplatedir();
				printf("templatedir = %s\n", templatedir);
				savesrc(templatedir, "main.c.cs", "main.c", hdf);
				savesrc(templatedir, "conf.h.cs", "conf.h", hdf);
				savesrc(templatedir, "nmath.h.cs", "nmath.h", hdf);
				savesrc(templatedir, "sampler.c.cs", "sampler.c", hdf);
				savesrc(templatedir, "sampler.h.cs", "sampler.h", hdf);
				savesrc(templatedir, "environment.c.cs", "environment.c", hdf);
				savesrc(templatedir, "environment.h.cs", "environment.h", hdf);
				
				hdf_destroy(&hdf);

#ifndef __VC
				system("gcc  main.c environment.c sampler.c -L/usr/local/lib -lnmath -I/usr/local/include -W -Wall -lm -pthread");
#else
				system("cl main.c environment.c sampler.c /FD /EHsc /MD /Gy /c /Zi /TC");
				snprintf(cmdbuf, 1024, "link main.obj environment.obj sampler.obj nmath.lib /LIBPATH:\"%s\"", bin_path);
				system(cmdbuf);
#endif
			}
		}
	}

	chain_free(&chain);
	
	return 0;
}