int main(int argc, char *argv[]) { char *buf; int done[NUMOPS]; int fd[NUMOPS]; int i; int numbytes, numfiles; if (argc < 2) { fprintf(stderr, "Usage: %s filename1 filename2 ...\n", argv[0]); return 1; } else if (argc > NUMOPS + 1) { fprintf(stderr, "%s: only supports %d simultaneous operations\n", argv[0], NUMOPS); return 1; } numfiles = argc - 1; for (i = 0; i < numfiles; i++) { /* set up the I/O operations */ done[i] = 0; if ((fd[i] = open(argv[i+1], O_RDONLY)) == -1) { fprintf(stderr, "Failed to open %s:%s\n", argv[i+1], strerror(errno)); return 1; } if (initaio(fd[i], i) == -1) { fprintf(stderr, "Failed to setup I/O op %d:%s\n", i, strerror(errno)); return 1; } if (readstart(i) == -1) { fprintf(stderr, "Failed to start read %d:%s\n", i, strerror(errno)); return 1; } } for ( ; ; ) { /* loop and poll */ dowork(); for (i = 0; i < numfiles; i++) { if (done[i]) continue; numbytes = readcheck(i, &buf); if ((numbytes == -1) && (errno == EINPROGRESS)) continue; if (numbytes <= 0) { if (numbytes == 0) fprintf(stderr, "End of file on %d\n", i); else fprintf(stderr, "Failed to read %d:%s\n", i, strerror(errno)); done[i] = 1; continue; } processbuffer(i, buf, numbytes); reinit(i); if (readstart(i) == -1) { fprintf(stderr, "Failed to start read %d:%s\n", i, strerror(errno)); done[i] = 1; } } } }
void cs_restore( boolean strip_slashes, /* should leading slash be stripped? */ int fcount, /* number of filename patterns in flist */ char **flist) /* filename patterns to select for restore */ { int file_data = 0; int file_data_read; csd_fhdr_t file_hdr; char name[MAXPATHLEN + 1]; /* name to compare for selective rstr */ char *name_compare = &name[0]; char slink[MAXPATHLEN + 1]; char *save_path = (char *)NULL; /* Prev path opened for SAM_fd */ char *last_path = NULL; /* Last compared path */ char *slash_loc; /* Last loc of a '/' in filename */ char unused; /* A safe value for slash_loc */ int namelen; int skipping; struct sam_perm_inode perm_inode; struct sam_stat sb; struct sam_vsn_section *vsnp; void *data; int n_acls; aclent_t *aclp; /* * Read the dump file */ while (csd_read_header(&file_hdr) > 0) { namelen = file_hdr.namelen; csd_read(name, namelen, &perm_inode); data = NULL; vsnp = NULL; aclp = NULL; n_acls = 0; if (file_hdr.flags & CSD_FH_DATA) { BUMP_STAT(data_files); file_data_read = 0; file_data++; } else { file_data_read = file_data = 0; } if (S_ISREQ(perm_inode.di.mode)) { SamMalloc(data, perm_inode.di.psize.rmfile); } csd_read_next(&perm_inode, &vsnp, slink, data, &n_acls, &aclp); if (dont_process_this_entry) { /* if problem reading inode */ BUMP_STAT(errors); goto skip_file; } /* Skip privileged files except root */ if (SAM_PRIVILEGE_INO(perm_inode.di.version, perm_inode.di.id.ino)) { if (perm_inode.di.id.ino != SAM_ROOT_INO) { goto skip_file; } } /* select subset of file names to restore. */ name_compare = &name[0]; if (fcount != 0) { /* Search the file name list */ int i; for (i = 0; i < fcount; i++) { /* * If stripping first slash to make relative * path and dump file name begins with '/' and * select file name does not being with '/', * then move name compare ahead past first '/' */ if (strip_slashes && ('/' == *name) && ('/' != *flist[i])) { name_compare = &name[1]; } /* * filecmp returns: * 0 for no match * 1 exact match, * 2 name prefix of flist[i], * 3 flist[i] prefix of name. */ if (filecmp(name_compare, flist[i]) != 0) { break; } } if (i >= fcount) { if (S_ISSEGI(&perm_inode.di)) { skipping = 1; goto skip_seg_file; } goto skip_file; } } /* * If stripping first slash to make relative path and * dump file name begins with '/' * then move location ahead past first '/' */ if (strip_slashes && ('/' == *name)) { name_compare = &name[1]; } /* * Make sure that we don't restore into a non-SAM-FS * filesystem. * If relative path to be restored, check current directory for * being in a SAM-FS filesystem. If absolute path to be * restored, search backwards through path for a viable * SAM-FS path by calling sam_stat() and checking the * directory attributes. */ if ((strip_slashes && ('/' == *name)) || ('/' != *name)) { char *check_name = "."; char *next_name; if ((save_path != (char *)NULL) && (strcmp(check_name, save_path) != 0)) { check_samfs_fs(); free(save_path); (void) close(SAM_fd); save_path = strdup(check_name); SAM_fd = open_samfs(save_path); } else if (save_path == (char *)NULL) { check_samfs_fs(); save_path = strdup(check_name); SAM_fd = open_samfs(save_path); } /* * Check if last path matches this path. Otherwise, * If subpath does not yet exist, make directories * as needed. */ if (last_path != NULL) { char *name_cmp; int path_len; path_len = strlen(last_path); /* Copy filename */ name_cmp = strdup(name_compare); if ((slash_loc = strrchr(name_cmp, '/')) != NULL) { *slash_loc = '\0'; if ((path_len == strlen(name_cmp)) && (last_path[path_len-1] == name_cmp[path_len-1]) && (bcmp(last_path, name_cmp, path_len) == 0)) { free(name_cmp); goto restore_file; } } free(name_cmp); } if (last_path != NULL) { free(last_path); last_path = NULL; } /* Save path for check above */ last_path = strdup(name_compare); if ((slash_loc = strrchr(last_path, '/')) != NULL) { *slash_loc = '\0'; } else { free(last_path); last_path = NULL; } /* Copy filename */ next_name = check_name = strdup(name_compare); while ((slash_loc = strchr(next_name, '/')) != (char *)NULL) { *slash_loc = '\0'; if (mkdir(check_name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { if (EEXIST == errno) { *slash_loc = '/'; next_name = slash_loc + 1; continue; } BUMP_STAT(errors); error(1, errno, catgets(catfd, SET, 222, "%s: Cannot mkdir()"), check_name); break; } *slash_loc = '/'; next_name = slash_loc + 1; } free(check_name); } else { /* * Search through absolute path from end for * SAM-FS file system */ /* Copy filename */ char *check_name = strdup(name_compare); slash_loc = &unused; do { *slash_loc = '\0'; /* * Make sure we don't restore into a * non-SAM-FS filesystem * by calling sam_stat() and checking the * directory attributes. */ if (sam_stat(check_name, &sb, sizeof (sb)) == 0 && SS_ISSAMFS(sb.attr)) { if ((save_path != (char *)NULL) && (strcmp(check_name, save_path) != 0)) { free(save_path); (void) close(SAM_fd); save_path = strdup(check_name); SAM_fd = open_samfs(save_path); } else if (save_path == (char *)NULL) { save_path = strdup(check_name); SAM_fd = open_samfs(save_path); } break; } slash_loc = strrchr(check_name, '/'); } while (slash_loc != (char *)NULL); /* * If no SAM-FS file system found in path, issue error */ if ((char *)NULL == slash_loc) { BUMP_STAT(errors); BUMP_STAT(errors_dir); error(1, 0, catgets(catfd, SET, 259, "%s: Not a SAM-FS file."), name_compare); } free(check_name); } restore_file: if (verbose) { sam_ls(name_compare, &perm_inode, NULL); } /* If not already offline */ if (!perm_inode.di.status.b.offline) { /* If archived */ if (perm_inode.di.arch_status) { print_reslog(log_st, name_compare, &perm_inode, "online"); } } else if (perm_inode.di.status.b.pextents) { print_reslog(log_st, name_compare, &perm_inode, "partial"); } /* * if segment index, we have to skip the data segment inodes * before we can get to the dumped data. */ if (!(S_ISSEGI(&perm_inode.di) && file_data)) { sam_restore_a_file(name_compare, &perm_inode, vsnp, slink, data, n_acls, aclp, file_data, &file_data_read); } skipping = 0; skip_seg_file: if (S_ISSEGI(&perm_inode.di)) { struct sam_perm_inode seg_inode; int i; offset_t seg_size; int no_seg; sam_id_t seg_parent_id = perm_inode.di.id; /* * If we are restoring the data, don't restore the * data segment inodes. This means we will lose the * archive copies, if any. */ if (file_data) skipping++; /* * Read each segment inode. If archive copies * overflowed, * read vsn sections directly after each segment inode. */ seg_size = (offset_t)perm_inode.di.rm.info.dk.seg_size * SAM_MIN_SEGMENT_SIZE; no_seg = (perm_inode.di.rm.size + seg_size - 1) / seg_size; for (i = 0; i < no_seg; i++) { struct sam_vsn_section *seg_vsnp; readcheck(&seg_inode, sizeof (seg_inode), 5002); if (swapped) { if (sam_byte_swap( sam_perm_inode_swap_descriptor, &seg_inode, sizeof (seg_inode))) { error(0, 0, catgets(catfd, SET, 13531, "%s: segment inode byte " "swap error - skipping."), name); dont_process_this_entry = 1; } } if (!(SAM_CHECK_INODE_VERSION( seg_inode.di.version))) { if (debugging) { fprintf(stderr, "cs_restore: seg %d " "inode version %d\n", i, seg_inode.di.version); } error(0, 0, catgets(catfd, SET, 739, "%s: inode version incorrect - " "skipping"), name); dont_process_this_entry = 1; } if (skipping || dont_process_this_entry) { continue; } seg_inode.di.parent_id = seg_parent_id; seg_vsnp = NULL; csd_read_mve(&seg_inode, &seg_vsnp); if (verbose) { sam_ls(name_compare, &seg_inode, NULL); } sam_restore_a_file(name_compare, &seg_inode, seg_vsnp, NULL, NULL, 0, NULL, file_data, NULL); if (seg_vsnp) { SamFree(seg_vsnp); } } /* * Now that we have skipped the data segment inodes * we can restore the dumped data if any. */ if (file_data) { sam_restore_a_file(name_compare, &perm_inode, vsnp, slink, data, n_acls, aclp, file_data, &file_data_read); } } skip_file: if (data) { SamFree(data); data = NULL; } if (vsnp) { SamFree(vsnp); vsnp = NULL; } if (aclp) { SamFree(aclp); aclp = NULL; } if (file_data && file_data_read == 0) { skip_embedded_file_data(); } } if (last_path != NULL) { free(last_path); } if (save_path != (char *)NULL) { free(save_path); } if (open_dir_fd > 0) { (void) close(open_dir_fd); } pop_permissions_stack(); pop_times_stack(); }
/*! */ void Reptation_method::runWithVariables(Properties_manager & prop, System * sys, Wavefunction_data * wfdata, Pseudopotential * psp, ostream & output) { allocateIntermediateVariables(sys, wfdata); prop.setSize(wf->nfunc(), nblock, nstep, 1, sys, wfdata); prop.initializeLog(average_var); Properties_manager prop_center; string logfile, label_temp; prop.getLog(logfile, label_temp); label_temp+="_cen"; prop_center.setLog(logfile, label_temp); prop_center.setSize(wf->nfunc(), nblock, nstep, 1, sys, wfdata); prop_center.initializeLog(average_var); cout.precision(10); output.precision(10); Sample_point * center_samp(NULL); sys->generateSample(center_samp); Reptile_point pt; Array1 <Reptile> reptiles; int nreptile=1; if(!readcheck(readconfig,reptiles)) { Array1 <Config_save_point> configs; generate_sample(sample,wf,wfdata,guidewf,nreptile,configs); reptiles.Resize(nreptile); for(int r=0; r< nreptile; r++) { reptiles[r].direction=1; configs(r).restorePos(sample); wf->notify(all_electrons_move,0); wf->updateLap(wfdata,sample); for(int i=0; i< reptile_length; i++) { doublevar main_diffusion; slither(1,reptiles[r].reptile, mygather,pt,main_diffusion); reptiles[r].reptile.push_back(pt); } } } nreptile=reptiles.GetDim(0); //assert(reptile.size()==reptile_length); //Branch limiting variables //we start off with no limiting, and establish the parameters after the //first block. This seems to be reasonably stable, since it's mostly //to keep the reptile from getting stuck. eref=0; energy_cutoff=1e16; //--------begin averaging.. Array3 <doublevar> derivatives_block(nblock, sys->nIons(), 3); for(int block=0; block< nblock; block++) { //clock_t block_start_time=clock(); doublevar avg_age=0; doublevar max_age=0; doublevar main_diff=0; double ntry=0, naccept=0; double nbounce=0; for(int r=0; r< nreptile; r++) { Reptile & curr_reptile=reptiles[r]; //Control variable that will be set to one when //we change direction, which signals to recalculate //the wave function int recalc=1; for(int step=0; step< nstep; step++) { psp->randomize(); if(recalc) { if(curr_reptile.direction==1) curr_reptile.reptile[reptile_length-1].restorePos(sample); else curr_reptile.reptile[0].restorePos(sample); } doublevar main_diffusion; doublevar accept=slither(curr_reptile.direction, curr_reptile.reptile,mygather, pt, main_diffusion); ntry++; if(accept+rng.ulec() > 1.0) { recalc=0; naccept++; main_diff+=main_diffusion; if(curr_reptile.direction==1) { curr_reptile.reptile.pop_front(); curr_reptile.reptile.push_back(pt); } else { curr_reptile.reptile.pop_back(); curr_reptile.reptile[0].branching=pt.branching; curr_reptile.reptile.push_front(pt); } } else { recalc=1; curr_reptile.direction*=-1; nbounce++; } for(deque<Reptile_point>::iterator i=curr_reptile.reptile.begin(); i!=curr_reptile.reptile.end(); i++) { i->age++; avg_age+=i->age/reptile_length; if(i->age > max_age) max_age=i->age; } Properties_point avgpt; get_avg(curr_reptile.reptile, avgpt); avgpt.parent=0; avgpt.nchildren=1; //just one walker avgpt.children(0)=0; prop.insertPoint(step, 0, avgpt); int cpt=reptile_length/2+1; Properties_point centpt; get_center_avg(curr_reptile.reptile, centpt); centpt.parent=0; centpt.nchildren=1; centpt.children(0)=0; prop_center.insertPoint(step, 0, centpt); curr_reptile.reptile[cpt].restorePos(center_samp); for(int i=0; i< densplt.GetDim(0); i++) densplt(i)->accumulate(center_samp,1.0); if(center_trace != "" && (block*nstep+step)%trace_wait==0) { ofstream checkfile(center_trace.c_str(), ios::app); if(!checkfile)error("Couldn't open ", center_trace); checkfile << "SAMPLE_POINT { \n"; write_config(checkfile, sample); checkfile << "}\n\n"; } } //step } //reptile prop.endBlock(); prop_center.endBlock(); double ntot=parallel_sum(nstep); Properties_block lastblock; prop.getLastBlock(lastblock); eref=lastblock.avg(Properties_types::total_energy,0); energy_cutoff=10*sqrt(lastblock.var(Properties_types::total_energy,0)); nbounce=parallel_sum(nbounce); naccept=parallel_sum(naccept); ntry=parallel_sum(ntry); avg_age=parallel_sum(avg_age); for(int i=0; i< densplt.GetDim(0); i++) densplt(i)->write(); storecheck(reptiles, storeconfig); main_diff=parallel_sum(main_diff); if(output) { output << "****Block " << block << " acceptance " << naccept/ntry << " average steps before bounce " << ntot/nbounce << endl; output << "average age " << avg_age/ntot << " max age " << max_age << endl; output << "eref " << eref << " cutoff " << energy_cutoff << endl; output << "Green's function sampler:" << endl; sampler->showStats(output); prop.printBlockSummary(output); output << "Center averaging: " << endl; prop_center.printBlockSummary(output); } sampler->resetStats(); //clock_t block_end_time=clock(); //cout << mpi_info.node << ":CPU block time " //// << double(block_end_time-block_start_time)/double(CLOCKS_PER_SEC) // << endl; } //block if(output) { output << "############## Reptation Done ################\n"; output << "End averages " << endl; prop.printSummary(output,average_var); output << "Center averages " << endl; prop_center.printSummary(output,average_var); //Print out a PDB file with one of the reptiles, for visualization purposes if(print_pdb) { ofstream pdbout("rmc.pdb"); pdbout.precision(3); pdbout << "REMARK 4 Mode COMPLIES WITH FORMAT V. 2.0\n"; int nelectrons=sample->electronSize(); int counter=1; string name="H"; for(int e=0; e<nelectrons; e++) { for(deque<Reptile_point>::iterator i=reptiles[0].reptile.begin(); i!=reptiles[0].reptile.end(); i++) { pdbout<<"ATOM"<<setw(7)<< counter <<" " <<name<<" UNK 1" <<setw(12)<< i->electronpos[e][0] <<setw(8)<< i->electronpos[e][1] <<setw(8)<< i->electronpos[e][2] << " 1.00 0.00\n"; counter++; } } int nions=sys->nIons(); Array1 <doublevar> ionpos(3); vector <string> atomnames; sys->getAtomicLabels(atomnames); for(int i=0; i< nions; i++) { sys->getIonPos(i,ionpos); pdbout<<"ATOM"<<setw(7)<< counter <<" " <<atomnames[i]<<" UNK 1" <<setw(12)<< ionpos[0] <<setw(8)<< ionpos[1] <<setw(8)<< ionpos[2] << " 1.00 0.00\n"; } counter=1; for(int e=0; e<nelectrons; e++) { for(deque<Reptile_point>::iterator i=reptiles[0].reptile.begin(); i!=reptiles[0].reptile.end(); i++) { if(i != reptiles[0].reptile.begin()) { pdbout << "CONECT" << setw(5) << counter << setw(5) << counter-1 << endl; } counter++; } } } //------------Done PDB file } delete center_samp; wfdata->clearObserver(); deallocateIntermediateVariables(); }