簡體   English   中英

是否可以在編譯時檢查包含枚舉的語句

[英]is it possible to check if statement containing an enum at compile time

我正在編寫“錯誤檢查”功能,其唯一任務是檢查單個if語句。 對於7種不同的錯誤類型,此函數已重載,但首先讓我介紹一下我的代碼:

ErrorCheck.h :(整個文件)

#pragma once
#ifndef __ERROR_CHECK_H__
#define __ERROR_CHECK_H__
// Header does not #include <***> anything. For obvious reasons.

// The function:
template <typename T>
bool errchk(const T check, const char* file, unsigned int line, const char* from, const char* func);

// How To call it:
#define ERRCHK(_check) \
    errchk(_check, __FILE__, __LINE__, __FUNC__, #_check)

#endif // !__ERROR_CHECK_H__

ErrorCheck.cpp :(簡化版)

// Include:
#include <cuda.h>
#include <cuda_runtime_api.h>
#include <cufft.h>
#include <cublas.h>
#include <curand_kernel.h>
#include <cusolver_common.h>
#include "cusparse.h"
#include "ErrorCheck.h"

// Functions bellow are overloaded 7 times for every error type from headers included above
const char * getErrorName(const Type & error) { /* ... */ };
const char * getErrorString(const Type & error) { /* ... */ };

// The function:
template <typename T, T successValue>
bool errchk(const T check, const char* file, unsigned int line, const char* from, const char* func)
{
    if (check != successValue) {
        // Report Error
        return true; // Error was found.
    }
    return false; // No error.
}

// Instantiations:
template bool errchk <bool            > (const bool             check, const char * file, unsigned int line, const char * from, const char * func);
template bool errchk <cudaError_t     > (const cudaError_t      check, const char * file, unsigned int line, const char * from, const char * func);
template bool errchk <cufftResult_t   > (const cufftResult_t    check, const char * file, unsigned int line, const char * from, const char * func);
template bool errchk <cublasStatus_t  > (const cublasStatus_t   check, const char * file, unsigned int line, const char * from, const char * func);
template bool errchk <curandStatus_t  > (const curandStatus_t   check, const char * file, unsigned int line, const char * from, const char * func);
template bool errchk <cusolverStatus_t> (const cusolverStatus_t check, const char * file, unsigned int line, const char * from, const char * func);
template bool errchk <cusparseStatus_t> (const cusparseStatus_t check, const char * file, unsigned int line, const char * from, const char * func);

問題:#1
是否可以優化bool errchk <***> (***) if語句
我知道應該在運行時調用此函數,但是如果再三考慮,就會發現我們正在比較兩個枚舉。 因此,meaby可以迫使編譯器檢查if語句的所有可能結果,然后在運行時運行正確的語句嗎?

問題2
甚至需要優化嗎?
使用#include <chrono> lib,我計算出,當檢測到“成功”值時,此代碼將花費40納秒。 當檢測到“錯誤”值時,最長為400毫秒。

是否可以優化bool errchk <***> (***) if語句?

在某些情況下, 編譯器是可能的,但您不可以。 您正在編寫一個無法對check的值進行編譯時假設的函數。 但是,編譯器可能會注意到您調用了errchk(cudaSuccess, whatever, etc, etc) 並且可以選擇內聯該函數,在這種情況下,它可以注意到if (check != successValue)始終為true,並且可以簡單地優化整個調用。

甚至需要優化嗎?

可能不是。 如果您的代碼處於性能至關重要的緊密循環中,則應將其從循環中取出; 如果在其他地方使用,則40 ns沒什么大不了的。 但是-您需要分析您的代碼以了解您應該優化的內容。 不要浪費時間來優化只占用執行時間一小部分的事情。

說到分析,CUDA提供了一個分析工具 ,該工具也可用於主機端代碼。 您也可以考慮通過我的C ++ ish包裝器將其用於CUDA Runtime API( 是特定於配置文件的API包裝器)。


PS:我認為,您可能根本不應該編寫errchk函數。 您正在使用C ++,記得嗎? 代替宏和模板的可怕的近交-使用異常 ; 而且您不必記住每次調用后都要檢查返回值。 異常也可以讓您按錯誤類別區分錯誤的類型。 將錯誤嵌套在錯誤中; 表達錯誤信息,而不僅僅是使用單個數字等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM