Commit f29fd47e authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Implemented new XACCLogger and api functions for using it (#22)


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 429fd55b
......@@ -12,5 +12,5 @@
# *******************************************************************************/
# Gather tests
file (GLOB test_files tests/*.cpp)
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${LIBRARY_NAME};${Boost_LIBRARIES};dl;pthread;xacc-quantum-aqc")
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${LIBRARY_NAME};${Boost_LIBRARIES};dl;pthread;xacc-quantum-aqc;xacc")
......@@ -31,12 +31,12 @@ Embedding TrivialEmbeddingAlgorithm::embed(
if (i < j && i != j
&& (problem->edgeExists(i, j) && !hdware->edgeExists(i, j))) {
if (failHard) {
XACCError(
XACCLogger::instance()->error(
"Trivial Embedding not possible, there is no hardware edge corresponding to ("
+ std::to_string(i) + ", "
+ std::to_string(j) + ") problem edge.");
} else {
XACCInfo("This embedding failed, but user requested to not fail hard. Returning empty embedding.");
XACCLogger::instance()->info("This embedding failed, but user requested to not fail hard. Returning empty embedding.");
xaccEmbedding.clear();
return xaccEmbedding;
}
......
......@@ -12,5 +12,5 @@
# *******************************************************************************/
# Gather tests
file (GLOB test_files tests/*.cpp)
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${Boost_LIBRARIES}")
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${Boost_LIBRARIES};xacc")
......@@ -67,7 +67,7 @@ public:
return f;
}
}
XACCError("Invalid kernel name - " + name);
XACCLogger::instance()->error("Invalid kernel name - " + name);
}
virtual std::vector<std::shared_ptr<Function>> getKernels() {
......
......@@ -51,7 +51,7 @@ public:
if (instructions.size() > idx) {
return *std::next(instructions.begin(), idx);
} else {
XACCError("Invalid instruction index.");
XACCLogger::instance()->error("Invalid instruction index.");
}
}
......@@ -142,19 +142,19 @@ public:
}
virtual InstructionParameter getParameter(const int idx) const {
XACCError("DWKernel does not contain runtime parameters.");
XACCLogger::instance()->error("DWKernel does not contain runtime parameters.");
}
virtual void setParameter(const int idx, InstructionParameter& p) {
XACCError("DWKernel does not contain runtime parameters.");
XACCLogger::instance()->error("DWKernel does not contain runtime parameters.");
}
virtual std::vector<InstructionParameter> getParameters() {
XACCError("DWKernel does not contain runtime parameters.");
XACCLogger::instance()->error("DWKernel does not contain runtime parameters.");
}
virtual void addParameter(InstructionParameter instParam) {
XACCError("DWKernel does not contain runtime parameters.");
XACCLogger::instance()->error("DWKernel does not contain runtime parameters.");
}
virtual bool isParameterized() {
......@@ -166,7 +166,7 @@ public:
}
virtual void evaluateVariableParameters(std::vector<InstructionParameter> parameters) {
XACCError("DWKernel does not contain runtime parameters.");
XACCLogger::instance()->error("DWKernel does not contain runtime parameters.");
}
......
......@@ -25,7 +25,7 @@ const std::string KernelReplacementPreprocessor::process(const std::string& sour
std::shared_ptr<Compiler> compiler,
std::shared_ptr<Accelerator> accelerator) {
XACCInfo("Running Kernel Replacement Preprocessor.\n");
XACCLogger::instance()->info("Running Kernel Replacement Preprocessor.\n");
// Find any occurrences of xacc::FUNCTION
// Get the first occurrence
......
......@@ -13,5 +13,5 @@
# Gather tests
file (GLOB test_files tests/*.cpp)
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${LIBRARY_NAME};${Boost_LIBRARIES};xacc-quantum-gate")
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${LIBRARY_NAME};${Boost_LIBRARIES};xacc;xacc-quantum-gate")
......@@ -72,7 +72,7 @@ public:
if (instructions.size() > idx) {
return *std::next(instructions.begin(), idx);
} else {
XACCError("Invalid instruction index.");
XACCLogger::instance()->error("Invalid instruction index.");
}
}
......@@ -142,7 +142,7 @@ public:
virtual InstructionParameter getParameter(const int idx) const {
if (idx + 1 > parameters.size()) {
XACCError("Invalid Parameter requested.");
XACCLogger::instance()->error("Invalid Parameter requested.");
}
return parameters[idx];
......@@ -150,7 +150,7 @@ public:
virtual void setParameter(const int idx, InstructionParameter& p) {
if (idx + 1 > parameters.size()) {
XACCError("Invalid Parameter requested.");
XACCLogger::instance()->error("Invalid Parameter requested.");
}
parameters[idx] = p;
......
......@@ -130,7 +130,7 @@ public:
virtual InstructionParameter getParameter(const int idx) const {
if (idx + 1 > parameters.size()) {
XACCError("Invalid Parameter requested from Parameterized Gate Instruction.");
XACCLogger::instance()->error("Invalid Parameter requested from Parameterized Gate Instruction.");
}
return parameters[idx];
......@@ -138,7 +138,7 @@ public:
virtual void setParameter(const int idx, InstructionParameter& p) {
if (idx + 1 > parameters.size()) {
XACCError("Invalid Parameter requested from Parameterized Gate Instruction.");
XACCLogger::instance()->error("Invalid Parameter requested from Parameterized Gate Instruction.");
}
parameters[idx] = p;
......
......@@ -97,7 +97,7 @@ public:
return f;
}
}
XACCError("Invalid kernel name.");
XACCLogger::instance()->error("Invalid kernel name.");
}
virtual bool kernelExists(const std::string& name) {
......
......@@ -14,6 +14,6 @@ include_directories(${CMAKE_SOURCE_DIR}/tpls/eigen)
# Gather tests
file (GLOB test_files tests/*.cpp)
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${Boost_LIBRARIES};xacc-quantum-gate")
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${Boost_LIBRARIES};xacc;xacc-quantum-gate")
......@@ -55,14 +55,14 @@ public:
bool add(const std::string& id,
CreatorFunctionPtr f) {
if (registry.find(id) != registry.end()) {
XACCInfo(id + " already exists in Registry. Ignoring and retaining previous Registry entry");
XACCLogger::instance()->info(id + " already exists in Registry. Ignoring and retaining previous Registry entry");
return true;
}
if (RuntimeOptions::instance()->exists("verbose-registry")) XACCInfo("Registry adding " + id);
if (RuntimeOptions::instance()->exists("verbose-registry")) XACCLogger::instance()->info("Registry adding " + id);
bool s = registry.emplace(std::make_pair(id, f)).second;
if (!s) {
XACCError("Could not add " + id + " to the Registry.");
XACCLogger::instance()->error("Could not add " + id + " to the Registry.");
} else {
return s;
}
......@@ -81,14 +81,14 @@ public:
bool add(const std::string& id,
CreatorFunctionPtr f, std::shared_ptr<options_description> options) {
if (registry.find(id) != registry.end()) {
XACCInfo(id + " already exists in Registry. Ignoring and retaining previous Registry entry");
XACCLogger::instance()->info(id + " already exists in Registry. Ignoring and retaining previous Registry entry");
return true;
}
if (RuntimeOptions::instance()->exists("verbose-registry")) XACCInfo("Registry adding " + id);
if (RuntimeOptions::instance()->exists("verbose-registry")) XACCLogger::instance()->info("Registry adding " + id);
bool s = registry.emplace(std::make_pair(id, f)).second;
bool s2 = registryOptions.insert(std::make_pair(id, std::move(options))).second;
if (!s || ! s2) {
XACCError("Could not add " + id + " to the Registry.");
XACCLogger::instance()->error("Could not add " + id + " to the Registry.");
} else {
return true;
}
......@@ -98,19 +98,19 @@ public:
std::shared_ptr<options_description> options,
std::function<bool(variables_map&)> optionsHandler) {
if (registry.find(id) != registry.end()) {
XACCInfo(
XACCLogger::instance()->info(
id
+ " already exists in Registry. Ignoring and retaining previous Registry entry");
return true;
}
if (RuntimeOptions::instance()->exists("verbose-registry"))
XACCInfo("Registry adding " + id);
XACCLogger::instance()->info("Registry adding " + id);
bool s = registry.emplace(std::make_pair(id, f)).second;
bool s2 =
registryOptions.insert(std::make_pair(id, std::move(options))).second;
bool s3 = registryOptionHandlers.insert(std::make_pair(id, std::move(optionsHandler))).second;
if (!s || !s2) {
XACCError("Could not add " + id + " to the Registry.");
XACCLogger::instance()->error("Could not add " + id + " to the Registry.");
} else {
return true;
}
......@@ -128,7 +128,7 @@ public:
if (search != registry.end()) {
return registry[id]->operator()(args...);
} else {
XACCError("Invalid Registry map id string - " + id);
XACCLogger::instance()->error("Invalid Registry map id string - " + id);
}
}
......
......@@ -165,7 +165,7 @@ public:
for (int i = 0; i < bufferState.size(); i++) {
XACCInfo(
XACCLogger::instance()->info(
bitStrings[i] + " -> ("
+ std::to_string(std::real(bufferState(i))) + ", "
+ std::to_string(std::imag(bufferState(i))) + ")");
......
......@@ -72,7 +72,7 @@ target_link_libraries(${LIBRARY_NAME} ${Boost_LIBRARIES} ${CppUsLib})
file(GLOB test_files utils/tests/*Tester.cpp accelerator/tests/*Tester.cpp)
# Add the tests
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}/utils" "${Boost_LIBRARIES}")
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}/utils" "${Boost_LIBRARIES};xacc")
install(FILES ${HEADERS} DESTINATION include/xacc)
install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
......@@ -17,6 +17,7 @@ namespace xacc {
bool xaccFrameworkInitialized = false;
std::shared_ptr<CLIParser> xaccCLParser = std::make_shared<CLIParser>();
int argc = 0;
char** argv = NULL;
......@@ -50,30 +51,22 @@ void Initialize(int arc, char** arv) {
argc = arc;
argv = arv;
// Create the iniitial xacc-console
std::shared_ptr<spdlog::logger> tmpInitConsole;
if (!spdlog::get("xacc-console")) {
tmpInitConsole = spdlog::stdout_logger_mt("xacc-console");
} else {
tmpInitConsole = spdlog::get("xacc-console");
}
XACCInfo("[xacc] Initializing XACC Framework.");
// Get reference to the service registry
auto serviceRegistry = xacc::ServiceRegistry::instance();
// Parse any user-supplied command line options
xaccCLParser->parse(argc, argv);
info("[xacc] Initializing XACC Framework.");
// Check that we have some
auto s = serviceRegistry->getServices<Compiler>().size();
auto a = serviceRegistry->getServices<Accelerator>().size();
XACCInfo(
info(
"[xacc::plugins] XACC has " + std::to_string(s) + " Compiler"
+ ((s == 1 || s == 0) ? "" : "s") + " available.");
XACCInfo(
info(
"[xacc::plugins] XACC has " + std::to_string(a) + " Accelerator"
+ ((s == 0 || s == 1) ? "" : "s") + " available.");
......@@ -84,6 +77,19 @@ void Initialize(int arc, char** arv) {
xacc::xaccFrameworkInitialized = true;
}
void info(const std::string& msg, MessagePredicate predicate) {
XACCLogger::instance()->info(msg, predicate);
}
void debug(const std::string& msg, MessagePredicate predicate) {
XACCLogger::instance()->debug(msg, predicate);
}
void error(const std::string& msg, MessagePredicate predicate) {
XACCLogger::instance()->error(msg, predicate);
}
void addCommandLineOption(const std::string& optionName,
const std::string& optionDescription) {
xaccCLParser->addStringOption(optionName, optionDescription);
......@@ -103,7 +109,7 @@ bool optionExists(const std::string& optionKey) {
const std::string getOption(const std::string& optionKey) {
if (!optionExists(optionKey)) {
XACCError("Invalid runtime option - " + optionKey);
error("Invalid runtime option - " + optionKey);
}
return (*RuntimeOptions::instance())[optionKey];
}
......@@ -126,12 +132,12 @@ void setAccelerator(const std::string& acceleratorName) {
std::shared_ptr<Accelerator> getAccelerator() {
if (!xacc::xaccFrameworkInitialized) {
XACCError(
error(
"XACC not initialized before use. Please execute "
"xacc::Initialize() before using API.");
}
if (!optionExists("accelerator")) {
XACCError("Invalid use of XACC API. getAccelerator() with no string argument "
error("Invalid use of XACC API. getAccelerator() with no string argument "
"requires that you set --accelerator at the command line.");
}
auto acc = ServiceRegistry::instance()->getService<Accelerator>(getOption("accelerator"));
......@@ -139,7 +145,7 @@ std::shared_ptr<Accelerator> getAccelerator() {
acc->initialize();
return acc;
} else {
XACCError(
error(
"Invalid Accelerator. Could not find " + getOption("accelerator")
+ " in Accelerator Registry.");
}
......@@ -147,7 +153,7 @@ std::shared_ptr<Accelerator> getAccelerator() {
std::shared_ptr<Accelerator> getAccelerator(const std::string& name) {
if (!xacc::xaccFrameworkInitialized) {
XACCError(
error(
"XACC not initialized before use. Please execute "
"xacc::Initialize() before using API.");
}
......@@ -156,7 +162,7 @@ std::shared_ptr<Accelerator> getAccelerator(const std::string& name) {
acc->initialize();
return acc;
} else {
XACCError(
error(
"Invalid Accelerator. Could not find " + name
+ " in Accelerator Registry.");
}
......@@ -169,7 +175,7 @@ bool hasAccelerator(const std::string& name) {
std::shared_ptr<Compiler> getCompiler(const std::string& name) {
if (!xacc::xaccFrameworkInitialized) {
XACCError(
error(
"XACC not initialized before use. Please execute "
"xacc::Initialize() before using API.");
}
......@@ -177,7 +183,7 @@ std::shared_ptr<Compiler> getCompiler(const std::string& name) {
if (c) {
return c;
} else {
XACCError(
error(
"Invalid Compiler. Could not find " + name
+ " in Service Registry.");
}
......@@ -185,20 +191,20 @@ std::shared_ptr<Compiler> getCompiler(const std::string& name) {
std::shared_ptr<Compiler> getCompiler() {
if (!xacc::xaccFrameworkInitialized) {
XACCError(
error(
"XACC not initialized before use. Please execute "
"xacc::Initialize() before using API.");
}
auto options = RuntimeOptions::instance();
if (!optionExists("compiler")) {
XACCError("Invalid use of XACC API. getCompiler() with no string argument "
error("Invalid use of XACC API. getCompiler() with no string argument "
"requires that you set --compiler at the command line.");
}
auto compiler = ServiceRegistry::instance()->getService<Compiler>(getOption("compiler"));
if (compiler) {
return compiler;
} else {
XACCError(
error(
"Invalid Compiler. Could not find " + (*options)["compiler"]
+ " in Compiler Registry.");
}
......@@ -211,7 +217,7 @@ bool hasCompiler(const std::string& name) {
std::shared_ptr<IRTransformation> getIRTransformations(
const std::string& name) {
if (!xacc::xaccFrameworkInitialized) {
XACCError(
error(
"XACC not initialized before use. Please execute "
"xacc::Initialize() before using API.");
}
......@@ -219,7 +225,7 @@ std::shared_ptr<IRTransformation> getIRTransformations(
if (t) {
return t;
} else {
XACCError(
error(
"Invalid IRTransformation. Could not find " + name
+ " in Service Registry.");
}
......@@ -268,11 +274,12 @@ const std::string translateWithVisitor(const std::string& originalSource, const
* be called after using the XACC API.
*/
void Finalize() {
XACCInfo("");
XACCInfo("[xacc::plugins] Cleaning up Plugin Registry.");
info("");
info("[xacc::plugins] Cleaning up Plugin Registry.");
xacc::ServiceRegistry::instance()->destroy();
// XACCLogger::instance()->destroy();
xacc::xaccFrameworkInitialized = false;
XACCInfo("[xacc] Finalizing XACC Framework.");
info("[xacc] Finalizing XACC Framework.");
}
}
......@@ -66,6 +66,11 @@ void addCommandLineOption(const std::string& optionName,
void addCommandLineOptions(const std::string& category, const std::map<std::string, std::string>& options);
void addCommandLineOptions(std::shared_ptr<options_description> options);
void info(const std::string& msg, MessagePredicate predicate = std::function<bool(void)>([]() {return true;}));
void debug(const std::string& msg, MessagePredicate predicate = std::function<bool(void)>([]() {return true;}));
void error(const std::string& msg, MessagePredicate predicate = std::function<bool(void)>([]() {return true;}));
/**
* Return true if the option exists.
*
......
......@@ -47,7 +47,7 @@ void Program::build() {
// Make sure we got a valid
if (!compiler) {
XACCError("Invalid Compiler.\n");
XACCLogger::instance()->error("Invalid Compiler.\n");
}
// Before compiling, run preprocessors
......@@ -61,14 +61,14 @@ void Program::build() {
src = preprocessor->process(src, compiler, accelerator);
}
XACCInfo("Executing " + compiler->getName() + " compiler.");
XACCLogger::instance()->info("Executing " + compiler->getName() + " compiler.");
// Execute the compilation
xaccIR = compiler->compile(src, accelerator);
// Validate the compilation
if (!xaccIR) {
XACCError("Bad source string or something.\n");
XACCLogger::instance()->error("Bad source string or something.\n");
}
// Execute hardware dependent IR Transformations
......
......@@ -55,7 +55,9 @@ public:
("persist-ir",value<std::string>(), "Persist generated IR to provided file name.")
("load", value<std::string>(), "Load a XACC plugin at the given path")
("list-compilers", "List all available XACC Compilers")
("list-accelerators", "List all available XACC Accelerators");
("list-accelerators", "List all available XACC Accelerators")
("no-color", "Turn off colored logger output (blue for INFO, red for ERROR, etc.).")
("use-cout", "Use std::cout for logs instead of SPDLOG Logger.");
}
void addOptionsDescription(std::shared_ptr<options_description> options) {
......@@ -80,7 +82,7 @@ public:
store(parse_command_line(argc, argv, *xaccOptions.get()), clArgs);
if (clArgs.count("help")) {
std::cout << *xaccOptions.get() << "\n";
XACCInfo(
XACCLogger::instance()->info(
"\n[xacc] XACC Finalizing\n[xacc::compiler] Cleaning up Compiler Registry."
"\n[xacc::accelerator] Cleaning up Accelerator Registry.");
exit(0);
......@@ -96,24 +98,24 @@ public:
bool listTypes = false;
if (clArgs.count("list-compilers")) {
auto ids = ServiceRegistry::instance()->getRegisteredIds<Compiler>();
XACCInfo("Available XACC Compilers:");
XACCLogger::instance()->info("Available XACC Compilers:");
for (auto i : ids) {
XACCInfo("\t" + i);
XACCLogger::instance()->info("\t" + i);
}
listTypes = true;
}
if (clArgs.count("list-accelerators")) {
auto ids = ServiceRegistry::instance()->getRegisteredIds<Accelerator>();
XACCInfo("Available XACC Accelerators:");
XACCLogger::instance()->info("Available XACC Accelerators:");
for (auto i : ids) {
XACCInfo("\t" + i);
XACCLogger::instance()->info("\t" + i);
}
listTypes = true;
}
if (listTypes) {
XACCInfo(
XACCLogger::instance()->info(
"\n[xacc] XACC Finalizing\n[xacc::compiler] Cleaning up Compiler Registry."
"\n[xacc::accelerator] Cleaning up Accelerator Registry.");
exit(0);
......@@ -121,7 +123,7 @@ public:
auto exitRequested = ServiceRegistry::instance()->handleOptions(clArgs);
if (exitRequested) {
XACCInfo(
XACCLogger::instance()->info(
"\n[xacc] XACC Finalizing\n[xacc::compiler] Cleaning up Compiler Registry."
"\n[xacc::accelerator] Cleaning up Accelerator Registry.");
exit(0);
......
......@@ -216,7 +216,7 @@ public:
auto edgeBoolPair = add_edge(vertex(srcIndex, *_graph.get()),
vertex(tgtIndex, *_graph.get()), *_graph.get());
if (!edgeBoolPair.second) {
XACCError("Failed to add an edge between " + std::to_string(srcIndex) + " and " + std::to_string(tgtIndex));
XACCLogger::instance()->error("Failed to add an edge between " + std::to_string(srcIndex) + " and " + std::to_string(tgtIndex));
}
boost::put(boost::edge_weight_t(), *_graph.get(), edgeBoolPair.first, edgeWeight);
......@@ -487,7 +487,7 @@ public:
*/
virtual void read(std::istream& stream) {
stream.fail();
XACCError("Reading must be implemented by subclasses.");
XACCLogger::instance()->error("Reading must be implemented by subclasses.");
}
void computeShortestPath(int startIndex,
......
......@@ -70,12 +70,12 @@ protected:
// GetBundleContext() later.
framework.Init();
} catch (const std::exception& e) {
XACCError(std::string(e.what()) + " - Could not initialize XACC Framework");
XACCLogger::instance()->error(std::string(e.what()) + " - Could not initialize XACC Framework");
}
context = framework.GetBundleContext();
if (!context) {
XACCError("Invalid XACC Framework plugin context.");
XACCLogger::instance()->error("Invalid XACC Framework plugin context.");
}
boost::filesystem::path xaccPluginPath(
......@@ -93,7 +93,7 @@ protected:
}
}
} else {
XACCInfo("There are no plugins. Install plugins to begin working with XACC.");
XACCLogger::instance()->info("There are no plugins. Install plugins to begin working with XACC.");
}
boost::filesystem::path xaccLibPath(
......@@ -127,7 +127,7 @@ protected:
}
} catch (const std::exception& e) {
XACCError("Failure to start XACC plugins. " + std::string(e.what()));
XACCLogger::instance()->error("Failure to start XACC plugins. " + std::string(e.what()));
}
}
......@@ -170,7 +170,7 @@ public:
}
}
}
XACCError("Could not find service with name " + name + ". "
XACCLogger::instance()->error("Could not find service with name " + name + ". "
"Perhaps the service is not Identifiable.");
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment