| 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>
|
|---|
| 44 |
|
|---|
| 45 | #ifdef _OPENMP
|
|---|
| 46 | #include <omp.h>
|
|---|
| 47 | #define TRUE 1
|
|---|
| 48 | #define FALSE 0
|
|---|
| 49 | #else
|
|---|
| 50 | #define omp_get_thread_num() 0
|
|---|
| 51 | #define omp_get_num_threads() 1
|
|---|
| 52 | #endif
|
|---|
| 53 |
|
|---|
| 54 | int calculate_sum(int length);
|
|---|
| 55 |
|
|---|
| 56 | int *pglobal;
|
|---|
| 57 |
|
|---|
| 58 | #pragma omp threadprivate(pglobal)
|
|---|
| 59 |
|
|---|
| 60 | int main()
|
|---|
| 61 | {
|
|---|
| 62 | int i, j, sum, TID, n = 5;
|
|---|
| 63 | int length[n], check[n];
|
|---|
| 64 |
|
|---|
| 65 | #ifdef _OPENMP
|
|---|
| 66 | (void) omp_set_dynamic(FALSE);
|
|---|
| 67 | if (omp_get_dynamic()) {printf("Warning: dynamic adjustment of threads has been set\n");}
|
|---|
| 68 | (void) omp_set_num_threads(3);
|
|---|
| 69 | #endif
|
|---|
| 70 |
|
|---|
| 71 | for (i=0; i<n; i++)
|
|---|
| 72 | {
|
|---|
| 73 | length[i] = 10 * (i+1);
|
|---|
| 74 | check[i] = length[i]*(length[i]+1)/2;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | #pragma omp parallel for shared(n,length,check) private(TID,i,j,sum)
|
|---|
| 78 | for (i=0; i<n; i++)
|
|---|
| 79 | {
|
|---|
| 80 | TID = omp_get_thread_num();
|
|---|
| 81 |
|
|---|
| 82 | if ( (pglobal = (int *) malloc(length[i]*sizeof(int))) != NULL ) {
|
|---|
| 83 |
|
|---|
| 84 | for (j=sum=0; j<length[i]; j++)
|
|---|
| 85 | pglobal[j] = j+1;
|
|---|
| 86 |
|
|---|
| 87 | sum = calculate_sum(length[i]);
|
|---|
| 88 |
|
|---|
| 89 | printf("TID %d: value of sum for i = %d is %8d (check = %8d)\n",
|
|---|
| 90 | TID,i,sum,check[i]);
|
|---|
| 91 |
|
|---|
| 92 | free(pglobal);
|
|---|
| 93 |
|
|---|
| 94 | } else {
|
|---|
| 95 | printf("TID %d: fatal error in malloc for length[%d] = %d\n",
|
|---|
| 96 | TID,i,length[i]);
|
|---|
| 97 | }
|
|---|
| 98 | } /*-- End of parallel for --*/
|
|---|
| 99 |
|
|---|
| 100 | return(0);
|
|---|
| 101 | }
|
|---|
| 102 | int calculate_sum(int length)
|
|---|
| 103 | {
|
|---|
| 104 | int sum = 0;
|
|---|
| 105 |
|
|---|
| 106 | for (int j=0; j<length; j++)
|
|---|
| 107 | sum += pglobal[j];
|
|---|
| 108 |
|
|---|
| 109 | return(sum);
|
|---|
| 110 | }
|
|---|