Original Sequential version of MXV in C
42 #include <stdio.h>
43 #include <stdlib.h>
44
45 void mxv(int m, int n, double * restrict a,
46 double * restrict b, double * restrict c);
47
48 int main(int argc, char *argv[])
49 {
50 double *a,*b,*c;
51 int i, j, m, n;
52
53 //printf("Please give m and n: ");
54 //scanf("%d %d",&m,&n);
55 printf("runnging 10000 * 10000 matrics x vector\n");
56 m = 10000;
57 n = 10000;
58
59 if ( (a=(double *)malloc(m*sizeof(double))) == NULL )
60 perror("memory allocation for a");
61 if ( (b=(double *)malloc(m*n*sizeof(double))) == NULL )
62 perror("memory allocation for b");
63 if ( (c=(double *)malloc(n*sizeof(double))) == NULL )
64 perror("memory allocation for c");
65
66 printf("Initializing matrix B and vector c\n");
67 for (j=0; j<n; j++)
68 c[j] = 2.0;
69 for (i=0; i<m; i++)
70 for (j=0; j<n; j++)
71 b[i*n+j] = i;
72
73 printf("Executing mxv function for m = %d n = %d\n",m,n);
74 (void) mxv(m, n, a, b, c);
75
76 free(a);free(b);free(c);
77 return(0);
78 }
79
80 void mxv(int m, int n, double * restrict a, double * restrict b,
81 double * restrict c)
82 {
83 int i, j;
84
85 for (i=0; i<m; i++)
86 {
87 a[i] = 0.0;
88 for (j=0; j<n; j++)
89 a[i] += b[i*n+j]*c[j];
90 }
and OpenMP
42 #include <stdio.h>
43 #include <stdlib.h>
44
45 void mxv(int m, int n, double * restrict a,
46 double * restrict b, double * restrict c);
47
48 int main(int argc, char *argv[])
49 {
50 double *a,*b,*c;
51 int i, j, m, n;
52
53 //printf("Please give m and n: ");
54 //scanf("%d %d",&m,&n);
55 printf("running 10000 * 10000 matrics x vectors\n");
56 m = 10000;
57 n = 10000;
58
59 if ( (a=(double *)malloc(m*sizeof(double))) == NULL )
60 perror("memory allocation for a");
61 if ( (b=(double *)malloc(m*n*sizeof(double))) == NULL )
62 perror("memory allocation for b");
63 if ( (c=(double *)malloc(n*sizeof(double))) == NULL )
64 perror("memory allocation for c");
65
66 printf("Initializing matrix B and vector c\n");
67 for (j=0; j<n; j++)
68 c[j] = 2.0;
69 for (i=0; i<m; i++)
70 for (j=0; j<n; j++)
71 b[i*n+j] = i;
72
73 printf("Executing mxv function for m = %d n = %d\n",m,n);
74 (void) mxv(m, n, a, b, c);
75
76 free(a);free(b);free(c);
77 return(0);
78 }
79
80 void mxv(int m, int n, double * restrict a, double * restrict b,
81 double * restrict c)
82 {
83 int i, j;
84 #pragma omp parallel for default(none) \
85 shared(m,n,a,b,c) private(i,j)
86 for (i=0; i<m; i++)
87 {
88 a[i] = 0.0;
89 for (j=0; j<n; j++)
90 a[i] += b[i*n+j]*c[j];
91 } /*-- End of omp parallel for --*/
92 }
OpenMP will check your processors and will create # of threads.
in my case it creates 16 threads for doing mxv functions.
you can check # of threads with omp_get_number_threads();
and also set # of threads with omp_set_number_threads(#number);
or #pragma omp num_threads(#number)
results for those two sample codes are
time ./fig3.5.exe
runnging 10000 * 10000 matrics x vector
Initializing matrix B and vector c
Executing mxv function for m = 10000 n = 10000
real 0m1.197s
user 0m0.688s
sys 0m0.508s
time ./fig3.10.exe
running 10000 * 10000 matrics x vectors
Initializing matrix B and vector c
Executing mxv function for m = 10000 n = 10000
real 0m0.831s
user 0m3.700s
sys 0m0.456s
No comments:
Post a Comment