<commit_before>#include "network_mesh_analytical.h"
#include "network_mesh_analytical_params.h"
#include "message_types.h"
#include "chip.h"
#include <math.h>

using namespace std;

//NetworkMeshAnalytical::NetworkMeshAnalytical(int tid, int num_threads, Core *core)
NetworkMeshAnalytical::NetworkMeshAnalytical(Core *core, int num_threads)
    :
        Network(core, num_threads),
        bytes_sent(0),
        cycles_spent_proc(0),
        cycles_spent_latency(0),
        cycles_spent_contention(0),
        global_utilization(0.0),
        local_utilization_last_update(0),
        local_utilization_flits_sent(0),
        update_interval(0)
{
   registerCallback(MCP_UTILIZATION_UPDATE_TYPE,
                    receiveMCPUpdate,
                    this);

   update_interval = g_config->getAnalyticNetworkParms()->update_interval;
}

NetworkMeshAnalytical::~NetworkMeshAnalytical()
{
   unregisterCallback(MCP_UTILIZATION_UPDATE_TYPE);
}

int NetworkMeshAnalytical::netSend(NetPacket packet)
{
    bytes_sent += packet.length;
    updateUtilization();
    return Network::netSend(packet);
}

NetPacket NetworkMeshAnalytical::netRecv(NetMatch match)
{
    updateUtilization();
    return Network::netRecv(match);
}

UINT64 NetworkMeshAnalytical::netProcCost(NetPacket packet)
{
    UINT64 cost = 10;
    cycles_spent_proc += cost;
    return cost;
}

UINT64 NetworkMeshAnalytical::netLatency(NetPacket packet)
{
    // We model a unidirectional network with end-around connections
    // using the network model in "Limits on Interconnect Performance"
    // (by Anant). Currently, this ignores communication locality and
    // sets static values for all parameters. We also assume uniform
    // packet distribution, so we do not take the packet destination
    // into account.
    // 
    // We combine the contention model (eq. 12) with the model for
    // limited bisection width (sec. 4.2).

    // TODO: Fix (if necessary) distance calculation for uneven
    // topologies, i.e. 8-node 2d mesh or 10-node 3d mesh.

    // TODO: Confirm model for latency computed based on actual number
    // of network hops.

    // Retrieve parameters
    const NetworkMeshAnalyticalParameters *pParams = g_config->getAnalyticNetworkParms();
    double Tw2 = pParams->Tw2;
    double s = pParams->s;
    int n = pParams->n;
    double W = pParams->W;
    double p = global_utilization;

    // This lets us derive the latency, ignoring contention

    int N;                    // number of nodes in the network
    double k;                 // length of mesh in one dimension
    double kd;                // number of hops per dimension
    double time_per_hop;      // time spent in one hop through the network
    double B;                 // number of flits for packet
    double hops_in_network;   // number of nodes visited
    double Tb;                // latency, without contention

    N = g_chip->getNumModules();
    k = pow(N, 1./n);                  // pg 5
    kd = (k-1.)/2.;                    // pg 5 (note this will be
      // different for different network configurations...say,
      // bidirectional networks)
    time_per_hop = s + pow(k, n/2.-1.);  // pg 6
    B = ceil(packet.length * 8. / W);
    
    // Compute the number of hops based on src, dest
    // Based on this eqn:
    // node number = x1 + x2 k + x3 k^2 + ... + xn k^(n-1)
    int network_distance = 0;
    int src = packet.sender;
    int dest = packet.receiver;
    int ki = (int)k;
    for (int i = 0; i < n; i++)
      {
        div_t q1, q2;
        q1 = div(src, ki);
        q2 = div(dest, ki);
        // This models a unidirectional network distance
        // Should use abs() for bidirectional.
        if (q1.rem > q2.rem)<commit_msg>[network/analytical] Fix calculation error in Anant's model.<commit_after>#include "network_mesh_analytical.h"
#include "network_mesh_analytical_params.h"
#include "message_types.h"
#include "chip.h"
#include <math.h>

using namespace std;

//NetworkMeshAnalytical::NetworkMeshAnalytical(int tid, int num_threads, Core *core)
NetworkMeshAnalytical::NetworkMeshAnalytical(Core *core, int num_threads)
    :
        Network(core, num_threads),
        bytes_sent(0),
        cycles_spent_proc(0),
        cycles_spent_latency(0),
        cycles_spent_contention(0),
        global_utilization(0.0),
        local_utilization_last_update(0),
        local_utilization_flits_sent(0),
        update_interval(0)
{
   registerCallback(MCP_UTILIZATION_UPDATE_TYPE,
                    receiveMCPUpdate,
                    this);

   update_interval = g_config->getAnalyticNetworkParms()->update_interval;
}

NetworkMeshAnalytical::~NetworkMeshAnalytical()
{
   unregisterCallback(MCP_UTILIZATION_UPDATE_TYPE);
}

int NetworkMeshAnalytical::netSend(NetPacket packet)
{
    bytes_sent += packet.length;
    updateUtilization();
    return Network::netSend(packet);
}

NetPacket NetworkMeshAnalytical::netRecv(NetMatch match)
{
    updateUtilization();
    return Network::netRecv(match);
}

UINT64 NetworkMeshAnalytical::netProcCost(NetPacket packet)
{
    UINT64 cost = 10;
    cycles_spent_proc += cost;
    return cost;
}

UINT64 NetworkMeshAnalytical::netLatency(NetPacket packet)
{
    // We model a unidirectional network with end-around connections
    // using the network model in "Limits on Interconnect Performance"
    // (by Anant). Currently, this ignores communication locality and
    // sets static values for all parameters. We also assume uniform
    // packet distribution, so we do not take the packet destination
    // into account.
    // 
    // We combine the contention model (eq. 12) with the model for
    // limited bisection width (sec. 4.2).

    // TODO: Fix (if necessary) distance calculation for uneven
    // topologies, i.e. 8-node 2d mesh or 10-node 3d mesh.

    // TODO: Confirm model for latency computed based on actual number
    // of network hops.

    // Retrieve parameters
    const NetworkMeshAnalyticalParameters *pParams = g_config->getAnalyticNetworkParms();
    double Tw2 = pParams->Tw2;
    double s = pParams->s;
    int n = pParams->n;
    double W = pParams->W;
    double p = global_utilization;

    // This lets us derive the latency, ignoring contention

    int N;                    // number of nodes in the network
    double k;                 // length of mesh in one dimension
    double kd;                // number of hops per dimension
    double time_per_hop;      // time spent in one hop through the network
    double B;                 // number of flits for packet
    double hops_in_network;   // number of nodes visited
    double Tb;                // latency, without contention

    N = g_chip->getNumModules();
    k = pow(N, 1./n);                  // pg 5
    kd = k/2.;                         // pg 5 (note this will be
      // different for different network configurations...say,
      // bidirectional networks)
    time_per_hop = s + pow(k, n/2.-1.);  // pg 6
    B = ceil(packet.length * 8. / W);
    
    // Compute the number of hops based on src, dest
    // Based on this eqn:
    // node number = x1 + x2 k + x3 k^2 + ... + xn k^(n-1)
    int network_distance = 0;
    int src = packet.sender;
    int dest = packet.receiver;
    int ki = (int)k;
    for (int i = 0; i < n; i++)
      {
        div_t q1, q2;
        q1 = div(src, ki);
        q2 = div(dest, ki);
        // This models a unidirectional network distance
        // Should use abs() for bidirectional.
        if (q1.rem > q2.rem)
{'fraction_non_alphanumeric': 0.09841944481338823, 'fraction_numerical': 0.013149156594501261, 'mean_word_length': 2.515406162464986, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '4727231', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2207, 'n_words': 677}