提交记录 48194


用户 题目 状态 得分 用时 内存 语言 代码长度
iMMIQ 1001. 测测你的排序 Wrong Answer 0 549.925 ms 32 KB C++17 2.54 KB
提交时间 评测时间
2026-09-16 13:20:47 2026-09-16 13:20:52
// P3: duck.ac clone 并行性探针(有界,不会 TLE)
// 3 个 worker:先各写 1MB 标记(内存可见),再 rdtsc 自旋 ~0.5s @3.6GHz,置 done。
// main:有界等待 done(~0.7s 封顶),然后自己自旋 ~0.2s。
// 判读(用时 / 内存):
//   ~0.5-0.8s, mem ~1.2MB  => clone 真并行 ✓
//   ~0.9-1.0s, mem ~1.2MB  => 线程能跑但串行(单核/无抢占)
//   ~0.2s,     mem ~24KB   => worker 从未运行(clone 失败或饿死)
//   RE                     => clone/TLS 崩溃
#include <cstdint>
#include <sched.h>

static inline unsigned long long RDTSC() {
  unsigned lo, hi;
  __asm__ volatile("rdtsc" : "=a"(lo), "=d"(hi));
  return ((unsigned long long)hi << 32) | lo;
}

struct TCB {
  void* tcb; void* dtv; void* self; long mt; long gscope; void* sysinfo; void* pad1;
  unsigned long guard;    // fs:0x28
  unsigned long ptr_guard;
};
static char stacks[3][1 << 18] __attribute__((aligned(4096)));
static TCB tcbs[3] __attribute__((aligned(64)));
static char marks[3][1 << 20] __attribute__((aligned(4096)));  // 1MB/worker 标记
struct Arg {
  volatile unsigned long long done;
  unsigned long long spin_cycles;
} __attribute__((aligned(64)));
static Arg args[3];

static int worker(void* p) {
  Arg* a = (Arg*)p;
  int id = (int)(a - args);
  volatile char* m = (volatile char*)marks[id];
  for (size_t i = 0; i < sizeof marks[0]; i += 64) m[i] = (char)id;  // 写满 1MB
  unsigned long long end = RDTSC() + a->spin_cycles;
  while (RDTSC() < end) {
  }
  __atomic_store_n(&a->done, 1, __ATOMIC_RELEASE);
  return 0;
}

void sort(unsigned* a, int n) {
  (void)a;
  (void)n;
  const unsigned long long SPIN = 1800000000ULL;   // 0.5s @ 3.6GHz
  const unsigned long long JOIN = 1980000000ULL;   // 0.55s 上限(略高于 worker 自旋)
  for (int t = 0; t < 3; ++t) {
    args[t].spin_cycles = SPIN;
    tcbs[t].tcb = &tcbs[t];
    tcbs[t].self = &tcbs[t];
    tcbs[t].mt = 1;
    tcbs[t].guard = 0x1234567890ABCDEFUL;
    tcbs[t].ptr_guard = 0xFEDCBA0987654321UL;
  }
  for (int t = 0; t < 3; ++t) {
    void* top = stacks[t] + sizeof(stacks[t]);
    clone(worker, top,
          CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD |
              CLONE_SYSVSEM | CLONE_SETTLS,
          &args[t], nullptr, &tcbs[t], &tcbs[t]);
  }
  unsigned long long jend = RDTSC() + JOIN;
  int all_done = 0;
  while (RDTSC() < jend) {
    if (__atomic_load_n(&args[0].done, __ATOMIC_ACQUIRE) &&
        __atomic_load_n(&args[1].done, __ATOMIC_ACQUIRE) &&
        __atomic_load_n(&args[2].done, __ATOMIC_ACQUIRE)) {
      all_done = 1;
      break;
    }
  }
  // main 自身 0.2s:若并行,总时长 ≈ max(等待≈0.5s) + 0.2s;若 worker 没跑 ≈ 0.9s + 0.2s
  unsigned long long end = RDTSC() + (all_done ? 720000000ULL : 0ULL);
  while (RDTSC() < end) {
  }
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #1549.925 ms32 KBWrong AnswerScore: 0


Judge Duck Online | 评测鸭在线
Server Time: 2026-09-20 14:38:24 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠