调度SIMD指令+ SIMDPP + qmake

时间:2016-09-14 06:43:05

标签: c++ qt qmake simd avx

我正在开发一个使用SIMD指令集的QT小部件。我编译了3个版本:SSE3,AVX和AVX2(simdpp允许通过单个#define在它们之间切换)。

现在,根据最佳支持的指令集,我想要的是我的小部件在这些实现之间自动切换。 simdpp提供的指南使用了一些makefile魔法:

CXXFLAGS=""

test: main.o test_sse2.o test_sse3.o test_sse4_1.o test_null.o
    g++ $^ -o test

main.o: main.cc
    g++ main.cc $(CXXFLAGS) -c -o main.o

test_null.o: test.cc
    g++ test.cc -c $(CXXFLAGS) -DSIMDPP_EMIT_DISPATCHER \
        -DSIMDPP_DISPATCH_ARCH1=SIMDPP_ARCH_X86_SSE2 \
        -DSIMDPP_DISPATCH_ARCH2=SIMDPP_ARCH_X86_SSE3 \
        -DSIMDPP_DISPATCH_ARCH3=SIMDPP_ARCH_X86_SSE4_1 -o test_null.o

test_sse2.o: test.cc
    g++ test.cc -c $(CXXFLAGS) -DSIMDPP_ARCH_X86_SSE2 -msse2 -o test_sse2.o

test_sse3.o: test.cc
    g++ test.cc -c $(CXXFLAGS) -DSIMDPP_ARCH_X86_SSE3 -msse3 -o test_sse3.o

test_sse4_1.o: test.cc
    g++ test.cc -c $(CXXFLAGS) -DSIMDPP_ARCH_X86_SSE4_1 -msse4.1 -o test_sse4_1.o

以下是指南的链接:http://p12tic.github.io/libsimdpp/v2.0~rc2/libsimdpp/arch/dispatch.html

我不知道如何使用qmake实现此类行为。有什么想法吗?

首先想到的是创建一个带有调度代码的共享库,并将其链接到项目。在这里,我再次陷入困境。应用程序是跨平台的,这意味着它必须使用GCC和MSVC(确切地说是vc120)进行编译,这迫使在Windows中使用nmake,我试过,实际上,但这就像我整个程序员生活中最糟糕的经历

提前感谢世界各地的程序员!

3 个答案:

答案 0 :(得分:2)

抱歉,如果这有点晚了。希望我还能提供帮助。

您需要考虑两个方面:编译时间和运行时间。

编译时间 - 需要创建代码以支持不同的功能。 运行时 - 需要创建代码来决定可以运行哪些功能。

您想要做的是创建一个调度员......

FuncImpl.h:

#pragma once
void execAvx2();
void execAvx();
void execSse();
void execDefault();

FuncImpl.cpp:

// Compile this file once for each variant with different compiler settings.
#if defined(__AVX2__)
void execAvx2()
{
 // AVX2 impl
...
}

#elif defined (__AVX__)

void execAvx()
{
// AVX impl
...
}

#elif defined (__SSE4_2__)

void execSse()
{
 // Sse impl
...
}

#else

void execDefault()
{
 // Vanilla impl
...
}

#endif

DispatchFunc.cpp

#include "FuncImpl.h"

// Decide at runtime which code to run
void dispatchFunc()
{
     if(CheckCpuAvx2Flag())
     {
         execAvx2();
     } 
     else if(CheckCpuAvxFlag())
     {
         execAvx();
     }
     else if(CheckCpuSseFlags())
     {
         execSse();
     }
     else
     {
         execDefault();
     }
}

您可以做的是创建一组QMAKE_EXTRA_COMPILERS。

SampleCompiler.pri(为每个变体执行此操作):

MyCompiler.name = MyCompiler         # Name
MyCompiler.input = MY_SOURCES        # Symbol of the source list to compile
MyCompiler.dependency_type = TYPE_C
MyCompiler.variable_out = OBJECTS
# EXTRA_CXXFLAGS = -mavx / -mavx2 / -msse4.2
# _var = creates FileName_var.o => replace with own variant (_sse, etc)  
MyCompiler.output = ${QMAKE_VAR_OBJECTS_DIR}${QMAKE_FILE_IN_BASE}_var$${first(QMAKE_EXT_OBJ)}
MyCompiler.commands = $${QMAKE_CXX} $(CXXFLAGS) $${EXTRA_CXXFLAGS} $(INCPATH) -c ${QMAKE_FILE_IN} -o${QMAKE_FILE_OUT}
QMAKE_EXTRA_COMPILERS += MyCompiler   # Add my compiler

MyProject.pro

...
include(SseCompiler.pri)
include(AvxCompiler.pri)
include(Avx2Compiler.pri)
..

# Normal sources
# Will create FuncImpl.o and DispatchFunc.o
SOURCES += FuncImpl.cpp \
           DispatchFunc.cpp

# Give the other compilers their sources
# Will create FuncImpl_avx2.o FuncImpl_avx.o FuncImpl_sse.o
AVX2_SOURCES += FuncImpl.cpp
AVX_SOURCES += FuncImpl.cpp
SSE_SOURCES += FuncImpl.cpp

# Link all objects
...

现在您只需要调用dispatchFunc()!

检查cpu标志是另一项练习: cpuid

答案 1 :(得分:1)

这些只是项目定义。您可以在.pro文件中使用DEFINES + =设置它们。您需要为要支持的指令集设置标志,并且simdpp负责在运行时为处理器选择最佳的标记。

例如,请参阅Add a define to qmake WITH a value?

答案 2 :(得分:0)

这是与SIMD调度程序一起使用的qmake .pro文件。它非常冗长,因此对于更多指令集,最好通过脚本生成调度的块,然后将其写入.pri文件,然后从主.pro文件中包含它。

TEMPLATE = app
TARGET = simd_test
INCLUDEPATH += .

QMAKE_CXXFLAGS = -O3 -std=c++17

SOURCES += main.cpp

SOURCES_dispatch = test.cpp
{
    # SSE2
    DISPATCH_CXXFLAGS = -msse2
    DISPATCH_SUFFIX = _sse2

    src_dispatch_sse2.name = src_dispatch_sse2
    src_dispatch_sse2.input = SOURCES_dispatch
    src_dispatch_sse2.dependency_type = TYPE_C
    src_dispatch_sse2.variable_out = OBJECTS
    src_dispatch_sse2.output = ${QMAKE_VAR_OBJECTS_DIR}${QMAKE_FILE_IN_BASE}$${DISPATCH_SUFFIX}$${first(QMAKE_EXT_OBJ)}
    src_dispatch_sse2.commands = $${QMAKE_CXX} $(CXXFLAGS) $${DISPATCH_CXXFLAGS} $(INCPATH) -c ${QMAKE_FILE_IN} -o ${QMAKE_FILE_OUT}
    QMAKE_EXTRA_COMPILERS += src_dispatch_sse2
}
{
    # SSE3
    DISPATCH_CXXFLAGS = -msse3
    DISPATCH_SUFFIX = _sse3

    src_dispatch_sse3.name = src_dispatch_sse3
    src_dispatch_sse3.input = SOURCES_dispatch
    src_dispatch_sse3.dependency_type = TYPE_C
    src_dispatch_sse3.variable_out = OBJECTS
    src_dispatch_sse3.output = ${QMAKE_VAR_OBJECTS_DIR}${QMAKE_FILE_IN_BASE}$${DISPATCH_SUFFIX}$${first(QMAKE_EXT_OBJ)}
    src_dispatch_sse3.commands = $${QMAKE_CXX} $(CXXFLAGS) $${DISPATCH_CXXFLAGS} $(INCPATH) -c ${QMAKE_FILE_IN} -o ${QMAKE_FILE_OUT}
    QMAKE_EXTRA_COMPILERS += src_dispatch_sse3
}
{
    # SSE41
    DISPATCH_CXXFLAGS = -msse4.1
    DISPATCH_SUFFIX = _sse41

    src_dispatch_sse41.name = src_dispatch_sse41
    src_dispatch_sse41.input = SOURCES_dispatch
    src_dispatch_sse41.dependency_type = TYPE_C
    src_dispatch_sse41.variable_out = OBJECTS
    src_dispatch_sse41.output = ${QMAKE_VAR_OBJECTS_DIR}${QMAKE_FILE_IN_BASE}$${DISPATCH_SUFFIX}$${first(QMAKE_EXT_OBJ)}
    src_dispatch_sse41.commands = $${QMAKE_CXX} $(CXXFLAGS) $${DISPATCH_CXXFLAGS} $(INCPATH) -c ${QMAKE_FILE_IN} -o ${QMAKE_FILE_OUT}
    QMAKE_EXTRA_COMPILERS += src_dispatch_sse41
}