⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fib.cilk

📁 cilk编程的内容
💻 CILK
字号:
#include <cilk.h>#include <cilk-lib.h>#include <stdio.h>/* Note: Comparison of serial to parallel times will not be favorable *       in a program like fib, unless you turn off CILK_CRITICAL_PATH *       measurements! *//* * serial code */int fib_serial(int n){     if (n < 2)	  return (n);     else {	  int x, y;	  x = fib_serial(n - 1);	  y = fib_serial(n - 2);	  return (x + y);     }}/* * Cilk code */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);     }}/* * Wrapper function to translate arguments and perform Cilk timing. */cilk int wrap(int *n){  int r;  Cilk_time cpstart, cpstop, workstart, workstop;  cpstart = Cilk_user_critical_path;  workstart = Cilk_user_work;  r = spawn fib(*n);  sync;  cpstop = Cilk_user_critical_path;  workstop = Cilk_user_work;  printf("Critical path: %f sec\n", Cilk_time_to_sec(cpstop-cpstart));  printf("Total work (accumulated): %f sec\n",	 Cilk_time_to_sec(workstop-workstart));  if (cpstop-cpstart>0)    printf("Average parallelism: %f\n",	   Cilk_time_to_sec(workstop-workstart)/	   Cilk_time_to_sec(cpstop-cpstart));  return r;}/* * initialization code */int main(int argc, char *argv[]){  int i,n,input,count;  Cilk_time wallstart, wallstop;    Cilk_options options;  if (Cilk_parse_command_line(&options, &argc, argv))    exit(1);  if (argc >= 2)    input = atoi(argv[1]);  else    input = 35;  if (argc >= 3)    count = atoi(argv[2]);  else    count = 4;  if (input <= 0 || count <= 0) {    fprintf(stderr, "Usage: %s [fib-argument [count]]\n", argv[0]);    exit(1);  }  Cilk_init(&options);    printf("Running fib(%d) alternating %d times with and without Cilk\n",	 input, count);  for(i=0; i<count; i++)    {      n = 0;  /* Just to be sure */            printf("Trial %d\n", i);            printf("Parallel\n");      wallstart = Cilk_get_wall_time();      n = Cilk_run(wrap, &input);      wallstop = Cilk_get_wall_time();      printf("Wall-clock time: %f sec\n",	     Cilk_wall_time_to_sec(wallstop-wallstart));      printf("fib(%d) = %d\n", input, n);      fflush(NULL);            printf("Serial\n");      wallstart = Cilk_get_wall_time();      n = fib_serial(input);      wallstop = Cilk_get_wall_time();      printf("Wall-clock time: %f sec\n",	     Cilk_wall_time_to_sec(wallstop-wallstart));      printf("fib(%d) = %d\n", input, n);      fflush(NULL);    }    Cilk_finish();  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -