/* * Given a stream to the input file in the *.td-format, this reads from the file * the decomposition represented by this file. If the file is not conforming to * the format, it throws a corresponding std::invalid_argument with one of the * error messages defined above. */ void read_tree_decomposition(std::ifstream& fin, tree_decomposition& T) { current_state = COMMENT_SECTION; n_graph = -1; n_decomp = 0; width = -2; bags_seen.clear(); bags.clear(); if(!fin.is_open()){ throw std::invalid_argument(FILE_ERROR); } std::string line; std::string delimiter = " "; while(std::getline(fin, line)) { if(line == "" || line == "\n") { throw std::invalid_argument(EMPTY_LINE); } std::vector<std::string> tokens; size_t oldpos = 0; size_t newpos = 0; while(newpos != std::string::npos) { newpos = line.find(delimiter, oldpos); tokens.push_back(line.substr(oldpos, newpos-oldpos)); oldpos = newpos + delimiter.size(); } if (tokens[0] == "c") { continue; } else if (tokens[0] == "s") { read_solution(tokens); } else if (tokens[0] == "b") { read_bag(tokens); } else { read_decomp_edge(tokens, T); } } if (current_state == BAGS) { for (auto it = bags.begin(); it != bags.end(); it++) { T.add_bag(*it); } } if (width != real_width) { throw std::invalid_argument(INV_SOLN); } }
double compare_solution(char* lpath, char* rpath, int timestep, int nump, int nSyncFiles) { int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); double* lsol; double* rsol; int lsize; int rsize; read_solution(&lsol, &lsize, NULL, NULL, nump, rank, timestep, nSyncFiles, lpath); read_solution(&rsol, &rsize, NULL, NULL, nump, rank, timestep, nSyncFiles, rpath); double maxdiff=0.0; double gblmaxdiff; if(lsize != rsize) { printf("Error: Solution sizes different: %d, %d\n", lsize, rsize); assert(lsize == rsize); } for(int i=0;i<lsize;i++) { double diff = fabs(lsol[i]-rsol[i]); if(diff > maxdiff) maxdiff = diff; } free(lsol); free(rsol); MPI_Reduce(&maxdiff, &gblmaxdiff, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD); MPI_Barrier(MPI_COMM_WORLD); //TODO: debugging only if(rank == 0) printf("Timestep: %d, maximum difference: %e\n", timestep, gblmaxdiff); return gblmaxdiff; }
main() { tsp_instance t; /* tsp points */ tsp_solution s; /* tsp solution */ int i; /* counter*/ double solution_cost(); read_tsp(&t); /*print_tsp(&t);*/ read_solution(&s); printf("OPTIMAL SOLUTION COST = %7.1f\n",solution_cost(&s,&t)); print_solution(&s); initialize_solution(t.n, &s); printf("solution_cost = %7.1f\n",solution_cost(&s,&t)); print_solution(&s); /* solution_count=0; random_sampling(&t,1500000,&s); printf("random sampling %d iterations, cost = %7.1f\n", solution_count,solution_cost(&s,&t)); print_solution(&s); solution_count=0; repeated_hill_climbing(&t,195,&s); printf("repeated hill climbing %d iterations, cost = %7.1f\n", solution_count,solution_cost(&s,&t)); print_solution(&s); */ solution_count=0; repeated_annealing(&t,3,&s); printf("repeated annealing %d iterations, cost = %7.1f\n", solution_count,solution_cost(&s,&t)); print_solution(&s); }
/** read initial guesses; * return vector of guesses on success; NULL on failure; * output numebr of guesses in the variable pointed by 'number_of_guesses' */ struct fclib_solution* fclib_read_guesses (const char *path, int *number_of_guesses) { struct fclib_solution *guesses; hid_t file_id, main_id, id; int nv, nr, nl, i; char num [128]; if ((file_id = H5Fopen (path, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) /* overwrite */ { fprintf (stderr, "ERROR: opening file failed\n"); return 0; } if (! read_nvnunrnl (file_id, &nv, &nr, &nl)) return 0; if (H5Lexists (file_id, "/guesses", H5P_DEFAULT)) { IO (main_id = H5Gopen (file_id, "/guesses", H5P_DEFAULT)); IO (H5LTread_dataset_int (file_id, "/guesses/number_of_guesses", number_of_guesses)); MM (guesses = malloc ((*number_of_guesses) * sizeof (struct fclib_solution))); for (i = 0; i < *number_of_guesses; i ++) { snprintf (num, 128, "%d", i+1); IO (id = H5Gopen (main_id, num, H5P_DEFAULT)); read_solution (id, nv, nr, nl, &guesses [i]); IO (H5Gclose (id)); } IO (H5Gclose (main_id)); } IO (H5Fclose (file_id)); return guesses; }
/** read solution; * return solution on success; NULL on failure */ struct fclib_solution* fclib_read_solution (const char *path) { struct fclib_solution *solution; hid_t file_id, id; int nv, nr, nl; if ((file_id = H5Fopen (path, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0) /* overwrite */ { fprintf (stderr, "ERROR: opening file failed\n"); return 0; } MM (solution = malloc (sizeof (struct fclib_solution))); if (! read_nvnunrnl (file_id, &nv, &nr, &nl)) return 0; IO (id = H5Gopen (file_id, "/solution", H5P_DEFAULT)); read_solution (id, nv, nr, nl, solution); IO (H5Gclose (id)); IO (H5Fclose (file_id)); return solution; }
int main(int argc, char** argv) { int rank; int size; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); if(argc != 5) { fprintf(stderr, "argc %d\n", argc); fprintf(stderr, "Usage: %s <left> <right> <numSyncFiles> <tolerance>\n" "where <left> and <right> are different" "N-procs_case directories\n", argv[0]); MPI_Finalize(); return 1; } char* lpath = argv[1]; char* rpath = argv[2]; int nSyncFiles = atoi(argv[3]); double tolerance = atof(argv[4]); int ndof; int nshg; int solsize; double* solution; std::set<int>* l_timesteps = find_timesteps(lpath, nSyncFiles); std::set<int>* r_timesteps = find_timesteps(rpath, nSyncFiles); std::set<int>* timesteps_to_check = new std::set<int>; std::set_intersection(l_timesteps->begin(), l_timesteps->end(), r_timesteps->begin(), r_timesteps->end(), std::inserter(*timesteps_to_check, timesteps_to_check->begin())); delete l_timesteps; delete r_timesteps; if(rank == 0) printf("Found %d common timesteps\n", timesteps_to_check->size()); #ifdef DBGONLY read_solution(&solution, &solsize, &nshg, &ndof, size, rank, 0, numSyncFiles, "./"); printf("nshg: %d, ndof: %d\n", nshg, ndof); assert(solsize == ndof*nshg); #endif double maxerror = 0.0; double error; double gblmaxerror; for(std::set<int>::iterator i = timesteps_to_check->begin(); i!=timesteps_to_check->end();i++) { error = compare_solution(lpath, rpath, *i, size, nSyncFiles); if(error>maxerror) maxerror = error; } delete timesteps_to_check; MPI_Barrier(MPI_COMM_WORLD); MPI_Reduce(&maxerror, &gblmaxerror, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD); if(rank == 0) printf("Maximum difference across all timesteps: %e\n", gblmaxerror); MPI_Finalize(); return (gblmaxerror > tolerance); }