示例#1
0
double
twoSiteDMRG(Environment const& mpienv,
            Hubbard& sites, 
            IQMPS& psi, 
            std::vector<IQMPO>const& Hs, 
            Sweeps const& sweeps, 
            Args const& args)
    {
 
    auto L = sites.N();
    auto nSweeps = sweeps.nsweep();
 
    psi.position(1);
 
    mpienv.barrier();
    std::vector<EnvironmentHandler<IQTensor>*> handlerLeft;
    std::vector<EnvironmentHandler<IQTensor>*> handlerRight;
 
    std::for_each(Hs.begin(), Hs.end(), [&] (IQMPO const& element) {
        auto left = new EnvironmentHandler<IQTensor>(element, psi, Left, 0, false);
        handlerLeft.push_back(left);
        auto right = new EnvironmentHandler<IQTensor>(element, psi, Right, 3, false);
        handlerRight.push_back(right);
    });

    mpienv.barrier();

    auto forN = AutoMPO(sites); for(int j = 1; j <= L; ++j)  { forN += 1,"Ntot",j; } MPOt<IQTensor> N(forN);
    auto forSz = AutoMPO(sites); for(int j = 1; j <= L; ++j)  { forSz += 1,"Sz",j; } MPOt<IQTensor> Sz(forSz);
    double nVal = itensor::overlap(psi, N, psi);
    double szVal = itensor::overlap(psi, Sz, psi);
 
    std::cout << "L " << L << " N " << nVal << " Sz" << szVal << std::endl;

    double lastExpec = 0;
    double minExpec;
    IQMPS minState;
    std::for_each(Hs.begin(), Hs.end(), [&] (IQMPO const& element) { lastExpec += overlap(psi, element, psi); });

    lastExpec = sum(mpienv, lastExpec);     
    if (mpienv.rank() == 0) {
        printStatus(-1, 0, L, lastExpec, 0, szVal, nVal, maxM(psi));
        double minExpec = lastExpec;
        minState = psi;
    } 
     
    vector<Direction> directions {Fromleft, Fromright};
 
    for (int nS = 1; nS <= nSweeps; ++nS)
        {
        Args args;
        args.add("Cutoff",sweeps.cutoff(nS));
        args.add("Maxm",sweeps.maxm(nS));
        args.add("Minm",sweeps.minm(nS));
        args.add("Niter",sweeps.niter(nS));
        args.add("Noise",sweeps.noise(nS));
     
        for (Direction &dir : directions)
            {
            int start = dir==Fromleft?1:L-1;
            int end = dir==Fromleft?L-1:1;
 
            for (int i = start; dir == Fromleft ? i < end : i > end; dir == Fromleft ? i++ : i--)
                {
                IQTensor guess = psi.A(i) * psi.A(i+1);
 
                std::vector<IQTensor> leftEn;
                std::vector<IQTensor> rightEn;
                std::vector<IQTensor> leftH;
                std::vector<IQTensor> rightH;

                std::for_each(handlerLeft.begin(), handlerLeft.end(), 
                [&] (EnvironmentHandler<IQTensor>* handler) {
                    leftEn.push_back(handler->currentElement());
                });

                std::for_each(handlerRight.begin(), handlerRight.end(), 
                [&] (EnvironmentHandler<IQTensor>* handler) { 
                    rightEn.push_back(handler->currentElement());
                });

                std::for_each(Hs.begin(), Hs.end(), [&] (IQMPO const& H) { 
                    leftH.push_back(H.A(i));
                    rightH.push_back(H.A(i+1));
                });

                LocalHSum<IQTensor> lop(leftH, rightH, leftEn, rightEn);
 
                double expec = MPI_davidson(mpienv, lop, guess, args);
                mpienv.barrier();

                if (i == 1) { MPI_insertBond(mpienv, psi, i, Fromleft, guess, leftEn, leftH, rightH, rightEn, args); }
                else if (i + 1 == L) { MPI_insertBond(mpienv, psi, i, Fromright, guess, leftEn, leftH, rightH, rightEn, args); }
                else { MPI_insertBond(mpienv, psi, i, dir, guess, leftEn, leftH, rightH, rightEn, args); }

                if (mpienv.rank() == 0)
                    {
                    double nVal = itensor::overlap(psi, N, psi);
                    double szVal = itensor::overlap(psi, Sz, psi);
                    printStatus(nS, i, L-i, expec, expec - lastExpec, szVal, nVal, maxM(psi));
                    if(expec < minExpec)
                        {
                        minExpec = expec;
                        minState = psi;
                        }
                    }
                broadcast(mpienv, psi);
                lastExpec = expec;
 
                auto doStep = [&] (EnvironmentHandler<IQTensor>* handler) { 
                    if (dir == Fromleft)
                        handler->stepToRightWithNewState(psi); 
                    else
                        handler->stepToLeftWithNewState(psi); 
                };
                std::for_each(handlerLeft.begin(), handlerLeft.end(), doStep);
                std::for_each(handlerRight.begin(), handlerRight.end(), doStep);
                }
 
            if (dir == Fromright && mpienv.rank() == 0)
                printOutIO((boost::format("--------------------  Finished Sweep %s of %s  ----------------------\n") % nS % nSweeps).str() );
            }
        }

    std::for_each(handlerLeft.begin(), handlerLeft.end(), [] (EnvironmentHandler<IQTensor>* el) { delete(el); });
    std::for_each(handlerRight.begin(), handlerRight.end(), [] (EnvironmentHandler<IQTensor>* el) { delete(el); });

    psi = minState;
    return minExpec;
    }
示例#2
0
int main(int argc, char **argv) {
    QTextStream qout(stdout, QIODevice::WriteOnly);
    QTextStream qerr(stderr, QIODevice::WriteOnly);
    QTextStream qin(stdin, QIODevice::ReadOnly);

    Args args;
    args.add(new Arg("h", "help", Arg::setTrue, QVariant(false)));
    args.add(new Arg("v", "verbose", Arg::setTrue, QVariant(false)));
    //args.add(new Arg("i", "input-file", Arg::readableFile, QVariant("/var/log/postgresql.log")));
    args.add(new Arg("i", "input-file", Arg::readableFile,
                     QVariant("/var/log/postgresql/postgresql-9.1-main.log")));
    args.add(new Arg("o", "output-file", Arg::writableFile,
                     QVariant("report.html")));
    args.add(new Arg("u", "users", Arg::toString, QVariant()));
    args.add(new Arg("d", "databases", Arg::toString, QVariant()));
    args.add(new Arg("top", Arg::toInt, QVariant(20)));
    args.add(new Arg("t", "query-types", Arg::toString,
                     QVariant("SELECT,UPDATE,INSERT,DELETE")));

    if(!args.parse(argc, argv)) {
        args.help();
        return -1;
    }

    QStringList users = args.getStringList("users");
    QStringList databases = args.getStringList("databases");
    QStringList query_types = args.getStringList("query-types");

    int ret;

    QString line;
    int start_index;
    int stop_index;

    int old_query_id;
    int new_query_id;
    int old_line_id;
    int new_line_id;
    QString database;
    QString user;
    QString statement;
    uint duration;

    QFile input_file;
    ret = args.getFile(&input_file, "input-file", QIODevice::ReadOnly | QIODevice::Text);
    if(ret)return ret;

    QFile output_file;
    ret = args.getFile(&output_file, "output-file", QIODevice::WriteOnly | QIODevice::Text);
    if(ret)return ret;
    QTextStream output(&output_file);

    /* display the top N queries */
    int top = args.getInt("top");

    Queries queries;
    old_query_id = -1;
    old_line_id = -0;
    statement = "";
    duration = 0;

    QTime timer;
    timer.start();

    uint lines = 0;

    if(input_file.atEnd()) {
        qerr << "The input file (" << input_file.fileName();
        qerr << ") seems to be empty" << endl;
    }

    while (!input_file.atEnd()) {
        line = input_file.readLine(4096);
        lines++;
        if(lines % 1000 == 0) {
            qout << "Read " << lines << " lines." << endl;
            qout.flush();
        }

        if(line[0] == '\t') {
            statement.append(line);
            continue;
        }

        start_index = line.indexOf("[", 15);
        start_index = line.indexOf("[", start_index + 3) + 1;
        stop_index = line.indexOf("-", start_index);

        new_query_id = line.mid(start_index, stop_index - start_index).toInt();

        start_index = stop_index + 1;
        stop_index = line.indexOf("]", start_index);

        new_line_id = line.mid(start_index, stop_index - start_index).toInt();

        if(new_query_id != old_query_id || old_line_id < new_line_id) {
            old_query_id = new_query_id;
            QString hashStatement = Query::normalize(statement);
            statement = Query::format(statement);
            if((
                        hashStatement.startsWith("INSERT") ||
                        hashStatement.startsWith("DELETE") ||
                        hashStatement.startsWith("UPDATE") ||
                        hashStatement.startsWith("SELECT")
                    )
                    && (!users.length() || users.contains(user))
                    && (!databases.length() || databases.contains(database))) {
                uint hash = qHash(hashStatement);
                if(queries.contains(hash)) {
                    queries[hash]->addStatement(duration, statement);
                } else {
                    queries.insert(hash, new Query(hashStatement, statement, user, database, duration));
                }
            }

            user = "";
            database = "";
            duration = 0;
            statement = "";

            start_index = line.indexOf("user="******",", start_index);
            user = line.mid(start_index, stop_index - start_index);

            start_index = line.indexOf("db=", stop_index) + 3;
            stop_index = line.indexOf(" ", start_index);
            if(start_index == -1 || stop_index == -1)continue;
            database = line.mid(start_index, stop_index - start_index);

            start_index = line.indexOf("duration: ", stop_index) + 10;
            stop_index = line.indexOf(" ", start_index);
            duration = line.mid(start_index, stop_index - start_index).toDouble() * 1000;

            start_index = line.indexOf("statement: ", stop_index) + 11;
            if(start_index < stop_index)continue;
            stop_index = line.length();
            statement = line.mid(start_index, stop_index - start_index);
        } else {
            start_index = line.indexOf("] ", stop_index);
            if(start_index != -1) {
                start_index += 2;

                stop_index = line.length() - start_index;
                statement.append(line.mid(start_index, line.length() - start_index));
            }
        }
    }

    QFile header(":templates/header.html");
    if (!header.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qout << "Unable to open templates/header.html" << endl;
        return -4;
    } else {
        output << header.readAll();
        header.close();
    }
    QList<Query*> queries_sorted;

    output << QString("<div class=\"information\">"
                      "<ul>"
                      "<li>Generated on %1</li>"
                      "<li>Parsed %2 (%3 lines) in %4 ms</li>"
                      "</ul>"
                      "</div>") \
           .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")) \
           .arg(args.getString("input_file")) \
           .arg(lines) \
           .arg(timer.elapsed());

    output << "<div class=\"reports\">";

    output << "<h2 id=\"NormalizedQueriesMostTimeReport\">Queries that took up the most time (N) <a href=\"#top\" title=\"Back to top\">^</a></h2>";
    output << print_queries(queries.sortedQueries(Queries::mostTotalDuration), top);

    output << "<h2 id=\"NormalizedQueriesMostFrequentReport\">Most frequent queries (N) <a href=\"#top\" title=\"Back to top\">^</a></h2>";
    output << print_queries(queries.sortedQueries(Queries::mostExecutions), top);

    output << "<h2 id=\"NormalizedQueriesSlowestAverageReport\">Slowest queries (N) <a href=\"#top\" title=\"Back to top\">^</a></h2>";
    output << print_queries(queries.sortedQueries(Queries::mostAverageDuration), top);

    QFile footer(":templates/footer.html");
    if (!footer.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qout << "Unable to open templates/footer.html" << endl;
        return -5;
    } else {
        output << footer.readAll();
        footer.close();
    }
    qout << "Wrote to file " << args.getString("output-file") << endl;
}