A program to multiply an n-by-m matrix with an n-vector some number of times. It takes 3 arguments, the number of times to multiply and n and m. To compile with OpenMP support you have to make sure you have a gcc w/ the OpenMP libraries installed (not installed by default on OS X 10.5.2, the system I was using to test this), and use this line to compile with: gcc -std=c99 -o test test.c -fopenmp
( C ) ✂
#include <stdio.h>
#include <stdlib.h>
void mxv( int m, int n, double * restrict a, double * restrict b, double * restrict c );
int main( int argc, char *argv[] ) {
double *a, *b, *c;
int i, j, m, n, k, loopLimit;
if (argc != 4) {
fputs( "usage: test <loopLimit> <m> <n>", stderr );
exit( EXIT_FAILURE );
}
loopLimit = (int)strtol( argv[1], NULL, 10 );
m = (int)strtol( argv[2], NULL, 10 );
n = (int)strtol( argv[3], NULL, 10 );
if ( (a=(double *)malloc(m*sizeof(double))) == NULL )
perror( "memory allocation for a" );
if ( (b=(double *)malloc(m*n*sizeof(double))) == NULL )
perror( "memory allocation for b" );
if ( (c=(double *)malloc(n*sizeof(double))) == NULL )
perror( "memory allocation for c" );
for (j=0; j<n; j++)
c[ j ] = 2.0;
for (i=0; i<m; i++)
for (j=0; j<n; j++)
b[ i*n + j ] = i;
for (k=0; k<loopLimit; k++)
(void) mxv( m, n, a, b, c );
free(a); free(b); free(c);
return EXIT_SUCCESS;
}
void mxv( int m, int n, double * restrict a, double * restrict b, double * restrict c ) {
int i, j;
#pragma omp parallel for default(none) shared( m, n, a, b, c ) private( i, j )
for (i=0; i<m; i++) {
a[ i ] = 0.0;
for (j=0; j<n; j++)
a[ i ] += b[ i*n + j ] * c[ j ];
}
// end of omp parallel for
}
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 voidmxv( int m, int n, double * restrict a, double * restrict b, double * restrict c ); 5 6 intmain( int argc, char *argv[] ) { 7 double *a, *b, *c; 8 int i, j, m, n, k, loopLimit; 9 10 if (argc != 4) { 11 fputs( "usage: test <loopLimit> <m> <n>", stderr ); 12 exit( EXIT_FAILURE ); 13 } 14 15 loopLimit = (int)strtol( argv[1], NULL, 10 ); 16 m = (int)strtol( argv[2], NULL, 10 ); 17 n = (int)strtol( argv[3], NULL, 10 ); 18 19 if ( (a=(double *)malloc(m*sizeof(double))) == NULL ) 20 perror( "memory allocation for a" ); 21 if ( (b=(double *)malloc(m*n*sizeof(double))) == NULL ) 22 perror( "memory allocation for b" ); 23 if ( (c=(double *)malloc(n*sizeof(double))) == NULL ) 24 perror( "memory allocation for c" ); 25 26 for (j=0; j<n; j++) 27 c[ j ] = 2.0; 28 for (i=0; i<m; i++) 29 for (j=0; j<n; j++) 30 b[ i*n + j ] = i; 31 32 for (k=0; k<loopLimit; k++) 33 (void) mxv( m, n, a, b, c ); 34 35 free(a);free(b);free(c); 36 return EXIT_SUCCESS; 37 } 38 39 voidmxv( int m, int n, double * restrict a, double * restrict b, double * restrict c ) { 40 int i, j; 41 42 #pragma omp parallel for default(none) shared( m, n, a, b, c ) private( i, j ) 43 44 for (i=0; i<m; i++) { 45 a[ i ] = 0.0; 46 for (j=0; j<n; j++) 47 a[ i ] += b[ i*n + j ] * c[ j ]; 48 } 49 // end of omp parallel for 50 }
This code is based on that given in Fig. 3.5 / 3.10 in the book Using OpenMP.