| [65657d6] | 1 | /*
|
|---|
| 2 |
|
|---|
| 3 | DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|---|
| 4 |
|
|---|
| 5 | Copyright 2009 Sun Microsystems, Inc. All rights reserved.
|
|---|
| 6 |
|
|---|
| 7 | The contents of this file are subject to the terms of the BSD License("BSD")(the "License").
|
|---|
| 8 | You can obtain a copy of the License at: http://www.opensparc.net/pubs/t1/licenses/BSD+_License.txt
|
|---|
| 9 |
|
|---|
| 10 | The BSD License
|
|---|
| 11 |
|
|---|
| 12 | Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | modification, are permitted provided that the following conditions are met:
|
|---|
| 14 |
|
|---|
| 15 | * Redistribution of source code must retain the above copyright
|
|---|
| 16 | notice, this list of conditions and the following disclaimer.
|
|---|
| 17 | * Redistribution in binary form must reproduce the above copyright
|
|---|
| 18 | notice, this list of conditions and the following disclaimer in
|
|---|
| 19 | the documentation and/or other materials provided with the
|
|---|
| 20 | distribution.
|
|---|
| 21 | * Neither the name of Sun Microsystems, Inc. or the names of
|
|---|
| 22 | contributors may be used to endorse or promote products derived
|
|---|
| 23 | from this software without specific prior written permission.
|
|---|
| 24 |
|
|---|
| 25 | This software is provided "AS IS," without a warranty of any kind. ALL
|
|---|
| 26 | EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
|
|---|
| 27 | IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
|
|---|
| 28 | NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND
|
|---|
| 29 | ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A
|
|---|
| 30 | RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
|
|---|
| 31 | IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
|
|---|
| 32 | OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
|
|---|
| 33 | PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
|
|---|
| 34 | ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
|
|---|
| 35 | BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
|---|
| 36 |
|
|---|
| 37 | You acknowledge that this software is not designed, licensed or intended for
|
|---|
| 38 | use in the design, construction, operation or maintenance of any nuclear facility.
|
|---|
| 39 |
|
|---|
| 40 | */
|
|---|
| 41 |
|
|---|
| 42 | #include <stdio.h>
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| [9a94b18] | 44 | #include <omp.h>
|
|---|
| 45 |
|
|---|
| 46 | #define M 10
|
|---|
| 47 | #define N 10
|
|---|
| [65657d6] | 48 |
|
|---|
| 49 | void mxv(int m, int n, double * restrict a,
|
|---|
| 50 | double * restrict b, double * restrict c);
|
|---|
| 51 |
|
|---|
| 52 | int main(int argc, char *argv[])
|
|---|
| 53 | {
|
|---|
| 54 | double *a,*b,*c;
|
|---|
| 55 | int i, j, m, n;
|
|---|
| 56 |
|
|---|
| [9a94b18] | 57 | /* REPLACED WITH defines
|
|---|
| [65657d6] | 58 | printf("Please give m and n: ");
|
|---|
| 59 | scanf("%d %d",&m,&n);
|
|---|
| 60 | printf("\n");
|
|---|
| [9a94b18] | 61 | */
|
|---|
| 62 | m = M;
|
|---|
| 63 | n = N;
|
|---|
| [65657d6] | 64 |
|
|---|
| 65 | if ( (a=(double *)malloc(m*sizeof(double))) == NULL )
|
|---|
| 66 | perror("memory allocation for a");
|
|---|
| 67 | if ( (b=(double *)malloc(m*n*sizeof(double))) == NULL )
|
|---|
| 68 | perror("memory allocation for b");
|
|---|
| 69 | if ( (c=(double *)malloc(n*sizeof(double))) == NULL )
|
|---|
| 70 | perror("memory allocation for c");
|
|---|
| 71 |
|
|---|
| 72 | printf("Initializing matrix B and vector c\n");
|
|---|
| 73 | for (j=0; j<n; j++)
|
|---|
| 74 | c[j] = 2.0;
|
|---|
| 75 | for (i=0; i<m; i++)
|
|---|
| 76 | for (j=0; j<n; j++)
|
|---|
| 77 | b[i*n+j] = i;
|
|---|
| 78 |
|
|---|
| 79 | printf("Executing mxv function for m = %d n = %d\n",m,n);
|
|---|
| 80 | (void) mxv(m, n, a, b, c);
|
|---|
| 81 |
|
|---|
| 82 | free(a);free(b);free(c);
|
|---|
| 83 | return(0);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | void mxv(int m, int n, double * restrict a, double * restrict b,
|
|---|
| 87 | double * restrict c)
|
|---|
| 88 | {
|
|---|
| 89 | int i, j;
|
|---|
| 90 |
|
|---|
| 91 | #pragma omp parallel for default(none) \
|
|---|
| 92 | shared(m,n,a,b,c) private(i,j)
|
|---|
| 93 | for (i=0; i<m; i++)
|
|---|
| 94 | {
|
|---|
| 95 | a[i] = 0.0;
|
|---|
| 96 | for (j=0; j<n; j++)
|
|---|
| 97 | a[i] += b[i*n+j]*c[j];
|
|---|
| 98 | } /*-- End of omp parallel for --*/
|
|---|
| 99 | }
|
|---|