An example of 40 pieces of Fibonacci numbers(sequences).
If you want to know about Fibonacci sequences, refer this link.
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
63245986
102334155
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
63245986
102334155
To find 40 pieces of Fibonacci sequence, we used this code with OpenMP library on ODROID-A4.
fibonacci_omp.c
fibonacci_omp.c
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <omp.h> #include <unistd.h> /* for open/close.. */ #include <fcntl.h> /* for O_RDONLY */ #include <sys\ioctl.h> /* for ioctl*/ #include <sys\types.h> /* for lseek() */ int Fibonacci(int n) { int x, y; if (n < 2) return n; else { x = Fibonacci(n - 1); y = Fibonacci(n - 2); return (x + y); } } int FibonacciTask(int n) { int x, y; if (n < 2) return n; else { #pragma omp task shared(x) x = Fibonacci(n - 1); #pragma omp task shared(y) y = Fibonacci(n - 2); #pragma omp taskwait return (x + y); } } #define MAX 41 int main(int argc, char * argv[]) { int FibNumber[MAX] = {0}; struct timeval time_start, time_end; int i = 0; // omp 관련 출력 printf(\"Number of CPUs=%d\n\", omp_get_num_procs()); printf(\"Number of max threads=%d\n\", omp_get_max_threads()); gettimeofday(&time_start, NULL); #pragma omp parallel { #pragma omp single private(i) for(i = 1; i < MAX; i++) { FibNumber[i] = FibonacciTask(i); } } gettimeofday(&time_end, NULL); time_end.tv_usec = time_end.tv_usec-time_start.tv_usec; time_end.tv_sec = time_end.tv_sec-time_start.tv_sec; time_end.tv_usec += (time_end.tv_sec*1000000); printf(\"Time of Fibonacci with OpenMP : %lf sec\n\", time_end.tv_usec / 1000000.0); for(i = 0; i < MAX; i++) printf(\"%d \", FibNumber[i]); printf(\"\n--------------------------------------\n\"); return 0;
Here is a Makefile example!
GCC = /usr/local/arm/ktoolchain-cortexa9-ver2.1-20110815/bin/arm-none-linux-gnueabi-gcc #Fibonacci: Fibonacci.c # $(GCC) -o Fibonacci $< -lgomp -lpthread -fopenmp -lm -static Fibonacci_omp: Fibonacci_omp.c $(GCC) -o Fibonacci_omp $< -lgomp -lpthread -fopenmp -lm -static -O3
Please note the toolchain for OpenMP could be downloadable via in this link.
http://www.kandroid.org/board/board.php?board=toolchain&command=body&no=15
You also need to change the kernel option to activate dual-core all the time, refer Page16~17 of this document.
http://com.odroid.com/sigong/nf_file_board/nfile_board_view.php?keyword=&tag=&bid=96
Test result !
We could know the dual-core utilized calculation of Fibonacci is 1.6 times faster than single-core.
root@android:/system/bin # ./Fibonacci Time of Fibonacci with recursion : 7.862591 sec 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 -------------------------------------- root@android:/system/bin # ./Fibonacci_omp Number of CPUs=2 Number of max threads=2 Time of Fibonacci with OpenMP : 4.878260 sec 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 --------------------------------------