From 4f04c4831cb318c8d9e24e54cfd25f1e7280afc6 Mon Sep 17 00:00:00 2001 From: Steven Qie <91923601+stevenqie@users.noreply.github.com> Date: Mon, 2 Dec 2024 23:36:21 -0600 Subject: [PATCH] added fibonacci with futures example --- .../charm++/fibonacci_with_futures/Makefile | 16 +++++ .../fibonacci_with_futures/fib_futures.C | 69 +++++++++++++++++++ .../fibonacci_with_futures/fib_futures.ci | 13 ++++ 3 files changed, 98 insertions(+) create mode 100644 examples/charm++/fibonacci_with_futures/Makefile create mode 100644 examples/charm++/fibonacci_with_futures/fib_futures.C create mode 100644 examples/charm++/fibonacci_with_futures/fib_futures.ci diff --git a/examples/charm++/fibonacci_with_futures/Makefile b/examples/charm++/fibonacci_with_futures/Makefile new file mode 100644 index 0000000000..5f59d07245 --- /dev/null +++ b/examples/charm++/fibonacci_with_futures/Makefile @@ -0,0 +1,16 @@ +# Variables +CHARMC = /Users/stevenqie/software/charm/netlrts-darwin-arm8/bin/charmc +TARGET = fibonacci +SRCS = fib_futures.ci fib_futures.C + +all: $(TARGET) + +$(TARGET): $(SRCS) + $(CHARMC) fib_futures.ci + $(CHARMC) -c fib_futures.C + $(CHARMC) -o $(TARGET) fib_futures.o + +clean: + rm -f *.o $(TARGET) fib.decl.h fib.def.h charmrun + +.PHONY: all clean \ No newline at end of file diff --git a/examples/charm++/fibonacci_with_futures/fib_futures.C b/examples/charm++/fibonacci_with_futures/fib_futures.C new file mode 100644 index 0000000000..009da73a46 --- /dev/null +++ b/examples/charm++/fibonacci_with_futures/fib_futures.C @@ -0,0 +1,69 @@ +#include "fib.decl.h" + +#include + +int THRESHOLD = 30; + +class ValueMsg: public CMessage_ValueMsg { + public: + int value; +}; + +class Main : public CBase_Main { + public: + Main(CkMigrateMessage *m) {}; + Main(CkArgMsg* m) { thisProxy.run(atoi(m->argv[1])); } + void run(int n) { + CkFuture f = CkCreateFuture(); + CProxy_Fib::ckNew(n, f); + ValueMsg *m = (ValueMsg*)CkWaitFuture(f); + CkPrintf("The requested Fibonacci number is : %d\n", m->value); + CkExit(); + } +}; + +class Fib : public CBase_Fib { + public: + int result; + Fib(CkMigrateMessage *m) {}; + Fib(int n, CkFuture f){ + thisProxy.run(n, f); + } + + void run(int n, CkFuture f) { + if (n < THRESHOLD) { + result = seqFib(n); + } else { + + CkEntryOptions opts; + opts.setQueueing(CK_QUEUEING_LIFO); + + CkFuture f1 = CkCreateFuture(); + CkFuture f2 = CkCreateFuture(); + CProxy_Fib::ckNew(n-1, f1, CK_PE_ANY, &opts); + CProxy_Fib::ckNew(n-2, f2, CK_PE_ANY, &opts); + ValueMsg* m1 = (ValueMsg*)CkWaitFuture(f1); + ValueMsg* m2 = (ValueMsg*)CkWaitFuture(f2); + result = m1->value + m2->value; + delete m1; + delete m2; + CkReleaseFuture(f1); + CkReleaseFuture(f2); + } + ValueMsg *m = new ValueMsg(); + m->value = result; + CkSendToFuture(f, m); + + delete this; + + } + + int seqFib(int n) { + if (n <= 1) { + return n; + } + return seqFib(n - 1) + seqFib(n - 2); + } +}; + +#include "fib.def.h" \ No newline at end of file diff --git a/examples/charm++/fibonacci_with_futures/fib_futures.ci b/examples/charm++/fibonacci_with_futures/fib_futures.ci new file mode 100644 index 0000000000..9df762406b --- /dev/null +++ b/examples/charm++/fibonacci_with_futures/fib_futures.ci @@ -0,0 +1,13 @@ +mainmodule fib { + message ValueMsg; + + mainchare Main { + entry Main(CkArgMsg* m); + entry void run(int n); + }; + + chare Fib { + entry Fib(int n, CkFuture f); + entry void run(int n, CkFuture f); + }; +}; \ No newline at end of file