<commit_before>#include <string.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "mpi.h"
#include "utils.h"

void check_merr(int e) {
  switch(e){
    case 22: // EINVAL
      printf("alignment error\n");
    break;

    case 12: //ENOMEM
      printf("no sufficient memory\n");
    break;
  }

}

// set the default values of the kernel parameters
void param_default(Parameters *p) {
  p->stencil_shape[0] = 256;
  p->stencil_shape[1] = 64;
  p->stencil_shape[2] = 64;

  p->alignment = 16;
  p->target_ts = 0; //Naive TS
  p->target_kernel = 0; //Basic ISO stencil kernel
  NHALO = KernelList[p->target_kernel].stencil_radius;
  p->n_tests = 3;
  p->nt = 100;
  p->verify = 0;
  p->source=NULL;
  p->verbose = 1;
  p->debug = 0;
  p->source_point_enabled = 0;
  p->array_padding = 1;

  // diamond method
  p->t_dim = -1;
//  p->nb_diamond_chunk=1;
  p->halo_concat = 1;

  p->cache_size = 8192;

  // get the number of available OpenMP threads
  p->num_threads = 1;
#if defined(_OPENMP)
  p->num_threads = omp_get_max_threads();
#endif
  // default thread group size for methods using single thread group
  p->stencil_ctx.thread_group_size = p->num_threads;
  p->wavefront = 1; // default to using wavefront in the tile

  p->stencil_ctx.num_wf = 1; // set 1 wavefront per wavefront update

  p->stencil_ctx.enable_likwid_m = 1;

  p->stencil_ctx.bs_x = 1e6;

  // Topology parameters
  p->t.is_periodic[0]=0;
  p->t.is_periodic[1]=0;
  p->t.is_periodic[2]=0;
  p->t.shape[0] = 1;
  p->t.shape[1] = 1;
  p->t.shape[2] = 1;

  p->h[0].is_contiguous = 1;
  p->h[1].is_contiguous = 1;
  p->h[2].is_contiguous = 1;

  p->h[0].size = 0;
  p->h[1].size = 0;
  p->h[2].size = 0;

  // Initialize the default stencil coefficients values
  FLOAT_PRECISION coef[] = {-0.28472, 0.16000, -0.02000, 0.00254,
      -0.00018, -0.18472, 0.19, -0.0500, 0.00554, -0.0009, 0.00354};
  int i;
  for(i=0; i<11; i++) p->g_coef[i] = (FLOAT_PRECISION) coef[i];

  // profiling information
  reset_timers(&(p->prof));
}
void reset_timers(Profile * p){
  p->compute = 0.;
  p->communicate = 0.;
  p->send_recv = 0.;
  p->wait = 0.;
  p->total = 0.;
  p->others = 0.;
  p->ts_main = 0.;
  p->ts_others = 0.;
}

void reset_wf_timers(Parameters * p){
  int i;
  int num_thread_groups = (int) ceil(1.0*p->num_threads/p->stencil_ctx.thread_group_size);

  // reset if the wavefront profiling is allocated
  if( (p->wavefront != 0) && (p->target_ts == 2) ) {

    for(i=0; i<p->num_threads; i++) p->stencil_ctx.t_wait[i] = 0.0;
    for(i=0; i<num_thread_groups; i++) p->stencil_ctx.t_wf_main[i] = 0.0;
    for(i=0; i<num_thread_groups; i++) p->stencil_ctx.t_wf_comm[i] = 0.0;
    for(i=0; i<num_thread_groups; i++) p->stencil_ctx.t_wf_prologue[i] = 0.0;
    for(i=0; i<num_thread_groups; i++) p->stencil_ctx.t_wf_epilogue[i] = 0.0;
    for(i=0; i<num_thread_groups; i++) p->stencil_ctx.wf_num_resolved_diamonds[i] = 0.0;
    for(i=0; i<num_thread_groups; i++) p->stencil_ctx.t_group_wait[i] = 0.0;
  }
}

void arrays_allocate(Parameters *p) {
  int male0, male1, male2;

  male1 = posix_memalign((void **)&(p->U1), p->alignment, sizeof(FLOAT_PRECISION)*p->ln_domain); check_merr(male1);
  male1 = posix_memalign((void **)&(p->U2), p->alignment, sizeof(FLOAT_PRECISION)*p->ln_domain); check_merr(male1);

  if(KernelList[p->target_kernel].time_order == 2)
    male1 = posix_memalign((void **)&(p->U3), p->alignment, sizeof(FLOAT_PRECISION)*p->ln_domain); check_merr(male1);

  if(p->source_point_enabled==1)
    male0 = posix_memalign((void **)&(p->source), p->alignment, sizeof(FLOAT_PRECISION)*p->nt); check_merr(male0);

  // allocate the size of the coefficients matrix according to the stencil type
  unsigned long coef_size;
  switch(KernelList[p->target_kernel].stencil_coeff){
  case CONSTANT_COEFFICIENT:
    coef_size = 10;
    break;

  case VARIABLE_COEFFICIENT:
    coef_size = p->ln_domain*(1 + KernelList[p->target_kernel].stencil_radius);
    break;

  case VARIABLE_COEFFICIENT_AXSYM:
    coef_size = p->ln_domain*(1 + 3*KernelList[p->target_kernel].stencil_radius);
    break;

  case VARIABLE_COEFFICIENT_NOSYM:
    coef_size = p->ln_domain*(1 + 6*KernelList[p->target_kernel].stencil_radius);
    break;

  default:
    printf("ERROR: unknown type of stencil\n");
    exit(1);
    break;
  }

  male2 = posix_memalign((void **)&(p->coef), p->alignment, sizeof(FLOAT_PRECISION)*coef_size); check_merr(male2);

  if (p->verbose == 1){
    if( (p->mpi_rank ==0)  || (p->mpi_rank == p->mpi_size-1))
    printf("[rank=%d] alloc. dom(err=%d):%fGiB coef(err=%d):%fGiB total:%fGiB\n", p->mpi_rank,
            male1, sizeof(FLOAT_PRECISION)*p->ln_domain*1.0/(1024*1024*1024),
            male2, sizeof(FLOAT_PRECISION)*coef_size*1.0/(1024*1024*1024),
            sizeof(FLOAT_PRECISION)*(coef_size+ 2*p->ln_domain)*1.0/(1024*1024*1024));
  }
}

void arrays_free(Parameters *p) {
  free(p->coef);
  free(p->U1);
  free(p->U2);

  if(KernelList[p->target_kernel].time_order == 2)
      free(p->U3);

  if(p->source_point_enabled==1)
    free(p->source);
}

void set_centered_source(Parameters *p) {
  p->source_pt[0] = (p->stencil_shape[0]+2*NHALO)/2 -1;
  p->source_pt[1] = (p->stencil_shape[1]+2*NHALO)/2 -1;
  p->source_pt[2] = (p->stencil_shape[2]+2*NHALO)/2 -1;
}

unsigned long get_mwf_size(Parameters *p, int t_dim){
  unsigned long diam_width, diam_height, wf_updates, wf_elements, lnx, t_order, total_points;
  lnx = p->ldomain_shape[0];
  t_order = KernelList[p->target_kernel].time_order;
  diam_width = (t_dim+1)*2*NHALO;
  int nwf = p->stencil_ctx.num_wf;
  diam_height = t_dim*2*NHALO + nwf;

  wf_updates = (t_dim+1)*(t_dim+1)*2 * NHALO; // Y-T projection
  wf_elements = (wf_updates - diam_width) * NHALO + diam_width + diam_width*(nwf-1);

  switch(KernelList[p->target_kernel].stencil_coeff){
  case CONSTANT_COEFFICIENT:
    total_points = ( (t_order+1)             *wf_elements + (diam_width + diam_height )*2*NHALO) * lnx * sizeof(FLOAT_PRECISION);
    break;

  case VARIABLE_COEFFICIENT:
    total_points = ( (t_order+1 + (1+NHALO) )*wf_elements + (diam_width + diam_height )*2*NHALO) * lnx * sizeof(FLOAT_PRECISION);
    break;

  case VARIABLE_COEFFICIENT_AXSYM:
    total_points = ( (t_order+1 + (1+3*NHALO) )*wf_elements + (diam_width + diam_height )*2*NHALO) * lnx * sizeof(FLOAT_PRECISION);
    break;
<commit_msg>incorporating the block size in x in the MWD cache block calculations<commit_after>#include <string.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "mpi.h"
#include "utils.h"

void check_merr(int e) {
  switch(e){
    case 22: // EINVAL
      printf("alignment error\n");
    break;

    case 12: //ENOMEM
      printf("no sufficient memory\n");
    break;
  }

}

// set the default values of the kernel parameters
void param_default(Parameters *p) {
  p->stencil_shape[0] = 256;
  p->stencil_shape[1] = 64;
  p->stencil_shape[2] = 64;

  p->alignment = 16;
  p->target_ts = 0; //Naive TS
  p->target_kernel = 0; //Basic ISO stencil kernel
  NHALO = KernelList[p->target_kernel].stencil_radius;
  p->n_tests = 3;
  p->nt = 100;
  p->verify = 0;
  p->source=NULL;
  p->verbose = 1;
  p->debug = 0;
  p->source_point_enabled = 0;
  p->array_padding = 1;

  // diamond method
  p->t_dim = -1;
//  p->nb_diamond_chunk=1;
  p->halo_concat = 1;

  p->cache_size = 8192;

  // get the number of available OpenMP threads
  p->num_threads = 1;
#if defined(_OPENMP)
  p->num_threads = omp_get_max_threads();
#endif
  // default thread group size for methods using single thread group
  p->stencil_ctx.thread_group_size = p->num_threads;
  p->wavefront = 1; // default to using wavefront in the tile

  p->stencil_ctx.num_wf = 1; // set 1 wavefront per wavefront update

  p->stencil_ctx.enable_likwid_m = 1;

  p->stencil_ctx.bs_x = 1e6;

  // Topology parameters
  p->t.is_periodic[0]=0;
  p->t.is_periodic[1]=0;
  p->t.is_periodic[2]=0;
  p->t.shape[0] = 1;
  p->t.shape[1] = 1;
  p->t.shape[2] = 1;

  p->h[0].is_contiguous = 1;
  p->h[1].is_contiguous = 1;
  p->h[2].is_contiguous = 1;

  p->h[0].size = 0;
  p->h[1].size = 0;
  p->h[2].size = 0;

  // Initialize the default stencil coefficients values
  FLOAT_PRECISION coef[] = {-0.28472, 0.16000, -0.02000, 0.00254,
      -0.00018, -0.18472, 0.19, -0.0500, 0.00554, -0.0009, 0.00354};
  int i;
  for(i=0; i<11; i++) p->g_coef[i] = (FLOAT_PRECISION) coef[i];

  // profiling information
  reset_timers(&(p->prof));
}
void reset_timers(Profile * p){
  p->compute = 0.;
  p->communicate = 0.;
  p->send_recv = 0.;
  p->wait = 0.;
  p->total = 0.;
  p->others = 0.;
  p->ts_main = 0.;
  p->ts_others = 0.;
}

void reset_wf_timers(Parameters * p){
  int i;
  int num_thread_groups = (int) ceil(1.0*p->num_threads/p->stencil_ctx.thread_group_size);

  // reset if the wavefront profiling is allocated
  if( (p->wavefront != 0) && (p->target_ts == 2) ) {

    for(i=0; i<p->num_threads; i++) p->stencil_ctx.t_wait[i] = 0.0;
    for(i=0; i<num_thread_groups; i++) p->stencil_ctx.t_wf_main[i] = 0.0;
    for(i=0; i<num_thread_groups; i++) p->stencil_ctx.t_wf_comm[i] = 0.0;
    for(i=0; i<num_thread_groups; i++) p->stencil_ctx.t_wf_prologue[i] = 0.0;
    for(i=0; i<num_thread_groups; i++) p->stencil_ctx.t_wf_epilogue[i] = 0.0;
    for(i=0; i<num_thread_groups; i++) p->stencil_ctx.wf_num_resolved_diamonds[i] = 0.0;
    for(i=0; i<num_thread_groups; i++) p->stencil_ctx.t_group_wait[i] = 0.0;
  }
}

void arrays_allocate(Parameters *p) {
  int male0, male1, male2;

  male1 = posix_memalign((void **)&(p->U1), p->alignment, sizeof(FLOAT_PRECISION)*p->ln_domain); check_merr(male1);
  male1 = posix_memalign((void **)&(p->U2), p->alignment, sizeof(FLOAT_PRECISION)*p->ln_domain); check_merr(male1);

  if(KernelList[p->target_kernel].time_order == 2)
    male1 = posix_memalign((void **)&(p->U3), p->alignment, sizeof(FLOAT_PRECISION)*p->ln_domain); check_merr(male1);

  if(p->source_point_enabled==1)
    male0 = posix_memalign((void **)&(p->source), p->alignment, sizeof(FLOAT_PRECISION)*p->nt); check_merr(male0);

  // allocate the size of the coefficients matrix according to the stencil type
  unsigned long coef_size;
  switch(KernelList[p->target_kernel].stencil_coeff){
  case CONSTANT_COEFFICIENT:
    coef_size = 10;
    break;

  case VARIABLE_COEFFICIENT:
    coef_size = p->ln_domain*(1 + KernelList[p->target_kernel].stencil_radius);
    break;

  case VARIABLE_COEFFICIENT_AXSYM:
    coef_size = p->ln_domain*(1 + 3*KernelList[p->target_kernel].stencil_radius);
    break;

  case VARIABLE_COEFFICIENT_NOSYM:
    coef_size = p->ln_domain*(1 + 6*KernelList[p->target_kernel].stencil_radius);
    break;

  default:
    printf("ERROR: unknown type of stencil\n");
    exit(1);
    break;
  }

  male2 = posix_memalign((void **)&(p->coef), p->alignment, sizeof(FLOAT_PRECISION)*coef_size); check_merr(male2);

  if (p->verbose == 1){
    if( (p->mpi_rank ==0)  || (p->mpi_rank == p->mpi_size-1))
    printf("[rank=%d] alloc. dom(err=%d):%fGiB coef(err=%d):%fGiB total:%fGiB\n", p->mpi_rank,
            male1, sizeof(FLOAT_PRECISION)*p->ln_domain*1.0/(1024*1024*1024),
            male2, sizeof(FLOAT_PRECISION)*coef_size*1.0/(1024*1024*1024),
            sizeof(FLOAT_PRECISION)*(coef_size+ 2*p->ln_domain)*1.0/(1024*1024*1024));
  }
}

void arrays_free(Parameters *p) {
  free(p->coef);
  free(p->U1);
  free(p->U2);

  if(KernelList[p->target_kernel].time_order == 2)
      free(p->U3);

  if(p->source_point_enabled==1)
    free(p->source);
}

void set_centered_source(Parameters *p) {
  p->source_pt[0] = (p->stencil_shape[0]+2*NHALO)/2 -1;
  p->source_pt[1] = (p->stencil_shape[1]+2*NHALO)/2 -1;
  p->source_pt[2] = (p->stencil_shape[2]+2*NHALO)/2 -1;
}

unsigned long get_mwf_size(Parameters *p, int t_dim){
  unsigned long diam_width, diam_height, wf_updates, wf_elements, lnx, t_order, total_points;
  lnx = (p->stencil_ctx.bs_x < p->ldomain_shape[0] ? p->stencil_ctx.bs_x : p->ldomain_shape[0]);
  t_order = KernelList[p->target_kernel].time_order;
  diam_width = (t_dim+1)*2*NHALO;
  int nwf = p->stencil_ctx.num_wf;
  diam_height = t_dim*2*NHALO + nwf;

  wf_updates = (t_dim+1)*(t_dim+1)*2 * NHALO; // Y-T projection
  wf_elements = (wf_updates - diam_width) * NHALO + diam_width + diam_width*(nwf-1);

  switch(KernelList[p->target_kernel].stencil_coeff){
  case CONSTANT_COEFFICIENT:
    total_points = ( (t_order+1)             *wf_elements + (diam_width + diam_height )*2*NHALO) * lnx * sizeof(FLOAT_PRECISION);
    break;

  case VARIABLE_COEFFICIENT:
    total_points = ( (t_order+1 + (1+NHALO) )*wf_elements + (diam_width + diam_height )*2*NHALO) * lnx * sizeof(FLOAT_PRECISION);
    break;

  case VARIABLE_COEFFICIENT_AXSYM:
    total_points = ( (t_order+1 + (1+3*NHALO) )*wf_elements + (diam_width + diam_height )*2*NHALO) * lnx * sizeof(FLOAT_PRECISION);
    break;

{'fraction_non_alphanumeric': 0.1753307392996109, 'fraction_numerical': 0.04256809338521401, 'mean_word_length': 3.944594074644094, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 232, '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': '7336681', 'n_tokens_mistral': 5826, 'n_tokens_neox': 5378, 'n_words': 944}