computing_func.c 1.53 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
 */
iker_martin's avatar
iker_martin committed
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++) {
iker_martin's avatar
iker_martin committed
17
      aux += (int)(matrix[row*n + col] * matrix[row*n + col]);
18
19
    }
  }
iker_martin's avatar
iker_martin committed
20
 
21
22
  return aux;
}
iker_martin's avatar
iker_martin committed
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
double computeMatrix(double *matrix, int n) { 
  int row, col;
  double aux;

  aux=0;
  for(row=0; row<n; row++) {
    for(col=0; col<n; col++) {
      aux += ( (int)(matrix[row*n + col] + exp(sqrt(row*col))) % n);
    }
  }
  return aux;
}*/
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

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
 */
55
56
void initMatrix(double **matrix, size_t n) {
  size_t i, j;
iker_martin's avatar
iker_martin committed
57
58
59
  double *aux = NULL;

  freeMatrix(matrix);
60
61

  // Init matrix
iker_martin's avatar
iker_martin committed
62
63
64
65
66
67
68
  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;
69
70
    }
  }
iker_martin's avatar
iker_martin committed
71
  *matrix = aux;
72
}
73
74
75
76
77
78
79
80
81


void freeMatrix(double **matrix) {
  // Init matrix
  if(*matrix != NULL) {
    free(*matrix);
    *matrix = NULL;
  }
}