[GRASS-SVN] r57266 - in sandbox: . soeren soeren/benchmarks soeren/benchmarks/neighborhood_openmp

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jul 24 13:33:53 PDT 2013


Author: hamish
Date: 2013-07-24 13:33:52 -0700 (Wed, 24 Jul 2013)
New Revision: 57266

Added:
   sandbox/soeren/
   sandbox/soeren/benchmarks/
   sandbox/soeren/benchmarks/neighborhood_openmp/
   sandbox/soeren/benchmarks/neighborhood_openmp/benchmark.sh
   sandbox/soeren/benchmarks/neighborhood_openmp/benchmark_auto.sh
   sandbox/soeren/benchmarks/neighborhood_openmp/neighbor_bench.c
Log:
+ neighborhood analysis benchmark code

Added: sandbox/soeren/benchmarks/neighborhood_openmp/benchmark.sh
===================================================================
--- sandbox/soeren/benchmarks/neighborhood_openmp/benchmark.sh	                        (rev 0)
+++ sandbox/soeren/benchmarks/neighborhood_openmp/benchmark.sh	2013-07-24 20:33:52 UTC (rev 57266)
@@ -0,0 +1,12 @@
+#!/bin/sh
+#
+# Author: Soeren Gebbert
+# License: GPL >=2
+
+gcc -Wall -fopenmp -lgomp -Ofast main.c -o neighbor
+export OMP_NUM_THREADS=1
+time ./neighbor 5000 5000 23
+export OMP_NUM_THREADS=2
+time ./neighbor 5000 5000 23
+export OMP_NUM_THREADS=4
+time ./neighbor 5000 5000 23


Property changes on: sandbox/soeren/benchmarks/neighborhood_openmp/benchmark.sh
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:mime-type
   + text/x-sh
Added: svn:eol-style
   + native

Added: sandbox/soeren/benchmarks/neighborhood_openmp/benchmark_auto.sh
===================================================================
--- sandbox/soeren/benchmarks/neighborhood_openmp/benchmark_auto.sh	                        (rev 0)
+++ sandbox/soeren/benchmarks/neighborhood_openmp/benchmark_auto.sh	2013-07-24 20:33:52 UTC (rev 57266)
@@ -0,0 +1,102 @@
+#!/bin/sh
+# Author: Hamish Bowman, Dunedin, New Zealand
+# (c) 2013 Hamish Bowman, and The GRASS Development Team
+# Licensed as GPL >=2. See the COPYING file which comes with GRASS for details.
+#
+# Purpose: Benchmark compilers and compiler flags for Soeren's neighborhood
+#   analysis benchmark program.
+#
+# Usage: edit compiler and CFLAGS variables below, then run it and paste the results to:
+#   http://grasswiki.osgeo.org/wiki/OpenMP/Benchmarks#Neighborhood_analysis
+#
+# Notes: * Written for Linux, some modification will probably be needed elsewhere.
+#        * Try not to run too much else in the background, not even web surfing.
+
+CC=gcc
+CFLAGS="-O0"
+
+ROWS=5000
+COLS=5000
+SIZE=23
+
+rm -f neighbor
+"$CC" -Wall -fopenmp -lgomp $CFLAGS neighbor_bench.c -o neighbor
+
+
+TIME="%e %U %S"
+export TIME
+TM=`which time`
+
+run_time()
+{
+   RESULT=`"$TM" ./neighbor $ROWS $COLS $SIZE 2>&1`
+   TIME_REAL=`echo "$RESULT" | cut -f1 -d ' '`
+   TIME_USER=`echo "$RESULT" | cut -f2 -d ' '`
+   TIME_SYS=`echo "$RESULT" | cut -f3 -d ' '`
+
+   if [ -n "$OMP_NUM_THREADS" ] ; then
+     NUM_THREADS="$OMP_NUM_THREADS"
+   else
+     NUM_THREADS="all"
+   fi
+}
+
+NUM_CORES=`cat /proc/cpuinfo | grep -cw '^processor'`
+RAM="`free -m | grep -w '^Mem:' | awk '{printf("%.1f\n", $2 / 1000)}'` gb"
+CPU_NAME=`cat /proc/cpuinfo | grep '^model name' | uniq | cut -f2 -d: | \
+   sed  -e 's/^ //' -e 's/ Processor$//' -e 's/ CPU//' -e 's/(R)//' \
+        -e 's/([Tt][Mm])//'`
+
+CC_VER=`$CC --version 2>&1 | head -n 1 | sed -e 's/ (.*)//' -e "s/$CC//"`
+LINUX_OS=`lsb_release -d | cut -f2- -d: | sed -e 's/^[ \t]*//'`
+
+wiki_table_template()
+{
+   echo "|-"
+   echo "|$CPU_NAME"
+   echo "|$NUM_CORES"
+   echo "|$NUM_THREADS"
+   echo "|${TIME_REAL}s"
+   echo "|${TIME_USER}s"
+   echo "|${TIME_SYS}s"
+   echo "|$CC"
+   echo "|$CC_VER"
+   echo "| $CFLAGS"
+   echo "|$LINUX_OS"
+   echo "|$RAM"
+   echo "| "
+   echo "| "
+}
+
+
+# once to set up the cache
+unset OMP_NUM_THREADS
+run_time
+
+for i in 1 `seq 2 2 "$NUM_CORES"` ; do
+   OMP_NUM_THREADS=$i
+   export OMP_NUM_THREADS
+
+   # run three times for a bit of replication
+   run_time
+   Tr1="$TIME_REAL"
+   Tu1="$TIME_USER"
+   Ts1="$TIME_SYS"
+
+   run_time
+   Tr2="$TIME_REAL"
+   Tu2="$TIME_USER"
+   Ts2="$TIME_SYS"
+
+   run_time
+   Tr3="$TIME_REAL"
+   Tu3="$TIME_USER"
+   Ts3="$TIME_SYS"
+
+   # average the results
+   TIME_REAL=`echo $Tr1 $Tr2 $Tr3 | awk '{printf("%.2f", ($1 + $2 + $3)/3.0)}'`
+   TIME_USER=`echo $Tu2 $Tu2 $Tu2 | awk '{printf("%.2f", ($1 + $2 + $3)/3.0)}'`
+   TIME_SYS=`echo  $Ts1 $Ts2 $Ts3 | awk '{printf("%.2f", ($1 + $2 + $3)/3.0)}'`
+
+   wiki_table_template
+done


Property changes on: sandbox/soeren/benchmarks/neighborhood_openmp/benchmark_auto.sh
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:mime-type
   + text/x-sh
Added: svn:eol-style
   + native

Added: sandbox/soeren/benchmarks/neighborhood_openmp/neighbor_bench.c
===================================================================
--- sandbox/soeren/benchmarks/neighborhood_openmp/neighbor_bench.c	                        (rev 0)
+++ sandbox/soeren/benchmarks/neighborhood_openmp/neighbor_bench.c	2013-07-24 20:33:52 UTC (rev 57266)
@@ -0,0 +1,173 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+/*
+ * Author: Soeren Gebbert
+ * (c) 2013 Soeren Gebbert and the GRASS Development Team
+ * Licensed as GPL >=2. See the COPYING file which comes with GRASS for details.
+ *
+ * http://thread.gmane.org/gmane.comp.gis.grass.user/47627/focus=47667
+ */ 
+
+/* #define DEBUG 1 */
+
+/* Prototypes for gathering and average computation */
+static int gather_values(double **input, double *buff, int nrows, 
+        int ncols, int mw_size, int col, int row, int dist);
+
+static double average(double *values, int size);
+
+int main(int argc, char **argv)
+{
+    int nrows, ncols, mw_size, size, dist;
+    double **input = NULL, **output = NULL;
+    int i, j;
+
+    /* Check and parse the input parameter */
+    if(argc != 4)
+    {
+
+        fprintf(stderr, "Warning!\n");
+        fprintf(stderr, "Please specifiy the number of rows and columns and the "
+                "\nsize of the moving window (must be an odd number)\n");
+        fprintf(stderr, "\nUsage: neighbor 5000 5000 51\n");
+        fprintf(stderr, "\nUsing default values: rows = 5000, cols = 5000, moving window = 51\n");
+        nrows = 5000;
+        ncols = 5000;
+        mw_size = 51;
+    } 
+    else 
+    {
+
+        sscanf(argv[1], "%d", &nrows);
+        sscanf(argv[2], "%d", &ncols);
+        sscanf(argv[3], "%d", &mw_size);
+
+        if(mw_size%2 == 0) {
+            fprintf(stderr,"The size of the moving window must be odd");
+            return -1;
+        }
+    }
+
+    size = mw_size * mw_size;
+    dist = mw_size / 2;
+
+    /* Allocate input and output */
+    input = (double**)calloc(nrows, sizeof(double*));
+    output= (double**)calloc(nrows, sizeof(double*));
+
+    if(input == NULL || output == NULL)
+    {
+        fprintf(stderr, "Unable to allocate arrays");
+        return -1;
+    }
+
+    for(i = 0; i < nrows; i++) 
+    {
+        input[i] = (double*)calloc(ncols, sizeof(double));
+        output[i]= (double*)calloc(ncols, sizeof(double));
+
+        if(input[i] == NULL || output[i] == NULL)
+        {
+            fprintf(stderr, "Unable to allocate arrays");
+            return -1;
+        }
+
+#ifdef DEBUG
+        for(j = 0; j < ncols; j++)
+            input[i][j] = i + j;
+#endif
+    }
+
+#pragma omp parallel for private(i, j)
+    for(i = 0; i < nrows; i++) 
+    {
+        for(j = 0; j < ncols; j++)
+        {
+
+        /* Value buffer with maximum size */
+        double *buff = NULL;
+        buff = (double*)calloc(size, sizeof(double));
+
+        /* Gather value in moving window */
+        int num = gather_values(input, buff, nrows, ncols, mw_size, i, j, dist);
+
+        output[i][j] = average(buff, num);
+
+        free(buff);
+        }
+    }
+
+#ifdef DEBUG
+    printf("\nInput\n");
+    for(i = 0; i < nrows; i++) 
+    {
+        for(j = 0; j < ncols; j++)
+        {
+            printf("%.2f ", input[i][j]);
+        }
+        printf("\n");
+    }
+
+    printf("\nOutput\n");
+    for(i = 0; i < nrows; i++) 
+    {
+        for(j = 0; j < ncols; j++)
+        {
+            printf("%.2f ", output[i][j]);
+        }
+        printf("\n");
+    }
+#endif
+
+    return 0;
+}
+
+int gather_values(double **input, double *buff, int nrows, 
+        int ncols, int mw_size, int col, int row, int dist)
+{
+    int i, j, k;
+
+    int start_row = row - dist;
+    int start_col = col - dist;
+    int end_row = start_row + mw_size;
+    int end_col = start_col + mw_size;
+
+    if(start_row < 0)
+        start_row = 0;
+
+    if(start_col < 0)
+        start_col = 0;
+
+    if(end_row > nrows)
+        end_row = nrows;
+
+    if(end_col > ncols)
+        end_col = ncols;
+
+    k = 0;
+
+    for(i = start_row; i < end_row; i++)
+    {
+        for(j = start_col; j < end_col; j++)
+        {
+            buff[k] = input[i][j];
+            k++;
+        }
+    }
+
+    return k;
+}
+
+double average(double *values, int size)
+{
+    int i;
+    double a = 0.0;
+    for(i = 0; i < size; i++)
+        a += values[i];
+
+    a = a / (double)size;
+    return a;
+}
+
+


Property changes on: sandbox/soeren/benchmarks/neighborhood_openmp/neighbor_bench.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native



More information about the grass-commit mailing list