# Cilk 技术应用实例

9 #include <algorithm>

10

11 #include <iostream>

12 #include <iterator>

13 #include <functional>

14

15 // Sort the range between begin and end.

16 // "end" is one past the final element in the range.

19 // This is pure C++ code before Cilk conversion.

20

21 void sample_qsort(int * begin, int * end)

22 {

23     if (begin != end) {

24         --end; // Exclude last element (pivot)

25     int * middle = std::partition(begin, end,

26     std::bind2nd(std::less<int>(),*end));

28     std::swap(*end, *middle); // pivot to middle

29     sample_qsort(begin, middle);

30     sample_qsort(++middle, ++end); // Exclude pivot

31     }

32 }

33

34 // A simple test harness

35 int qmain(int n)

36 {

37     int *a = new int[n];

38

39     for (int i = 0; i < n; ++i)

40     a[i] = i;

41

42     std::random_shuffle(a, a + n);

43     std::cout << "Sorting " << n << " integers"

<<     std::endl;

44

45     sample_qsort(a, a + n);

48

49 // Confirm that a is sorted and that each element

// contains the index.

50     for (int i = 0; i < n-1; ++i) {

51         if ( a[i] >= a[i+1] || a[i] != i ) {

52             std::cout << "Sort failed at location i="

<< i << " a[i] = "

53              << a[i] << " a[i+1] = " << a[i+1]

<< std::endl;

54             delete[] a;

55             return 1;

56             }

57     }

58     std::cout << "Sort succeeded." << std::endl;

59     delete[] a;

60     return 0;

61 }

62

63 int main(int argc, char* argv[])

64 {

65     int n = 10*1000*1000;

66     if (argc > 1)

67         n = std::atoi(argv[1]);

68

69     return qmain(n);

70 }

Cilk_spawn 关键字表示一个函数（“子”）可以和其后语句（“父”）并行执行。关键字允许但不要求并行操作。当系统有多个处理器可用时 Cilk 技术会动态地决定哪些操作会被并行执行。

_Cilk_sync 语句表示它将等待同一函数中的所有_Cilk_spawn请求被处理完成后，该函数才能继续执行。_Cilk_sync 语句不会影响在其他函数中衍生的并行 strand（strand 是一串没有任何并行控制的串行指令序列）。

21 void sample_qsort(int * begin, int * end)

22 {

23     if (begin != end) {

24         --end; // Exclude last element (pivot)

25     int * middle = std::partition(begin, end,

26     std::bind2nd(std::less<int>(),*end));

28     std::swap(*end, *middle); // pivot to middle

29     _Cilk_spawn sample_qsort(begin, middle);

30     sample_qsort(++middle, ++end); // Exclude pivot

31     _Cilk_sync;

32     }

33 }

19 include <cilk.h>

21 void sample_qsort(int * begin, int * end)

22 {

23     if (begin != end) {

24         --end; // Exclude last element (pivot)

25     int * middle = std::partition(begin, end,

26     std::bind2nd(std::less<int>(),*end));

28     std::swap(*end, *middle); // pivot to middle

29     cilk_spawn sample_qsort(begin, middle);

30     sample_qsort(++middle, ++end); // Exclude pivot

31     cilk_sync;

32     }

33 }

For more complete information about compiler optimizations, see our Optimization Notice.