ADIOS2 icon indicating copy to clipboard operation
ADIOS2 copied to clipboard

How to turn off error messages in C API calls

Open pnorbert opened this issue 2 years ago • 4 comments

Exceptions thrown in the core are translated into errors in the C API, and are always printed by the Logger, even if the user just wants to use the result of the call (valid return or error code), resulting in many unwanted error messages (that are useless warnings from the user's point of view). We need a mechanism to suppress error messages in the C API temporarily.

pnorbert avatar May 24 '22 20:05 pnorbert

this issue came up because of the issue in #3232: trying to use one function call (that throws errors) as an existence check. But in general, if tools use the C API, they don't necessarily need error messages in the log if they properly handle error codes returned by the functions.

pnorbert avatar May 24 '22 20:05 pnorbert

@pnorbert The current implementation of helper::Throw does not print anything through cout or cerr. It only adds some extra information into the exception message itself and then throw it. If these exceptions are caught correctly in the C API then it should not print anything. If there is a bug which actually prints something then please send me a test to re-produce it.

JasonRuonanWang avatar Jun 02 '22 17:06 JasonRuonanWang

The test code is the following:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <adios2_c.h>
int main(int argc, char **argv)
{
    MPI_Init(&argc, &argv);
    adios2_adios *adios = adios2_init(MPI_COMM_WORLD, adios2_debug_mode_on);
    adios2_io *io =  adios2_at_io(adios, "Read");
    if (io == NULL){
        adios2_io *io = adios2_declare_io(adios, "Read");
    }
    MPI_Finalize();
    return 0;
}

It prints out:

[Fri Jun  3 12:27:42 2022] [ADIOS2 ERROR] <Helper> <adiosSystem> <ExceptionToError> : adios2_at_io: [Fri Jun  3 12:27:42 2022] [ADIOS2 EXCEPTION] <Core> <ADIOS> <AtIO> : IO Read being used is not declared

It would be nice to treat this situation as a warning and aggregate into one message from all ranks because now log files are hardly readable.

dmitry-ganyushin avatar Jun 03 '22 16:06 dmitry-ganyushin

This was not introduced in the helper::Throw that I implemented. It has been printing since we ever had the C API. Before helper::Throw was introduced, it printed out to std::cerr,

int ExceptionToError(const std::string &function)
{
    try
    {
        throw;
    }
    catch (std::invalid_argument &e)
    {
        std::cerr << e.what() << "\n";
        std::cerr << function << "\n";
        return 1;
    }
    catch (std::system_error &e)
    {
        std::cerr << e.what() << "\n";
        std::cerr << function << "\n";
        return 2;
    }
    catch (std::runtime_error &e)
    {
        std::cerr << e.what() << "\n";
        std::cerr << function << "\n";
        return 3;
    }
    catch (std::exception &e)
    {
        std::cerr << e.what() << "\n";
        std::cerr << function << "\n";
        return 4;
    }
}

If this is not necessary then we can remove it. But we need to discuss carefully because there may be a reason why it has been there before.

Aggregating errors messages is not a smart idea, because it will kill performance for large-scale applications. This is common sense in any large-scale MPI apps or libraries. Even if you don't care about performance, it's still impossible to implement it right, because there is no guarantee that all ranks will always throw the same exception at the same time. If some ranks throw while others not, then MPI collective calls will hang.

JasonRuonanWang avatar Jun 03 '22 17:06 JasonRuonanWang