algorithm1 implemented
authorDavid Kaufmann <astra@fsinf.at>
Fri, 13 Jan 2012 15:03:46 +0000 (16:03 +0100)
committerDavid Kaufmann <astra@fsinf.at>
Fri, 13 Jan 2012 15:03:46 +0000 (16:03 +0100)
openmp/prefix/algorithm_1.c
openmp/prefix/algorithm_1.h
openmp/prefix/prefix.c

index 27f3effe5f356692790239ef9944ae144893d22c..ed0a1be5fd8ec23b67e48fc7d20c2e018f20217f 100644 (file)
@@ -3,6 +3,26 @@
  */
 #include "algorithm_1.h"
 
-int algorithm_1 (numtype y[], int n, int ops[]) {
-       return 0;
+void algorithm_1 (numtype x[], int n, int ops[]) {
+       int i;
+       numtype y[n/2];
+       if (n == 1) {
+               return;
+       }
+
+       for (i=0; i < (n/2); i++) {
+               y[i] = x[2*i] + x[2*i+1];
+       }
+
+       algorithm_1(y, n/2, ops);
+
+       x[1] = y[0];
+
+       for (i = 1; i < (n/2); i++){
+               x[2*i] = y[i-1] + x[2*i];
+               x[2*i+1] = y[i];
+       }
+       if ((n%2)==1) {
+               x[n-1] = y[n/2-1] + x[n-1];
+       }
 }
index 1d537aa8e42c62437c1cf6dd73a3bc343dbc78ef..c6c9ded08cdf85789a8d294170028750170ec614 100644 (file)
@@ -4,4 +4,4 @@
 #include <stdio.h>
 #include "prefix.h"
 
-int algorithm_1 (numtype y[], int n, int ops[]);
+void algorithm_1 (numtype y[], int n, int ops[]);
index 9297e71cb74955ef773af2501b593caf49c7c24c..33f67c9b820ca1364c3451e3a367d3e89c7da28c 100644 (file)
@@ -5,35 +5,37 @@
 #include "algorithm_2.h"
 #include "algorithm_3.h"
 
+#define NUMBERS 800000
+
 int main (int argc, char* argv[]) {
        int result = 0;
-       numtype numarray[5];
-       int countarray[5];
+       numtype numarray[NUMBERS];
+       int countarray[NUMBERS];
        int i;
        double startTime, endTime;
 
-       for (i = 0; i < 5; i++) {
-               numarray[i] = i;
+       for (i = 0; i < NUMBERS; i++) {
+               numarray[i] = 10;
                countarray[i] = 0;
        }
 
        startTime = omp_get_wtime();
        printf ("calling algorithm 1:");
-       result = algorithm_1(numarray, 5, countarray);
-       printf ("sum: %i\n", result);
+       algorithm_1(numarray, NUMBERS, countarray);
+       printf ("sum: %i\n", numarray[NUMBERS-1]);
        endTime = omp_get_wtime();
        printf("took %f seconds.\n", endTime-startTime);
        
        startTime = omp_get_wtime();
        printf ("calling algorithm 2:");
-       result = algorithm_2(numarray, 5, countarray);
+       result = algorithm_2(numarray, NUMBERS, countarray);
        printf ("sum: %i\n", result);
        endTime = omp_get_wtime();
        printf("took %f seconds.\n", endTime-startTime);
        
        startTime = omp_get_wtime();
        printf ("calling algorithm 3:");
-       result = algorithm_3(numarray, 5, countarray);
+       result = algorithm_3(numarray, NUMBERS, countarray);
        printf ("sum: %i\n", result);
        endTime = omp_get_wtime();
        printf("took %f seconds.\n", endTime-startTime);