From b8fb173de753147be239f95dc166c96edc15ba3b Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Tue, 24 Jan 2012 12:12:00 +0100 Subject: [PATCH] now with getopt, usage and not working randomfile reading. --- openmp/prefix/datapar.c | 9 +++-- openmp/prefix/prefix.c | 83 ++++++++++++++++++++++++++++++++++++++--- openmp/prefix/seq.c | 12 +----- 3 files changed, 84 insertions(+), 20 deletions(-) diff --git a/openmp/prefix/datapar.c b/openmp/prefix/datapar.c index 9a53c1a..0f562ce 100644 --- a/openmp/prefix/datapar.c +++ b/openmp/prefix/datapar.c @@ -7,20 +7,21 @@ void algorithm (numtype x[], unsigned long size, unsigned int ops[]) { unsigned long k; unsigned long kk = 1; unsigned long i; - for(k=1; k < size; k = kk){ kk = k<<1; - for(i = kk-1; i < size; i+= kk){ + #pragma omp parallel for shared(x,k,kk,size) private(i) + for(i = kk-1; i < size; i+= kk) { x[i] = x[i-k] + x[i]; } - //barrier; + #pragma omp barrier } for(k=kk>>1; k > 1; k = kk){ kk = k>>1; + #pragma omp parallel for shared(x,k,kk,size) private(i) for(i = k-1; i < size-kk; i+= k){ x[i+kk] = x[i] + x[i+kk]; } - //barrier; + #pragma omp barrier } } diff --git a/openmp/prefix/prefix.c b/openmp/prefix/prefix.c index 31271b4..246d3e5 100644 --- a/openmp/prefix/prefix.c +++ b/openmp/prefix/prefix.c @@ -1,9 +1,23 @@ #include +#include +#include +#include +#include +#include #include #include "prefix.h" -/* TODO: Replace with file read. */ -#define NUMBERS 16 +/* This one's binary name. */ +char* binname = "unset"; + + +void usage () { + fprintf (stderr, "\nUsage: %s [-f ] [-n ]\n", binname); + fprintf (stderr, "\t -f: set the filename of the randfile. (defaults to \"numlist.bin\")\n"); + fprintf (stderr, "\t -n: set the number count to read from randfile. (defaults to -1 = read all)\n"); + fprintf (stderr, "\nThis application prints only TIME-INFO to stdout. Everything else goes to stderr.\n"); + exit(1); +} void array_contents(numtype array[], unsigned long size) { printf ("[%li", array[0]); @@ -15,16 +29,73 @@ void array_contents(numtype array[], unsigned long size) { int main (int argc, char* argv[]) { - numtype numarray[NUMBERS]; + FILE *file = NULL; unsigned long size; + numtype *numarray; //unsigned int countarray[NUMBERS]; double startTime, endTime; -//TODO: read num file here. -//first value in file = numcount - for (size = 0; size < NUMBERS; size++) { + /* options */ + unsigned long n = -1; + char *filename = "numlist.bin"; + + /* store out name for usage(); */ + binname = argv[0]; + + /* getopt stuff */ + int c; + opterr = 0; + while ((c = getopt (argc, argv, "n:f:")) != -1) switch (c){ + case 'n': + n = strtoul (optarg,NULL,0); + break; + case 'f': + filename = optarg; + break; + case '?': + if (optopt == 'f' || optopt == 'n') + fprintf (stderr, "Option -%c requires an argument.\n", optopt); + else if (isprint (optopt)) + fprintf (stderr, "Unknown option `-%c'.\n", optopt); + else + fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt); + usage(); + default: + usage(); + } + + /* sanize n. */ + if ((file = fopen(filename, "r"))) { + fclose(file); + file = NULL; + struct stat st; + stat(filename, &st); + size = st.st_size; + if (n == -1 || n > size){ + // n = size; //TODO: uncomment after reading file is implemented! + } + }else{ + fprintf (stderr, "File %s does not exist.\n", filename); + usage(); + } + + /* print info and start reading file. */ + fprintf(stderr, "Will read %li numbers from \"%s\".\n", n, filename); + + numarray = malloc(n * sizeof(numtype)); + if(numarray == NULL) { + fprintf(stderr, "malloc for numarray failed.\n"); + exit(2); + } else { + fprintf(stderr, "malloc for numarray sucessful.\n"); + } + + /* read file. TODO */ + size = n-1; + for (size = 0; size < n; size++) { numarray[size] = 10; } + fprintf(stderr, "TODO: file read (set all = 10 for now) - ready.\n"); #ifdef DEBUG /* might want to comment this out, if numarray is big */ diff --git a/openmp/prefix/seq.c b/openmp/prefix/seq.c index 59418a3..1465f86 100644 --- a/openmp/prefix/seq.c +++ b/openmp/prefix/seq.c @@ -4,15 +4,7 @@ * Sequential algorithm to compute correct values. */ void algorithm (numtype in[], unsigned long size, unsigned int ops[]) { - numtype out[size]; - out[1] = in[0]; - for (unsigned long i = 1; i < size; i++){ - out[i+1] = out[i] + in[i]; - } - out[size] = out[size-1] + in[size-1]; - - /* Copy out array to in */ - for (unsigned long i = 0; i < size; i++){ - in[i] = out[i+1]; + for (unsigned long i = 1; i <= size; i++){ + in[i] = in[(i-1)] + in[i]; } } -- 2.43.0