cs205-lecture-examples

Example codes used during Harvard CS205 lectures
git clone https://git.0xfab.ch/cs205-lecture-examples.git
Log | Files | Refs | README | LICENSE

syncbench.c (6061B)


      1 /****************************************************************************
      2 *                                                                           *
      3 *             OpenMP MicroBenchmark Suite - Version 3.1                     *
      4 *                                                                           *
      5 *                            produced by                                    *
      6 *                                                                           *
      7 *             Mark Bull, Fiona Reid and Nix Mc Donnell                      *
      8 *                                                                           *
      9 *                                at                                         *
     10 *                                                                           *
     11 *                Edinburgh Parallel Computing Centre                        *
     12 *                                                                           *
     13 *         email: markb@epcc.ed.ac.uk or fiona@epcc.ed.ac.uk                 *
     14 *                                                                           *
     15 *                                                                           *
     16 *      This version copyright (c) The University of Edinburgh, 2015.        *
     17 *                                                                           *
     18 *                                                                           *
     19 *  Licensed under the Apache License, Version 2.0 (the "License");          *
     20 *  you may not use this file except in compliance with the License.         *
     21 *  You may obtain a copy of the License at                                  *
     22 *                                                                           *
     23 *      http://www.apache.org/licenses/LICENSE-2.0                           *
     24 *                                                                           *
     25 *  Unless required by applicable law or agreed to in writing, software      *
     26 *  distributed under the License is distributed on an "AS IS" BASIS,        *
     27 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
     28 *  See the License for the specific language governing permissions and      *
     29 *  limitations under the License.                                           *
     30 *                                                                           *
     31 ****************************************************************************/
     32 
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <math.h>
     36 #include <omp.h>
     37 
     38 #include "common.h"
     39 #include "syncbench.h"
     40 
     41 omp_lock_t lock;
     42 
     43 int main(int argc, char **argv) {
     44 
     45     // Start Paraver tracing
     46 #ifdef PARAVERTRACE
     47     Extrae_init();
     48 #endif
     49 
     50     init(argc, argv);
     51 
     52     omp_init_lock(&lock);
     53 
     54     /* GENERATE REFERENCE TIME */
     55     reference("reference time 1", &refer);
     56 
     57     /* TEST PARALLEL REGION */
     58     benchmark("PARALLEL", &testpr);
     59 
     60     /* TEST FOR */
     61     benchmark("FOR", &testfor);
     62 
     63     /* TEST PARALLEL FOR */
     64     benchmark("PARALLEL FOR", &testpfor);
     65 
     66     /* TEST BARRIER */
     67     benchmark("BARRIER", &testbar);
     68 
     69     /* TEST SINGLE */
     70     benchmark("SINGLE", &testsing);
     71 
     72     /* TEST  CRITICAL*/
     73     benchmark("CRITICAL", &testcrit);
     74 
     75     /* TEST  LOCK/UNLOCK */
     76     benchmark("LOCK/UNLOCK", &testlock);
     77 
     78     /* TEST ORDERED SECTION */
     79     benchmark("ORDERED", &testorder);
     80 
     81     /* GENERATE NEW REFERENCE TIME */
     82     reference("reference time 2", &referatom);
     83 
     84     /* TEST ATOMIC */
     85     benchmark("ATOMIC", &testatom);
     86 
     87     /* GENERATE NEW REFERENCE TIME */
     88     reference("reference time 3", &referred);
     89 
     90     /* TEST REDUCTION (1 var)  */
     91     benchmark("REDUCTION", &testred);
     92 
     93 #ifdef PARAVERTRACE
     94     Extrae_fini();
     95 #endif
     96 
     97     finalise();
     98 
     99     return EXIT_SUCCESS;
    100 }
    101 
    102 void refer() {
    103     int j;
    104     for (j = 0; j < innerreps; j++) {
    105 	delay(delaylength);
    106     }
    107 }
    108 
    109 void referatom(){
    110     int j;
    111     double aaaa = 0.0;
    112     double epsilon = 1.0e-15;
    113     double b, c;
    114     b = 1.0;
    115     c = (1.0 + epsilon);
    116     for (j = 0; j < innerreps; j++) {
    117 	aaaa += b;
    118 	b *= c;
    119     }
    120     if (aaaa < 0.0)
    121 	printf("%f\n", aaaa);
    122 }
    123 
    124 void referred() {
    125     int j;
    126     int aaaa = 0;
    127     for (j = 0; j < innerreps; j++) {
    128 	delay(delaylength);
    129 	aaaa += 1;
    130     }
    131 }
    132 
    133 void testpr() {
    134     int j;
    135     for (j = 0; j < innerreps; j++) {
    136 #pragma omp parallel
    137 	{
    138 	    delay(delaylength);
    139 	}
    140     }
    141 }
    142 
    143 void testfor() {
    144     int i, j;
    145 #pragma omp parallel private(j)
    146     {
    147 	for (j = 0; j < innerreps; j++) {
    148 #pragma omp for
    149 	    for (i = 0; i < nthreads; i++) {
    150 		delay(delaylength);
    151 	    }
    152 	}
    153     }
    154 }
    155 
    156 void testpfor() {
    157     int i, j;
    158     for (j = 0; j < innerreps; j++) {
    159 #pragma omp parallel for
    160 	for (i = 0; i < nthreads; i++) {
    161 	    delay(delaylength);
    162 	}
    163     }
    164 }
    165 
    166 void testbar() {
    167     int j;
    168 #pragma omp parallel private(j)
    169     {
    170 	for (j = 0; j < innerreps; j++) {
    171 	    delay(delaylength);
    172 #pragma omp barrier
    173 	}
    174     }
    175 }
    176 
    177 void testsing() {
    178     int j;
    179 #pragma omp parallel private(j)
    180     {
    181 	for (j = 0; j < innerreps; j++) {
    182 #pragma omp single
    183 	    delay(delaylength);
    184 	}
    185     }
    186 }
    187 
    188 void testcrit() {
    189     int j;
    190 #pragma omp parallel private(j)
    191     {
    192 	for (j = 0; j < innerreps / nthreads; j++) {
    193 #pragma omp critical
    194 	    {
    195 		delay(delaylength);
    196 	    }
    197 	}
    198     }
    199 }
    200 
    201 void testlock() {
    202     int j;
    203 
    204 #pragma omp parallel private(j)
    205     {
    206 	for (j = 0; j < innerreps / nthreads; j++) {
    207 	    omp_set_lock(&lock);
    208 	    delay(delaylength);
    209 	    omp_unset_lock(&lock);
    210 	}
    211     }
    212 }
    213 
    214 void testorder() {
    215     int j;
    216 #pragma omp parallel for ordered schedule (static,1)
    217     for (j = 0; j < (int)innerreps; j++) {
    218 #pragma omp ordered
    219 	delay(delaylength);
    220     }
    221 }
    222 
    223 void testatom() {
    224     int j;
    225     double aaaa = 0.0;
    226     double epsilon = 1.0e-15;
    227     double b,c;
    228     b = 1.0;
    229     c = (1.0 + epsilon);
    230 #pragma omp parallel private(j) firstprivate(b)
    231     {
    232 	for (j = 0; j < innerreps / nthreads; j++) {
    233 #pragma omp atomic	
    234 	    aaaa += b;
    235 	    b *= c;
    236 	}
    237     }
    238     if (aaaa < 0.0)
    239 	printf("%f\n", aaaa);
    240 }
    241 
    242 void testred() {
    243     int j;
    244     int aaaa = 0;
    245     for (j = 0; j < innerreps; j++) {
    246 #pragma omp parallel reduction(+:aaaa)
    247 	{
    248 	    delay(delaylength);
    249 	    aaaa += 1;
    250 	}
    251     }
    252 }
    253