<commit_before>#include <engpar_support.h>
#include <apfGraph.h>
#include <apfMesh2.h>
#include <apfNumbering.h>
#include <apfMDS.h>
#include <gmi_mesh.h>
#include <apf.h>
#include <engpar.h>
#include <engpar_input.h>
#include <binGraph.h>
#include <parma.h>

int main(int argc, char* argv[]) {
  MPI_Init(&argc,&argv);
  EnGPar_Initialize();
  EnGPar_Open_Log();
  
  if (argc != 5 && argc != 6) {
    if ( !PCU_Comm_Self() ) {
      printf("Usage: %s <model> <mesh> <tolerance> <render=[1:on|0:off]> [multi-edgetypes]\n", argv[0]);
    }
    EnGPar_Finalize();
    assert(false);
  }

  int isMultiEdge = 0;
  if( argc == 6 ) isMultiEdge = 1;

  apf::Mesh2* m=NULL;
  agi::Ngraph* g=NULL;
  //Load mesh
  gmi_register_mesh();
  m = apf::loadMdsMesh(argv[1],argv[2]);

  double tol = atof(argv[3]);
  const int render = atoi(argv[4]);
  if (!PCU_Comm_Self())
    printf("render vtk %s\n", render == 0 ? "off" : "on" );

  Parma_PrintPtnStats(m,"before");
  if(render)
    apf::writeVtkFiles("pre", m);

  double times[10];
  times[0] = PCU_Time();
  times[4] = PCU_Time();
  if (isMultiEdge) {
    int edges[2] = {0,2};
    //balance vtx>edge>elm
    g = agi::createAPFGraph(m,3,edges,2);
  }
  else {
    //balance vtx>elm
    g = agi::createAPFGraph(m,3,0);
  }
  times[0] = PCU_Time()-times[0];
  times[1] = PCU_Time();
  
  engpar::Input* input = new engpar::Input(g);
  input->priorities.push_back(0);
  input->tolerances.push_back(tol);
  if (isMultiEdge) {
    input->priorities.push_back(1);
    input->tolerances.push_back(tol);
  }
  input->priorities.push_back(-1);
  input->tolerances.push_back(tol);

  input->step_factor=.1;

  input->useDistanceQueue=true;
  //Create the balancer
  agi::Balancer* balancer = engpar::makeBalancer(input);
  balancer->balance(tol);

  engpar::evaluatePartition(g);
  //Destroy balancer
  delete balancer;
  times[1] = PCU_Time() - times[1];

  //Ensure the graph is still valid
  agi::checkValidity(g);

  times[2] = PCU_Time();

  //Migration of original data structure
  //Only implemented for mesh
  agi::PartitionMap* map = g->getPartition();

  //map can be used to migrate the original structure
  apf::Migration* plan = new apf::Migration(m);
  apf::GlobalNumbering* gids = m->findGlobalNumbering("primary_ids_global");
  apf::MeshIterator* mitr = m->begin(3);
  apf::MeshEntity* ent;
  while ((ent = m->iterate(mitr))) {
    agi::gid_t gid = apf::getNumber(gids,ent,0);
    assert(map->find(gid)!=map->end());<commit_msg>balanceMesh: print the per-iteration imbalance<commit_after>#include <engpar_support.h>
#include <apfGraph.h>
#include <apfMesh2.h>
#include <apfNumbering.h>
#include <apfMDS.h>
#include <gmi_mesh.h>
#include <apf.h>
#include <engpar.h>
#include <engpar_input.h>
#include <binGraph.h>
#include <parma.h>

int main(int argc, char* argv[]) {
  MPI_Init(&argc,&argv);
  EnGPar_Initialize();
  EnGPar_Open_Log();
  
  if (argc != 5 && argc != 6) {
    if ( !PCU_Comm_Self() ) {
      printf("Usage: %s <model> <mesh> <tolerance> <render=[1:on|0:off]> [multi-edgetypes]\n", argv[0]);
    }
    EnGPar_Finalize();
    assert(false);
  }

  int isMultiEdge = 0;
  if( argc == 6 ) isMultiEdge = 1;

  apf::Mesh2* m=NULL;
  agi::Ngraph* g=NULL;
  //Load mesh
  gmi_register_mesh();
  m = apf::loadMdsMesh(argv[1],argv[2]);

  double tol = atof(argv[3]);
  const int render = atoi(argv[4]);
  if (!PCU_Comm_Self())
    printf("render vtk %s\n", render == 0 ? "off" : "on" );

  Parma_PrintPtnStats(m,"before");
  if(render)
    apf::writeVtkFiles("pre", m);

  double times[10];
  times[0] = PCU_Time();
  times[4] = PCU_Time();
  if (isMultiEdge) {
    int edges[2] = {0,2};
    //balance vtx>edge>elm
    g = agi::createAPFGraph(m,3,edges,2);
  }
  else {
    //balance vtx>elm
    g = agi::createAPFGraph(m,3,0);
  }
  times[0] = PCU_Time()-times[0];
  times[1] = PCU_Time();
  
  engpar::Input* input = new engpar::Input(g);
  input->priorities.push_back(0);
  input->tolerances.push_back(tol);
  if (isMultiEdge) {
    input->priorities.push_back(1);
    input->tolerances.push_back(tol);
  }
  input->priorities.push_back(-1);
  input->tolerances.push_back(tol);

  input->step_factor=.1;

  input->useDistanceQueue=true;
  //Create the balancer
  int verbosity = 1;
  agi::Balancer* balancer = engpar::makeBalancer(input, verbosity);
  balancer->balance(tol);

  engpar::evaluatePartition(g);
  //Destroy balancer
  delete balancer;
  times[1] = PCU_Time() - times[1];

  //Ensure the graph is still valid
  agi::checkValidity(g);

  times[2] = PCU_Time();

  //Migration of original data structure
  //Only implemented for mesh
  agi::PartitionMap* map = g->getPartition();

  //map can be used to migrate the original structure
  apf::Migration* plan = new apf::Migration(m);
  apf::GlobalNumbering* gids = m->findGlobalNumbering("primary_ids_global");
  apf::MeshIterator* mitr = m->begin(3);
  apf::MeshEntity* ent;
  while ((ent = m->iterate(mitr))) {
    agi::gid_t gid = apf::getNumber(gids,ent,0);
    assert(map->find(gid)!=map->end());
{'fraction_non_alphanumeric': 0.18151026100816897, 'fraction_numerical': 0.015341701534170154, 'mean_word_length': 4.148717948717949, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1509054', 'n_tokens_mistral': 2045, 'n_tokens_neox': 1988, 'n_words': 379}