多线程对拍 —— 转存

多线程对拍(来自 Eafoo (有改动)

首先先把它编译了,记得加一句 -pthread ,要不然编译不了

用法见博客

#include <bits/stdc++.h>
using namespace std;
void Black() { printf("\033[37m"); }
void Red() { printf("\033[31m"); }
void Green() { printf("\033[32m"); }
void Yellow() { printf("\033[33m"); }
void Blue() { printf("\033[34m"); }
void Purple() { printf("\033[35m"); }
void DeepGreen() { printf("\033[36m"); }
void White() { printf("\033[37m"); }
int sys(string cmd) { return system(cmd.c_str()); }
#define nnow steady_clock::now()
#define dc duration_cast<duration<double>>(t2 - t1)
#define time ut.count()
#define pc putchar
#define pr printf
#define ps puts
#define Wall ps("--------------------------------------------")
#define CE(x)  Re(), ps("Error"), Bl(), Wall, exit(1);
#define COM(x) Pu(), pr("Compile " #x " "), Bl();
#define WA()   Wh(), pr("%03d ", i), Re(), ps("Wrong Answer"), Bl(), exit(1);
#define Bls "\033[37m"
#define Res "\033[31m"
#define Grs "\033[32m"
#define Yes "\033[33m"
#define BLs "\033[34m"
#define Pus "\033[35m"
#define DGs "\033[36m"
void BLK() { pr("\033[30m"); }
void Bl() { pr("\033[37m"); }
void Re() { pr("\033[31m"); }
void Gr() { pr("\033[32m"); }
void Ye() { pr("\033[33m"); }
void BL() { pr("\033[34m"); }
void Pu() { pr("\033[35m"); }
void DG() { pr("\033[36m"); }
void Wh() { pr("\033[37m"); }
void Bo() { pr("\033[1m"); }
#include <thread>
#include <mutex>
typedef long long ll;
using namespace std;
using namespace chrono;
string args[10], targ[10];
 
int cnt;
bool no_com = 0, out_ac = 0, qstop = 0;
int cas = 1000; double t = 1000; bool fast = 0; bool fast2 = 0;
int mem = 128 * 1024; int Max = 10; string cstd="14";
queue<thread> q;
 
bool Compile(string filename, string tf) {
    if (sys("g++ " + tf + ".cpp -o " + filename + " -Wall -Wno-unused-result -DDEBUG -DPAI_PAI -DONLINE_JUDGE -std=c++" + cstd + (fast ? " -O2" : (fast2 ? "" : " -O2 -fsanitize=address,undefined")))) return Bo(), 0;
    Bo(), Gr(), ps("Done"), Bl();
    return 1;
}

bool Compile_No_Sanitizer(string filename, string tf) {
    if (sys("g++ " + tf + ".cpp -o " + filename + " -Wall -Wno-unused-result -DDEBUG -DPAI_PAI -DONLINE_JUDGE -std=c++" + cstd + (fast2 ? "" : " -O2"))) return Bo(), 0;
    Bo(), Gr(), ps("Done"), Bl();
    return 1;
}
 
mutex m;
double cpp_tot = 0, std_tot = 0, rd_tot = 0;
ll cpp_mem = 0, std_mem = 0, rd_mem = 0;
int cas_tot = 0, unac_tot = 0;
 
bool Test(int i) {
    char s = 'P', s2 = 'P';
    double cpp = NAN, std = NAN, rd = NAN;
    int cppm = -1, stdm  = -1, rdm = -1;
    FILE * fe; int ret; char tmp[34];
    // run rand
    string tm = (string) "./data/tm" + to_string(i).c_str() + ".log";
    ret = sys("ulimit -s " + to_string(mem + 100) + " && /usr/bin/time -f \"%e %M\" -o " + tm + " echo \"\" | ./" + args[3] + " > ./data/rd" + to_string(i));
    if (ret) goto out;
    fe = fopen(tm.c_str(), "r"); fscanf(fe, "%lf%d", &rd, &rdm); rd *= 1000; fclose(fe);
    // run cpp
    ret = sys("ulimit -s " + to_string(mem + 100) + " && timeout 2> sb " + to_string(t / 1000 + 0.3) + " /usr/bin/time -f \"%e %M\" -o " + tm + " ./" + args[1] + " < ./data/rd" + to_string(i) + " > ./data/cpp" + to_string(i));
    if (ret == 31744) { s = 'T'; goto out; }
    if (ret == 35584) { s = 'M'; goto out; }
    if (ret) { s = 'R'; goto out; }
    fe = fopen(tm.c_str(), "r"); fscanf(fe, "%lf%d", &cpp, &cppm); cpp *= 1000; fclose(fe);
    if (cpp >= t) { s = 'T'; goto out; }
    if (cppm > mem) { s = 'M'; goto out; }
    // run std
    ret = sys("ulimit -s " + to_string(mem + 100) + " && timeout 2> sb " + to_string(t / 1000 + 0.3) + " /usr/bin/time -f \"%e %M\" -o " + tm + " ./" + args[2] + " < ./data/rd" + to_string(i) + " > ./data/std" + to_string(i));
    if (ret == 31744) { s2 = 'T'; goto out; }
    if (ret == 35584) { s2 = 'M'; goto out; }
    if (ret) { s2 = 'R'; goto out; }
    fe = fopen(tm.c_str(), "r"); fscanf(fe, "%lf%d", &std, &stdm); std *= 1000; fclose(fe);
    if (std >= t) { s2 = 'T'; goto out; }
    if (stdm > mem) { s2 = 'M'; goto out; }
    // diff
    if (sys("diff -Z ./data/cpp" + to_string(i) + " ./data/std" + to_string(i) + " > ./data/diff.log")) s = s2 = 'W';
    else s = s2 = 'A';
    // output
    out:
    lock_guard<mutex> Lock(m);
    ++cas_tot;
    if (s != 'A') ++unac_tot;
    if (s == 'A') cpp_tot += cpp, std_tot += std, rd_tot += rd, cpp_mem += cppm, std_mem += stdm, rd_mem += rdm;
    if (s != 'A' || out_ac) {
        if (s == 'A') Gr();
        else if (s == 'T' || s2 == 'T') Ye();
        else if (s == 'R' || s2 == 'R') Pu();
        else if (s == 'M' || s2 == 'M') BLK();
        else if (s == 'W') Re();
        pr("%03d ", i);
        putchar(s), putchar(' '), putchar(s2);
        pr(" %.0lfms %.0lfms %.0lfms %dMB %dMB %dMB\n", rd, cpp, std, rdm / 1024, cppm / 1024, stdm / 1024), Bl();
        if (qstop && s != 'A') { Wall, abort(); }
    }
    if (s != 'P' && s2 != 'P') sys(("rm " + tm).c_str());
    if (s != 'P') sys("rm ./data/cpp" + to_string(i));
    if (s2 != 'P') sys("rm ./data/std" + to_string(i));
    if (s == 'A') sys("rm ./data/rd" + to_string(i));
    return 0;
}
 
int main(int argv, char **argc) {
    Bo(), sys("rm -f ./data/*");
    for (int i = 1; i < argv; ++i) {
        string tmp = argc[i];
        if (tmp == "-n") no_com = 1;
        // no_com complie
        else if (tmp == "-l") ++i, cas = stoi(string(argc[i]));
        // testcases
        else if (tmp == "-t") ++i, t = stoi(string(argc[i]));
        // time limit
        else if (tmp == "-f") fast = 1;
        // fast mode (no_com fsanitize)
        else if (tmp == "-ff") fast2 = 1;
        // fast2 mode (no_com O2 & fsanitize)
        else if (tmp == "-m") ++i, mem = stoi(string(argc[i])) * 1024;
        // memory limit
        else if (tmp == "-a") out_ac = 1;
        // echo accept message
        else if (tmp == "-s") qstop = 1;
        // immediately stop when unaccepted
        else if (tmp == "-c") ++i, Max = stoi(string(argc[i]));
        // max number of threads
        else if (tmp == "-std") ++i, cstd = argc[i];
        // set compile environment (like -std=c++14)
        else args[++cnt] = argc[i];
    }
    targ[1] = args[1], targ[2] = args[2], targ[3] = args[3];
    pr("%scpp: %s%s.cpp\n", Pus, BLs, args[1].c_str());
    pr("%sstd: %s%s.cpp\n", Pus, BLs, args[2].c_str());
    pr("%srand: %s%s.cpp\n", Pus, BLs, args[3].c_str());
    if (fast) pr("%sFast Mode %sOn\n", Pus, BLs), pr("%sFsanitize %sDisabled\n", Pus, BLs);
    else if (fast2) pr("%sFast II Mode %sOn\n", Pus, BLs), pr("%sO2 %sDisabled\n", Pus, BLs), pr("%sFsanitize %sDisabled\n", Pus, BLs);
    if (no_com) pr("%sCompile %sDisabled\n", Pus, BLs);
    pr("%sTest Count: %s%d\n", Pus, BLs, cas);
    pr("%sThread Number: %s%d\n", Pus, BLs, Max);
    pr("%sTime Limit: %s%dms\n", Pus, BLs, int(t));
    pr("%sMemory Limit: %s%dMB\n", Pus, BLs, mem / 1024);
    pr("%sCompile as: %s-std=c++%s\n", Pus, BLs, cstd.c_str()), Bl();
    Wall;
    if (!no_com) {
        COM(cpp); if (!Compile(args[1], targ[1])) CE(cpp);
        COM(std); if (!Compile(args[2], targ[2])) CE(std);
        COM(rand); if (!Compile_No_Sanitizer(args[3], targ[3])) CE(rand);
    }
    Pu(), ps("Now Begin Test"), Bl(), Wall;
    steady_clock::time_point t1, t2; duration<double> ut;
    t1 = nnow;
    for (int i = 1; i != (cas + 1); ++i) {
        if ((int) q.size() > Max) q.front().join(), q.pop();
        q.push(thread(Test, i));
        if (i % 100 == 0) {
            lock_guard<mutex> Lock(m);
            int ac = cas_tot - unac_tot;
            printf("%s%d %sCases, %s%d %sWrongs, %s%.2lf%%%s\n", BLs, cas_tot, Pus, BLs, unac_tot, Pus, BLs, ac * 1.0 / cas_tot * 100, Pus);
            t2 = nnow; ut = dc;
            printf("Total: %s%.3lfs\n", BLs, ut.count());
            printf("%sAverage: %s%.0lfms %.0lfms %.0lfms %.0lfMB %.0lfMB %.0lfMB\n", Pus, BLs, rd_tot / ac, cpp_tot / ac, std_tot / ac, 1.0 * rd_mem / ac / 1024, 1.0 * cpp_mem / ac / 1024, 1.0 * std_mem / ac / 1024), Bl();
            Wall;
        }
    }
    while (!q.empty()) q.front().join(), q.pop();
    t2 = nnow; ut = dc;
    int ac = cas_tot - unac_tot;
    printf("%s%d %sCases, %s%d %sWrongs, %s%.2lf%%%s\n", BLs, cas_tot, Pus, BLs, unac_tot, Pus, BLs, ac * 1.0 / cas_tot * 100, Pus);
    printf("Total: %s%.3lfs\n", BLs, ut.count());
    printf("%sAverage: %s%.0lfms %.0lfms %.0lfms %.0lfMB %.0lfMB %.0lfMB\n", Pus, BLs, rd_tot / ac, cpp_tot / ac, std_tot / ac, 1.0 * rd_mem / ac / 1024, 1.0 * cpp_mem / ac / 1024, 1.0 * std_mem / ac / 1024), Bl();
    Bl(), Wall;
    sys("rm -f ./data/.f*");
    Gr(), ps("Finished"), Bl(), Wall;
}

毫秒级种子 :std::chrono::system_clock::now().time_since_epoch().count()

posted @ 2023-12-11 12:21  xrlong  阅读(18)  评论(0编辑  收藏  举报