linear_reg.c 3.74 KB
Newer Older
1
2
#include <stdlib.h>
#include <stdio.h>
3
#include <math.h>
4
5
6
7
#include <mpi.h>
#include "Main_datatypes.h"
#include "linear_reg.h"

8
9
10
11
12

// Array for linear regression computation
// Cantidades                          10b 100b 1Kb   5Kb  10Kb   50Kb  100Kb   500Kb   1Mb      10Mb      100Mb
double LR_bytes_array[LR_ARRAY_TAM] = {10, 100, 1000, 5000,10000, 50000,100000, 500000, 1000000, 10000000, 100000000};

13
14
15
16
17
18
19
20
// Linear regression
// Y = a +bX
// Bytes = a +b(Time)
//
// X is the independent variable, which correlates to the Communication time
// Y is the dependent variable, which correlates to the number of bytes
//

21
22
23
24
25
26
27
28
29
void lr_avg_plus_diff(int tam, double *array, double *avg, double *diffs);

/*
 * Computes and returns the related Y value to a given linear regression
 */
void lr_calc_Y(double slope, double intercept, double x_value, int *y_result) {
  *y_result = (int) ceil(intercept + slope * x_value);
}

30
31
32
33
34
35

/*
 * Computes the slope and intercept for a given array of times
 * so users can calculate the number of bytes for a given time.
 *
 */
36
void lr_compute(int tam, double *bytes, double *times, double *slope, double *intercept) {
37
38
39
40
41
  int i;
  double avgX, avgY;
  double *diffsX, *diffsY;
  double SSxx, SSxy;

42
43
  diffsX = malloc(tam * sizeof(double));
  diffsY = malloc(tam * sizeof(double));
44
45
  SSxx = SSxy = 0;

46
47
  lr_avg_plus_diff(tam, times, &avgX, diffsX);
  lr_avg_plus_diff(tam, bytes, &avgY, diffsY);
48

49
  for(i=0; i<tam; i++) {
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    SSxx+= diffsX[i]*diffsX[i];
    SSxy+= diffsX[i]*diffsY[i];
  }
  *slope = SSxy / SSxx;
  *intercept = avgY - (*slope * avgX);
  
  free(diffsX);
  free(diffsY);
}

/*
 * Computes the average of an arrray and 
 * the difference of each element in respect to the average.
 *
 * Returns the average and an the difference of each element.
 */
66
void lr_avg_plus_diff(int tam, double *array, double *avg, double *diffs) {
67
68
  int i;
  double sum = 0;
69
  for(i=0; i<tam; i++) {
70
71
    sum+= array[i];
  }
72
  *avg = sum / tam;
73

74
  for(i=0; i<tam; i++) {
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    diffs[i]= *avg - array[i];
  }
}


//======================================================||
//======================================================||
//==================TIMES COMPUTATION===================||
//======================================================||
//======================================================||

/*
 * Obtains an array of times to perform a "Broadcast"
 * operation depending on a predifined set of number of bytes.
 */
90
91
92
93
void lr_times_bcast(int myId, int numP, int root, MPI_Comm comm, int loop_iters, double *times) {
  int i, j, n;
  double start_time;
  char *aux = NULL;
94
95
96
97
98

  for(i=0; i<LR_ARRAY_TAM; i++) {
    n = LR_bytes_array[i];
    aux = malloc(n * sizeof(char));

99
100
101
    for(j=0; j<loop_iters; j++) {
      MPI_Barrier(comm);
      start_time = MPI_Wtime();
102
      MPI_Bcast(aux, n, MPI_CHAR, root, comm);
103
      times[i*loop_iters+j] = MPI_Wtime() - start_time;
104
105
    }

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    free(aux);
    aux = NULL;
  }
}


/*
 * Obtains an array of times to perform an "Allgatherv"
 * operation depending on a predifined set of number of bytes.
 */
/*
void lr_times_allgatherv(int myId, int numP, int root, MPI_Comm comm, int loop_iters, double *times) {
  int i, j, n;
  int *counts, *displs;
  double start_time;
  char *aux = NULL;

  counts = calloc(numP,sizeof(int));
  displs = calloc(numP,sizeof(int));

  for(i=0; i<LR_ARRAY_TAM; i++) {
    n = LR_bytes_array[i];
    aux = malloc( * sizeof(char));
    aux_full = malloc(n * sizeof(char));

    for(j=0; j<loop_iters; j++) {
      MPI_Barrier(comm);
      start_time = MPI_Wtime();
      MPI_Allgatherv(aux, stage_data.my_bytes, MPI_CHAR, aux_full, counts, displs, MPI_CHAR, comm);
      times[i*loop_iters+j] = MPI_Wtime() - start_time;
136
    }
137
138
    

139
    free(aux);
140
141
142
    free(aux_full);
    aux_full = NULL;
    aux = NULL;
143
  }
144
145
  free(counts);
  free(displs);
146
}
147
*/