-
Notifications
You must be signed in to change notification settings - Fork 66
/
parallel_mat_vec.c
48 lines (46 loc) · 1.09 KB
/
parallel_mat_vec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
44
45
46
47
48
#include <stdio.h>
#include <omp.h>
#include <stdlib.h>
//Multiply matrix A of size n * n by vector x of length n, return vector y of length n
int *p_mat_vec(int *A, int *x, int n)
{
int *y = (int *) malloc(sizeof(int) * n);
int i, j;
#pragma omp parallel shared(A, x, n) private(i, j)
{
#pragma omp for
for (i = 0; i < n; i++)
y[i] = 0;
#pragma omp for
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
y[i] = y[i] + A[i * n + j] * x[j];
}
return y;
}
int main(int argc, char *argv[])
{
int * p_mat_vec(int *A, int *x, int n);
int i, j, n;
int *A, *x, *y;
printf("Please give n: ");
scanf("%d",&n);
if ( (A=(int *)malloc(n * n *sizeof(int))) == NULL )
perror("memory allocation for a");
if ( (x=(int *)malloc(n*sizeof(int))) == NULL )
perror("memory allocation for b");
printf("Initializing matrix A and vector x\n");
for (j=0; j<n; j++)
x[j] = j;
for (i=0; i<n; i++)
for (j=0; j<n; j++)
A[i*n+j] = i;
printf("Executing parallel mxv function for n = %d\n",n);
y = p_mat_vec(A, x, n);
for (i = 0; i < n; i++)
printf("%d\t", y[i]);
free(A);
free(x);
free(y);
return(0);
}