请问如何在C++中使用SIMD?



  • 使用sw5CC编译使用了带SIMD的程序

    #ifdef __cplusplus
    extern "C" {
    #endif
    #include <simd.h>
    #ifdef __cplusplus
    }
    #endif

    int main()
    {
    doublev4 a;
    double *p = (double *)&a;
    p[0] = 1.0;
    p[1] = 2.0;
    p[2] = 2.5;
    p[3] = 3.0;
    simd_print_doublev4(a);
    doublev4 b = -1.0;
    simd_print_doublev4(b);
    simd_print_doublev4(simd_vmuld(a, b));
    return 0;
    }
    这样编译会报错:
    simd.cpp: In function 'int main()':
    simd.cpp:21: error: cannot convert 'double' to 'double vector' in initialization
    simd.cpp:24: error: '__builtin_sw64_dlum' was not declared in this scope
    请问如何在C++中使用SIMD?



  • @老泽征尔请问如何在C++中使用SIMD? 中说:

    simd.cpp:24: error: '__builtin_sw64_dlum' was not declared in this scope

    目测是因为c++里面类型检查更为严格,再加上神威自己的c++编译器识别不了神威的simd builtin。
    嵌汇编就好了。。。T_T。

    #ifdef __cplusplus
    extern "C" {
    #endif
    #include <simd.h>
    #ifdef __cplusplus
    }
    #endif
    extern doublev4 __builtin_sw64_dlum(doublev4 a, doublev4 b);
    int main()
    {
      doublev4 a;
      double *p = (double *)&a;
      p[0] = 1.0;
      p[1] = 2.0;
      p[2] = 2.5;
      p[3] = 3.0;
      simd_print_doublev4(a);
      doublev4 b = simd_set_doublev4(-1.0, -1.0, -1.0, -1.0);
      doublev4 c;
      asm ("vmuld %1, %2, %0\n\t": "=f"(c) : "f"(a), "f"(b));
      simd_print_doublev4(b);
      simd_print_doublev4(c);
      return 0;
    }
    


  • 感谢您的回复

    如何在sw5CC编译的函数中使用向量作为返回值呢?
    例如函数

    static inline doublev4 myset1(double a, double b, double c, double d)
    {
    	doublev4 r;
    	r = simd_set_doublev4(a, b, c, d);
    	return r;
    }
    

    编译就会报错

    [cpcXXX@psn004 Athread]$ sw5CC -host -msimd simd.cpp 
    
    ### Assertion failure at line 5510 of ../../wgen/wgen_decl.cxx:
    ### Compiler Error during Writing WHIRL file phase:
    ### WGEN_Handle_Named_Return_Value: nrv type not in mem
    CC INTERNAL ERROR: /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/wgen42 returned non-zero status 1
    
    

    @popo


    另,在编译修改后的Gromacs中的pme-spread.cpp文件会报错

    Signal: Segmentation fault in Code_Expansion phase.
    Error: Signal Segmentation fault in phase Code_Expansion -- processing aborted
    *** Internal stack backtrace:
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/be.so(+0x32a1f2) [0x2b88dafa11f2]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/be.so(+0x32a280) [0x2b88dafa1280]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/be.so(+0x32b963) [0x2b88dafa2963]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/be.so(+0x32ba57) [0x2b88dafa2a57]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/be.so(ErrMsgLine+0xce) [0x2b88dafa2c26]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/be.so(+0x32995b) [0x2b88dafa095b]
        /lib64/libc.so.6() [0x306ec326a0]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/cg.so(_Z32REGISTER_CLASS_OP_Update_MappingP2op+0x10e) [0x2b88dbae334f]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/cg.so(+0x2b8218) [0x2b88dbb3e218]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/cg.so(_Z13BB_Append_OpsP2bbP3ops+0x7b) [0x2b88dbb3ec9c]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/cg.so(+0x260d09) [0x2b88dbae6d09]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/cg.so(+0x26202a) [0x2b88dbae802a]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/cg.so(+0x266b09) [0x2b88dbaecb09]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/cg.so(+0x26c086) [0x2b88dbaf2086]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/cg.so(+0x26c8a7) [0x2b88dbaf28a7]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/cg.so(+0x26c5eb) [0x2b88dbaf25eb]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/cg.so(_Z20Convert_WHIRL_To_OPsP2WN+0x2dc) [0x2b88dbaf2baa]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/cg.so(CG_Generate_Code+0x1dc) [0x2b88dba41415]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/be() [0x40dff2]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/be() [0x40e862]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/be() [0x40f599]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/be(main+0x6a8) [0x4100f9]
        /lib64/libc.so.6(__libc_start_main+0xfd) [0x306ec1ed5d]
        /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/be() [0x40b9e9]
    CC INTERNAL ERROR: /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/be died due to signal 4
    CC ERROR: core dumped
    

    或者

    ### Assertion failure at line 421 of ../../be/cg/alpha/exp_loadstore.cxx:
    ### Compiler Error in file /home/export/online1/cpc124/sunjun/projects/final/src/gromacs/ewald/pme-spread.cpp during Code_Expansion phase:
    ### NYI: Pick_Load_Instruction mtype
    cc INTERNAL ERROR: /usr/sw-mpp/swcc/lib/gcc-lib/sw_64-swcc-linux/5.421-sw-500/be returned non-zero status 1
    

    其余cpp文件编译没问题,就这个文件有问题



  • @老泽征尔 doublev4这个类型sw5CC应该没有定义,你需要自己定义一个doublev4类型


登录后回复