computing_func.c 1.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <mpi.h>
#include "computing_func.h"

/*
 * Realiza una multiplicación de matrices de tamaño n
 */
10
double computeMatrix(double *matrix, int n) { 
11
12
13
14
15
16
  int row, col;
  double aux;

  aux=0;
  for(row=0; row<n; row++) {
    for(col=0; col<n; col++) {
17
      aux += (int)(matrix[row*n + col] * matrix[row*n + col]);
18
19
    }
  }
20
 
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  return aux;
}

double computePiSerial(int n) {
    int i;
    double h, sum, x, pi;

    h   = 1.0 / (double) n;  //wide of the rectangle
    sum = 0.0;
    for (i = 0; i < n; i++) {
        x = h * ((double)i + 0.5);   //height of the rectangle
        sum += 4.0 / (1.0 + x*x);
    }

    return pi = h * sum;
    //MPI_Reduce(&sum, &res, 1, MPI_DOUBLE, MPI_SUM, root, MPI_COMM_WORLD);
}



/*
 * Init matrix
 */
44
45
void initMatrix(double **matrix, size_t n) {
  size_t i, j;
46
47
48
  double *aux = NULL;

  freeMatrix(matrix);
49
50

  // Init matrix
51
52
53
54
55
56
57
  aux = (double *) malloc(n * n * sizeof(double));
  if(aux == NULL) { perror("Computing matrix could not be allocated"); MPI_Abort(MPI_COMM_WORLD, -1);}

  for(i=0; i < n; i++) {
    for(j=0; j < n; j++) {

      aux[i*n + j] = (i+j) * 1.1;
58
59
    }
  }
60
  *matrix = aux;
61
}
62
63


64

65
66
67
68
69
70
71
void freeMatrix(double **matrix) {
  // Init matrix
  if(*matrix != NULL) {
    free(*matrix);
    *matrix = NULL;
  }
}