Skip to content

Commit

Permalink
custom print
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelyr committed Nov 30, 2024
1 parent 83d7c6b commit 1f2aad1
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ option(PCMS_ENABLE_XGC "enable xgc field adapter" ON)
option(PCMS_ENABLE_OMEGA_H "enable Omega_h field adapter" OFF)
option(PCMS_ENABLE_C "Enable pcms C api" ON)

option(PCMS_PRINT_ENABLED "PUMIPIC print statements enabled" ON)
option(PCMS_PRINT_ENABLED "PCMS print statements enabled" ON)
find_package(spdlog QUIET)

# find package before fortran enabled, so we don't require the adios2 fortran interfaces
Expand Down
13 changes: 12 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ set(PCMS_HEADERS
set(PCMS_SOURCES
pcms.cpp
pcms/assert.cpp
pcms/print.cpp
pcms/xgc_field_adapter.h)

configure_file(
pcms/version.h.in
pcms/version.h
)

set(PCMS_SOURCES pcms.cpp pcms/assert.cpp)
if(PCMS_ENABLE_XGC)
list(APPEND PCMS_SOURCES pcms/xgc_reverse_classification.cpp)
list(APPEND PCMS_HEADERS pcms/xgc_reverse_classification.h)
Expand Down Expand Up @@ -75,6 +75,17 @@ if(PCMS_HAS_ASAN)
target_compile_options(pcms_core PRIVATE -fsanitize=address -fno-omit-frame-pointer)
endif()

if(PCMS_PRINT_ENABLED)
add_definitions(-DPCMS_PRINT_ENABLED)
target_compile_definitions(pcms_core INTERFACE -DPCMS_PRINT_ENABLED)
endif()

if (spdlog_FOUND)
add_definitions(-DPCMS_SPDLOG_ENABLED)
target_compile_definitions(pcms_core INTERFACE -DPCMS_SPDLOG_ENABLED)
target_link_libraries(pcms_core PUBLIC spdlog::spdlog)
endif()


## export the library
set_target_properties(pcms_core PROPERTIES
Expand Down
2 changes: 1 addition & 1 deletion src/pcms/assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <cstdlib>
namespace pcms {
void Pcms_Assert_Fail(const char* msg) {
pPrintError("%s", msg);
printError("%s", msg);
abort();
}
}
2 changes: 1 addition & 1 deletion src/pcms/capi/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ PcmsFieldAdapterHandle pcms_create_xgc_field_adapter(
name, comm, data, size, *reverse_classification, in_overlap, *field_adapter);
break;
default:
pcms::pPrintError("tyring to create XGC adapter with invalid type! %d", data_type);
pcms::printError("tyring to create XGC adapter with invalid type!\n");
std::abort();
}
return {reinterpret_cast<void*>(field_adapter)};
Expand Down
20 changes: 20 additions & 0 deletions src/pcms/print.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "print.h"

namespace pcms {

FILE* pp_stdout = stdout;
FILE* pp_stderr = stderr;

FILE* getStdout() { return pp_stdout; }
FILE* getStderr() { return pp_stderr; }

void setStdout(FILE* out) {
assert(out != NULL);
pp_stdout = out;
}

void setStderr(FILE* err) {
assert(err != NULL);
pp_stderr = err;
}
}
29 changes: 19 additions & 10 deletions src/pcms/print.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#ifndef PCMS_PRINT_H
#define PCMS_PRINT_H

#ifdef SPDLOG_ENABLED
#ifdef PCMS_SPDLOG_ENABLED
#include "spdlog/spdlog.h"
#include <spdlog/fmt/bundled/printf.h>
#endif
Expand All @@ -13,23 +14,31 @@ namespace pcms {
#define ACTIVE_GPU_EXECUTION
#endif

FILE* getStdout();
FILE* getStderr();

void setStdout(FILE* out);
void setStderr(FILE* err);

template<typename... Args>
void pPrintError(const char* fmt, const Args&... args) {
#if defined(SPDLOG_ENABLED) && defined(PCMS_PRINT_ENABLED)
void printError(const char* fmt, const Args&... args) {
#if defined(PCMS_SPDLOG_ENABLED) && defined(PCMS_PRINT_ENABLED)
spdlog::error("{}", fmt::sprintf(fmt, args...));
#elif defined(PCMS_PRINT_ENABLED)
fprintf(stderr, fmt, args...);
fprintf(getStdout(), fmt, args...);
#endif
}

template<typename... Args>
KOKKOS_INLINE_FUNCTION
void pPrintInfo(const char* fmt, const Args&... args) {
#if defined(SPDLOG_ENABLED) && defined(PCMS_PRINT_ENABLED) && !defined(ACTIVE_GPU_EXECUTION)
void printInfo(const char* fmt, const Args&... args) {
#if defined(PCMS_SPDLOG_ENABLED) && defined(PCMS_PRINT_ENABLED) && !defined(ACTIVE_GPU_EXECUTION)
spdlog::info("{}", fmt::sprintf(fmt, args...));
#elif defined(PCMS_PRINT_ENABLED)
Kokkos::printf(fmt, args...);
#elif defined(PCMS_PRINT_ENABLED) && !defined(ACTIVE_GPU_EXECUTION)
fprintf(getStdout(), fmt, args...);
#endif
}

}
}

#endif //PCMS_PRINT_H
2 changes: 1 addition & 1 deletion tools/CpnFromOsh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
int main(int argc, char** argv)
{
if (argc != 3) {
pcms::pPrintError("Usage: %s <mesh file> <nparts>\n", argv[0]);
pcms::printError("Usage: %s <mesh file> <nparts>\n", argv[0]);
std::abort();
}
Omega_h::Library lib(&argc, &argv);
Expand Down
2 changes: 1 addition & 1 deletion tools/XgcRCfromOsh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
int main(int argc, char** argv)
{
if ((argc != 2) && (argc != 3)) {
pcms::pPrintError("Usage: %s <mesh file> <numbering>\n", argv[0]);
pcms::printError("Usage: %s <mesh file> <numbering>\n", argv[0]);
std::abort();
}
Omega_h::Library lib(&argc, &argv);
Expand Down

0 comments on commit 1f2aad1

Please sign in to comment.