简体   繁体   English

是否可以在编译时检查包含枚举的语句

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

I am writing a "error-check" function whose only task is to check single if statement. 我正在编写“错误检查”功能,其唯一任务是检查单个if语句。 This function is overloaded for 7 different error types, but first let me present my code: 对于7种不同的错误类型,此函数已重载,但首先让我介绍一下我的代码:

ErrorCheck.h: (whole file) 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: (simplified version) 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);

Question: #1 问题:#1
Is it possible to optimize if statement inside bool errchk <***> (***) 是否可以优化bool errchk <***> (***) if语句
I know that this function is meant to be called at run time, but if we give it a second thought, we will see that we are comparing two enums. 我知道应该在运行时调用此函数,但是如果再三考虑,就会发现我们正在比较两个枚举。 And therefore meaby could we force compiler to check every possible outcome of if statement, and then just run the right one at run-time? 因此,meaby可以迫使编译器检查if语句的所有可能结果,然后在运行时运行正确的语句吗?

Question: #2 问题2
Does it even need to be optimized?? 甚至需要优化吗?
With the #include <chrono> lib I have calculated that this code will take up to 40 nanoseconds when "success" value is detected. 使用#include <chrono> lib,我计算出,当检测到“成功”值时,此代码将花费40纳秒。 And up to 400 milliseconds when "error" value is detected. 当检测到“错误”值时,最长为400毫秒。

Is it possible to optimize if statement inside bool errchk <***> (***) ? 是否可以优化bool errchk <***> (***) if语句?

It is possible for the compiler in some cases, but not for you. 在某些情况下, 编译器是可能的,但您不可以。 You're writing a function that cannot make compile-time assumptions regarding the value of check . 您正在编写一个无法对check的值进行编译时假设的函数。 The compiler, however, might notice that you call, say errchk(cudaSuccess, whatever, etc, etc) ; 但是,编译器可能会注意到您调用了errchk(cudaSuccess, whatever, etc, etc) and it can choose to inline the function, in which case it can notice the if (check != successValue) is always true, and simply optimize away the entire call. 并且可以选择内联该函数,在这种情况下,它可以注意到if (check != successValue)始终为true,并且可以简单地优化整个调用。

Does it even need to be optimized?? 甚至需要优化吗?

Probably not. 可能不是。 If you have this code in a performance-critical tight loop, you should just take it out of the loop; 如果您的代码处于性能至关重要的紧密循环中,则应将其从循环中取出; and if you have it elsewhere, 40 ns are not such a big deal. 如果在其他地方使用,则40 ns没什么大不了的。 But - you need to profile your code to know what you should be optimizing. 但是-您需要分析您的代码以了解您应该优化的内容。 Don't waste your time optimizing things that take up just a small fraction of the execution time. 不要浪费时间来优化只占用执行时间一小部分的事情。

Speaking of profiling, CUDA offers a profiling facility which can also be used for host-side code. 说到分析,CUDA提供了一个分析工具 ,该工具也可用于主机端代码。 You could also consider using it via my C++ish wrappers for the CUDA Runtime API ( here are the profiling-specific API wrappers). 您也可以考虑通过我的C ++ ish包装器将其用于CUDA Runtime API( 是特定于配置文件的API包装器)。


PS: In my opinion, you should probably not be writing an errchk function at all. PS:我认为,您可能根本不应该编写errchk函数。 You're working with C++, remember? 您正在使用C ++,记得吗? Instead of this awful inbreeding of macros and templates - use exceptions ; 代替宏和模板的可怕的近交-使用异常 ; and you'll no longer have to remember to check the return value after every call. 而且您不必记住每次调用后都要检查返回值。 Exceptions will also let you distinguish types of errors by their class; 异常也可以让您按错误类别区分错误的类型。 to nest errors within errors; 将错误嵌套在错误中; to express error information using more than just a single number etc. 表达错误信息,而不仅仅是使用单个数字等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM