Skip to content

Commit

Permalink
Move printf and fprintf mpi wrappers to log module
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Danish committed Oct 15, 2020
1 parent e2ec5fc commit 47c2550
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 58 deletions.
54 changes: 54 additions & 0 deletions log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

#include <cstdarg>

struct SINGLE_FILE {
FILE *fp;
};

static bool started = 0;
static long start_time;
static int rank;
Expand Down Expand Up @@ -64,3 +68,53 @@ void LOG(const char *fmt, ...) {
printf("%s", buf);
}

SINGLE_FILE *single_fopen(const char *path, const char *mode) {
#ifdef BUILDMPI
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank != 0)
return nullptr;
#endif
FILE *fp = fopen(path, mode);
return new SINGLE_FILE{fp};
}

int fclose(SINGLE_FILE *stream) {
#ifdef BUILDMPI
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank != 0)
return 0;
#endif
int ret = fclose(stream->fp);
delete stream;
return ret;
}

int fprintf(SINGLE_FILE *stream, const char *format, ...) {
#ifdef BUILDMPI
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank != 0)
return 0;
#endif
va_list ap;
va_start(ap, format);
int ret = vfprintf(stream->fp, format, ap);
va_end(ap);
return ret;
}

int single_printf(const char *fmt, ...) {
#ifdef BUILDMPI
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank != 0)
return 0;
#endif
va_list ap;
va_start(ap, fmt);
int ret = printf(fmt, ap);
va_end(ap);
return ret;
}
8 changes: 8 additions & 0 deletions log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ class StopWatch
int init_logging();
void LOG(const char *fmt, ...);
#endif

struct SINGLE_FILE;
SINGLE_FILE *single_fopen(const char *path, const char *mode);
int fclose(SINGLE_FILE *stream);
int fprintf(SINGLE_FILE *stream, const char *format, ...);
int single_printf(const char *fmt, ...);


61 changes: 13 additions & 48 deletions setsm_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include <atomic>
#include <memory>
#include <cstdarg>

#include "setsm_code.hpp"
#include "log.hpp"
Expand Down Expand Up @@ -12090,57 +12089,23 @@ double FindNebPts_F_M_IDW(const float *input, const unsigned char *matching_flag
return result;
}


// Find the interpolated value of a patch given the nominal position and the X and Y offsets
// along with the image itself

SINGLE_FILE *single_fopen(const char *path, const char *mode) {
#ifdef BUILDMPI
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank != 0)
return nullptr;
#endif
FILE *fp = fopen(path, mode);
return new SINGLE_FILE{fp};
}

int fclose(SINGLE_FILE *stream) {
#ifdef BUILDMPI
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank != 0)
return 0;
#endif
int ret = fclose(stream->fp);
delete stream;
return ret;
}

int fprintf(SINGLE_FILE *stream, const char *format, ...) {
#ifdef BUILDMPI
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank != 0)
return 0;
#endif
va_list ap;
va_start(ap, format);
int ret = vfprintf(stream->fp, format, ap);
va_end(ap);
return ret;
}

int single_printf(const char *fmt, ...) {
#ifdef BUILDMPI
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank != 0)
return 0;
#endif
va_list ap;
va_start(ap, fmt);
int ret = printf(fmt, ap);
va_end(ap);
return ret;
}













10 changes: 0 additions & 10 deletions setsm_code.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,4 @@ class TileIndexer {
virtual ~TileIndexer() {};
};

struct SINGLE_FILE {
FILE *fp;
};

SINGLE_FILE *single_fopen(const char *path, const char *mode);
int fclose(SINGLE_FILE *stream);
int fprintf(SINGLE_FILE *stream, const char *format, ...);
int single_printf(const char *fmt, ...);


#endif // SETSM_CODE_H

0 comments on commit 47c2550

Please sign in to comment.