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

schedbench.c (4435B)


      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 
     34 #include <stdio.h>
     35 #include <stdlib.h>
     36 #include <math.h>
     37 #include <omp.h>
     38 
     39 #include "common.h"
     40 #include "schedbench.h"
     41 
     42 int cksz, itersperthr = 128;
     43 char testName[32];
     44 
     45 int main(int argc, char **argv) {
     46 
     47     init(argc, argv);
     48 
     49     /* GENERATE REFERENCE TIME */
     50     reference("reference time", &refer);
     51 
     52     /* TEST STATIC */
     53     benchmark("STATIC", &teststatic);
     54 
     55     /* TEST STATIC,n */
     56     cksz = 1;
     57     while (cksz <= itersperthr) {
     58 	sprintf(testName, "STATIC %d", cksz);
     59 	benchmark(testName, &teststaticn);
     60 	cksz *= 2;
     61     }
     62 
     63     /* TEST DYNAMIC,n */
     64     cksz = 1;
     65     while (cksz <= itersperthr) {
     66 	sprintf(testName, "DYNAMIC %d", cksz);
     67 	benchmark(testName, &testdynamicn);
     68 	cksz *= 2;
     69     }
     70 
     71     /* TEST GUIDED,n */
     72     cksz = 1;
     73     while (cksz <= itersperthr / nthreads) {
     74 	sprintf(testName, "GUIDED %d", cksz);
     75 	benchmark(testName, &testguidedn);
     76 	cksz *= 2;
     77     }
     78 
     79     finalise();
     80 
     81     return EXIT_SUCCESS;
     82 
     83 }
     84 
     85 void refer() {
     86     int i, j;
     87     for (j = 0; j < innerreps; j++) {
     88 	for (i = 0; i < itersperthr; i++) {
     89 	    delay(delaylength);
     90 	}
     91     }
     92 }
     93 
     94 void teststatic() {
     95 
     96     int i, j;
     97 #pragma omp parallel private(j)
     98     {
     99 	for (j = 0; j < innerreps; j++) {
    100 #pragma omp for schedule(static)
    101 	    for (i = 0; i < itersperthr * nthreads; i++) {
    102 		delay(delaylength);
    103 	    }
    104 	}
    105     }
    106 }
    107 
    108 void teststaticn() {
    109     int i, j;
    110 #pragma omp parallel private(j)
    111     {
    112 	for (j = 0; j < innerreps; j++) {
    113 #pragma omp for schedule(static,cksz)
    114 	    for (i = 0; i < itersperthr * nthreads; i++) {
    115 		delay(delaylength);
    116 	    }
    117 	}
    118     }
    119 }
    120 
    121 void testdynamicn() {
    122     int i, j;
    123 #pragma omp parallel private(j)
    124     {
    125 	for (j = 0; j < innerreps; j++) {
    126 #pragma omp for schedule(dynamic,cksz)
    127 	    for (i = 0; i < itersperthr * nthreads; i++) {
    128 		delay(delaylength);
    129 	    }
    130 	}
    131     }
    132 }
    133 
    134 void testguidedn() {
    135     int i, j;
    136 #pragma omp parallel private(j)
    137     {
    138 	for (j = 0; j < innerreps; j++) {
    139 #pragma omp for schedule(guided,cksz)
    140 	    for (i = 0; i < itersperthr * nthreads; i++) {
    141 		delay(delaylength);
    142 	    }
    143 	}
    144     }
    145 }