Exemple #1
0
static int
test_zlib_header (grub_gzio_t gzio)
{
    grub_uint8_t cmf, flg;

    cmf = get_byte (gzio);
    flg = get_byte (gzio);

    /* Check that compression method is DEFLATE.  */
    if ((cmf & 0xf) != DEFLATED)
    {
        grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "unsupported gzip format");
        return 0;
    }

    if ((cmf * 256 + flg) % 31)
    {
        grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "unsupported gzip format");
        return 0;
    }

    /* Dictionary isn't supported.  */
    if (flg & 0x20)
    {
        grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "unsupported gzip format");
        return 0;
    }

    gzio->data_offset = 2;
    initialize_tables (gzio);

    return 1;
}
Exemple #2
0
static int
test_gzip_header (grub_file_t file)
{
    struct {
        grub_uint16_t magic;
        grub_uint8_t method;
        grub_uint8_t flags;
        grub_uint32_t timestamp;
        grub_uint8_t extra_flags;
        grub_uint8_t os_type;
    } hdr;
    grub_uint16_t extra_len;
    grub_uint32_t orig_len;
    grub_gzio_t gzio = file->data;

    if (grub_file_tell (gzio->file) != 0)
        grub_file_seek (gzio->file, 0);

    /*
     *  This checks if the file is gzipped.  If a problem occurs here
     *  (other than a real error with the disk) then we don't think it
     *  is a compressed file, and simply mark it as such.
     */
    if (grub_file_read (gzio->file, &hdr, 10) != 10
            || ((hdr.magic != GZIP_MAGIC)
                && (hdr.magic != OLD_GZIP_MAGIC)))
        return 0;

    /*
     *  This does consistency checking on the header data.  If a
     *  problem occurs from here on, then we have corrupt or otherwise
     *  bad data, and the error should be reported to the user.
     */
    if (hdr.method != DEFLATED
            || (hdr.flags & UNSUPPORTED_FLAGS)
            || ((hdr.flags & EXTRA_FIELD)
                && (grub_file_read (gzio->file, &extra_len, 2) != 2
                    || eat_field (gzio->file,
                                  grub_le_to_cpu16 (extra_len))))
            || ((hdr.flags & ORIG_NAME) && eat_field (gzio->file, -1))
            || ((hdr.flags & COMMENT) && eat_field (gzio->file, -1)))
        return 0;

    gzio->data_offset = grub_file_tell (gzio->file);

    /* FIXME: don't do this on not easily seekable files.  */
    {
        grub_file_seek (gzio->file, grub_file_size (gzio->file) - 4);
        if (grub_file_read (gzio->file, &orig_len, 4) != 4)
            return 0;
        /* FIXME: this does not handle files whose original size is over 4GB.
           But how can we know the real original size?  */
        file->size = grub_le_to_cpu32 (orig_len);
    }

    initialize_tables (gzio);

    return 1;
}
Exemple #3
0
ARingZZp::ARingZZp(size_t p0)
    : charac(p0),
      p(static_cast<int>(p0)),
      p1(p-1)
{
    if (p==2)
        minus_one = 1;
    else
        minus_one = (p-1)/2;

    initialize_tables();
}
Exemple #4
0
/**
* @attention 本注释得到了"核高基"科技重大专项2012年课题“开源操作系统内核分析和安全性评估
*(课题编号:2012ZX01039-004)”的资助。
*
* @copyright 注释添加单位:清华大学——03任务(Linux内核相关通用基础软件包分析)承担单位
*
* @author 注释添加人员:谢文学
*
* @date 注释添加日期:2013年5月10日
*
* @note 注释详细内容:
* 
* 本函数实现使用gunzip的解压缩读入的功能。
*/
int
gunzip_read (char *buf, int len)
{
  int ret = 0;

  compressed_file = 0;
  gunzip_swap_values ();
  /*
   *  Now "gzip_*" values refer to the uncompressed data.
   */

  /* do we reset decompression to the beginning of the file? */
  if (saved_filepos > gzip_filepos + WSIZE)
    initialize_tables ();

  /*
   *  This loop operates upon uncompressed data only.  The only
   *  special thing it does is to make sure the decompression
   *  window is within the range of data it needs.
   */

  while (len > 0 && !errnum)
    {
      register int size;
      register char *srcaddr;

      while (gzip_filepos >= saved_filepos)
	inflate_window ();

      srcaddr = (char *) ((gzip_filepos & (WSIZE - 1)) + slide);
      size = saved_filepos - gzip_filepos;
      if (size > len)
	size = len;

      memmove (buf, srcaddr, size);

      buf += size;
      len -= size;
      gzip_filepos += size;
      ret += size;
    }

  compressed_file = 1;
  gunzip_swap_values ();
  /*
   *  Now "gzip_*" values refer to the compressed data.
   */

  if (errnum)
    ret = 0;

  return ret;
}
bootstrap::bootstrap(char* name) {
  file_name = name;
  _has_error = false;
  open_file();
  if (!has_error()) {
    //    initialize_tables(file_size > 0 ? (file_size / 32) : (10 * K));
    initialize_tables(64 * K);
    parse_file();
    close_file();
    Universe  ::cleanup_after_bootstrap();
  }
}
Exemple #6
0
static grub_ssize_t
grub_gzio_read (grub_file_t file, char *buf, grub_size_t len)
{
  grub_ssize_t ret = 0;
  grub_gzio_t gzio = file->data;
  grub_off_t offset;

  /* Do we reset decompression to the beginning of the file?  */
  if (gzio->saved_offset > file->offset + WSIZE)
    initialize_tables (file);

  /*
   *  This loop operates upon uncompressed data only.  The only
   *  special thing it does is to make sure the decompression
   *  window is within the range of data it needs.
   */

  offset = file->offset;

  while (len > 0 && grub_errno == GRUB_ERR_NONE)
    {
      register grub_size_t size;
      register char *srcaddr;

      while (offset >= gzio->saved_offset)
	inflate_window (file);

      srcaddr = (char *) ((offset & (WSIZE - 1)) + gzio->slide);
      size = gzio->saved_offset - offset;
      if (size > len)
	size = len;

      grub_memmove (buf, srcaddr, size);

      buf += size;
      len -= size;
      ret += size;
      offset += size;
    }

  if (grub_errno != GRUB_ERR_NONE)
    ret = -1;

  return ret;
}
Exemple #7
0
void LDA::initialize(){

  Document target;

  initialize_tables();

//  std::cout << "init matrices\n";
  for(int i=0; i < filenames.size(); ++i){
    
    target = Document(filenames[i]);
    target.load_document();
    target.init_random_topics(K);
    target.save_topics();

    update_tables(target);

    target.clear();
  }

}
Exemple #8
0
static void
disassemble(Program * prog, Printer p, void *data)
{
    Stream *s = new_stream(100);
    Stream *insn = new_stream(50);
    int i, l;
    unsigned pc;
    Bytecodes bc;
    const char *ptr;
    const char **names = prog->var_names;
    unsigned tmp, num_names = prog->num_var_names;
#   define NAMES(i)	(tmp = i,					\
			 tmp < num_names ? names[tmp]			\
					 : "*** Unknown variable ***")
    Var *literals = prog->literals;

    initialize_tables();
    print = p;
    print_data = data;
    stream_printf(s, "Language version number: %d", (int) prog->version);
    output(s);
    stream_printf(s, "First line number: %d", prog->first_lineno);
    output(s);

    for (i = -1; i < 0 || i < prog->fork_vectors_size; i++) {
	output(s);
	if (i == -1) {
	    stream_printf(s, "Main code vector:");
	    output(s);
	    stream_printf(s, "=================");
	    output(s);
	    bc = prog->main_vector;
	} else {
	    stream_printf(s, "Forked code vector %d:", i);
	    l = stream_length(s);
	    output(s);
	    while (l--)
		stream_add_char(s, '=');
	    output(s);
	    bc = prog->fork_vectors[i];
	}

	stream_printf(s, "[Bytes for labels = %d, literals = %d, ",
		      bc.numbytes_label, bc.numbytes_literal);
	stream_printf(s, "forks = %d, variables = %d, stack refs = %d]",
		      bc.numbytes_fork, bc.numbytes_var_name,
		      bc.numbytes_stack);
	output(s);
	stream_printf(s, "[Maximum stack size = %d]", bc.max_stack);
	output(s);

	max_bytes_width = 5;

	for (pc = 0; pc < bc.size;) {
	    Byte b;
	    unsigned arg;
#	    define ADD_BYTES(n)	(arg = add_bytes(s, bc.vector, pc, n),	\
				 pc += n,				\
				 arg)
	    unsigned a1, a2;

	    new_insn(s, pc);
	    b = add_bytes(s, bc.vector, pc++, 1);
	    if (b != OP_EXTENDED)
		stream_add_string(insn, COUNT_TICK(b) ? " * " : "   ");
	    if (IS_OPTIM_NUM_OPCODE(b))
		stream_printf(insn, "NUM %d", OPCODE_TO_OPTIM_NUM(b));
#ifdef BYTECODE_REDUCE_REF
	    else if (IS_PUSH_CLEAR_n(b))
		stream_printf(insn, "PUSH_CLEAR %s", NAMES(PUSH_CLEAR_n_INDEX(b)));
#endif /* BYTECODE_REDUCE_REF */
	    else if (IS_PUSH_n(b))
		stream_printf(insn, "PUSH %s", NAMES(PUSH_n_INDEX(b)));
	    else if (IS_PUT_n(b))
		stream_printf(insn, "PUT %s", NAMES(PUT_n_INDEX(b)));
	    else if (b == OP_EXTENDED) {
		b = ADD_BYTES(1);
		stream_add_string(insn, COUNT_EOP_TICK(b) ? " * " : "   ");
		stream_add_string(insn, ext_mnemonics[b]);
		switch ((Extended_Opcode) b) {
		case EOP_WHILE_ID:
		    a1 = ADD_BYTES(bc.numbytes_var_name);
		    a2 = ADD_BYTES(bc.numbytes_label);
		    stream_printf(insn, " %s %d", NAMES(a1), a2);
		    break;
		case EOP_EXIT_ID:
		    stream_printf(insn, " %s",
				  NAMES(ADD_BYTES(bc.numbytes_var_name)));
		    /* fall thru */
		case EOP_EXIT:
		    a1 = ADD_BYTES(bc.numbytes_stack);
		    a2 = ADD_BYTES(bc.numbytes_label);
		    stream_printf(insn, " %d %d", a1, a2);
		    break;
		case EOP_PUSH_LABEL:
		case EOP_END_CATCH:
		case EOP_END_EXCEPT:
		case EOP_TRY_FINALLY:
		    stream_printf(insn, " %d", ADD_BYTES(bc.numbytes_label));
		    break;
		case EOP_TRY_EXCEPT:
		    stream_printf(insn, " %d", ADD_BYTES(1));
		    break;
		case EOP_LENGTH:
		    stream_printf(insn, " %d", ADD_BYTES(bc.numbytes_stack));
		    break;
		case EOP_SCATTER:
		    {
			int i, nargs = ADD_BYTES(1);

			a1 = ADD_BYTES(1);
			a2 = ADD_BYTES(1);
			stream_printf(insn, " %d/%d/%d:", nargs, a1, a2);
			for (i = 0; i < nargs; i++) {
			    a1 = ADD_BYTES(bc.numbytes_var_name);
			    a2 = ADD_BYTES(bc.numbytes_label);
			    stream_printf(insn, " %s/%d", NAMES(a1), a2);
			}
			stream_printf(insn, " %d",
				      ADD_BYTES(bc.numbytes_label));
		    }
		    break;
		default:
		    break;
		}
	    } else {
		stream_add_string(insn, mnemonics[b]);
		switch ((Opcode) b) {
		case OP_IF:
		case OP_IF_QUES:
		case OP_EIF:
		case OP_AND:
		case OP_OR:
		case OP_JUMP:
		case OP_WHILE:
		    stream_printf(insn, " %d", ADD_BYTES(bc.numbytes_label));
		    break;
		case OP_FORK:
		    stream_printf(insn, " %d", ADD_BYTES(bc.numbytes_fork));
		    break;
		case OP_FORK_WITH_ID:
		    a1 = ADD_BYTES(bc.numbytes_fork);
		    a2 = ADD_BYTES(bc.numbytes_var_name);
		    stream_printf(insn, " %d %s", a1, NAMES(a2));
		    break;
		case OP_FOR_LIST:
		case OP_FOR_RANGE:
		    a1 = ADD_BYTES(bc.numbytes_var_name);
		    a2 = ADD_BYTES(bc.numbytes_label);
		    stream_printf(insn, " %s %d", NAMES(a1), a2);
		    break;
		case OP_G_PUSH:
#ifdef BYTECODE_REDUCE_REF
		case OP_G_PUSH_CLEAR:
#endif /* BYTECODE_REDUCE_REF */
		case OP_G_PUT:
		    stream_printf(insn, " %s",
				  NAMES(ADD_BYTES(bc.numbytes_var_name)));
		    break;
		case OP_IMM:
		    {
			Var v;

			v = literals[ADD_BYTES(bc.numbytes_literal)];
			switch (v.type) {
			case TYPE_OBJ:
			    stream_printf(insn, " #%d", v.v.obj);
			    break;
			case TYPE_INT:
			    stream_printf(insn, " %d", v.v.num);
			    break;
			case TYPE_STR:
			    stream_add_string(insn, " \"");
			    for (ptr = v.v.str; *ptr; ptr++) {
				if (*ptr == '"' || *ptr == '\\')
				    stream_add_char(insn, '\\');
				stream_add_char(insn, *ptr);
			    }
			    stream_add_char(insn, '"');
			    break;
			case TYPE_ERR:
			    stream_printf(insn, " %s", error_name(v.v.err));
			    break;
			default:
			    stream_printf(insn, " <literal type = %d>",
					  v.type);
			    break;
			}
		    }
		    break;
		case OP_BI_FUNC_CALL:
		    stream_printf(insn, " %s", name_func_by_num(ADD_BYTES(1)));
		default:
		    break;
		}
	    }

	    finish_insn(s, insn);
	}
    }

    free_stream(s);
    free_stream(insn);
}
Exemple #9
0
int main(int argc, char **argv) {
    //printf("%d %c\n",argc,argv[0][0]);
    int		    i, DOPLOT, quiet, tmp;
    double	    pixel, thr;
    void	    *values[32];
/* opencl options */
    int		    bat_pltsel=0, bat_devsel=0, bat_wgropitems=0, bat_batchitems=0;
    int script_mode = 0;
    int g_mem_size = -1, c_mem_size = -1, l_mem_size = -1, c_ProtonWater_Energy_size = -1, g_traceA_size = -1, g_arena_size = -1;
/* grab the parameters from the command line */
    tmp = 0;
    values[tmp++] = (void *) &NMC;
    values[tmp++] = (void *) &EMEAN;
    values[tmp++] = (void *) &ESTDV;
    values[tmp++] = (void *) &SIGMA;
    values[tmp++] = (void *) &TPATH;
    values[tmp++] = (void *) &MRIFIL;
    values[tmp++] = (void *) &TRACE;
    values[tmp++] = (void *) &DOPLOT;
    values[tmp++] = (void *) &XYANGLE;
    values[tmp++] = (void *) &AZIMUTH;
    values[tmp++] = (void *) &TOUTFIL;
    values[tmp++] = (void *) &BOUTFIL;
    values[tmp++] = (void *) &LOCFIL;
    values[tmp++] = (void *) &quiet;
    values[tmp++] = (void *) &pixel;
    values[tmp++] = (void *) &thr;
    values[tmp++] = (void *) &NTHREAD;
    values[tmp++] = (void *) &bat_pltsel;
    values[tmp++] = (void *) &bat_devsel;
    values[tmp++] = (void *) &bat_wgropitems;
    values[tmp++] = (void *) &bat_batchitems;
    values[tmp++] = (void *) &nx;
    if (ParseParams(argc-1, argv+1, _params, values) == -1) {
	fprintf(stderr, "flags were not parsed correctly!\n");
	exit(-1);
    }    
#if CMD_MODE        
    if (argc >= 2)
    {
        char *intrace = argv[1];
        if (intrace[0] == 'c')
        {
            NMC = atoi(argv[2]);           
            bat_pltsel = -1;
        }        
        if (intrace[0] == 'q')
        {
            quiet = 1;
            bat_pltsel = 99;
        }        
        if (intrace[0] == 'o')
        {
            NMC = atoi(argv[2]);
            bat_pltsel = atoi(argv[3]);
            bat_devsel = atoi(argv[4]);
            bat_wgropitems = atoi(argv[5]);
            bat_batchitems = atoi(argv[6]);
        }
        if (intrace[0] == 's' && intrace[1] == 'o')  //script opencl
        {
            script_mode = 2;
            quiet = 0;
            NMC = atoi(argv[2]);
            NTHREAD = -1;
            bat_pltsel = atoi(argv[3]);
            bat_devsel = atoi(argv[4]);
            bat_wgropitems = atoi(argv[5]);
            bat_batchitems = atoi(argv[6]);
            nx = atoi(argv[7]);
            MAXSTEP = atof(argv[8]);
            EMEAN = atof(argv[9]);
        }
        
        if (intrace[0] == 's' && intrace[1] == 'c')  //script cpu
        {
            script_mode = 1;
            quiet = 0;
            NMC = atoi(argv[2]);
            NTHREAD = atoi(argv[3]);
            bat_pltsel = -1;
            bat_devsel = -1;
            bat_wgropitems = -1;
            bat_batchitems = -1;
            nx = atoi(argv[4]);
            MAXSTEP = atof(argv[5]);
            EMEAN = atof(argv[6]);
        }
    }
#endif

    FILE *csv_file = NULL;

    if (script_mode)
    { 	
        csv_file = fopen("jack_script.csv", "r");
        if (csv_file == NULL)
        {
            csv_file = fopen("jack_script.csv", "a");
            fprintf(csv_file, "NTHREAD, PLATFORM, DEVICE, Items_WG, Items_batch, NMC, dC, dS(mm), E(MeV), Time(sec), Global_MEM(B), Const_MEM(B), Local_MEM(B), Trace Size(B), Arena Size(B), WaterTable Size(B)\n");  
        }
        else
        {
            csv_file = fopen("jack_script.csv", "a");
        }
    }
	ny = nz = nx;
    PIXEL = (REAL) pixel;
    THR = (REAL) thr;
    VERBOSE = ! quiet;  
    if (VERBOSE) {
        printf("Number of Monte-Carlo trials (-nmc): %d\n", NMC);
        printf("Incident Particle Energy (-mean,-stdv,-sigma): N(%g,%g,%g) direction (-angle,-azimuth) (%g,%g)\n",
               EMEAN, ESTDV, SIGMA, XYANGLE, AZIMUTH);
        printf("Tables Read From (-path): %s\n", TPATH);
        if (MRIFIL) printf("MRI Read From (-mri): %s with pixel size %.2f\n", MRIFIL, PIXEL);
        if (LOCFIL) printf("Location Read From (-loc): %s\n", LOCFIL);
        if (BOUTFIL) printf("Output binary dose will be written to (-dump): %s\n", BOUTFIL);
        if (TOUTFIL) printf("Output text dose will be written to (-dump): %s\n", TOUTFIL);
        printf("Dump Traces (-trace): %s\n", TRACE ? "yes" : "no");
        printf("Make Plot File (-plot): %s\n", DOPLOT ? "yes" : "no");
        printf("OpenCL Device: %d Platform: %d WorkGroupItems: %d BatchItems: %d\n", bat_pltsel, bat_devsel, bat_wgropitems, bat_batchitems);
    }
/* initialize */
    initialize_queu(NQUEU);
/* initialize the particle collection domain */
    ARENA = initialize_collect(MRIFIL, LOCFIL);
/* any table or physics initializations go here */
    initialize_tables();
    initialize_fluctuations();
/* create the global collector */
    C = allocate_collector();

    clock_t looper = clock();
    gettimeofday(&gstart, NULL); 
//    printf("bat_pltsel = %d\n", bat_pltsel);
        
/* if opencl device was specified, then run the opencl version */
    if (bat_pltsel != -1) {
        JackCL(NMC, bat_pltsel, bat_devsel, bat_wgropitems, bat_batchitems, EMEAN, ESTDV, SIGMA, XYANGLE, AZIMUTH, C,
               &g_mem_size, &c_mem_size, &l_mem_size, &c_ProtonWater_Energy_size, &g_traceA_size, &g_arena_size);
    } else if (NTHREAD < 2) {
        MonteCarlo(C, NMC, (REAL) EMEAN, (REAL) ESTDV, (REAL) SIGMA, (REAL) XYANGLE, (REAL) AZIMUTH);
    } else {
        ITHR = (int *) malloc(NTHREAD*sizeof(int));
        CTHR = (collector **) malloc(NTHREAD*sizeof(collector *));
        PTHR = (pthread_t *) malloc(NTHREAD*sizeof(pthread_t));
        for (i=0; i<NTHREAD; i++) {
            CTHR[i] = allocate_collector();
        }
        for (i=0; i<NTHREAD; i++) {
            ITHR[i] = i;
            pthread_create(&(PTHR[i]), NULL, (void *) &ThreadSimulate, (void *) &(ITHR[i]));
        }
        for (i=0; i<NTHREAD; i++) {
            pthread_join(PTHR[i], NULL);
        }
        for (i=0; i<NTHREAD; i++) accumulate_collector(C, CTHR[i]);
    }
    if (bat_pltsel == 99) return 0;
    clock_t end = clock();
    gettimeofday(&gend, NULL); 
    float delta = ((gend.tv_sec  - gstart.tv_sec) * 1000000u +           
		   gend.tv_usec - gstart.tv_usec) / 1.e6;   
    float second_time = (float) (end - looper) / CLOCKS_PER_SEC;

/* dump files */
    if (TOUTFIL) dump_dose_text_file(C, TOUTFIL);
    if (BOUTFIL) dump_dose_binary_file(C, BOUTFIL);
    if (LOCFIL) dump_good_bad_dose(C);
/* print the results */
    summarize_collect(C, DOPLOT);
    
    if (script_mode)
    { 	
        fprintf(csv_file, "%d, %d, %d, %d, %d, %d, %d, %.2f, %f, %f, %d, %d, %d, %d, %d, %d\n", 
            NTHREAD, bat_pltsel, bat_devsel, bat_wgropitems, bat_batchitems, NMC, nx, MAXSTEP, EMEAN, delta, g_mem_size, c_mem_size, l_mem_size, g_traceA_size, g_arena_size, c_ProtonWater_Energy_size);  
    }
     
    printf("\nSimulation Loop Time: %f seconds \n", second_time);
    printf("Simulation Loop Time (gettimeofday): %f seconds \n", delta);
    return(0);
}
LocalPlanner::LocalPlanner(){
	initialize_tables();
}
Exemple #11
0
void LDA::run_iterations_mpi(int num_iterations){
  
  Document target;
  int first_file_idx, last_file_idx;

#if MPI_ENABLED
    int rank, namelen, num_procs;
    char processor_name[MPI_MAX_PROCESSOR_NAME];

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

    //sync nodes before broadcast
    //MPI_Barrier(MPI_COMM_WORLD);

    //distribute the initial tables to all nodes
//    broadcast_data(topic_x_words, K*V);
//    broadcast_data(total_words_in_topics, K);

    if (rank == 0)    {
      std::cout <<"[proc "<<rank<<"]" << "Tables sent to children" << std::endl;
    }

    int num_files_per_proc = ceil((float)filenames.size() / num_procs);
    first_file_idx = rank * num_files_per_proc;
    last_file_idx = first_file_idx + num_files_per_proc - 1;

    if (rank == num_procs - 1)
        last_file_idx = filenames.size() - 1;

      std::cout <<"[proc "<<rank<<"]" << "My first index is "<<first_file_idx<<std::endl;
      std::cout <<"[proc "<<rank<<"]" << "My last index is "<<last_file_idx<<std::endl;
#else
    first_file_idx = 0; last_file_idx = filenames.size();
#endif

  for(int iter_idx=0; iter_idx < num_iterations; ++iter_idx){
#if MPI_ENABLED
    if (rank == 0)    {
      std::cout <<"[proc "<<rank<<"]" << "Iteration " << iter_idx << std::endl;
    }

    if (iter_idx % sync_frequency == 0)  {
      if (rank == 0)  {
        // recount the tables from current topic assignments
        initialize_tables();

        for (int i = 0; i<filenames.size(); i++)    {
            Document tmp_doc = Document(filenames[i]);
//            tmp_doc.load_document();
//            tmp_doc.load_topics();
            update_tables(tmp_doc);
        }
      }
    }

    // send tables to all processes in the pool
    broadcast_data(topic_x_words, K*V);
    broadcast_data(total_words_in_topics, K);
#else
    std::cout << "Iteration " << iter_idx << std::endl;
    first_file_idx = 0; last_file_idx = filenames.size();
#endif

    //Big loop of iteration over files
    //TODO: MPI Goes Here

    for(int file_idx=first_file_idx; file_idx < last_file_idx; ++file_idx){
            
      //std::cout << rank << "am I here " << first_file_idx << std::endl;   
      target = Document(filenames[file_idx]);
      target.load_document();
      target.load_topics();

      //loop of iteration over words
      int size_of_doc = target.num_words();
      
      //create_document_topic_distribution
      boost::numeric::ublas::matrix<double> document_x_topic(1,K);

      //initialize dist
      for(int i=0; i<K; ++i){
        document_x_topic(0,i) = 0;
      }
      
      //fill distribution
      for(int word_idx=0; word_idx < size_of_doc; ++word_idx){
        int topic = target.get_word_topic(word_idx);
        assert( topic>=0 && topic<=K );
        document_x_topic(0,topic) += 1;
      }

      //actual gibbs sampling
      //this is where OpenMP would be nice.
      //TODO: OpenMP atomic or barrier/syncs
    int word_idx = 0;
#if OMP_ENABLED 
    int threadCount = 4;
    omp_set_num_threads(threadCount);
    #pragma omp parallel shared(target, document_x_topic, size_of_doc) private(word_idx)
#endif 
    {
#if OMP_ENABLED
      #pragma omp for
#endif 
      for(word_idx=0; word_idx < size_of_doc; ++word_idx){
        //getword
        int word = target.get_word(word_idx);
        //gettopic
        int topic = target.get_word_topic(word_idx);

        //update dists.
        //comment out the following since with OMP, topic_x_words could be poisoned a little bit and go below 1
        //assert((*topic_x_words)(topic,word) > 0);
#if MPI_ENABLED
        //_topic_x_words(topic,word) -= 1;
        //_total_words_in_topics(topic,0) -= 1;

        topic_x_words[V*topic + word] -= 1;
        total_words_in_topics[topic] -= 1;
#else
        (*topic_x_words)(topic,word) -= 1;
        (*total_words_in_topics)(topic,0) -= 1;
#endif
        assert(document_x_topic(0,topic) > 0);
        document_x_topic(0,topic) -= 1;
        boost::numeric::ublas::matrix<double> topic_dist(K,1);
        for(int topic_idx=0;topic_idx < K; ++topic_idx){
#if MPI_ENABLED
        // double topic_word_prob = ((double) _topic_x_words(topic_idx,word) + beta)/
        //   ((double)_total_words_in_topics(topic_idx,0) + V*beta);
        double topic_word_prob = ((double) topic_x_words[V*topic_idx + word] + beta)/
           ((double )total_words_in_topics[topic_idx] + V*beta);
#else
          double topic_word_prob = ((double) (*topic_x_words)(topic_idx,word) + beta)/
            ((double)(*total_words_in_topics)(topic_idx,0) + V*beta);
#endif
          double topic_doc_prob = ((double)(document_x_topic(0,topic_idx) + alpha)/
            ((double) size_of_doc + K*alpha/*CHECKTHIS*/));

          assert (topic_idx>=0 && topic_idx<=K);
          topic_dist(topic_idx,0) = topic_word_prob * topic_doc_prob;
        }

        //sum of the topic dist vector
        double normalizing_constant = sum(prod(
          boost::numeric::ublas::scalar_vector<double>(topic_dist.size1()),
          topic_dist));
        for(int topic_idx=0; topic_idx < K; ++topic_idx){
          topic_dist(topic_idx,0) = topic_dist(topic_idx,0)/normalizing_constant;
        }
        double prob = unif();
        int new_topic = -1;
        
        while(prob > 0){
          new_topic += 1;
          prob -= topic_dist(new_topic,0);
        }
        assert(new_topic < K);

        //assign_topic
        target.set_word_topic(word_idx,new_topic);

        //update dists
#if MPI_ENABLED
        //_topic_x_words(new_topic,word) += 1;
        //_total_words_in_topics(new_topic,0) += 1;
        topic_x_words[V*new_topic + word] += 1;
        total_words_in_topics[new_topic] += 1;
#else
        (*topic_x_words)(new_topic,word) += 1;
        (*total_words_in_topics)(new_topic,0) += 1;
#endif
        document_x_topic(0,new_topic) += 1;
      }
      target.save_topics();
    } //omp parallel

    }

#if MPI_ENABLED
    if (rank == 0) {
      if(iter_idx % thinning == 0){
        if(iter_idx < burnin){
          continue;
        }
        print_neg_log_likelihood(vocab_path.substr(0, vocab_path.length()-9) + "neg_log_likeMPI.csv");
        //TODO: PRINT
      }
    }
#elif OMP_ENABLED
    if(iter_idx % thinning == 0){
      if(iter_idx < burnin){
        continue;
      }
      print_neg_log_likelihood(vocab_path.substr(0, vocab_path.length()-9) + "neg_log_likeOMP.csv");
      //TODO: PRINT
    }
#else
    if(iter_idx % thinning == 0){
      if(iter_idx < burnin){
        continue;
      }
      print_neg_log_likelihood(vocab_path.substr(0, vocab_path.length()-9) + "neg_log_likeSER.csv");
      //TODO: PRINT
    }
#endif
  } // outer for loop
}
Exemple #12
0
int
gunzip_test_header (void)
{
  unsigned char buf[10];
  
  /* "compressed_file" is already reset to zero by this point */

  /*
   *  This checks if the file is gzipped.  If a problem occurs here
   *  (other than a real error with the disk) then we don't think it
   *  is a compressed file, and simply mark it as such.
   */
  if (no_decompression
      || grub_read (buf, 10) != 10
      || ((*((unsigned short *) buf) != GZIP_HDR_LE)
	  && (*((unsigned short *) buf) != OLD_GZIP_HDR_LE)))
    {
      filepos = 0;
      return ! errnum;
    }

  /*
   *  This does consistency checking on the header data.  If a
   *  problem occurs from here on, then we have corrupt or otherwise
   *  bad data, and the error should be reported to the user.
   */
  if (buf[2] != DEFLATED
      || (buf[3] & UNSUPP_FLAGS)
      || ((buf[3] & EXTRA_FIELD)
	  && (grub_read (buf, 2) != 2
	      || bad_field (*((unsigned short *) buf))))
      || ((buf[3] & ORIG_NAME) && bad_field (-1))
      || ((buf[3] & COMMENT) && bad_field (-1)))
    {
      if (! errnum)
	errnum = ERR_BAD_GZIP_HEADER;
      
      return 0;
    }

  gzip_data_offset = filepos;
  
  filepos = filemax - 8;
  
  if (grub_read (buf, 8) != 8)
    {
      if (! errnum)
	errnum = ERR_BAD_GZIP_HEADER;
      
      return 0;
    }

  gzip_crc = *((unsigned long *) buf);
  gzip_fsmax = gzip_filemax = *((unsigned long *) (buf + 4));

  initialize_tables ();

  compressed_file = 1;
  gunzip_swap_values ();
  /*
   *  Now "gzip_*" values refer to the compressed data.
   */

  filepos = 0;

  return 1;
}
Exemple #13
0
int
gunzip_test_header (void)
{
  unsigned char buf[10];
  
  /* "compressed_file" is already reset to zero by this point */

  /*
   *  This checks if the file is gzipped.  If a problem occurs here
   *  (other than a real error with the disk) then we don't think it
   *  is a compressed file, and simply mark it as such.
   */
  if (no_decompression
      || grub_read (buf, 10) != 10
      || ((*((unsigned short *) buf) != GZIP_HDR_LE)
	  && (*((unsigned short *) buf) != OLD_GZIP_HDR_LE)))
    {
      filepos = 0;
      return ! errnum;
    }

  /*
   *  This does consistency checking on the header data.  If a
   *  problem occurs from here on, then we have corrupt or otherwise
   *  bad data, and the error should be reported to the user.
   */
  if (buf[2] != DEFLATED
      || (buf[3] & UNSUPP_FLAGS)
      || ((buf[3] & EXTRA_FIELD)
	  && (grub_read (buf, 2) != 2
	      || bad_field (*((unsigned short *) buf))))
      || ((buf[3] & ORIG_NAME) && bad_field (-1))
      || ((buf[3] & COMMENT) && bad_field (-1)))
    {
      if (! errnum)
	errnum = ERR_BAD_GZIP_HEADER;
      
      return 0;
    }

  gzip_data_offset = filepos;
  
  /* We could read the last 8 bytes of the file to get the uncompressed
   * size. Doing so under tftp would cause the file to be downloaded
   * twice, which can be problem with large files. So we set it to
   * MAXINT and correct it later when we get to the end of the file
   * in get_byte().
   */
  gzip_fsmax = gzip_filemax = MAXINT;

  initialize_tables ();

  compressed_file = 1;
  gunzip_swap_values ();
  /*
   *  Now "gzip_*" values refer to the compressed data.
   */

  filepos = 0;

  crc = (ulg)0xffffffffUL;

  return 1;
}
Exemple #14
0
int
gunzip_read (char *buf, int len)
{
  int ret = 0;
  int check_crc;
  ulg crc_value = 0xffffffffUL;

  compressed_file = 0;
  gunzip_swap_values ();
  /*
   *  Now "gzip_*" values refer to the uncompressed data.
   */

  /* do we reset decompression to the beginning of the file? */
  if (saved_filepos > gzip_filepos + WSIZE)
    initialize_tables ();

  /* perform CRC check only if reading the entire file */
  check_crc = (saved_filepos == 0 && len == MAXINT);

  /*
   *  This loop operates upon uncompressed data only.  The only
   *  special thing it does is to make sure the decompression
   *  window is within the range of data it needs.
   */

  while (len > 0 && !errnum)
    {
      register int size;
      register char *srcaddr;

      while (gzip_filepos >= saved_filepos && !errnum)
	inflate_window ();

      if (errnum)
	break;

      /* We could have started with an unknown gzip_filemax (MAXINT)
       * which has been updated in get_byte(). If so, update len
       * to avoid reading beyond the end.
       */
      if (len > (gzip_filemax - gzip_filepos)) {
        len = gzip_filemax - gzip_filepos;
	if (len < 0) {
	  errnum = ERR_BAD_GZIP_DATA;
	  break;
	}
      }

      srcaddr = (char *) ((gzip_filepos & (WSIZE - 1)) + slide);
      size = saved_filepos - gzip_filepos;
      if (size > len)
	size = len;

      memmove (buf, srcaddr, size);

      /* do CRC calculation here! */
      crc_value = updcrc(buf, (unsigned)size);

      buf += size;
      len -= size;
      gzip_filepos += size;
      ret += size;
    }

  /* check for CRC error if reading entire file */
  if (!errnum && check_crc && gzip_crc != crc_value) {
#if 0
    printf ("gunzip: crc value 0x%x, expected 0x%x\n", crc_value, gzip_crc);
#endif
    errnum = ERR_BAD_GZIP_CRC;
  }

  compressed_file = 1;
  gunzip_swap_values ();
  /*
   *  Now "gzip_*" values refer to the compressed data.
   */

  if (errnum)
    ret = 0;

  return ret;
}