See this link (despite it being a couple years old) regarding the failure of dgetrf+dgetri on a singular matrix. It also suggests a way to check the "invertibility" of the matrix in question: estimating the condition number (function dgecon
).
The condition number can help because it is a property of the matrix itself, and therefore not related to the accuracy of your algorithm and/or round-off error of the PC. The larger the conditioning number, the closer to singularity your matrix is, so you could essentially set an (variable) upper-bound on your conditioning number. See the tables at the bottom of this page to see a potential function-calling-order based on the type of matrices you're using.