csci2041/public-class-repo/SamplePrograms/Concurrency/fib.cilk
Michael Zhang 399845160c
f
2018-01-29 17:35:31 -06:00

57 lines
1.6 KiB
Text

static const char *ident __attribute__((__unused__))
= "$HeadURL: https://bradley.csail.mit.edu/svn/repos/cilk/5.4.3/examples/fib.cilk $ $LastChangedBy: sukhaj $ $Rev: 517 $ $Date: 2003-10-27 10:05:37 -0500 (Mon, 27 Oct 2003) $";
/*
* Copyright (c) 1994-2003 Massachusetts Institute of Technology
* Copyright (c) 2003 Bradley C. Kuszmaul
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <cilk-lib.cilkh>
#include <stdlib.h>
#include <stdio.h>
int START_OF_CILK_CODE = 0;
cilk int fib(int n)
{
if (n < 2)
return (n);
else {
int x, y;
x = spawn fib(n - 1);
y = spawn fib(n - 2);
sync;
return (x + y);
}
}
cilk int main(int argc, char *argv[])
{
int n, result;
if (argc != 2) {
fprintf(stderr, "Usage: fib [<cilk options>] <n>\n");
Cilk_exit(1);
}
n = atoi(argv[1]);
result = spawn fib(n);
sync;
printf("Result: %d\n", result);
return 0;
}