提交记录 125752


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_codex_maker test. 自定义测试 Accepted 100 10.495 s 1053252 KB C++17 13.66 KB
提交时间 评测时间
2026-10-06 02:12:11 2026-10-06 02:12:25
/*

   BLIS
   An object-based framework for developing high-performance BLAS-like
   libraries.

   Copyright (C) 2014, The University of Texas at Austin
   Copyright (C) 2018 - 2023, Advanced Micro Devices, Inc. All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are
   met:
    - Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    - Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    - Neither the name(s) of the copyright holder(s) nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
// Classical float matrix multiplication, AVX2/FMA only.
#pragma GCC optimize("O3")
#pragma GCC target("avx2,fma")
#include <immintrin.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
namespace classic {
constexpr int MR=6,NR=16,MC=144,NC=32,KC=256;
alignas(4096) static float ap[MC*8192],ct[MC*NC];
// The FMA/load pipeline is adapted from BLIS Haswell SGEMM 6x16.
// It uses classical KC-block partial sums, then adds the existing C tile.
__attribute__((always_inline)) static inline void asm6_bulk(const float*a,const float*b,float*c,int stride,int groups){
    const float*aa=a;const float*bb=b;size_t ld=(size_t)stride*sizeof(float);float*cc=c;
    asm volatile(
        "movl %[groups], %%r9d\n\t"
        "movq %%rbx, %%r8\n\t"
        ".p2align 5\n\t"
        "2:\n\t"
        "leaq (%%rdi,%%rdi,2), %%rdx\n\t"
        "addq %%rcx, %%rdx\n\t"
        "vxorps %%ymm4, %%ymm4, %%ymm4\n\t"
        "vxorps %%ymm5, %%ymm5, %%ymm5\n\t"
        "prefetcht0 (%%rcx)\n\t"
        "vxorps %%ymm6, %%ymm6, %%ymm6\n\t"
        "vxorps %%ymm7, %%ymm7, %%ymm7\n\t"
        "prefetcht0 (%%rcx,%%rdi)\n\t"
        "vxorps %%ymm8, %%ymm8, %%ymm8\n\t"
        "vxorps %%ymm9, %%ymm9, %%ymm9\n\t"
        "prefetcht0 (%%rcx,%%rdi,2)\n\t"
        "vxorps %%ymm10, %%ymm10, %%ymm10\n\t"
        "vxorps %%ymm11, %%ymm11, %%ymm11\n\t"
        "prefetcht0 (%%rdx)\n\t"
        "vxorps %%ymm12, %%ymm12, %%ymm12\n\t"
        "vxorps %%ymm13, %%ymm13, %%ymm13\n\t"
        "prefetcht0 (%%rdx,%%rdi)\n\t"
        "vxorps %%ymm14, %%ymm14, %%ymm14\n\t"
        "vxorps %%ymm15, %%ymm15, %%ymm15\n\t"
        "prefetcht0 (%%rdx,%%rdi,2)\n\t"
        "vmovaps (%%rbx), %%ymm0\n\t"
        "vmovaps 32(%%rbx), %%ymm1\n\t"
        "movl $64, %%esi\n\t"
        ".p2align 5\n\t"
        "1:\n\t"
        "prefetcht0 256(%%rax)\n\t"
        "vbroadcastss 0(%%rax), %%ymm2\n\t"
        "vbroadcastss 4(%%rax), %%ymm3\n\t"
        "vfmadd231ps %%ymm0, %%ymm2, %%ymm4\n\t"
        "vfmadd231ps %%ymm1, %%ymm2, %%ymm5\n\t"
        "vfmadd231ps %%ymm0, %%ymm3, %%ymm6\n\t"
        "vfmadd231ps %%ymm1, %%ymm3, %%ymm7\n\t"
        "vbroadcastss 8(%%rax), %%ymm2\n\t"
        "vbroadcastss 12(%%rax), %%ymm3\n\t"
        "vfmadd231ps %%ymm0, %%ymm2, %%ymm8\n\t"
        "vfmadd231ps %%ymm1, %%ymm2, %%ymm9\n\t"
        "vfmadd231ps %%ymm0, %%ymm3, %%ymm10\n\t"
        "vfmadd231ps %%ymm1, %%ymm3, %%ymm11\n\t"
        "vbroadcastss 16(%%rax), %%ymm2\n\t"
        "vbroadcastss 20(%%rax), %%ymm3\n\t"
        "vfmadd231ps %%ymm0, %%ymm2, %%ymm12\n\t"
        "vfmadd231ps %%ymm1, %%ymm2, %%ymm13\n\t"
        "vfmadd231ps %%ymm0, %%ymm3, %%ymm14\n\t"
        "vfmadd231ps %%ymm1, %%ymm3, %%ymm15\n\t"
        "vmovaps 64(%%rbx), %%ymm0\n\t"
        "vmovaps 96(%%rbx), %%ymm1\n\t"
        "vbroadcastss 24(%%rax), %%ymm2\n\t"
        "vbroadcastss 28(%%rax), %%ymm3\n\t"
        "vfmadd231ps %%ymm0, %%ymm2, %%ymm4\n\t"
        "vfmadd231ps %%ymm1, %%ymm2, %%ymm5\n\t"
        "vfmadd231ps %%ymm0, %%ymm3, %%ymm6\n\t"
        "vfmadd231ps %%ymm1, %%ymm3, %%ymm7\n\t"
        "vbroadcastss 32(%%rax), %%ymm2\n\t"
        "vbroadcastss 36(%%rax), %%ymm3\n\t"
        "vfmadd231ps %%ymm0, %%ymm2, %%ymm8\n\t"
        "vfmadd231ps %%ymm1, %%ymm2, %%ymm9\n\t"
        "vfmadd231ps %%ymm0, %%ymm3, %%ymm10\n\t"
        "vfmadd231ps %%ymm1, %%ymm3, %%ymm11\n\t"
        "vbroadcastss 40(%%rax), %%ymm2\n\t"
        "vbroadcastss 44(%%rax), %%ymm3\n\t"
        "vfmadd231ps %%ymm0, %%ymm2, %%ymm12\n\t"
        "vfmadd231ps %%ymm1, %%ymm2, %%ymm13\n\t"
        "vfmadd231ps %%ymm0, %%ymm3, %%ymm14\n\t"
        "vfmadd231ps %%ymm1, %%ymm3, %%ymm15\n\t"
        "vmovaps 128(%%rbx), %%ymm0\n\t"
        "vmovaps 160(%%rbx), %%ymm1\n\t"
        "prefetcht0 304(%%rax)\n\t"
        "vbroadcastss 48(%%rax), %%ymm2\n\t"
        "vbroadcastss 52(%%rax), %%ymm3\n\t"
        "vfmadd231ps %%ymm0, %%ymm2, %%ymm4\n\t"
        "vfmadd231ps %%ymm1, %%ymm2, %%ymm5\n\t"
        "vfmadd231ps %%ymm0, %%ymm3, %%ymm6\n\t"
        "vfmadd231ps %%ymm1, %%ymm3, %%ymm7\n\t"
        "vbroadcastss 56(%%rax), %%ymm2\n\t"
        "vbroadcastss 60(%%rax), %%ymm3\n\t"
        "vfmadd231ps %%ymm0, %%ymm2, %%ymm8\n\t"
        "vfmadd231ps %%ymm1, %%ymm2, %%ymm9\n\t"
        "vfmadd231ps %%ymm0, %%ymm3, %%ymm10\n\t"
        "vfmadd231ps %%ymm1, %%ymm3, %%ymm11\n\t"
        "vbroadcastss 64(%%rax), %%ymm2\n\t"
        "vbroadcastss 68(%%rax), %%ymm3\n\t"
        "vfmadd231ps %%ymm0, %%ymm2, %%ymm12\n\t"
        "vfmadd231ps %%ymm1, %%ymm2, %%ymm13\n\t"
        "vfmadd231ps %%ymm0, %%ymm3, %%ymm14\n\t"
        "vfmadd231ps %%ymm1, %%ymm3, %%ymm15\n\t"
        "vmovaps 192(%%rbx), %%ymm0\n\t"
        "vmovaps 224(%%rbx), %%ymm1\n\t"
        "vbroadcastss 72(%%rax), %%ymm2\n\t"
        "vbroadcastss 76(%%rax), %%ymm3\n\t"
        "vfmadd231ps %%ymm0, %%ymm2, %%ymm4\n\t"
        "vfmadd231ps %%ymm1, %%ymm2, %%ymm5\n\t"
        "vfmadd231ps %%ymm0, %%ymm3, %%ymm6\n\t"
        "vfmadd231ps %%ymm1, %%ymm3, %%ymm7\n\t"
        "vbroadcastss 80(%%rax), %%ymm2\n\t"
        "vbroadcastss 84(%%rax), %%ymm3\n\t"
        "vfmadd231ps %%ymm0, %%ymm2, %%ymm8\n\t"
        "vfmadd231ps %%ymm1, %%ymm2, %%ymm9\n\t"
        "vfmadd231ps %%ymm0, %%ymm3, %%ymm10\n\t"
        "vfmadd231ps %%ymm1, %%ymm3, %%ymm11\n\t"
        "vbroadcastss 88(%%rax), %%ymm2\n\t"
        "vbroadcastss 92(%%rax), %%ymm3\n\t"
        "vfmadd231ps %%ymm0, %%ymm2, %%ymm12\n\t"
        "vfmadd231ps %%ymm1, %%ymm2, %%ymm13\n\t"
        "vfmadd231ps %%ymm0, %%ymm3, %%ymm14\n\t"
        "vfmadd231ps %%ymm1, %%ymm3, %%ymm15\n\t"
        "addq $96, %%rax\n\t"
        "addq $256, %%rbx\n\t"
        "vmovaps (%%rbx), %%ymm0\n\t"
        "vmovaps 32(%%rbx), %%ymm1\n\t"
        "decl %%esi\n\t"
        "jnz 1b\n\t"
        "vaddps (%%rcx), %%ymm4, %%ymm4\n\t"
        "vaddps 32(%%rcx), %%ymm5, %%ymm5\n\t"
        "vmovups %%ymm4, (%%rcx)\n\t"
        "vmovups %%ymm5, 32(%%rcx)\n\t"
        "vaddps (%%rcx,%%rdi), %%ymm6, %%ymm6\n\t"
        "vaddps 32(%%rcx,%%rdi), %%ymm7, %%ymm7\n\t"
        "vmovups %%ymm6, (%%rcx,%%rdi)\n\t"
        "vmovups %%ymm7, 32(%%rcx,%%rdi)\n\t"
        "vaddps (%%rcx,%%rdi,2), %%ymm8, %%ymm8\n\t"
        "vaddps 32(%%rcx,%%rdi,2), %%ymm9, %%ymm9\n\t"
        "vmovups %%ymm8, (%%rcx,%%rdi,2)\n\t"
        "vmovups %%ymm9, 32(%%rcx,%%rdi,2)\n\t"
        "vaddps (%%rdx), %%ymm10, %%ymm10\n\t"
        "vaddps 32(%%rdx), %%ymm11, %%ymm11\n\t"
        "vmovups %%ymm10, (%%rdx)\n\t"
        "vmovups %%ymm11, 32(%%rdx)\n\t"
        "vaddps (%%rdx,%%rdi), %%ymm12, %%ymm12\n\t"
        "vaddps 32(%%rdx,%%rdi), %%ymm13, %%ymm13\n\t"
        "vmovups %%ymm12, (%%rdx,%%rdi)\n\t"
        "vmovups %%ymm13, 32(%%rdx,%%rdi)\n\t"
        "vaddps (%%rdx,%%rdi,2), %%ymm14, %%ymm14\n\t"
        "vaddps 32(%%rdx,%%rdi,2), %%ymm15, %%ymm15\n\t"
        "vmovups %%ymm14, (%%rdx,%%rdi,2)\n\t"
        "vmovups %%ymm15, 32(%%rdx,%%rdi,2)\n\t"
        "leaq (%%rdi,%%rdi,2), %%rdx\n\t"
        "leaq (%%rcx,%%rdx,2), %%rcx\n\t"
        "movq %%r8, %%rbx\n\t"
        "decl %%r9d\n\t"
        "jnz 2b\n\t"
        : "+a"(aa), "+b"(bb), "+c"(cc)
        : "D"(ld), [groups]"r"(groups)
        : "rdx", "rsi", "r8", "r9", "cc", "memory", "ymm0", "ymm1", "ymm2", "ymm3", "ymm4", "ymm5", "ymm6", "ymm7", "ymm8", "ymm9", "ymm10", "ymm11", "ymm12", "ymm13", "ymm14", "ymm15");
}

template<int R> static inline void micro(const float*a,const float*b,float*c,int stride){
    __m256 c00,c01;if(R>0){c00=_mm256_loadu_ps(c+0*stride);c01=_mm256_loadu_ps(c+0*stride+8);}
    __m256 c10,c11;if(R>1){c10=_mm256_loadu_ps(c+1*stride);c11=_mm256_loadu_ps(c+1*stride+8);}
    __m256 c20,c21;if(R>2){c20=_mm256_loadu_ps(c+2*stride);c21=_mm256_loadu_ps(c+2*stride+8);}
    __m256 c30,c31;if(R>3){c30=_mm256_loadu_ps(c+3*stride);c31=_mm256_loadu_ps(c+3*stride+8);}
    __m256 c40,c41;if(R>4){c40=_mm256_loadu_ps(c+4*stride);c41=_mm256_loadu_ps(c+4*stride+8);}
    __m256 c50,c51;if(R>5){c50=_mm256_loadu_ps(c+5*stride);c51=_mm256_loadu_ps(c+5*stride+8);}
#pragma GCC unroll 4
    for(int k=0;k<KC;++k){
        __m256 b0=_mm256_load_ps(b),b1=_mm256_load_ps(b+8);
        if(R>0){__m256 v=_mm256_broadcast_ss(a+0);c00=_mm256_fmadd_ps(v,b0,c00);c01=_mm256_fmadd_ps(v,b1,c01);}
        if(R>1){__m256 v=_mm256_broadcast_ss(a+1);c10=_mm256_fmadd_ps(v,b0,c10);c11=_mm256_fmadd_ps(v,b1,c11);}
        if(R>2){__m256 v=_mm256_broadcast_ss(a+2);c20=_mm256_fmadd_ps(v,b0,c20);c21=_mm256_fmadd_ps(v,b1,c21);}
        if(R>3){__m256 v=_mm256_broadcast_ss(a+3);c30=_mm256_fmadd_ps(v,b0,c30);c31=_mm256_fmadd_ps(v,b1,c31);}
        if(R>4){__m256 v=_mm256_broadcast_ss(a+4);c40=_mm256_fmadd_ps(v,b0,c40);c41=_mm256_fmadd_ps(v,b1,c41);}
        if(R>5){__m256 v=_mm256_broadcast_ss(a+5);c50=_mm256_fmadd_ps(v,b0,c50);c51=_mm256_fmadd_ps(v,b1,c51);}
        a+=MR;b+=NR;
    }
    if(R>0){_mm256_storeu_ps(c+0*stride,c00);_mm256_storeu_ps(c+0*stride+8,c01);}
    if(R>1){_mm256_storeu_ps(c+1*stride,c10);_mm256_storeu_ps(c+1*stride+8,c11);}
    if(R>2){_mm256_storeu_ps(c+2*stride,c20);_mm256_storeu_ps(c+2*stride+8,c21);}
    if(R>3){_mm256_storeu_ps(c+3*stride,c30);_mm256_storeu_ps(c+3*stride+8,c31);}
    if(R>4){_mm256_storeu_ps(c+4*stride,c40);_mm256_storeu_ps(c+4*stride+8,c41);}
    if(R>5){_mm256_storeu_ps(c+5*stride,c50);_mm256_storeu_ps(c+5*stride+8,c51);}
}
static void multiply(int n,const float*A,const float*B,float*C){
    if(n%NC||n%KC){
        memset(C,0,(size_t)n*n*sizeof(float));
        for(int i=0;i<n;++i)for(int k=0;k<n;++k)for(int j=0;j<n;++j)
            C[(size_t)i*n+j]+=A[(size_t)i*n+k]*B[(size_t)k*n+j];
        return;
    }
    float*bp=nullptr;
    if(posix_memalign((void**)&bp,4096,((size_t)n*n+16)*sizeof(float)))abort();
    memset(bp+(size_t)n*n,0,16*sizeof(float));
    for(int pc=0;pc<n;pc+=KC)for(int q=0;q<n;q+=NR)for(int k=0;k<KC;++k){
        float*out=bp+((pc/KC*(n/NR)+q/NR)*KC+k)*NR;
        _mm256_store_ps(out,_mm256_loadu_ps(B+(size_t)(pc+k)*n+q));
        _mm256_store_ps(out+8,_mm256_loadu_ps(B+(size_t)(pc+k)*n+q+8));
    }
    for(int ic=0;ic<n;ic+=MC){
        int rows=n-ic<MC?n-ic:MC;
        for(int pc=0;pc<n;pc+=KC)for(int r=0;r<rows;r+=MR)for(int k=0;k<KC;++k)for(int i=0;i<MR;++i)
            ap[((pc/KC*(MC/MR)+r/MR)*KC+k)*MR+i]=r+i<rows?A[(size_t)(ic+r+i)*n+pc+k]:0;
        for(int jc=0;jc<n;jc+=NC){
            memset(ct,0,sizeof(ct));
            for(int pc=0;pc<n;pc+=KC){
            for(int q=jc;q<jc+NC;q+=NR){
                const float*a0=ap+(pc/KC)*(MC/MR)*KC*MR;
                const float*b=bp+((pc/KC)*(n/NR)+q/NR)*KC*NR;
                if(rows/MR)asm6_bulk(a0,b,ct+q-jc,NC,rows/MR);
                int r=(rows/MR)*MR;
                if(r<rows){
                    const float*a=a0+(r/MR)*KC*MR;
                    float*c=ct+(size_t)r*NC+q-jc;

                if(rows-r==4)micro<4>(a,b,c,NC);
                else if(rows-r==2)micro<2>(a,b,c,NC);
                else {for(int i=0;i<rows-r;++i)for(int j=0;j<NR;++j){float v=c[(size_t)i*NC+j];for(int k=0;k<KC;++k)v+=a[k*MR+i]*b[k*NR+j];c[(size_t)i*NC+j]=v;}}
                }
            }
            }
            for(int i=0;i<rows;++i)for(int j=0;j<NC;j+=8)
                _mm256_stream_ps(C+(size_t)(ic+i)*n+jc+j,_mm256_load_ps(ct+(size_t)i*NC+j));
        }
    }
    _mm_sfence();
    free(bp);
}
}
void matrix_multiply(int n,const float*A,const float*B,float*C){classic::multiply(n,A,B,C);}

// DUCK_PUBLIC_SURROGATE_BENCHMARK
// Public analytic matrices unrelated to production input; no random seed.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(){
 constexpr int n=8192;constexpr size_t z=(size_t)n*n;
 float*A=nullptr,*B=nullptr,*C=nullptr;
 if(posix_memalign((void**)&A,4096,z*4)||posix_memalign((void**)&B,4096,z*4)||posix_memalign((void**)&C,4096,z*4))return 1;
 for(int i=0;i<n;++i)for(int j=0;j<n;++j){
  A[(size_t)i*n+j]=float(((i*23+j*7+19)%1009)-504)/504.f;
  B[(size_t)i*n+j]=float(((i*37+j*11+29)%1013)-506)/506.f;
 }
 matrix_multiply(n,A,B,C);
 double maxerr=0;
 for(int t=0;t<64;++t){int i=(t*127+31)%n,j=(t*337+53)%n;double v=0;for(int k=0;k<n;++k)v+=(double)A[(size_t)i*n+k]*B[(size_t)k*n+j];double e=fabs(v-C[(size_t)i*n+j]);if(e>maxerr)maxerr=e;if(e>0.1)return 2;}
 printf("classical_full_matrix_checked max_error=%g\n",maxerr);return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #110.495 s1028 MB + 580 KBAcceptedScore: 100


Judge Duck Online | 评测鸭在线
Server Time: 2026-10-06 07:26:04 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠