gmm
gmm

Reputation: 1020

Read QT .pro file within program

I would like to know if there is some way to read a variable defined in the .pro file of a QT project, during runtime. the thing is that Im trying to compile cuda, for just one architecture (Sm_21), and I want to decide on runtime to use the cuda device that has that capability.

.pro file:

QT       += core gui opengl

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = hello-opengl
TEMPLATE = app


SOURCES += main.cpp\
       mainwindow.cpp \
       glwidget.cpp \



HEADERS  += mainwindow.h \
            glwidget.h \



FORMS    += mainwindow.ui


CUDA_ARCH = sm_21           # Type of CUDA architecture

I would like some way to use this CUDA_ARCH variable in my .cpp. For example

if (CUDA_ARCH == sm_21)
  then pick device 0
else
  pick device 1

Thank you very much!

Upvotes: 2

Views: 514

Answers (2)

hyde
hyde

Reputation: 62878

You can add preprosessor macro with the value, in .pro do:

CUDA_ARCH = sm_21           # Type of CUDA architecture

DEFINES += CUDA_ARCH=$${CUDA_ARCH}

So this is basically equivalent to adding this to your C code:

#define CUDA_ARCH sm_21

Then in code you can just use the macro, like you'd use any #define, for example:

// enum is most convenient way to get the architectures as symbols
enum CudaArchEnum { sm_21, sm_22};

//... initialize a variable
enum CudaArchEnum value = CUDA_ARCH; // value = sm_21;

//.. or from your question
if (CUDA_ARCH == sm_21) {
  // pick device 0
} else {
  // pick device 1
}

You can also put it to a variable as string, like this:

const char *CudaArchStr = #CUDA_ARCH; // CudaArchStr = "sm_21"

Upvotes: 3

Sebastian Lange
Sebastian Lange

Reputation: 4029

You can use

DEFINES += CUDA_ARCH_SM_21

and ask in the code for

#ifdef CUDA_ARCH_SM_21

I do not think its possible to directly create a "global" variable in .pro file. But you could just set your global variable CUDA_ARCH in the #ifdef block

#define CA_SM_21 0
#define CA_SM_OTHER 1
#ifdef CUDA_ARCH_SM_21
  int CUDA_ARCH = CA_SM_21
#elseif
  int CUDA_ARCH = CA_SM_OTHER
#endif

if(CUDA_ARCH == CA_SM_21)...

Upvotes: 6

Related Questions