Exemple #1
0
int main(int argc, char **argv) {
	char input_string[DEFAULT_INPUT_LENGTH];

	scanf("%s", input_string);

	int input_string_len = strlen(input_string);

	int missing_index = 0;
	char missing_char = 'a';
	if (input_string_len & 0x1) {
		int split_pos = input_string_len >> 1;
		bool flag = find_missing_char(input_string, 0, split_pos, split_pos, input_string_len, missing_char, missing_index);
		if (flag == true)
		{
			output_result(input_string, missing_index, missing_char);
			return 0;
		}

		flag = find_missing_char(input_string, split_pos + 1, input_string_len, 0, split_pos + 1, missing_char, missing_index);
		if (flag == true)
		{
			output_result(input_string, missing_index, missing_char);
			return 0;
		}

		printf("NA\n");
	} else {
Exemple #2
0
static grn_obj *
command_ruby_eval(grn_ctx *ctx, int nargs, grn_obj **args,
                  grn_user_data *user_data)
{
  grn_obj *script;
  mrb_value result;

  script = VAR(0);
  switch (script->header.domain) {
  case GRN_DB_SHORT_TEXT :
  case GRN_DB_TEXT :
  case GRN_DB_LONG_TEXT :
    break;
  default :
    {
      grn_obj inspected;
      GRN_TEXT_INIT(&inspected, 0);
      grn_inspect(ctx, &inspected, script);
      ERR(GRN_INVALID_ARGUMENT, "script must be a string: <%.*s>",
          (int)GRN_TEXT_LEN(&inspected), GRN_TEXT_VALUE(&inspected));
      GRN_OBJ_FIN(ctx, &inspected);
      return NULL;
    }
    break;
  }

  result = grn_mrb_eval(ctx, GRN_TEXT_VALUE(script), GRN_TEXT_LEN(script));
  output_result(ctx, result);

  return NULL;
}
Exemple #3
0
void print_result_to_file(myResult* result, const char* file, char delimiter) {
    FILE *fp = NULL;
    if ((fp = my_fopen(fp, file, "w")) != NULL) {
        output_result(result, fp, delimiter);
        fclose(fp);
    }
}
Exemple #4
0
static int
serial_loop(WORKER_INFO *info, struct cfg_s *cfg)
{
  P7_BUILDER *bld         = NULL;
  ESL_MSA    *msa         = NULL;
  ESL_MSA    *postmsa     = NULL;
  ESL_MSA   **postmsa_ptr = (cfg->postmsafile != NULL) ? &postmsa : NULL;
  P7_HMM     *hmm         = NULL;
  char        errmsg[eslERRBUFSIZE];
  int         status;

  double      entropy;

  cfg->nali = 0;
  while ((status = esl_msa_Read(cfg->afp, &msa)) == eslOK)
    {
      cfg->nali++;  

      if ((status = set_msa_name(cfg, errmsg, msa)) != eslOK) p7_Fail("%s\n", errmsg); /* cfg->nnamed gets incremented in this call */

                /*         bg   new-HMM trarr gm   om  */
      if ((status = p7_Builder(info->bld, msa, info->bg, &hmm, NULL, NULL, NULL, postmsa_ptr)) != eslOK) p7_Fail("build failed: %s", bld->errbuf);

      entropy = p7_MeanMatchRelativeEntropy(hmm, info->bg);
      if ((status = output_result(cfg, errmsg, cfg->nali, msa, hmm, postmsa, entropy))         != eslOK) p7_Fail(errmsg);

      p7_hmm_Destroy(hmm);
      esl_msa_Destroy(msa);
      esl_msa_Destroy(postmsa);
    }

  return status;
}
Exemple #5
0
void rna_output_subopt(toptions *opts, tsequence *seq, char *algebra, int score, char *result_prettyprint)
{
  if (strcmp(algebra, "count")) {
    //    printf("%s  (%.2f)\n", result_prettyprint, ((float) score) / 100 );
    output_result(opts, seq, 1, // TODO: number of results
                  &score, NULL, result_prettyprint, NULL, -1, -1);
  }
}
Exemple #6
0
void binary_search_column(int matrix[][4], int row, int maxrow, int elem){
    if (matrix == NULL || row < 0 || maxrow < 0)
        return ;
    if (matrix[row][0] == elem)
        return output_result(elem, row, 0);
    else if (matrix[maxrow][0] == elem)
        return output_result(elem, maxrow, 0);
    int mid = row + (maxrow - row)/2;
    while (mid < maxrow && mid > 0){
        if (matrix[mid][0] == elem)
            return output_result(elem, mid, 0);
        else if (matrix[mid][0] > elem){
            maxrow = mid;
        }else
            row = mid;
        mid = row + (maxrow - row)/2;
    }
}
Exemple #7
0
void binary_search_row(int matrix[][4], int row, int column, int elem){
    if (matrix == NULL || row < 0 || column < 0)
        return ;
    int col = 0;
    if (matrix[row][col] == elem)
        return output_result(elem, row, col);
    else if (matrix[row][column] == elem)
        return output_result(elem, row, column);
    int mid = col + (column - col)/2;
    while (mid < column && mid > 0){
        if (matrix[row][mid] == elem)
            return output_result(elem, row, mid);
        else if (matrix[row][mid] > elem){
            column = mid;
        }else
            col = mid;
        mid = col + (column - col)/2;
    }
}
Exemple #8
0
void pipe_read(int signo) {
    int i,num;
    int buf[2];

    read(pipes[0], &num, sizeof(int));
    for (i=0; i<(num/2); i++) {
        read(pipes[0], &buf, sizeof(int)*2);
        record_coordinate(buf[0], buf[1]);
    }
    commit_draw_result();
    output_result();
    free_coordinates();
}
Exemple #9
0
static gboolean time_out(gpointer data) {
    if (full_screen)
        kill(pid, SIGUSR1);
    else {
        if (get_coordinates_num() == 0)
            return FALSE;
        record_coordinate(0xff, 0xff);
        commit_draw_result();
        output_result();
        clear_draw_area(GTK_WIDGET(papier));
        free_coordinates();
    }
    return FALSE;
}
Exemple #10
0
/* init_master_cfg()
 * Called by masters, mpi or serial.
 * Already set:
 *    cfg->hmmfile     - command line arg 1
 *    cfg->alifile     - command line arg 2
 *    cfg->postmsafile - option -O (default NULL)
 *    cfg->fmt         - format of alignment file
 * Sets: 
 *    cfg->afp       - open alignment file                
 *    cfg->abc       - digital alphabet
 *    cfg->hmmfp     - open HMM file
 *    cfg->postmsafp - open MSA resave file, or NULL
 *                   
 * Errors in the MPI master here are considered to be "recoverable",
 * in the sense that we'll try to delay output of the error message
 * until we've cleanly shut down the worker processes. Therefore
 * errors return (code, errmsg) by the ESL_FAIL mech.
 */
static int
init_master_cfg(const ESL_GETOPTS *go, struct cfg_s *cfg, char *errmsg)
{
  int status;

  if (esl_opt_GetString(go, "-o") != NULL) {
    if ((cfg->ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL) 
      ESL_FAIL(eslFAIL, errmsg, "Failed to open -o output file %s\n", esl_opt_GetString(go, "-o"));
  } else cfg->ofp = stdout;

  status = esl_msafile_Open(cfg->alifile, cfg->fmt, NULL, &(cfg->afp));
  if (status == eslENOTFOUND)    ESL_FAIL(status, errmsg, "Alignment file %s doesn't exist or is not readable\n", cfg->alifile);
  else if (status == eslEFORMAT) ESL_FAIL(status, errmsg, "Couldn't determine format of alignment %s\n", cfg->alifile);
  else if (status != eslOK)      ESL_FAIL(status, errmsg, "Alignment file open failed with error %d\n", status);

  if      (esl_opt_GetBoolean(go, "--amino"))   cfg->abc = esl_alphabet_Create(eslAMINO);
  else if (esl_opt_GetBoolean(go, "--dna"))     cfg->abc = esl_alphabet_Create(eslDNA);
  else if (esl_opt_GetBoolean(go, "--rna"))     cfg->abc = esl_alphabet_Create(eslRNA);
  else {
    int type;
    status = esl_msafile_GuessAlphabet(cfg->afp, &type);
    if (status == eslEAMBIGUOUS)    ESL_FAIL(status, errmsg, "Failed to guess the bio alphabet used in %s.\nUse --dna, --rna, or --amino option to specify it.", cfg->alifile);
    else if (status == eslEFORMAT)  ESL_FAIL(status, errmsg, "Alignment file parse failed: %s\n", cfg->afp->errbuf);
    else if (status == eslENODATA)  ESL_FAIL(status, errmsg, "Alignment file %s is empty\n", cfg->alifile);
    else if (status != eslOK)       ESL_FAIL(status, errmsg, "Failed to read alignment file %s\n", cfg->alifile);
    cfg->abc = esl_alphabet_Create(type);
  }
  esl_msafile_SetDigital(cfg->afp, cfg->abc);

  if ((cfg->hmmfp = fopen(cfg->hmmfile, "w")) == NULL) ESL_FAIL(status, errmsg, "Failed to open HMM file %s for writing", cfg->hmmfile);

  if (cfg->postmsafile != NULL) {
    if ((cfg->postmsafp = fopen(cfg->postmsafile, "w")) == NULL) ESL_FAIL(status, errmsg, "Failed to MSA resave file %s for writing", cfg->postmsafile);
  } else cfg->postmsafp = NULL;

  output_header(go, cfg);

  /* with msa == NULL, output_result() prints the tabular results header, if needed */
  output_result(cfg, errmsg, 0, NULL, NULL, NULL, 0.0);
  return eslOK;
}
Exemple #11
0
int main() {
    ExpDiaDBHandler diadb_h = diadb_load();
    ExpDataHandler db_handler = data_load(diadb_h);
    // 表示線区情報
    ExpErr err;
    ExpDLineData_Initiate(db_handler, diadb_h, "data/displine/dlinemst.dat", "data/displine/ptn_match.dat", &err);

    // 探索
    ExpNaviHandler   navi_handler   = ExpNavi_NewHandler(db_handler, &err);
    ExpStationCode code1;
    ExpStationCode code2;
    ExpStation_SetEmptyCode(&code1);
    ExpStation_SetEmptyCode(&code2);
    ExpStation_SharedCodeToCode( db_handler, 22828, &code1);
    ExpStation_SharedCodeToCode( db_handler, 25853, &code2);
    ExpNavi_SetStationEntry( navi_handler, 1, &code1 );
    ExpNavi_SetStationEntry( navi_handler, 2, &code2 );

    //出発
    ExpInt16 searchMode = 0;
    ExpDate  searchDate = 20150625;
    ExpInt16 searchTime = 600;
    ExpDiaVehicles vehicles;
    ExpDiaVehicles_Clear(&vehicles, EXP_TRUE);
    ExpRouteResHandler searchResult = ExpRoute_DiaSearch(  navi_handler, 
                                                            searchMode,
                                                            searchDate,
                                                            searchTime,
                                                            nullptr, 
                                                            0,
                                                            &vehicles );


    output_result(searchResult);

    ExpNavi_DeleteHandler(navi_handler);
    ExpRoute_Delete(searchResult);
    ExpDB_Terminate(db_handler);
    ExpDiaDB_Terminate(diadb_h);
}
Exemple #12
0
static void
serial_master(ESL_GETOPTS *go, struct cfg_s *cfg)
{
  P7_HMM     *hmm = NULL;     
  double     *xv  = NULL;	/* results: array of N scores */
  int        *av  = NULL;	/* optional results: array of N alignment lengths */
  char        errbuf[eslERRBUFSIZE];
  int         status;


  if ((status = init_master_cfg(go, cfg, errbuf)) != eslOK) p7_Fail(errbuf);
  if ((xv = malloc(sizeof(double) * cfg->N)) == NULL)       p7_Fail("allocation failed");
  if (esl_opt_GetBoolean(go, "-a") && 
      (av = malloc(sizeof(int)    * cfg->N)) == NULL)       p7_Fail("allocation failed");

  while ((status = p7_hmmfile_Read(cfg->hfp, &(cfg->abc), &hmm)) != eslEOF) 
    {
      if      (status == eslEOD)       p7_Fail("read failed, HMM file %s may be truncated?", cfg->hmmfile);
      else if (status == eslEFORMAT)   p7_Fail("bad file format in HMM file %s",             cfg->hmmfile);
      else if (status == eslEINCOMPAT) p7_Fail("HMM file %s contains different alphabets",   cfg->hmmfile);
      else if (status != eslOK)        p7_Fail("Unexpected error in reading HMMs from %s",   cfg->hmmfile);

      if (cfg->bg == NULL) {
        if (esl_opt_GetBoolean(go, "--bgflat")) cfg->bg = p7_bg_CreateUniform(cfg->abc);
        else                                    cfg->bg = p7_bg_Create(cfg->abc);
        p7_bg_SetLength(cfg->bg, esl_opt_GetInteger(go, "-L"));  /* set the null model background length in both master and workers. */
      }


      if (esl_opt_GetBoolean(go, "--recal")) {
        if (recalibrate_model(go, cfg, errbuf, hmm)      != eslOK) p7_Fail(errbuf);
      }
      if (process_workunit(go, cfg, errbuf, hmm, xv, av) != eslOK) p7_Fail(errbuf);
      if (output_result   (go, cfg, errbuf, hmm, xv, av) != eslOK) p7_Fail(errbuf);

      p7_hmm_Destroy(hmm);      
    }
  free(xv);
  if (av != NULL) free(av);
}
Exemple #13
0
void findElemFromMatrix(int matrix[][4], int row, int column, int elem)
{
    if (matrix == NULL || row <= 0 || column <= 0)
            return ;

    int top_right_row = 0;
    int top_right_col = column - 1;

    // Socialite Problem
    // Check the top right corner element matrix[i][j]
    //       if matrix[i][j] > elem, delete the whole j column,
    //       otherwise, delete the whole i row.
    while ((top_right_row <= (row - 1)) && (top_right_col >= 0)){
        if (matrix[top_right_row][top_right_col] == elem ){
            return output_result(elem, top_right_row, top_right_col);
        }else if (matrix[top_right_row][top_right_col] < elem){
            ++top_right_row;
        }else{
            --top_right_col;
        }
    }

    // In case the last row,
    // Binary search to find the elem
    if (top_right_row == row - 1){
        return binary_search_row(matrix, row - 1, top_right_col, elem);
    }

    // In case the last column
    // Binary search to find the elem
    if (top_right_col == 0){
        return binary_search_column(matrix, top_right_row, row - 1, elem);
    }

    cout << "The elem " << elem << " is not in this matrix" <<endl;
    return ;
}
Exemple #14
0
int main(int argc, char *argv[])
{
  init();

  if (process_arguments(argc, argv) || help_flag)
  {
    output_help();
    return !help_flag;
  }

  read_input();

  Timer::start();
  vector<Move> list_moves = calculate();
  Timer::stop();

  output_result(list_moves);
  /*
  if (!Board::win(initial_board, 0))
    printf("no!!!\n");
  if (Board::win(make_pair((1 << 65)-1, 0LL), 0))
  printf("yay white wins!!!\n");*/
  return 0;
}
Exemple #15
0
int main(void)
{
	int errcode;
	initscr();
	cbreak();
	noecho();
	while(1){
		unsigned int **result;
		size_t size,points_size;
		point_t *points;
		errcode = input_nqueens_data(stdscr,&size,&points_size,&points);
		if(errcode == 0)return -1;
		if(errcode == 2)break;
		errcode = get_result(size,points_size,points,&result);
		free(points);
		if(errcode == 0)return -1;
		errcode = output_result(stdscr,size,result);
		free_result(result);
		if(errcode == 0)return -1;
		if(errcode == 2)break;
	}
	endwin();
	return 0;
}
static void perform_actual_search(unsigned int edge_num, struct node* start, struct node* end, unsigned int via_nodes_count) {
    struct stack stack, previous_stack;
    unsigned int weight = 0, previous_weight = UINT_MAX, via_nodes_visited_count = 0;
    struct node* current_node;
    struct node* next_node;
    struct path* current_path;

    stack_init(&stack, edge_num);
    stack_init(&previous_stack, edge_num);

    current_node = start;
    current_node->has_been_visited = true;
    current_node->current_outward_path_index = 0;
    if (current_node->is_via_node) {
        via_nodes_visited_count++;
    }
    for (; ;) {
        assert(current_node->current_outward_path_index <= current_node->outward_paths_size);

        if (current_node == end) {
            assert(via_nodes_visited_count <= via_nodes_count);
            if (via_nodes_visited_count == via_nodes_count && weight < previous_weight) {
                stack_copy(&previous_stack, &stack);
                previous_weight = weight;
            }
        }

        if (current_node == end || current_node->current_outward_path_index == current_node->outward_paths_size) {
            if (current_node == start) {
                break;
            }

            current_path = stack_pop(&stack);

            if (current_node->is_via_node) {
                via_nodes_visited_count--;
            }
            current_node->has_been_visited = false;
            weight -= current_path->weight;

            current_node = current_path->from;
        } else {
            current_path = current_node->outward_paths[current_node->current_outward_path_index];
            next_node = current_path->to;
            current_node->current_outward_path_index++;
            if (!next_node->has_been_visited) {
                weight += current_path->weight;
                stack_push(&stack, current_path);
                current_node = next_node;
                current_node->has_been_visited = true;
                current_node->current_outward_path_index = 0;
                if (current_node->is_via_node) {
                    via_nodes_visited_count++;
                }
            }
        }
    }

    output_result(previous_stack);

    stack_free(&stack);
    stack_free(&previous_stack);
}
Exemple #17
0
int test(int argc, char* argv[])
{
    if ( argc != 3 ) {
        std::cout << "error."    << std::endl;
        std::cout << "./run <n> <endtimestep>" << std::endl;
        return 1;
    }

    const int n = std::atoi(argv[1]); 
    
    //----------------------------------------------
    // define space domain
    //----------------------------------------------
    const int  D = 2;
    const real l = 1.0;
    const int hn = 3;
    //const int hn = 2;

    //----------------------------------------------
    // define mesh
    //----------------------------------------------
    const Mesh mesh( hn, l, n, l, n );
    //mesh.print_mesh_data();

    //----------------------------------------------
    // define time domain
    //----------------------------------------------
    const real      dt = 0.1 * mesh.delta(0) / 1.0; 
    //const real dt = 1.0e-5;
    const real endtime = 200 * 1.0 / 1.0;
    const int  endstep = static_cast<int>(endtime / dt);
    //const int  endstep = std::atoi(argv[2]);

    real * u = new real[mesh.size()];
    real * v = new real[mesh.size()];
    real * p = new real[mesh.size()];

    real * nu = new real[mesh.size()];
    real * nv = new real[mesh.size()];

    uniform_value(  u, 0.0, mesh );
    uniform_value(  v, 0.0, mesh );
    uniform_value( nu, 0.0, mesh );
    uniform_value( nv, 0.0, mesh );
    uniform_value(  p, 0.0, mesh );

    boundary_update_u_v( u, v, mesh );

    for ( int timestep = 0 ; timestep < endstep ; timestep++ ) {

        const Iter iter = time_marching_RK1_cavity( nu, nv, p, u, v, dt, mesh );

        swap( &nu, &u );
        swap( &nv, &v );
        //const Iter iter = time_marching_RK2_cavity( &u, &v, &p, dt, mesh );

        if ( timestep % 100 == 0 ) {

            real * const div = new real[mesh.size()];
            uniform_value( div, 0.0, mesh );

            calc_div( div, u, v, mesh );

            const std::string div_filename( "div.dat" );
            const std::string div_position( "cell_center" );
            //save_array( D, div, div_filename, div_position, mesh );

            abs_array( div, mesh );
            const real max_abs_div_value = max_from_array( div, mesh );
            delete[] div;
            
            std::cout << timestep / static_cast<real>(endstep) * 100 << " %";
            std::cout << "\t timestep = " << timestep;
            std::cout << "\titer = " << iter.get_iter() << "\terr = " << iter.get_error();
            std::cout << "\tdiv = " << std::scientific << std::showpos << max_abs_div_value << std::endl;
        }
    }

    const std::string u_filename( "u.dat" );
    const std::string u_position( "staggered_x" );
    save_array( D, u, u_filename, u_position, mesh );

    const std::string v_filename( "v.dat" );
    const std::string v_position( "staggered_y" );
    save_array( D, v, v_filename, v_position, mesh );

    const std::string p_filename( "p.dat" );
    const std::string p_position( "cell_center" );
    save_array( D, p, p_filename, p_position, mesh );

    output_result( "result_u.dat", "result_v.dat", u, v, mesh );

    delete[] u;
    delete[] v;
    delete[] p;
    delete[] nu;
    delete[] nv;

    return 0;
}
Exemple #18
0
int main(int argc, char ** argv){
  gsl_rng * r = gsl_rng_alloc (gsl_rng_taus2);
  //  gsl_rng_set(r,time(NULL));
  FILE * log = fopen("recover_shanon.log","w");

#ifdef MPI
  MPI_Init(&argc, &argv);
#endif

  int n_samples = 500;
  if(argc != 2 && argc != 3 && argc != 4 ){
    printf("recover_shanon <input_image> [total n photons scattered] [n samples]\n");
    printf("The defaults are n photons = n pixels and n samples = 500\n");
    exit(0);
  }
  if(argc >= 4){
    n_samples = atoi(argv[3]);
  }
  Image * a = sp_image_read(argv[1],0);
  double image_photons = 0;
  for(int i = 0;i<sp_image_size(a);i++){
    image_photons += sp_real(a->image->data[i]);
  }
  int n_photons = sp_image_size(a);
  if(argc >= 3){
    n_photons = atoi(argv[2]);
  }
  sp_image_scale(a,n_photons/image_photons);

  image_photons = 0;
  for(int i = 0;i<sp_image_size(a);i++){
    image_photons += sp_real(a->image->data[i]);
  }
  printf("Total photons %f\n",image_photons);
  printf("Average photon count %f\n",image_photons/sp_image_size(a));

  printf("Creating rotated samples...");
  fflush(stdout);
  Image * avg_samples = sp_image_alloc(sp_image_x(a),sp_image_y(a),sp_image_z(a));
  sp_matrix ** random_orient_samples = get_rotated_samples(a,n_samples,r,avg_samples);

  //  Image ** orient_samples = get_orient_samples(a,n_samples,r);
  printf("done\n");

  image_photons = 0;
  for(int i = 0;i<sp_image_size(a);i++){
    image_photons += random_orient_samples[0]->data[i];
  }
  printf("Total photons in sample 1 - %f\n",image_photons);


  //  Image * avg_samples = get_image_avg(orient_samples,n_samples);
  /*  printf("Rotating samples...");
  fflush(stdout);
  sp_matrix ** random_orient_samples = get_random_orient_samples(orient_samples,n_samples,r);
  printf("done\n");*/

  //  sp_image_write(random_orient_samples[1],"sample.png",COLOR_GRAYSCALE);
  /*  for(int i = 0;i<n_samples;i++){
    sp_image_free(orient_samples[i]);
  }
  free(orient_samples);*/

  Image * avg_random_samples = sp_image_alloc(sp_image_x(a),sp_image_y(a),sp_image_z(a));
  for(int i = 0;i<n_samples;i++){
    for(int k = 0;k<sp_image_size(avg_random_samples);k++){
      sp_real(avg_random_samples->image->data[k]) += random_orient_samples[i]->data[k];
    }
  }
  
  /* Create rotation list */
  /*  printf("Creating rotation list...");
  fflush(stdout);
  sp_matrix *** rotated_random_orient_samples = get_rotation_list(random_orient_samples,  n_samples);
  printf("done\n");*/


  /* Initialize with uniform distribution in [0,1) */
  Image * restore = sp_image_alloc(sp_image_x(a),sp_image_y(a),sp_image_z(a));
  Image * after = sp_image_alloc(sp_image_x(a),sp_image_y(a),sp_image_z(a));
  for(int i = 0;i<sp_image_size(a);i++){
    restore->image->data[i] = sp_cinit(gsl_rng_uniform(r),0);
  }
  image_normalize(restore);

  sp_image_write(a,"orig.png",COLOR_GRAYSCALE);
  sp_image_write(avg_samples,"avg_sample.png",COLOR_GRAYSCALE);
  sp_image_write(avg_random_samples,"avg_random_sample.png",COLOR_GRAYSCALE);
  for(int iter = 0;;iter++){
    tomas_iteration(restore,after,random_orient_samples,n_samples);
    Image * tmp = restore;
    restore = after;
    after = tmp;

    for(int i = 0;i<sp_image_size(a);i++){
      sp_real(after->image->data[i]) = 0;
    }
    
    output_result(a,restore,log,iter);
  }
  return 0;
}
Exemple #19
0
SBMLSIM_EXPORT void print_result(myResult* result) {
    output_result(result, stdout, ' ');
}
Exemple #20
0
/* Print the result R out to stdout.  */
static void print_result(result * r) {
    output_result(stdout, r);
}
Exemple #21
0
/* mpi_master()
 * The MPI version of hmmbuild.
 * Follows standard pattern for a master/worker load-balanced MPI program (J1/78-79).
 * 
 * A master can only return if it's successful. 
 * Errors in an MPI master come in two classes: recoverable and nonrecoverable.
 * 
 * Recoverable errors include all worker-side errors, and any
 * master-side error that do not affect MPI communication. Error
 * messages from recoverable messages are delayed until we've cleanly
 * shut down the workers.
 * 
 * Unrecoverable errors are master-side errors that may affect MPI
 * communication, meaning we cannot count on being able to reach the
 * workers and shut them down. Unrecoverable errors result in immediate
 * p7_Fail()'s, which will cause MPI to shut down the worker processes
 * uncleanly.
 */
static void
mpi_master(const ESL_GETOPTS *go, struct cfg_s *cfg)
{
  int         xstatus       = eslOK;	/* changes from OK on recoverable error */
  int         status;
  int         have_work     = TRUE;	/* TRUE while alignments remain  */
  int         nproc_working = 0;	        /* number of worker processes working, up to nproc-1 */
  int         wi;          	        /* rank of next worker to get an alignment to work on */
  char       *buf           = NULL;	/* input/output buffer, for packed MPI messages */
  int         bn            = 0;
  ESL_MSA    *msa           = NULL;
  P7_HMM     *hmm           = NULL;
  P7_BG      *bg            = NULL;
  ESL_MSA   **msalist       = NULL;
  ESL_MSA    *postmsa       = NULL;
  int        *msaidx        = NULL;
  char        errmsg[eslERRBUFSIZE];
  MPI_Status  mpistatus; 
  int         n;
  int         pos;

  double      entropy;
  
  /* Master initialization: including, figure out the alphabet type.
   * If any failure occurs, delay printing error message until we've shut down workers.
   */
  if (xstatus == eslOK) { if ((status = init_master_cfg(go, cfg, errmsg)) != eslOK) xstatus = status; }
  if (xstatus == eslOK) { bn = 4096; if ((buf = malloc(sizeof(char) * bn)) == NULL) { sprintf(errmsg, "allocation failed"); xstatus = eslEMEM; } }
  if (xstatus == eslOK) { if ((msalist = malloc(sizeof(ESL_MSA *) * cfg->nproc)) == NULL) { sprintf(errmsg, "allocation failed"); xstatus = eslEMEM; } }
  if (xstatus == eslOK) { if ((msaidx  = malloc(sizeof(int)       * cfg->nproc)) == NULL) { sprintf(errmsg, "allocation failed"); xstatus = eslEMEM; } }
  MPI_Bcast(&xstatus, 1, MPI_INT, 0, MPI_COMM_WORLD);
  if (xstatus != eslOK) {  MPI_Finalize(); p7_Fail(errmsg); }
  ESL_DPRINTF1(("MPI master is initialized\n"));

  bg = p7_bg_Create(cfg->abc);

  for (wi = 0; wi < cfg->nproc; wi++) { msalist[wi] = NULL; msaidx[wi] = 0; } 

  /* Worker initialization:
   * Because we've already successfully initialized the master before we start
   * initializing the workers, we don't expect worker initialization to fail;
   * so we just receive a quick OK/error code reply from each worker to be sure,
   * and don't worry about an informative message. 
   */
  MPI_Bcast(&(cfg->abc->type), 1, MPI_INT, 0, MPI_COMM_WORLD);
  MPI_Reduce(&xstatus, &status, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
  if (status != eslOK) { MPI_Finalize(); p7_Fail("One or more MPI worker processes failed to initialize."); }
  ESL_DPRINTF1(("%d workers are initialized\n", cfg->nproc-1));


  /* Main loop: combining load workers, send/receive, clear workers loops;
   * also, catch error states and die later, after clean shutdown of workers.
   * 
   * When a recoverable error occurs, have_work = FALSE, xstatus !=
   * eslOK, and errmsg is set to an informative message. No more
   * errmsg's can be received after the first one. We wait for all the
   * workers to clear their work units, then send them shutdown signals,
   * then finally print our errmsg and exit.
   * 
   * Unrecoverable errors just crash us out with p7_Fail().
   */
  wi = 1;
  while (have_work || nproc_working)
    {
      if (have_work) 
	{
	  if ((status = esl_msa_Read(cfg->afp, &msa)) == eslOK) 
	    {
	      cfg->nali++;  
	      ESL_DPRINTF1(("MPI master read MSA %s\n", msa->name == NULL? "" : msa->name));
	    }
	  else 
	    {
	      have_work = FALSE;
	      if      (status == eslEFORMAT)  { xstatus = eslEFORMAT; snprintf(errmsg, eslERRBUFSIZE, "Alignment file parse error:\n%s\n", cfg->afp->errbuf); }
	      else if (status == eslEINVAL)   { xstatus = eslEFORMAT; snprintf(errmsg, eslERRBUFSIZE, "Alignment file parse error:\n%s\n", cfg->afp->errbuf); }
	      else if (status != eslEOF)      { xstatus = status;     snprintf(errmsg, eslERRBUFSIZE, "Alignment file read unexpectedly failed with code %d\n", status); }
	      ESL_DPRINTF1(("MPI master has run out of MSAs (having read %d)\n", cfg->nali));
	    } 
	}

      if ((have_work && nproc_working == cfg->nproc-1) || (!have_work && nproc_working > 0))
	{
	  if (MPI_Probe(MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &mpistatus) != 0) { MPI_Finalize(); p7_Fail("mpi probe failed"); }
	  if (MPI_Get_count(&mpistatus, MPI_PACKED, &n)                != 0) { MPI_Finalize(); p7_Fail("mpi get count failed"); }
	  wi = mpistatus.MPI_SOURCE;
	  ESL_DPRINTF1(("MPI master sees a result of %d bytes from worker %d\n", n, wi));

	  if (n > bn) {
	    if ((buf = realloc(buf, sizeof(char) * n)) == NULL) p7_Fail("reallocation failed");
	    bn = n; 
	  }
	  if (MPI_Recv(buf, bn, MPI_PACKED, wi, 0, MPI_COMM_WORLD, &mpistatus) != 0) { MPI_Finalize(); p7_Fail("mpi recv failed"); }
	  ESL_DPRINTF1(("MPI master has received the buffer\n"));

	  /* If we're in a recoverable error state, we're only clearing worker results;
           * just receive them, don't unpack them or print them.
           * But if our xstatus is OK, go ahead and process the result buffer.
	   */
	  if (xstatus == eslOK)	
	    {
	      pos = 0;
	      if (MPI_Unpack(buf, bn, &pos, &xstatus, 1, MPI_INT, MPI_COMM_WORLD)     != 0) { MPI_Finalize();  p7_Fail("mpi unpack failed");}
	      if (xstatus == eslOK) /* worker reported success. Get the HMM. */
		{
		  ESL_DPRINTF1(("MPI master sees that the result buffer contains an HMM\n"));
		  if (p7_hmm_MPIUnpack(buf, bn, &pos, MPI_COMM_WORLD, &(cfg->abc), &hmm) != eslOK) {  MPI_Finalize(); p7_Fail("HMM unpack failed"); }
		  ESL_DPRINTF1(("MPI master has unpacked the HMM\n"));

		  if (cfg->postmsafile != NULL) {
		    if (esl_msa_MPIUnpack(cfg->abc, buf, bn, &pos, MPI_COMM_WORLD, &postmsa) != eslOK) { MPI_Finalize(); p7_Fail("postmsa unpack failed");}
		  } 

		  entropy = p7_MeanMatchRelativeEntropy(hmm, bg);
		  if ((status = output_result(cfg, errmsg, msaidx[wi], msalist[wi], hmm, postmsa, entropy)) != eslOK) xstatus = status;

		  esl_msa_Destroy(postmsa); postmsa = NULL;
		  p7_hmm_Destroy(hmm);      hmm     = NULL;
		}
	      else	/* worker reported an error. Get the errmsg. */
		{
		  if (MPI_Unpack(buf, bn, &pos, errmsg, eslERRBUFSIZE, MPI_CHAR, MPI_COMM_WORLD) != 0) { MPI_Finalize(); p7_Fail("mpi unpack of errmsg failed"); }
		  ESL_DPRINTF1(("MPI master sees that the result buffer contains an error message\n"));
		}
	    }
	  esl_msa_Destroy(msalist[wi]);
	  msalist[wi] = NULL;
	  msaidx[wi]  = 0;
	  nproc_working--;
	}

      if (have_work)
	{   
	  ESL_DPRINTF1(("MPI master is sending MSA %s to worker %d\n", msa->name == NULL ? "":msa->name, wi));
	  if (esl_msa_MPISend(msa, wi, 0, MPI_COMM_WORLD, &buf, &bn) != eslOK) p7_Fail("MPI msa send failed");
	  msalist[wi] = msa;
	  msaidx[wi]  = cfg->nali; /* 1..N for N alignments in the MSA database */
	  msa = NULL;
	  wi++;
	  nproc_working++;
	}
    }
  
  /* On success or recoverable errors:
   * Shut down workers cleanly. 
   */
  ESL_DPRINTF1(("MPI master is done. Shutting down all the workers cleanly\n"));
  for (wi = 1; wi < cfg->nproc; wi++) 
    if (esl_msa_MPISend(NULL, wi, 0, MPI_COMM_WORLD, &buf, &bn) != eslOK) p7_Fail("MPI msa send failed");

  free(buf);
  free(msaidx);
  free(msalist);
  p7_bg_Destroy(bg);

  if (xstatus != eslOK) { MPI_Finalize(); p7_Fail(errmsg); }
  else                  return;
}
Exemple #22
0
/* mpi_master()
 * The MPI version of hmmsim.
 * Follows standard pattern for a master/worker load-balanced MPI program (J1/78-79).
 * 
 * A master can only return if it's successful. 
 * Errors in an MPI master come in two classes: recoverable and nonrecoverable.
 * 
 * Recoverable errors include all worker-side errors, and any
 * master-side error that do not affect MPI communication. Error
 * messages from recoverable messages are delayed until we've cleanly
 * shut down the workers.
 * 
 * Unrecoverable errors are master-side errors that may affect MPI
 * communication, meaning we cannot count on being able to reach the
 * workers and shut them down. Unrecoverable errors result in immediate
 * p7_Fail()'s, which will cause MPI to shut down the worker processes
 * uncleanly.
 */
static void
mpi_master(ESL_GETOPTS *go, struct cfg_s *cfg)
{
  int              xstatus       = eslOK; /* changes in the event of a recoverable error */
  P7_HMM          *hmm           = NULL;  /* query HMM                                 */
  P7_HMM         **hmmlist       = NULL;  /* queue of HMMs being worked on, 1..nproc-1 */
  char            *wbuf          = NULL;  /* working buffer for sending packed profiles and receiving packed results. */
  int              wn            = 0;
  double          *xv            = NULL;  /* results: array of N scores */
  int             *av            = NULL;  /* optional results: array of N alignment lengths */
  int              have_work     = TRUE;
  int              nproc_working = 0;
  int              wi;
  int              pos;
  char             errbuf[eslERRBUFSIZE];
  int              status;
  MPI_Status       mpistatus;
  

  /* Master initialization. */
  if (init_master_cfg(go, cfg, errbuf)            != eslOK) p7_Fail(errbuf);
  if (minimum_mpi_working_buffer(go, cfg->N, &wn) != eslOK) p7_Fail("mpi pack sizes must have failed");
  ESL_ALLOC(wbuf,    sizeof(char)     * wn);
  ESL_ALLOC(xv,      sizeof(double)   * cfg->N);
  if (esl_opt_GetBoolean(go, "-a"))
    ESL_ALLOC(av,    sizeof(int)      * cfg->N);
  ESL_ALLOC(hmmlist, sizeof(P7_HMM *) * cfg->nproc);
  for (wi = 0; wi < cfg->nproc; wi++) hmmlist[wi] = NULL;

  /* Standard design pattern for data parallelization in a master/worker model. (J1/78-79).  */
  wi = 1;
  while (have_work || nproc_working)
    {
      /* Get next work unit: one HMM, <hmm> */
      if (have_work) 
	{
	  if ((status = p7_hmmfile_Read(cfg->hfp, &(cfg->abc), &hmm)) != eslOK) 
	    {
	      have_work = FALSE;
	      if      (status == eslEOD)       { xstatus = status; sprintf(errbuf, "read failed, HMM file %s may be truncated?", cfg->hmmfile); }
	      else if (status == eslEFORMAT)   { xstatus = status; sprintf(errbuf, "bad file format in HMM file %s",             cfg->hmmfile); }
	      else if (status == eslEINCOMPAT) { xstatus = status; sprintf(errbuf, "HMM file %s contains different alphabets",   cfg->hmmfile); }
	      else if (status != eslEOF)       { xstatus = status; sprintf(errbuf, "Unexpected error in reading HMMs from %s",   cfg->hmmfile); }


        if (cfg->bg == NULL) { // first time only
          if (esl_opt_GetBoolean(go, "--bgflat")) cfg->bg = p7_bg_CreateUniform(cfg->abc);
          else                                    cfg->bg = p7_bg_Create(cfg->abc);
        }
        //this next step is redundant, but it avoids a race condition above.
        p7_bg_SetLength(cfg->bg, esl_opt_GetInteger(go, "-L"));  /* set the null model background length in both master and workers. */


	    }
	}

      /* If we have work but no free workers, or we have no work but workers
       * are still working, then wait for a result to return from any worker.
       */
      if ( (have_work && nproc_working == cfg->nproc-1) || (! have_work && nproc_working > 0))
	{
	  if (MPI_Recv(wbuf, wn, MPI_PACKED, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &mpistatus) != 0) p7_Fail("mpi recv failed");
	  wi = mpistatus.MPI_SOURCE;
	  
	  /* Check the xstatus before printing results.
           * If we're in a recoverable error state, we're only clearing worker results, prior to a clean failure
	   */
	  if (xstatus == eslOK)	
	    {
	      pos = 0;
	      if (MPI_Unpack(wbuf, wn, &pos, &xstatus, 1, MPI_INT, MPI_COMM_WORLD)     != 0)     p7_Fail("mpi unpack failed");
	      if (xstatus == eslOK) /* worker reported success. Get the results. */
		{
		  if (MPI_Unpack(wbuf, wn, &pos, xv,     cfg->N, MPI_DOUBLE, MPI_COMM_WORLD) != 0)   p7_Fail("score vector unpack failed");
		  if (esl_opt_GetBoolean(go, "-a") &&
		      MPI_Unpack(wbuf, wn, &pos, av,     cfg->N, MPI_INT,    MPI_COMM_WORLD) != 0)   p7_Fail("alilen vector unpack failed");
		  if ((status = output_result(go, cfg, errbuf, hmmlist[wi], xv, av))  != eslOK) xstatus = status;
		}
	      else	/* worker reported a user error. Get the errbuf. */
		{
		  if (MPI_Unpack(wbuf, wn, &pos, errbuf, eslERRBUFSIZE, MPI_CHAR, MPI_COMM_WORLD) != 0) p7_Fail("mpi unpack of errbuf failed");
		  have_work = FALSE;
		  p7_hmm_Destroy(hmm);
		}
	    }
	  p7_hmm_Destroy(hmmlist[wi]);
	  hmmlist[wi] = NULL;
	  nproc_working--;
	}
	
      /* If we have work, assign it to a free worker; else, terminate the free worker. */
      if (have_work) 
	{
	  p7_hmm_mpi_Send(hmm, wi, 0, MPI_COMM_WORLD, &wbuf, &wn);
	  hmmlist[wi] = hmm;
	  wi++;
	  nproc_working++;
	}
    }

  /* Tell all the workers (1..nproc-1) to shut down by sending them a NULL workunit. */
  for (wi = 1; wi < cfg->nproc; wi++)
    if (p7_hmm_mpi_Send(NULL, wi, 0, MPI_COMM_WORLD, &wbuf, &wn) != eslOK) p7_Fail("MPI HMM send failed");	


  free(hmmlist);
  free(wbuf);
  free(xv);
  if (av != NULL) free(av);
  if (xstatus != eslOK) p7_Fail(errbuf);
  else                  return;

 ERROR:
  if (hmmlist != NULL) free(hmmlist);
  if (wbuf    != NULL) free(wbuf);
  if (xv      != NULL) free(xv);
  if (av      != NULL) free(av);
  p7_Fail("Fatal error in mpi_master");
}
Exemple #23
0
static int
thread_loop(ESL_THREADS *obj, ESL_WORK_QUEUE *queue, struct cfg_s *cfg)
{
  int          status    = eslOK;
  int          sstatus   = eslOK;
  int          processed = 0;
  WORK_ITEM   *item;
  void        *newItem;

  int           next     = 1;
  PENDING_ITEM *top      = NULL;
  PENDING_ITEM *empty    = NULL;
  PENDING_ITEM *tmp      = NULL;

  char        errmsg[eslERRBUFSIZE];

  esl_workqueue_Reset(queue);
  esl_threads_WaitForStart(obj);

  status = esl_workqueue_ReaderUpdate(queue, NULL, &newItem);
  if (status != eslOK) esl_fatal("Work queue reader failed");
      
  /* Main loop: */
  item = (WORK_ITEM *) newItem;
  while (sstatus == eslOK) {
    sstatus = esl_msa_Read(cfg->afp, &item->msa);
    if (sstatus == eslOK) {
      item->nali = ++cfg->nali;
      if (set_msa_name(cfg, errmsg, item->msa) != eslOK) p7_Fail("%s\n", errmsg);
    }
    if (sstatus == eslEOF && processed < cfg->nali) sstatus = eslOK;
	  
    if (sstatus == eslOK) {
      status = esl_workqueue_ReaderUpdate(queue, item, &newItem);
      if (status != eslOK) esl_fatal("Work queue reader failed");

      /* process any results */
      item = (WORK_ITEM *) newItem;
      if (item->processed == TRUE) {
	++processed;

	/* try to keep the input output order the same */
	if (item->nali == next) {
	  sstatus = output_result(cfg, errmsg, item->nali, item->msa, item->hmm, item->postmsa, item->entropy);
	  if (sstatus != eslOK) p7_Fail(errmsg);

	  p7_hmm_Destroy(item->hmm);
	  esl_msa_Destroy(item->msa);
	  esl_msa_Destroy(item->postmsa);

	  ++next;

	  /* output any pending msa as long as the order
	   * remains the same as read in.
	   */
	  while (top != NULL && top->nali == next) {
	    sstatus = output_result(cfg, errmsg, top->nali, top->msa, top->hmm, top->postmsa, top->entropy);
	    if (sstatus != eslOK) p7_Fail(errmsg);

	    p7_hmm_Destroy(top->hmm);
	    esl_msa_Destroy(top->msa);
	    esl_msa_Destroy(top->postmsa);

	    tmp = top;
	    top = tmp->next;

	    tmp->next = empty;
	    empty     = tmp;
	    
	    ++next;
	  }
	} else {
	  /* queue up the msa so the sequence order is the same in
	   * the .sto and .hmm
	   */
	  if (empty != NULL) {
	    tmp   = empty;
	    empty = tmp->next;
	  } else {
	    ESL_ALLOC(tmp, sizeof(PENDING_ITEM));
	  }

	  tmp->nali     = item->nali;
	  tmp->hmm      = item->hmm;
	  tmp->msa      = item->msa;
	  tmp->postmsa  = item->postmsa;
	  tmp->entropy  = item->entropy;

	  /* add the msa to the pending list */
	  if (top == NULL || tmp->nali < top->nali) {
	    tmp->next = top;
	    top       = tmp;
	  } else {
	    PENDING_ITEM *ptr = top;
	    while (ptr->next != NULL && tmp->nali > ptr->next->nali) {
	      ptr = ptr->next;
	    }
	    tmp->next = ptr->next;
	    ptr->next = tmp;
	  }
	}

	item->nali      = 0;
	item->processed = FALSE;
	item->hmm       = NULL;
	item->msa       = NULL;
	item->postmsa   = NULL;
	item->entropy   = 0.0;
      }
    }
  }

  if (top != NULL) esl_fatal("Top is not empty\n");

  while (empty != NULL) {
    tmp   = empty;
    empty = tmp->next;
    free(tmp);
  }

  status = esl_workqueue_ReaderUpdate(queue, item, NULL);
  if (status != eslOK) esl_fatal("Work queue reader failed");

  if (sstatus == eslEOF)
    {
      /* wait for all the threads to complete */
      esl_threads_WaitForFinish(obj);
      esl_workqueue_Complete(queue);  
    }

  return sstatus;

 ERROR:
  return eslEMEM;
}