<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <mpi.h>
#include <math.h>


///////////////////////////////////////////////////////
//// program_abort() and print_usage() functions //////
///////////////////////////////////////////////////////

static void program_abort(char *exec_name, char *message);
static void print_usage();

// Abort, printing the usage information only if the
// first argument is non-NULL (and set to argv[0]), and
// printing the second argument regardless.
static void program_abort(char *exec_name, char *message) {
  int my_rank;
  MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
  if (my_rank == 0) {
    if (message) {
      fprintf(stderr,"%s",message);
    }
    if (exec_name) {
      print_usage(exec_name);
    }
  }
  MPI_Abort(MPI_COMM_WORLD, 1);
  exit(1);
}

// Print the Usage information
static void print_usage(char *exec_name) {
  int my_rank;
  MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);

  if (my_rank == 0) {
    fprintf(stderr,"Usage: mpirun --cfg=smpi/bcast:mpich --cfg=smpi/running_power:1Gf -np <num processes>\n");
    fprintf(stderr,"              -platform <XML platform file> -hostfile <host file> %s \n",exec_name);
    fprintf(stderr,"MPIRUN arguments:\n");
    fprintf(stderr,"\t<XML platform file>: a simgrid platform description file\n");
    fprintf(stderr,"\t<host file>: MPI host file with host names from the XML platform file\n");
    fprintf(stderr,"\n");
  }
}
//////////////////////////////////////////////
////// Matrix and local-global routines //////
//////////////////////////////////////////////

void print_matrix(double *A, int num_cols, int num_rows) {
  int i,j;

  for (i=0; i < num_cols; i++) {
    printf("  ");
    for (j=0; j < num_rows; j++) {
      printf("%2.0lf ",A[i*num_rows+j]);
    }
    printf("\n");
  }
  printf("-------------------------------\n");
  return;
}

int owner_global_column(int col_num, int procs, int N) {
	return col_num * procs / N;
}

int local_to_global_column(int local_col, int rank, int np, int N) {
	int global_col;
	global_col = local_col + rank * N / np;
	return global_col;
}

int global_to_local_column(int global_col, int np, int N) {
	int local_col;
	local_col = global_col % (N / np);
	return local_col;
}

double calculate_cell_value(int i, int j) {
	double sum;
	int k;
        int min_i_j = (i < j ? i : j);
	for (k=0; k<=min_i_j; k++) {
		sum += (k + 1) * (k + 1);
	}
	return (double)sum;
}



///////////////////////////
////// Main function //////
///////////////////////////

int main(int argc, char *argv[])
{
  // Get N from command line arg
  if (argc != 4) {
    fprintf(stderr,"Missing an arg, or too many args.\nUsage: %s <N>\n",argv[0]);
    exit(1);
  }
  int N;
  N = atoi(argv[3]); 

  MPI_Init(&argc, &argv);

  // Determine rank and number of processes
  int num_procs;
  int rank;
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

  // Abort if num_procs doesn't divide N
  int columns_per_processor;
  columns_per_processor = N / num_procs;
  if (columns_per_processor * num_procs != N) {
	  program_abort(argv[0], "Number of processors does not divide N, I give up...\n");
  }
 
  // Declare matrices
  double* A = (double*)SMPI_SHARED_MALLOC(sizeof(double)*columns_per_processor*N);
  double* buffer = (double*)SMPI_SHARED_MALLOC(sizeof(double)*N);   // holds one column

  /*
  // Populate this processor's chunk of the matrix
  int i, j;
  int global_col;
  double value;
  for (i=0; i<N; i++) {
	  for (j=0; j<columns_per_processor; j++) {
		  global_col = j + rank * N / num_procs;
		  A[i*columns_per_processor + j] = calculate_cell_value(i, global_col);
	  }
  }
  */

  // MAIN ALGORITHM
  int k, local_j; 

  // Start the timer
  double start_time, idle_start_time, idle_time;
  MPI_Barrier(MPI_COMM_WORLD);
  if (rank == 0) {  
    start_time = MPI_Wtime();
  }

  // Simulate computation
#define FLOP_CALIBRATION_FACTOR 0.03  // TODO
  double flops;
  flops = N*N*N; // TODO how many flops 4 rill?

  SMPI_SAMPLE_FLOPS(flops) {

  /*
  for (k=0; k < N-1; k++) {
	  int k_owner, local_k;
	  k_owner = owner_global_column(k, num_procs, N);
	  local_k = global_to_local_column(k, num_procs, N);
	  // update k-th column if I have it
	  if (k_owner == rank) {
		  for (i=k+1; i<N; i++) {<commit_msg>did my best to estimate flops. okay maybe not my best<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <mpi.h>
#include <math.h>


///////////////////////////////////////////////////////
//// program_abort() and print_usage() functions //////
///////////////////////////////////////////////////////

static void program_abort(char *exec_name, char *message);
static void print_usage();

// Abort, printing the usage information only if the
// first argument is non-NULL (and set to argv[0]), and
// printing the second argument regardless.
static void program_abort(char *exec_name, char *message) {
  int my_rank;
  MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
  if (my_rank == 0) {
    if (message) {
      fprintf(stderr,"%s",message);
    }
    if (exec_name) {
      print_usage(exec_name);
    }
  }
  MPI_Abort(MPI_COMM_WORLD, 1);
  exit(1);
}

// Print the Usage information
static void print_usage(char *exec_name) {
  int my_rank;
  MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);

  if (my_rank == 0) {
    fprintf(stderr,"Usage: mpirun --cfg=smpi/bcast:mpich --cfg=smpi/running_power:1Gf -np <num processes>\n");
    fprintf(stderr,"              -platform <XML platform file> -hostfile <host file> %s \n",exec_name);
    fprintf(stderr,"MPIRUN arguments:\n");
    fprintf(stderr,"\t<XML platform file>: a simgrid platform description file\n");
    fprintf(stderr,"\t<host file>: MPI host file with host names from the XML platform file\n");
    fprintf(stderr,"\n");
  }
}
//////////////////////////////////////////////
////// Matrix and local-global routines //////
//////////////////////////////////////////////

void print_matrix(double *A, int num_cols, int num_rows) {
  int i,j;

  for (i=0; i < num_cols; i++) {
    printf("  ");
    for (j=0; j < num_rows; j++) {
      printf("%2.0lf ",A[i*num_rows+j]);
    }
    printf("\n");
  }
  printf("-------------------------------\n");
  return;
}

int owner_global_column(int col_num, int procs, int N) {
	return col_num * procs / N;
}

int local_to_global_column(int local_col, int rank, int np, int N) {
	int global_col;
	global_col = local_col + rank * N / np;
	return global_col;
}

int global_to_local_column(int global_col, int np, int N) {
	int local_col;
	local_col = global_col % (N / np);
	return local_col;
}

double calculate_cell_value(int i, int j) {
	double sum;
	int k;
        int min_i_j = (i < j ? i : j);
	for (k=0; k<=min_i_j; k++) {
		sum += (k + 1) * (k + 1);
	}
	return (double)sum;
}



///////////////////////////
////// Main function //////
///////////////////////////

int main(int argc, char *argv[])
{
  // Get N from command line arg
  if (argc != 4) {
    fprintf(stderr,"Missing an arg, or too many args.\nUsage: %s <N>\n",argv[0]);
    exit(1);
  }
  int N;
  N = atoi(argv[3]); 

  MPI_Init(&argc, &argv);

  // Determine rank and number of processes
  int num_procs;
  int rank;
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

  // Abort if num_procs doesn't divide N
  int columns_per_processor;
  columns_per_processor = N / num_procs;
  if (columns_per_processor * num_procs != N) {
	  program_abort(argv[0], "Number of processors does not divide N, I give up...\n");
  }
 
  // Declare matrices
  double* A = (double*)SMPI_SHARED_MALLOC(sizeof(double)*columns_per_processor*N);
  double* buffer = (double*)SMPI_SHARED_MALLOC(sizeof(double)*N);   // holds one column

  /*
  // Populate this processor's chunk of the matrix
  int i, j;
  int global_col;
  double value;
  for (i=0; i<N; i++) {
	  for (j=0; j<columns_per_processor; j++) {
		  global_col = j + rank * N / num_procs;
		  A[i*columns_per_processor + j] = calculate_cell_value(i, global_col);
	  }
  }
  */

  // MAIN ALGORITHM
  int k, local_j; 

  // Start the timer
  double start_time, idle_start_time, idle_time;
  MPI_Barrier(MPI_COMM_WORLD);
  if (rank == 0) {  
    start_time = MPI_Wtime();
  }

  // Simulate computation
#define FLOP_CALIBRATION_FACTOR 0.03  // TODO
  double flops;
  flops = N*columns_per_processor*2;  // not super confident in this formula. 
  				      // doesn't consider update k-th column.

  SMPI_SAMPLE_FLOPS(flops) {

  /*
  for (k=0; k < N-1; k++) {
	  int k_owner, local_k;
	  k_owner = owner_global_column(k, num_procs, N);
	  local_k = global_to_local_column(k, num_procs, N);
	  // update k-th column if I have it
	  if (k_owner == rank) {
		  for (i=k+1; i<N; i++) {
{'fraction_non_alphanumeric': 0.2035448827901658, 'fraction_numerical': 0.006403659233847913, 'mean_word_length': 2.542324827865533, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 25, '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': '6695350', 'n_tokens_mistral': 3262, 'n_tokens_neox': 3155, 'n_words': 813}