Example #1
0
// Read matrix from file
void readMatrix(const char *filename, Eigen::MatrixXf& result)
{
    File matfile((std::string(filename)));
    // get number of rows from file
    const int rows = matfile.number_of_line();
    /* check whether number of lines in words is equal to the number of words into vocab file */
    if (rows != vocab_size){
        throw std::runtime_error("Size mismatch between words and vocabulary files!!!\n");
    }
    // get number of rows from file
    const int cols = matfile.number_of_column(' ');
    if (verbose) fprintf(stderr, "words vector size = %d\n",cols);

    // opening file
    matfile.open();

    // Populate matrix with numbers.
    result.resize(rows,cols);
    char *line=NULL;
    char delim=' ';
    for (int i = 0; i < rows; i++){
        line = matfile.getline();
        char *ptr = line;
        char *olds = line;
        char olddelim = delim;
        int j=0;
        while(olddelim && *line) {
            while(*line && (delim != *line)) line++;
            *line ^= olddelim = *line; // olddelim = *line; *line = 0;
            result(i,j++) = atof(olds);
            *line++ ^= olddelim; // *line = olddelim; line++;
            olds = line;
        }
        free(ptr);
    }
    // closing file
    matfile.close();
};
Example #2
0
int main(int argc, char **argv)
{
    MPI_Init(&argc, &argv);
    int commsize, commrank;
    MPI_Comm_size(MPI_COMM_WORLD, &commsize);
    MPI_Comm_rank(MPI_COMM_WORLD, &commrank);
    const bool isRoot = commrank == 0;
    const bool isLast = commrank == commsize - 1;

    if (isRoot)
        std::cout << "Initialization...\n" << std::endl;

    RuntimeConfiguration conf(argc, argv);

    // Compute my start and end time
    const double timeStart = conf.timeSliceSize() * commrank;
    const double timeEnd = conf.timeSliceSize() * (commrank + 1);

    if (isRoot)
    std::cout << "Running with:\n"
        << " - initial diffusion coefficient: " << conf.nu0() << "\n"
        << " - frequence of diffusion coefficient: " << conf.nufreq() << "\n"
        << " - advection velocity in x: " << conf.cx() << "\n"
        << " - advection velocity in y: " << conf.cy() << "\n"
        << " - advection velocity in z: " << conf.cz() << "\n"
        << " - spatial discretization step: " << conf.dx() << "\n"
        << " - endtime: " << conf.endTime() << "\n"
        << " - number of time slices: " << conf.timeSlices() << "\n"
        << " - time slice size: " << conf.timeSliceSize() << "\n"
        << " - CFL fine: " << conf.cflFine() << "\n"
        << " - CFL coarse: " << conf.cflCoarse() << "\n"
        << " - timestep size fine: " << conf.dtFine() << "\n"
        << " - timestep size coarse: " << conf.dtCoarse() << "\n"
        << " - timesteps per slice fine propagator: " << conf.timeStepsFinePerTimeSlice() << "\n"
        << " - timesteps per slice coarse propagator: " << conf.timeStepsCoarsePerTimeSlice() << "\n"
        << " - parareal iterations: " << conf.kmax() << "\n"
        << " - asynchronous communications: " << (conf.async() ? "Enabled" : "Disabled") << "\n"
        << " - intermediate fields in mat files: " << (conf.mat() ? "Yes" : "No") << "\n"
        << std::endl;

    // Calculation domain and boundaries
    IJKSize domain; domain.Init(conf.gridSize(), conf.gridSize(), conf.gridSize());
    KBoundary kboundary; kboundary.Init(-convectionBoundaryLines, convectionBoundaryLines);

    // Initialize fields
    ConvectionField q, qinitial;
    q.Init("q", domain, kboundary);
    qinitial.Init("qinitial", domain, kboundary);
    Convection convection(conf.gridSize(), conf.gridSize(), conf.gridSize(), conf.dx(), conf.nu0(), conf.nufreq(), conf.cx(), conf.cy(), conf.cz());

    // Initialize parareal
    Parareal<Convection, ConvectionField> parareal(convection, qinitial, q, timeStart, conf, MPI_COMM_WORLD);

    if (conf.mode() == ModeCompare)
    {
        // Measure time required by convection
        const int tauSamples = 4;
        double tauF = MPI_Wtime();
        convection.DoRK4(qinitial, qinitial, 0., conf.dtFine(), tauSamples*conf.timeStepsFinePerTimeSlice());
        SynchronizeCUDA();
        tauF = MPI_Wtime() - tauF;
        double tauG = MPI_Wtime();
        convection.DoEuler(qinitial, qinitial, 0., conf.dtCoarse(), tauSamples*conf.timeStepsCoarsePerTimeSlice());
        SynchronizeCUDA();
        tauG = MPI_Wtime() - tauG;

        const double tauRatio = tauG / tauF;
        const double Nit_Np = static_cast<double>(conf.kmax()) / commsize;
        const double maxSpeedup = 1. / (tauRatio * (1. + Nit_Np) + Nit_Np);

        // Fill initial solution
        SynchronizeHost(qinitial);
        fillQ(qinitial, conf.nu0(), conf.nufreq(), conf.cx(), conf.cy(), conf.cz(), 0., 0., 1., 0., 1., 0., 1.);
        SynchronizeDevice(qinitial);

        // Run serial
        MPI_Barrier(MPI_COMM_WORLD);
        double eserial = MPI_Wtime();
        parareal.DoSerial();
        eserial = MPI_Wtime() - eserial;

        // Save reference
        ConvectionField qreference = q;
        SynchronizeHost(qreference);

        // Fill initial solution
        SynchronizeHost(qinitial);
        fillQ(qinitial, conf.nu0(), conf.nufreq(), conf.cx(), conf.cy(), conf.cz(), 0., 0., 1., 0., 1., 0., 1.);
        SynchronizeDevice(qinitial);

        // Run serial
        MPI_Barrier(MPI_COMM_WORLD);
        double eparallel = MPI_Wtime();
        parareal.DoParallel();
        eparallel = MPI_Wtime() - eparallel;

        // Output
        MPI_Barrier(MPI_COMM_WORLD);
        if (isLast)
        {
            double e = computeErrorReference(q, qreference);
            std::cout << "\n"
                << "Serial run time: " << eserial << "\n"
                << "Parallel run time: " << eparallel << "\n"
                << "Speedup: " << eserial / eparallel << "\n"
                << "Maximal speedup: " << maxSpeedup << "\n"
                << "Error at end: " << e << "\n"
                << std::endl;

            MatFile matfile("result.mat");
            matfile.addField("q", q);
            matfile.addField("qreference", qreference);
        }
    }
    else if (conf.mode() == ModeSerial)
    {
        // Fill initial solution
        SynchronizeHost(qinitial);
        fillQ(qinitial, conf.nu0(), conf.nufreq(), conf.cx(), conf.cy(), conf.cz(), 0., 0., 1., 0., 1., 0., 1.);
        SynchronizeDevice(qinitial);

        // Run serial
        double e = MPI_Wtime();
        double energyStart = energy();
        double deviceEnergyStart = deviceEnergy();

        parareal.DoSerial();

        e = MPI_Wtime() - e;
        double energyEnd = energy();
        double deviceEnergyEnd = deviceEnergy();

        const double totDevice = totalEnergy(deviceEnergyStart, deviceEnergyEnd);
        const double totNode = totalEnergy(energyStart, energyEnd) - totDevice;
        const double totNetwork = e * powerNetwork;
        const double totBlower = e * powerBlower;
        const double totEnergy = totNode + totDevice + totNetwork + totBlower;

        // Output
        MPI_Barrier(MPI_COMM_WORLD);
        if (isLast)
        {
            std::cout << "\n" << "Serial run time: " << e << "\n";
            std::printf("Node energy   : %8f J  (%8.3e W/node)\n", totNode   , totNode/e);
            std::printf("Device energy : %8f J  (%8.3e W/node)\n", totDevice , totDevice/e);
            std::printf("Network energy: %8f J  (%8.3e W/node)\n", totNetwork, totNetwork/e);
            std::printf("Blower energy : %8f J  (%8.3e W/node)\n", totBlower , totBlower/e);
            std::printf("Total energy  : %8f J  (%8.3e W/node)\n", totEnergy , totEnergy/e);
            std::cout << std::endl;
        }
    }
    else if (conf.mode() == ModeParallel)
    {
        // Fill initial solution
        SynchronizeHost(qinitial);
        fillQ(qinitial, conf.nu0(), conf.nufreq(), conf.cx(), conf.cy(), conf.cz(), 0., 0., 1., 0., 1., 0., 1.);
        SynchronizeDevice(qinitial);

        // Run serial
        parareal.DoSerial();
        std::cout << " -- The serial computation is done\n";
        ConvectionField qreference = q;

        // Run parallel
        MPI_Barrier(MPI_COMM_WORLD);
        double e = MPI_Wtime();
        double energyStart = energy();
        double deviceEnergyStart = deviceEnergy();

        parareal.DoParallel();

        MPI_Barrier(MPI_COMM_WORLD);
        e = MPI_Wtime() - e;

        std::cout << " -- The parallel computation is done\n";
        double energyEnd = energy();
        double deviceEnergyEnd = deviceEnergy();

        const double totDevice = totalEnergy(deviceEnergyStart, deviceEnergyEnd, MPI_COMM_WORLD);
        const double totNode = totalEnergy(energyStart, energyEnd, MPI_COMM_WORLD) - totDevice;
        const double totNetwork = e * powerNetwork * commsize;
        const double totBlower = e * powerBlower * commsize;
        const double totEnergy = totNode + totDevice + totNetwork + totBlower;

        // Compute error
        double error = computeErrorReference(q, qreference);

        // Output
        MPI_Barrier(MPI_COMM_WORLD);
        if (isLast)
        {
            const double fac = 1./e/commsize;

            std::cout << std::endl;
            std::printf("Parallel run time: %f s\n", e);
            std::printf("Node energy   : %8f J  (%8.3e W/node)\n", totNode   , fac*totNode);
            std::printf("Device energy : %8f J  (%8.3e W/node)\n", totDevice , fac*totDevice);
            std::printf("Network energy: %8f J  (%8.3e W/node)\n", totNetwork, fac*totNetwork);
            std::printf("Blower energy : %8f J  (%8.3e W/node)\n", totBlower , fac*totBlower);
            std::printf("Total energy  : %8f J  (%8.3e W/node)\n", totEnergy , fac*totEnergy);
            std::printf("Error of parareal: %.4e\n", error);
            std::cout << std::endl;
        }
    }
    else if (conf.mode() == ModeTiming)
    {
        // Fill initial solution
        SynchronizeHost(qinitial);
        fillQ(qinitial, conf.nu0(), conf.nufreq(), conf.cx(), conf.cy(), conf.cz(), 0., 0., 1., 0., 1., 0., 1.);
        SynchronizeDevice(qinitial);

        // Run serial
        std::vector<double> times;
        MPI_Barrier(MPI_COMM_WORLD);
        parareal.DoTimedParallel(times);

        // Gather on root
        const int s = times.size();
        std::vector<double> timesGlobal;
        timesGlobal.resize(s * commsize);
        MPI_Gather(&times[0], s, MPI_DOUBLE, &timesGlobal[0], s, MPI_DOUBLE, 0, MPI_COMM_WORLD);

        // Output
        if (isRoot)
        {
            std::cout << "\nTimes:\n";
            for(int i = 0; i < s; ++i)
            {
                for(int p = 0; p < commsize; ++p)
                {
                    std::cout << std::scientific << std::setprecision(6) << timesGlobal[p*s + i] << "   ";
                }
                std::cout << "\n";
            }
        }
    }

    // Finalize
    MPI_Finalize();

    return 0;
}
Example #3
0
int main(int argc, char* argv[])
{

  std::cerr << "STARTING\n";
  std::cerr << "Opening config file" << argv[1] << "\n";
  std::ifstream inFile(argv[1],std::ios::in);
  std::vector<double> dataIn(7,0.0F);
  std::vector< std::vector<double> > vectorIn(1000,dataIn);
  int column = 0;
  int row = 0;
  int family = 0;
  bool comment = false;

  std::string in;
  std::cerr << "Parsing config file" << argv[1] << "\n";
  while (inFile >> in)
  {

    if(!in.compare("#")) //comment code # found
    {
      if(!comment)
      {
        comment = true;
      }
      else      //end of comment
      {
        comment = false;
      }
    }
    if((!comment) && in.compare("#")) //real line found
    {
      if(column == 1)
      {
        //vectorIn[row][family] = atof(in.c_str());
        //std::cerr << "Adding " << in << "[" << row << "]"
        //     << "[" << family*2 << "]\n";
      }
      if(column == 3)
      {
        vectorIn[row][family] = atof(in.c_str());
        //std::cerr << "Adding " << in << "[" << row << "]"
        //     << "[" << (family*2)+1 << "]\n";
      }
      column++;
      if(column == 5)
      {
        column = 0;
        row++;
      }
      if(!in.compare("%"))
      {
        row = 0;
        column = 0;
        family++;
        std::cerr << "New Family " << family << "\n";
      }
    }
  }

  // start timer
  Timer tim;
  tim.reset();
  uint64 t0 = tim.get();  // to measure display time
  // get test image

  if(argc > 2)
    itemNumber = atoi(argv[2]);
  else
    itemNumber = -666;

  // create operating objects
  configIn.openFile("NPclassify.conf");
  polySet.openFile("polySet.conf");

  NPclassify NP(configIn,polySet,true);
  if(argc > 3)
  {
    NP.inputCommandLineSettings(atof(argv[3]),atof(argv[4]),atof(argv[5]),
                                atof(argv[6]),atoi(argv[7]),atoi(argv[8]),
                                atof(argv[9]),atof(argv[10]),atof(argv[11]));
  }

  //inport data

  std::vector<double> feature(2,0);
  std::vector<long> roots;
  std::vector<long> parents;
  std::vector<double> density;

  NP.addSpace(vectorIn,(row-1));
  NP.classifySpaceNew();
  uint64 t1 = tim.get();
  t0 = t1 - t0;

  roots = NP.getStems();
  parents = NP.getParents();
  density = NP.getDensity();

  std::vector<std::vector<long> > theReturn = NP.getChildren();
  for(int i = 0; i < NP.getStemNumber(); i++)
  {
    if(NP.getClassSize(i) > NP.getMinClassSize())
    {
      LINFO("CLASS %d size %ld",i,NP.getClassSize(i));
    }
  }
  //outport classification data

  NP.metaClassify(itemNumber);

  std::ofstream outfile("feature_trian.dat",std::ios::out);
  std::ofstream matfile("feature_trian_mat.dat",std::ios::out);
  for(int i = 0; i < NP.getStemNumber(); i++)
  {
    for(int j = 0; j < NP.getClassSize(i); j++)
    {
      long item = NP.getClass(i,j);
      outfile << item << "\t" << i << "\t" << j << "\t"
              << NP.getFeature(item,0) << "\t"
              << NP.getFeature(item,1) << "\n";
      matfile << item << "\t" << i << "\n";
    }
  }


}
Example #4
0
int main(int argc, char** argv)
{
    DynareParams params(argc, argv);
    if (params.help) {
        params.printHelp();
        return 0;
    }
    if (params.version) {
        printf("Dynare++ v. %s. Copyright (C) 2004-2011, Ondra Kamenik\n",
               DYNVERSION);
        printf("Dynare++ comes with ABSOLUTELY NO WARRANTY and is distributed under\n");
        printf("GPL: modules integ, tl, kord, sylv, src, extern and documentation\n");
        printf("LGPL: modules parser, utils\n");
        printf(" for GPL  see http://www.gnu.org/licenses/gpl.html\n");
        printf(" for LGPL see http://www.gnu.org/licenses/lgpl.html\n");
        return 0;
    }
    THREAD_GROUP::max_parallel_threads = params.num_threads;

    try {
        // make journal name and journal
        std::string jname(params.basename);
        jname += ".jnl";
        Journal journal(jname.c_str());

        // make dynare object
        Dynare dynare(params.modname, params.order, params.ss_tol, journal);
        // make list of shocks for which we will do IRFs
        vector<int> irf_list_ind;
        if (params.do_irfs_all)
            for (int i = 0; i < dynare.nexog(); i++)
                irf_list_ind.push_back(i);
        else
            irf_list_ind = ((const DynareNameList&)dynare.getExogNames()).selectIndices(params.irf_list);

        // write matlab files
        FILE* mfd;
        std::string mfile1(params.basename);
        mfile1 += "_f.m";
        if (NULL == (mfd=fopen(mfile1.c_str(), "w"))) {
            fprintf(stderr, "Couldn't open %s for writing.\n", mfile1.c_str());
            exit(1);
        }
        ogdyn::MatlabSSWriter writer0(dynare.getModel(), params.basename.c_str());
        writer0.write_der0(mfd);
        fclose(mfd);

        std::string mfile2(params.basename);
        mfile2 += "_ff.m";
        if (NULL == (mfd=fopen(mfile2.c_str(), "w"))) {
            fprintf(stderr, "Couldn't open %s for writing.\n", mfile2.c_str());
            exit(1);
        }
        ogdyn::MatlabSSWriter writer1(dynare.getModel(), params.basename.c_str());
        writer1.write_der1(mfd);
        fclose(mfd);

        // open mat file
        std::string matfile(params.basename);
        matfile += ".mat";
        mat_t* matfd = Mat_Create(matfile.c_str(), NULL);
        if (matfd == NULL) {
            fprintf(stderr, "Couldn't open %s for writing.\n", matfile.c_str());
            exit(1);
        }

        // write info about the model (dimensions and variables)
        dynare.writeMat(matfd, params.prefix);
        // write the dump file corresponding to the input
        dynare.writeDump(params.basename);


        system_random_generator.initSeed(params.seed);

        tls.init(dynare.order(),
                 dynare.nstat()+2*dynare.npred()+3*dynare.nboth()+
                 2*dynare.nforw()+dynare.nexog());

        Approximation app(dynare, journal, params.num_steps, params.do_centralize, params.qz_criterium);
        try {
            app.walkStochSteady();
        } catch (const KordException& e) {
            // tell about the exception and continue
            printf("Caught (not yet fatal) Kord exception: ");
            e.print();
            JournalRecord rec(journal);
            rec << "Solution routine not finished (" << e.get_message()
                << "), see what happens" << endrec;
        }

        std::string ss_matrix_name(params.prefix);
        ss_matrix_name += "_steady_states";
        ConstTwoDMatrix(app.getSS()).writeMat(matfd, ss_matrix_name.c_str());

        // check the approximation
        if (params.check_along_path || params.check_along_shocks
                || params.check_on_ellipse) {
            GlobalChecker gcheck(app, THREAD_GROUP::max_parallel_threads, journal);
            if (params.check_along_shocks)
                gcheck.checkAlongShocksAndSave(matfd, params.prefix,
                                               params.getCheckShockPoints(),
                                               params.getCheckShockScale(),
                                               params.check_evals);
            if (params.check_on_ellipse)
                gcheck.checkOnEllipseAndSave(matfd, params.prefix,
                                             params.getCheckEllipsePoints(),
                                             params.getCheckEllipseScale(),
                                             params.check_evals);
            if (params.check_along_path)
                gcheck.checkAlongSimulationAndSave(matfd, params.prefix,
                                                   params.getCheckPathPoints(),
                                                   params.check_evals);
        }

        // write the folded decision rule to the Mat-4 file
        app.getFoldDecisionRule().writeMat(matfd, params.prefix);

        // simulate conditional
        if (params.num_condper > 0 && params.num_condsim > 0) {
            SimResultsDynamicStats rescond(dynare.numeq(), params.num_condper, 0);
            ConstVector det_ss(app.getSS(),0);
            rescond.simulate(params.num_condsim, app.getFoldDecisionRule(), det_ss, dynare.getVcov(), journal);
            rescond.writeMat(matfd, params.prefix);
        }

        // simulate unconditional
        //const DecisionRule& dr = app.getUnfoldDecisionRule();
        const DecisionRule& dr = app.getFoldDecisionRule();
        if (params.num_per > 0 && params.num_sim > 0) {
            SimResultsStats res(dynare.numeq(), params.num_per, params.num_burn);
            res.simulate(params.num_sim, dr, dynare.getSteady(), dynare.getVcov(), journal);
            res.writeMat(matfd, params.prefix);

            // impulse response functions
            if (! irf_list_ind.empty()) {
                IRFResults irf(dynare, dr, res, irf_list_ind, journal);
                irf.writeMat(matfd, params.prefix);
            }
        }

        // simulate with real-time statistics
        if (params.num_rtper > 0 && params.num_rtsim > 0) {
            RTSimResultsStats rtres(dynare.numeq(), params.num_rtper, params.num_burn);
            rtres.simulate(params.num_rtsim, dr, dynare.getSteady(), dynare.getVcov(), journal);
            rtres.writeMat(matfd, params.prefix);
        }

        Mat_Close(matfd);

    } catch (const KordException& e) {
        printf("Caugth Kord exception: ");
        e.print();
        return e.code();
    } catch (const TLException& e) {
        printf("Caugth TL exception: ");
        e.print();
        return 255;
    } catch (SylvException& e) {
        printf("Caught Sylv exception: ");
        e.printMessage();
        return 255;
    } catch (const DynareException& e) {
        printf("Caught Dynare exception: %s\n", e.message());
        return 255;
    } catch (const ogu::Exception& e) {
        printf("Caught ogu::Exception: ");
        e.print();
        return 255;
    } catch (const ogp::ParserException& e) {
        printf("Caught parser exception: %s\n", e.message());
        return 255;
    }

    return 0;
}