Last active
August 29, 2015 14:00
-
-
Save tell/11390398 to your computer and use it in GitHub Desktop.
CilkPlus sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cassert> | |
#include <cstdlib> | |
#include <cilk/cilk.h> | |
#include <gmpxx.h> | |
using namespace std; | |
mpz_class fast_fib(unsigned int n) { | |
if (n < 2) { return mpz_class(n); } | |
mpz_class a(0), b(1); | |
for (unsigned int i = 0; i < n; i++) { | |
mpz_class t = a + b; | |
swap(b, a); | |
swap(a, t); | |
} | |
return a; | |
} | |
unsigned int fib(unsigned int n) { | |
if (n < 2) { return n; } | |
const unsigned int a = cilk_spawn fib(n - 1); | |
const unsigned int b = fib(n - 2); | |
cilk_sync; | |
return a + b; | |
} | |
mpz_class fib(const mpz_class& n) { | |
if (n < 2) { return n; } | |
const mpz_class a = cilk_spawn fib(n - 1); | |
const mpz_class b = fib(n - 2); | |
cilk_sync; | |
return a + b; | |
} | |
int main(int argc, const char **argv) { | |
assert(argc >= 2); | |
const unsigned int x = atoi(argv[1]); | |
bool use_gmp = false; | |
if (argc >= 3) { | |
const string m = argv[2]; | |
if (m == "gmp") { use_gmp = true; } | |
} | |
const mpz_class verify = fast_fib(x); | |
if (use_gmp) { | |
cout << "use gmp" << endl; | |
const mpz_class y(x); | |
const mpz_class result = fib(y); | |
cout << result << endl; | |
assert(result == verify); | |
} else { | |
unsigned int result = fib(x); | |
cout << result << endl; | |
assert(result == verify); | |
} | |
return 0; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GCC_VERSION := $(shell $(CXX) -dumpversion) | |
GCC_MAJOR_VERSION = $(shell echo $(GCC_VERSION) | cut -d '.' -f 1) | |
GCC_MINOR_VERSION = $(shell echo $(GCC_VERSION) | cut -d '.' -f 2) | |
CPPFLAGS = -Wall -Wextra -O3 -g3 -fcilkplus | |
LDFLAGS = -g3 -lcilkrts -lgmpxx -lgmp -lstdc++ | |
TARGETs = fib | |
.PHONY: all clean precheck | |
all: precheck $(TARGETs) | |
clean: | |
$(RM) $(TARGETs) | |
precheck: | |
[ "$(findstring g++,$(CXX))" = "g++" ] | |
[ $$($(CXX) -dumpversion | cut -d '.' -f 1) -ge 4 ] | |
[ $$($(CXX) -dumpversion | cut -d '.' -f 2) -ge 9 ] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This bash script assumes gcc with CilkPlus is installed with | |
# configure script option '--prefix=${HOME}/cilkplus --program-suffix=-4.8-cp'. | |
export PATH=${HOME}/cilkplus/bin${PATH:+:${PATH}} | |
export CPATH=${HOME}/cilkplus/include${CPATH:+:${CPATH}} | |
export LIBRARY_PATH=${HOME}/cilkplus/lib${LIBRARY_PATH:+:${LIBRARY_PATH}} | |
export LD_LIBRARY_PATH=${HOME}/cilkplus/lib64${HOME}/cilkplus/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} | |
export CC=${HOME}/cilkplus/bin/gcc-4.8-cp | |
export CXX=${HOME}/cilkplus/bin/g++-4.8-cp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment