Exemplo n.º 1
0
//____________________________________________________________________
void
optionmm::basic_option::print(int ll, std::ostream& o) const
{
    if (ll <= 0) ll=16;
    if (_short_name != '\0') o << "-" << _short_name <<", ";
    else                     o << "    ";
    if (!_long_name.empty()) {
        o << "--" << _long_name;
        if (need_argument()) o << "=VALUE";
        else                 o << "      ";
        for (int i = _long_name.size(); i < ll; i++) o << " ";
    } else {
        o << std::setw(ll+2) << " ";
        if (need_argument()) o << " VALUE";
        else                 o << "      ";
    }
    o << "\t" << _help_string;
    if (many_values()) o << " (*)";
}
Exemplo n.º 2
0
//____________________________________________________________________
int
optionmm::basic_option::handle(const std::string& arg, int pos)
{
    if (_long_name.empty() || arg.compare(CMP_ARG(2,_long_name))) return 0;
    if (need_argument()) {
        std::string::size_type eq = arg.find_last_of('=');
        if (eq == std::string::npos) {
            std::cerr << "Option --" << _long_name << " need an argument"
            << std::endl;
            return -1;
        }
        std::string value(arg);
        value.erase(0, eq+1);
        push_arg(value.c_str(), pos);
    } else
        push_arg(pos);
    return 1;
}
Exemplo n.º 3
0
//____________________________________________________________________
int
optionmm::basic_option::handle(char*& opt, char*& arg, int pos)
{
    if (_short_name == '\0' || opt[0] != _short_name) return 0;
    if (need_argument()) {
        if (opt[1] != '\0') {
            push_arg(&(opt[1]), pos);
            opt[1] = '\0';
        } else if (arg) {
            push_arg(arg, pos);
            // Flag argument as used.
            arg = 0;
        } else {
            std::cerr << "Option -" << _short_name << " need an argument"
            << std::endl;
            return -1;
        }
    } else
        push_arg(pos);
    return 1;
}
int main(int argc, char*argv[])
{
	// 6 config, each has three files to read
	char *files[] = {
		"../resources/config_32N32M_B.txt",
		"../resources/config_32N32M_A.txt",
		"../resources/config_32N32M_prior.txt",
		"../resources/config_64N64M_B.txt",
		"../resources/config_64N64M_A.txt",
		"../resources/config_64N64M_prior.txt",
		"../resources/config_128N128M_B.txt",
		"../resources/config_128N128M_A.txt",
		"../resources/config_128N128M_prior.txt",
		"../resources/config_256N256M_B.txt",
		"../resources/config_256N256M_A.txt",
		"../resources/config_256N256M_prior.txt",
		"../resources/config_512N512M_B.txt",
		"../resources/config_512N512M_A.txt",
		"../resources/config_512N512M_prior.txt",
		"../resources/config_1024N1024M_B.txt",
		"../resources/config_1024N1024M_A.txt",
		"../resources/config_1024N1024M_prior.txt",
	};

	// variables
	int i,j,k;
	int Len;
	int debug=0;

	int job=0;

	// select job frome commmand line
	int argi;
	if (argc == 1)
	{
		puts("Please specify an option.\nUsage: \"./ocl_fo -job number(0-5) \"\n");
		exit(1);
	}

	for (argi = 1; argi < argc; ++argi)
	{
		if (!strcmp(argv[argi], "-job"))
		{
			need_argument(argc, argv,argi);
			job = atoi(argv[++argi]) ;
			continue;
		}

		if (argv[argi][0] == '-')
		{
			fatal("'%s' is not a valid command-line option.\n",argv[argi]);
		}
	}

	//printf("job = %d\n", job);
	if( job  > 5) {
		printf("Job number exceeds the limit 5! Exit Programm!\n");
		exit(1);	
	}



	HMM *word;
	word = (HMM*)malloc(sizeof(HMM));

	Len = getLineNum(files[job*3+2]);	
	printf("config_%dN_%dM\n",Len, Len);

	//read B,A,prior
	printf("Read the following files...");

	//read_config(files,job,B,A,prior,Len);
	read_config(word,files,job,Len,Len);

	printf("Done!\n");
	if( debug && job == 0 ) {
		puts("a");
		check_a(word);	
		puts("b");
		check_b(word);	
		puts("pri");
		check_pri(word);	
	}


	//----------------------
	// run forward algorithm
	//----------------------

	//---------------------------
	// GPU Version
	//---------------------------

	run_opencl_fo(word);


	//---------------------------
	// CPU Version
	//---------------------------

	puts("\n=>CPU");


	struct timeval cpu_timer;

	int N = word->nstates;
	int T = word->len;
	float *B = word->b;
	float *A = word->a;
	float *prior = word->pri;

	double tmp, alpha_sum;
	double log_likelihood;

	float *alpha; // NxT
	alpha = (float*)malloc(sizeof(float)*N*T);

	float *A_t; // NxN
	A_t = (float*)malloc(sizeof(float)*N*N);

	log_likelihood = 0.0;

	// start timing
	tic(&cpu_timer);

	transpose(A, A_t, N, T);	



	for(j=0;j<T;++j)
	{
		alpha_sum = 0.0;

		if(j==0){ // initialize
			for(i=0;i<N;++i){
				alpha[i*T + 0] = B[i*T + 0] * prior[i];	
				alpha_sum += alpha[i*T + 0];
			}
		}else{ // move forward
			for(i=0;i<N;++i)
			{ // go through each state
				tmp = 0.0;	
				for(k=0;k<N;++k){
					tmp += A_t[i*N + k] * alpha[k*T + j-1];
				}

				alpha[i*T + j] = (float)tmp * B[i*T + j];
				alpha_sum += alpha[i*T + j];
			}
		}

		// scaling
		for(i=0;i<N;++i){			
			alpha[i*T + j] /= alpha_sum;
		}

		log_likelihood += log(alpha_sum);
	}
	// end timing
	toc(&cpu_timer);

	printf("log_likelihood = %lf\n", log_likelihood);


	// free memory

	free_hmm(word);
	free(A_t);
	free(alpha);


	return 0;
}
Exemplo n.º 5
0
static void
apply_opt(const tw_optdef *def, const char *value)
{
    switch (def->type)
    {
    case TWOPTTYPE_ULONG:
    case TWOPTTYPE_UINT:
    {
        unsigned long v;
        char *end;

        if (!value)
            need_argument(def);
        v = strtoul(value, &end, 10);
        if (*end)
            need_argument(def);
        switch (def->type)
        {
        case TWOPTTYPE_ULONG:
            *((unsigned long*)def->value) = v;
            break;
        case TWOPTTYPE_UINT:
            *((unsigned int*)def->value) = (unsigned int)v;
            break;
        default:
            tw_error(TW_LOC, "Option type not supported here.");
        }
        break;
    }

    case TWOPTTYPE_STIME:
    {
        tw_stime v;
        char *end;

        if (!value)
            need_argument(def);
        v = strtod(value, &end);
        if (*end)
            need_argument(def);
        *((tw_stime*)def->value) = v;
        break;
    }

    case TWOPTTYPE_CHAR:
    {
        if (!value)
            need_argument(def);

        //*((char **)def->value) = tw_calloc(TW_LOC, "string arg", strlen(value) + 1, 1);
        strcpy(def->value, value);
        break;
    }

    case TWOPTTYPE_SHOWHELP:
        if (tw_ismaster())
            show_help();
        tw_net_stop();
        exit(0);
        break;

    default:
        tw_error(TW_LOC, "Option type not supported here.");
    }
}