#ifndef INTERP_LB_PROFILE_H
#define INTERP_LB_PROFILE_H

#include <mpi.h>

namespace InterpLBProfile {

static const unsigned int MAGIC   = 0x494C4250; // "ILBP"
static const unsigned int VERSION = 1;

struct ProfileHeader {
    unsigned int magic;
    unsigned int version;
    int nprocs;
    int num_heavy;
    double threshold_ratio;
};

// Write profile file (rank 0 only)
bool write_profile(const char *filepath, int nprocs,
                   const double *rank_times,
                   const int *heavy_ranks, int num_heavy,
                   double threshold_ratio);

// Read profile file (rank 0 reads, then broadcasts to all)
// Returns true if file found and valid for current nprocs
bool read_profile(const char *filepath, int current_nprocs,
                  int *heavy_ranks, int &num_heavy,
                  double *rank_times, MPI_Comm comm);

// Identify heavy ranks: those with time > threshold_ratio * mean
int identify_heavy_ranks(const double *rank_times, int nprocs,
                         double threshold_ratio,
                         int *heavy_ranks, int max_heavy);

} // namespace InterpLBProfile

#endif /* INTERP_LB_PROFILE_H */
