int main() { boost::timer timer; double h_CallResult[OPT_N]; double h_CallConfidence[OPT_N]; double S[OPT_N], X[OPT_N], T[OPT_N]; for (int i = 0; i < OPT_N; ++i) { S[i] = 100.; X[i] = 100.; T[i] = 0.25; } MonteCarlo(h_CallResult, h_CallConfidence, S, X, T); for (int i = 0; i < OPT_N; ++i) { std::cout.precision(4); std::cout.width(10); std::cout << S[i] << ": "; std::cout.width(10); std::cout << h_CallResult[i]; std::cout.width(15); std::cout << h_CallConfidence[i] << std::endl; } std::cout << "\ntime = " << timer.elapsed() << " sec" << std::endl; return 0; }
// 主搜索例程 void SearchMain(int maxNode) { int i, vl, vlLast, nDraw; int nCurrTimer, nLimitTimer, nLimitNodes; bool bUnique; #ifndef CCHESS_A3800 int nBookMoves; uint32_t dwMoveStr; BookStruct bks[MAX_GEN_MOVES]; #endif // 主搜索例程包括以下几个步骤: // 1. 遇到和棋则直接返回 if (Search.pos.IsDraw() || Search.pos.RepStatus(3) > 0) { #ifndef CCHESS_A3800 printf("nobestmove\n"); fflush(stdout); #endif return; } // 3. 如果深度为零则返回静态搜索值 if (maxNode == 0) { #ifndef CCHESS_A3800 printf("info depth 0 score %d\n", SearchQuiesc(Search.pos, -MATE_VALUE, MATE_VALUE)); fflush(stdout); printf("nobestmove\n"); fflush(stdout); #endif return; } MonteCarlo(maxNode); }
int main (int argc, char const *argv[]) { int i; double total = 0.0; double est = 0.0; double var; double error, aberr; /* This time uses lib's implmentation. */ for (i = 0; i < N; i++) { var = drand48(); total += MonteCarlo(var); } est = total/N; printf("The final result is %5.10f\t\n", est); error = est - PI; printf("The error is %.10f\t\n", error); aberr = (double)1/(double)(2 * sqrt(N)); printf("The absolute error is %.10f\t\n", aberr); return 0; }
int main(int argc, const char * argv[]) { // Benchmark count from 2^7 to 2^22 for (std::size_t c = 128; c <= 4194304; c *= 2) { // Do 100 measurements for low point count stats<MonteCarlo, Real>(MonteCarlo(c), 100); } return 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); }
void ThreadSimulate(int *n) { MonteCarlo(CTHR[*n], NMC/NTHREAD, (REAL) EMEAN, (REAL) ESTDV, (REAL) SIGMA, (REAL) XYANGLE, (REAL) AZIMUTH); pthread_exit(0); }
// main code int main(int argc, const char *argv[]) { ///////////////////////// Initial conditions //////////////////////// int dimensions = 2; random_seed(); //Initial coodinates (Random) double *coordinates = malloc(dimensions * sizeof(double)); for (int i =0; i<dimensions; i++) { coordinates[i] = RandomRange(-0.1, 0.1) ; } //Opening log file FILE *LogFile, *Energyfile, *CoordinateFile; LogFile=fopen("/Users/abel/Programes/MonteCarloLib/example/test.log", "w"); CoordinateFile=fopen("/Users/abel/Programes/MonteCarloLib/example/coordinates.out", "w"); Energyfile=fopen("/Users/abel/Programes/MonteCarloLib/example/energy.out", "w"); conditions InitialConditions; InitialConditions.coordinates = coordinates; InitialConditions.dimensions = dimensions; InitialConditions.factor = 0.1; InitialConditions.numberofcycles = 1000000; InitialConditions.kb = 0.0019872041; // kcal/(mol·K) InitialConditions.temperature = 1000; // K InitialConditions.takesample = 50; InitialConditions.regulator = 0.001; InitialConditions.coordinatesfile = CoordinateFile; InitialConditions.energyfile = Energyfile; //Print initial coodinates printf ("Initial Coordinates:"); for (int i =0; i< InitialConditions.dimensions; i++) { printf (" %f",InitialConditions.coordinates[i]); } printf ("\n"); printf("Initial energy: %f\n",HarmonicPotential2D(InitialConditions.coordinates)); //////////////// Call Monte Carlo Function ////////////////////// results Res = MonteCarlo(HarmonicPotential2D, AlterationFree, InitialConditions, LogFile); ///////////////////// Print final results /////////////////////// fclose(CoordinateFile); fclose(Energyfile); fclose(LogFile); printf("Final energy %f\n",Res.energy); printf ("Final coordinates:"); for (int i =0; i< InitialConditions.dimensions; i++) { printf (" %f",Res.coordinates[i]); } printf ("\n"); printf("Final specific heat (Cv): %f\n",Res.Cv); printf ("\n"); return 0; }
/* * This is the main function * 1: scale input parameters from [-10, +10] to fit within desired range values * 2: read from <hostname>.tmp and write to <hostname>.* * 3: execute 'SPICE <hostname>.*' * 4: read output <hostname>.out * 5: find cost * 6: save log information to file <hostname>.log * 7: ALTER and MC simulation * 8: return cost */ double errfunc(char *filename, double *x) { /*double currentcost;*/ /*total cost*/ int i, ii; int ccode; char laux[LONGSTRINGSIZE], hostname[SHORTSTRINGSIZE] = "0", filename_x[SHORTSTRINGSIZE] = "0"; /* <hostname>.* <hostname>.out <hostname>.tmp <hostname>.log*/ FILE *fspice_input, *fspice_output, *fspice_tmp, *fspice_log; /**/ /*Step1: scale input parameters from [-10, +10] to fit within desired range values*/ #ifdef DEBUG printf("DEBUG: errfunc.c - Step1\n"); #endif for (i = 0; i < MAXPARAMETERS; i++) { if (parameters[i].format) { parameters[i].value = scaleto(x[i], -10, +10, parameters[i].minimum, parameters[i].maximum, parameters[i].format); if (parameters[i].value<0) { ii=1; /*printf("INFO: errfunc.c - Step1 -- Negative values\n");*/ } } } /**/ /*Step2: read from <hostname>.tmp and write to <hostname>.**/ /* */ /* It is not necessary to check every time for the possibility to read/write. Move to initicialization if possible!! */ /* */ #ifdef DEBUG printf("DEBUG: errfunc.c - Step2\n"); #endif if ((ccode = gethostname(hostname, sizeof(hostname))) != 0) { printf("errfunc.c - Step2 -- gethostname failed, ccode = %d\n", ccode); exit(EXIT_FAILURE); } /* printf("host name: %s\n", hostname); */ ii=strpos2(hostname, ".", 1); if (ii) /* hostname is "longmorn.xx.xx.xx" */ hostname[ii-1]='\0'; sprintf(lkk, "%s%s", hostname, ".tmp"); /* hostname is "longmorn" */ if ((fspice_tmp =fopen(lkk ,"rt")) == 0) { /* netlist to simulate given by "hostname" */ printf("errfunc.c - Step2 -- Cannot read from tmp file: %s\n", lkk); exit(EXIT_FAILURE); } switch(spice) { case 1: /*Eldo*/ sprintf(lkk, "%s%s", hostname, ".cir"); break; case 2: /*HSPICE*/ sprintf(lkk, "%s%s", hostname, ".sp"); break; case 3: /*LTspice*/ sprintf(lkk, "%s%s", hostname, ".net"); break; case 4: /*Spectre*/ sprintf(lkk, "%s%s", hostname, ".scs"); break; case 50: /*Qucs*/ sprintf(lkk, "%s%s", hostname, ".txt"); break; case 51: /*ngspice*/ sprintf(lkk, "%s%s", hostname, ".sp"); break; case 100: /*general*/ sprintf(lkk, "%s%s", hostname, ".txt"); break; default: printf("errfunc.c - Step2 -- Something unexpected has happened!\n"); exit(EXIT_FAILURE); } if ((fspice_input =fopen(lkk ,"wt")) == 0) { /*netlist to simulate given by 'hostname'*/ printf("errfunc.c - Step2 -- Cannot write to output file: %s\n", lkk); exit(EXIT_FAILURE); } /*Step2.1: ".end" not yet found*/ fgets2(lkk, LONGSTRINGSIZE, fspice_tmp); /*read and*/ fprintf(fspice_input, "%s\n", lkk); /*write the first line*/ while (!P_eof(fspice_tmp)) { fgets2(lkk, LONGSTRINGSIZE, fspice_tmp); strcpy(laux, lkk); /*detect ".end", ".END", ".End", ... */ Str2Lower(laux); StripSpaces(laux); /* avoid spaces after the command ".end" */ if (!strcmp(laux, ".end")) break; /***** -------------- *********** -------------- *****/ /***** -------------- ** BEGIN ** -------------- *****/ if (lkk[0]!='*') { i=inlinestrpos(lkk); ii=1; ReadSubKey(laux, lkk, &ii, '#', '#', 0); if ( (laux[0]=='\0') || (ii>(int)strlen(lkk)) || ((i<ii) && (i!=0)) ) { /* does it contains #<text>#? */ if ((int)strlen(lkk) && (!RFModule(lkk, 1, fspice_input)) ) fprintf(fspice_input, "%s\n", lkk); /*no, write line to <hostname>.* */ } else { /*yes, replace #<text># in this line */ if (!RFModule(lkk, 1, fspice_input)) { ReplaceSymbol(lkk, 1); fprintf(fspice_input, "%s\n", lkk); /* write line to <hostname>.* */ } } } /***** -------------- ** END ** -------------- *****/ /***** -------------- *********** -------------- *****/ } switch(spice) { case 1: /*Eldo*/ if (strcmp(laux, ".end")) { /*Exit if ".end" is not found*/ printf("errfunc.c - Step2.1 -- End not found in %s.cir\n", filename); exit(EXIT_FAILURE); } break; case 2: /*HSPICE*/ if (strcmp(laux, ".end")) { /*Exit if ".end" is not found*/ printf("errfunc.c - Step2.1 -- End not found in %s.sp\n", filename); exit(EXIT_FAILURE); } break; case 3: /*LTspice*/ if (strcmp(laux, ".end")) { /*Exit if ".end" is not found*/ printf("errfunc.c - Step2.1 -- End not found in %s.net\n", filename); exit(EXIT_FAILURE); } break; case 4: /*Spectre*/ /* ".end" does not exist in Spectre syntax */ break; case 50: /*Qucs*/ /* ".end" does not exist in Qucs syntax */ break; case 51: /*ngspice*/ if (strcmp(laux, ".end")) { /*Exit if ".end" is not found*/ printf("errfunc.c - Step2.1 -- End not found in %s.sp\n", filename); exit(EXIT_FAILURE); } break; case 100: /*general*/ break; default: printf("errfunc.c - Step2.1 -- Something unexpected has happened!\n"); exit(EXIT_FAILURE); } /*Step2.3: Add ".end" where required*/ switch(spice) { case 1: /*Eldo*/ fprintf(fspice_input, "%s\n", ".end"); break; case 2: /*HSPICE*/ fprintf(fspice_input, "%s\n", ".end"); break; case 3: /*LTspice*/ fprintf(fspice_input, "%s\n", ".end"); break; case 4: /*Spectre*/ break; case 50: /*Qucs*/ break; case 51: /*ngspice*/ fprintf(fspice_input, "%s\n", ".end"); break; case 100: /*general*/ break; default: printf("errfunc.c - Step2.3 -- Something unexpected has happened!\n"); exit(EXIT_FAILURE); } fclose(fspice_input); fclose(fspice_tmp); /**/ /*Step3: execute 'SPICE <hostname>.*'*/ #ifdef DEBUG printf("DEBUG: errfunc.c - Step3\n"); #endif switch(spice) { case 1: /*Eldo*/ sprintf(lkk, "nice -n 19 eldo -noconf -i %s.cir > %s.out", hostname, hostname); break; case 2: /*HSPICE*/ #ifndef __MINGW32__ sprintf(lkk, "nice -n 19 hspice -i %s.sp -o %s.lis > /dev/null", hostname, hostname); #else sprintf(lkk, "hspice -i %s.sp -o %s.lis > NUL", hostname, hostname); #endif break; case 3: /*LTspice*/ #ifndef __MINGW32__ sprintf(lkk, "nice -n 19 ltspice -b %s.net > /dev/null", hostname); #else sprintf(lkk, "ltspice -b %s.net > NUL", hostname); #endif break; case 4: /*Spectre*/ sprintf(lkk, "nice -n 19 spectremdl -batch %s.mdl -design %s.scs > /dev/null", hostname, hostname); break; case 50: /*Qucs*/ #ifndef __MINGW32__ sprintf(lkk, "nice -n 19 qucsator -i %s.txt -o %s.dat > /dev/null", hostname, hostname); #else sprintf(lkk, "qucsator -i %s.txt -o %s.dat > NUL", hostname, hostname); #endif break; case 51: /*ngspice*/ #ifndef __MINGW32__ sprintf(lkk, "nice -n 19 ngspice -b -o %s.out %s.sp > /dev/null 2>&1", hostname, hostname); #else sprintf(lkk, "ngspice -o %s.out %s.sp > NUL", hostname, hostname); #endif break; case 100: /*general*/ #ifndef __MINGW32__ sprintf(lkk, "nice -n 19 ./general.sh %s %s", hostname, hostname); #else sprintf(lkk, "./general.sh %s %s", hostname, hostname); #endif break; default: printf("errfunc.c - Step3 -- Something unexpected has happened!\n"); exit(EXIT_FAILURE); } ii=system(lkk); #ifndef __MINGW32__ if (WIFSIGNALED(ii) && (WTERMSIG(ii) == SIGINT || WTERMSIG(ii) == SIGQUIT)) { printf("errfunc.c - Step3 -- Ctrl-C key pressed. Exiting optimization loop.\n"); fflush(stdout); #ifdef MPI /* exit(EXIT_FAILURE); */ #else return(0); /*returned simulation cost is zero; Ctrl-C detection*/ #endif } #endif /**/ /*Step4: read output <hostname>.out, find maxcost and save log information to file <hostname>.log*/ #ifdef DEBUG printf("DEBUG: errfunc.c - Step4\n"); #endif switch(spice) { case 1: /*Eldo*/ sprintf(lkk, "%s.out", hostname); break; case 2: /*HSPICE*/ sprintf(lkk, "%s.lis", hostname); break; case 3: /*LTspice*/ sprintf(lkk, "%s.log", hostname); break; case 4: /*Spectre*/ sprintf(lkk, "%s.measure", hostname); break; case 50: /*Qucs*/ sprintf(lkk, "%s.dat", hostname); break; case 51: /*ngspice*/ sprintf(lkk, "%s.out", hostname); break; case 100: /*general*/ sprintf(lkk, "%s.out", hostname); break; default: printf("errfunc.c - Step4 -- Something unexpected has happened!\n"); exit(EXIT_FAILURE); } maxcost=0; #ifdef DEBUG ProcessOutputFile(lkk, 3); /* =>3:mem+file */ #else ProcessOutputFile(lkk, 1); /* =>1:mem */ #endif /**/ /*Step5: find cost*/ /* #ifdef DEBUG */ /* printf("DEBUG: errfunc.c - Step5\n"); */ /* #endif */ /* currentcost=CostFunction(); */ /* if (maxcost<currentcost) */ /* maxcost=currentcost; */ /**/ /*Step6: save log information to file <hostname>.log*/ /* #ifdef DEBUG */ /* printf("DEBUG: errfunc.c - Step6\n"); */ /* #endif */ /* if (LOG) { */ /* LogtoFile(currentcost); */ /* } */ /**/ /*Step7: ALTER and MC simulation*/ #ifdef DEBUG printf("DEBUG: errfunc.c - Step7\n"); #endif if ( (((AllConstraintsMet()) && (AlterMCcost > maxcost)) || (AlterMCcost > maxcost)) && (AlterMC)) { switch (AlterMC) { case 1: /*Monte Carlo simulation*/ /* bla bla bla*/ printf("INFO: errfunc.c - Step7 -- altermc=%d\n", AlterMC); fflush(stdout); if (LOG) { sprintf(laux, "%s.log", hostname); if ((fspice_log=fopen(laux,"at")) == 0) { printf("errfunc.c - Step7 -- Cannot open log file: %s\n", laux); exit(EXIT_FAILURE); } fprintf(fspice_log, "Alter: 1 - altermc=%d\n", AlterMC); fclose(fspice_log); } strcpy (filename_x, filename); strcat(filename_x, ".cfg"); sprintf(lkk, "%s%s", hostname, ".tmp"); /* hostname is "longmorn" */ sprintf(laux, "%s%s", hostname, ".mc"); /* hostname is "longmorn" */ MonteCarlo(filename_x, lkk, laux); /* execute the 'monte' tool */ sprintf(lkk, "%s%s", hostname, ".mc"); /* hostname is "longmorn" */ if ((fspice_input =fopen(lkk ,"rt")) == 0) { /*netlist to simulate given by "hostname" */ printf("errfunc.c - Step7 -- Cannot read from mc file: %s\n", lkk); exit(EXIT_FAILURE); } sprintf(lkk, "%s%s", hostname, ".tmp"); if ((fspice_output =fopen(lkk ,"wt")) == 0) { /*netlist to simulate given by 'hostname' */ printf("errfunc.c - Step7 -- Cannot write to tmp file: %s\n", lkk); exit(EXIT_FAILURE); } while (!P_eof(fspice_input)) { /* reads from <hostname>.mc and writes to <hostname>.tmp */ fgets2(lkk, LONGSTRINGSIZE, fspice_input); /* read line from <hostname>.mc */ strcpy(laux, lkk); Str2Lower(laux); fprintf(fspice_output, "%s\n", lkk); /* write line to <hostname>.tmp */ } fclose(fspice_input); fclose(fspice_output); AlterMC=AlterMC-1; /*decrease by one; nothing more will be executed*/ break; case 2: /*Alter simulation - variable 'AlterMC'=2*/ case 3: /*Alter simulation - variable 'AlterMC'=3 and will execute a MonteCarlo simulation afterwards*/ /* bla bla bla*/ printf("INFO: errfunc.c - Step7 -- altermc=%d\n", AlterMC); fflush(stdout); if (LOG) { sprintf(laux, "%s.log", hostname); if ((fspice_log=fopen(laux,"at")) == 0) { printf("errfunc.c - Step7 -- Cannot open log file: %s\n", laux); exit(EXIT_FAILURE); } fprintf(fspice_log, "Alter: 2 - altermc=%d\n", AlterMC); fclose(fspice_log); } sprintf(lkk, "%s%s", hostname, ".tmp"); /*hostname is "longmorn"*/ if ((fspice_tmp =fopen(lkk ,"r+t")) == 0) { /*netlist to simulate given by "hostname"*/ printf("errfunc.c - Step7 -- Cannot read from tmp file: %s\n", lkk); exit(EXIT_FAILURE); } if ((fspice_log =fopen("alter.inc" ,"rt")) == 0) { /*If file 'alter.inc' does not exist */ strcpy (filename_x, filename); strcat(filename_x, ".cfg"); CreateALTERinc(filename_x, lkk, 1); /*execute the 'alter' tool*/ fseek(fspice_tmp, 0, SEEK_END); /*properly position the pointer*/ } else { /* file 'alter.inc', so use it instead of the 'alter' tool */ #ifndef __MINGW32__ fseek(fspice_tmp, -5, SEEK_END); /*properly position the pointer*/ #else fseek(fspice_tmp, -6, SEEK_END); /*properly position the pointer*/ #endif fprintf(fspice_tmp, ".INCLUDE alter.inc\n"); /*write*/ fclose(fspice_log); } /* fseek(fspice_tmp, 0, SEEK_END); */ /*properly position the pointer*/ fprintf(fspice_tmp, ".end\n"); /*add ".end"*/ fclose(fspice_tmp); AlterMC=AlterMC-2; /*decrease by two; it will not execute a Monte Carlo simulation next time if equal to zero*/ break; default: printf("errfunc.c - Step7 -- Something unexpected has happened!\n"); exit(EXIT_FAILURE); } } /**/ /*Step8: return maximum cost (ALTER on MONTECARLO)*/ #ifdef DEBUG printf("DEBUG: errfunc.c - Step8\n"); #endif return (maxcost); }