tc-linarr.c - numeric - C++ library with numerical algorithms
(HTM) git clone git://src.adamsgaard.dk/numeric
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
tc-linarr.c (1062B)
---
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 void matrixMult(double* A, double* B, double* C, unsigned int N)
5 {
6 unsigned int i, j, k;
7 double sum;
8 #pragma omp parallel for private (j,k,sum) default(none) shared(A,B,C,N) if(N>100)
9 for (i = 0; i<N; i++) {
10 for (j = 0; j<N; j++) {
11 sum = 0.0;
12 for (k = 0; k<N; k++)
13 sum += A[k*N+i] * B[j*N+k];
14 C[j*N+i] = sum;
15 }
16 }
17 }
18
19
20 int main(int argc, char* argv[])
21 {
22 unsigned int i, N;
23 double* A;
24 double* B;
25 double* C;
26
27 if (argc == 2) {
28 N = atoi(argv[1]);
29 } else {
30 fprintf(stderr, "Sorry, I need matrix width as command line argument\n");
31 return 1;
32 }
33
34 A = (double*) malloc(N * N * sizeof(double));
35 B = (double*) malloc(N * N * sizeof(double));
36 C = (double*) malloc(N * N * sizeof(double));
37
38 for (i = 0; i < N*N; i++) {
39 A[i] = 2.0;
40 B[i] = (double)i;
41 }
42
43 matrixMult(A, B, C, N);
44
45 free(A);
46 free(B);
47 free(C);
48
49 /* Exit with success */
50 return 0;
51 }