taskloop-6.C 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // { dg-do run }
  2. typedef __PTRDIFF_TYPE__ ptrdiff_t;
  3. extern "C" void abort ();
  4. template <typename T>
  5. class I
  6. {
  7. public:
  8. typedef ptrdiff_t difference_type;
  9. I ();
  10. ~I ();
  11. I (T *);
  12. I (const I &);
  13. T &operator * ();
  14. T *operator -> ();
  15. T &operator [] (const difference_type &) const;
  16. I &operator = (const I &);
  17. I &operator ++ ();
  18. I operator ++ (int);
  19. I &operator -- ();
  20. I operator -- (int);
  21. I &operator += (const difference_type &);
  22. I &operator -= (const difference_type &);
  23. I operator + (const difference_type &) const;
  24. I operator - (const difference_type &) const;
  25. template <typename S> friend bool operator == (I<S> &, I<S> &);
  26. template <typename S> friend bool operator == (const I<S> &, const I<S> &);
  27. template <typename S> friend bool operator < (I<S> &, I<S> &);
  28. template <typename S> friend bool operator < (const I<S> &, const I<S> &);
  29. template <typename S> friend bool operator <= (I<S> &, I<S> &);
  30. template <typename S> friend bool operator <= (const I<S> &, const I<S> &);
  31. template <typename S> friend bool operator > (I<S> &, I<S> &);
  32. template <typename S> friend bool operator > (const I<S> &, const I<S> &);
  33. template <typename S> friend bool operator >= (I<S> &, I<S> &);
  34. template <typename S> friend bool operator >= (const I<S> &, const I<S> &);
  35. template <typename S> friend typename I<S>::difference_type operator - (I<S> &, I<S> &);
  36. template <typename S> friend typename I<S>::difference_type operator - (const I<S> &, const I<S> &);
  37. template <typename S> friend I<S> operator + (typename I<S>::difference_type , const I<S> &);
  38. private:
  39. T *p;
  40. };
  41. template <typename T> I<T>::I () : p (0) {}
  42. template <typename T> I<T>::~I () {}
  43. template <typename T> I<T>::I (T *x) : p (x) {}
  44. template <typename T> I<T>::I (const I &x) : p (x.p) {}
  45. template <typename T> T &I<T>::operator * () { return *p; }
  46. template <typename T> T *I<T>::operator -> () { return p; }
  47. template <typename T> T &I<T>::operator [] (const difference_type &x) const { return p[x]; }
  48. template <typename T> I<T> &I<T>::operator = (const I &x) { p = x.p; return *this; }
  49. template <typename T> I<T> &I<T>::operator ++ () { ++p; return *this; }
  50. template <typename T> I<T> I<T>::operator ++ (int) { return I (p++); }
  51. template <typename T> I<T> &I<T>::operator -- () { --p; return *this; }
  52. template <typename T> I<T> I<T>::operator -- (int) { return I (p--); }
  53. template <typename T> I<T> &I<T>::operator += (const difference_type &x) { p += x; return *this; }
  54. template <typename T> I<T> &I<T>::operator -= (const difference_type &x) { p -= x; return *this; }
  55. template <typename T> I<T> I<T>::operator + (const difference_type &x) const { return I (p + x); }
  56. template <typename T> I<T> I<T>::operator - (const difference_type &x) const { return I (p - x); }
  57. template <typename T> bool operator == (I<T> &x, I<T> &y) { return x.p == y.p; }
  58. template <typename T> bool operator == (const I<T> &x, const I<T> &y) { return x.p == y.p; }
  59. template <typename T> bool operator != (I<T> &x, I<T> &y) { return !(x == y); }
  60. template <typename T> bool operator != (const I<T> &x, const I<T> &y) { return !(x == y); }
  61. template <typename T> bool operator < (I<T> &x, I<T> &y) { return x.p < y.p; }
  62. template <typename T> bool operator < (const I<T> &x, const I<T> &y) { return x.p < y.p; }
  63. template <typename T> bool operator <= (I<T> &x, I<T> &y) { return x.p <= y.p; }
  64. template <typename T> bool operator <= (const I<T> &x, const I<T> &y) { return x.p <= y.p; }
  65. template <typename T> bool operator > (I<T> &x, I<T> &y) { return x.p > y.p; }
  66. template <typename T> bool operator > (const I<T> &x, const I<T> &y) { return x.p > y.p; }
  67. template <typename T> bool operator >= (I<T> &x, I<T> &y) { return x.p >= y.p; }
  68. template <typename T> bool operator >= (const I<T> &x, const I<T> &y) { return x.p >= y.p; }
  69. template <typename T> typename I<T>::difference_type operator - (I<T> &x, I<T> &y) { return x.p - y.p; }
  70. template <typename T> typename I<T>::difference_type operator - (const I<T> &x, const I<T> &y) { return x.p - y.p; }
  71. template <typename T> I<T> operator + (typename I<T>::difference_type x, const I<T> &y) { return I<T> (x + y.p); }
  72. template <typename T>
  73. class J
  74. {
  75. public:
  76. J(const I<T> &x, const I<T> &y) : b (x), e (y) {}
  77. const I<T> &begin ();
  78. const I<T> &end ();
  79. private:
  80. I<T> b, e;
  81. };
  82. template <typename T> const I<T> &J<T>::begin () { return b; }
  83. template <typename T> const I<T> &J<T>::end () { return e; }
  84. int results[2000];
  85. template <typename T>
  86. void
  87. baz (I<T> &i)
  88. {
  89. if (*i < 0 || *i >= 2000)
  90. abort ();
  91. results[*i]++;
  92. }
  93. void
  94. f1 (const I<int> &x, const I<int> &y)
  95. {
  96. #pragma omp parallel
  97. #pragma omp single
  98. #pragma omp taskloop num_tasks(22)
  99. for (I<int> i = x; i <= y; i += 6)
  100. baz (i);
  101. }
  102. void
  103. f2 (const I<int> &x, const I<int> &y)
  104. {
  105. I<int> i;
  106. #pragma omp parallel
  107. #pragma omp single
  108. #pragma omp taskloop grainsize(384) private(i)
  109. for (i = x; i < y - 1; i = 1 - 6 + 7 + i)
  110. baz (i);
  111. }
  112. template <typename T>
  113. void
  114. f3 (const I<int> &x, const I<int> &y)
  115. {
  116. #pragma omp parallel
  117. #pragma omp single
  118. #pragma omp taskloop default(none) firstprivate (x, y)
  119. for (I<int> i = x; i <= y; i = i + 9 - 8)
  120. baz (i);
  121. }
  122. template <typename T>
  123. void
  124. f4 (const I<int> &x, const I<int> &y)
  125. {
  126. I<int> i;
  127. #pragma omp parallel
  128. #pragma omp single
  129. #pragma omp taskloop lastprivate(i)
  130. for (i = x + 2000 - 64; i > y + 10; --i)
  131. baz (i);
  132. }
  133. void
  134. f5 (const I<int> &x, const I<int> &y)
  135. {
  136. #pragma omp parallel
  137. #pragma omp single
  138. #pragma omp taskloop
  139. for (I<int> i = x + 2000 - 64; i > y + 10; i -= 10)
  140. baz (i);
  141. }
  142. template <int N>
  143. void
  144. f6 (const I<int> &x, const I<int> &y)
  145. {
  146. #pragma omp parallel
  147. #pragma omp single
  148. #pragma omp taskloop
  149. for (I<int> i = x + 2000 - 64; i > y + 10; i = i - 12 + 2)
  150. {
  151. I<int> j = i + N;
  152. baz (j);
  153. }
  154. }
  155. template <int N>
  156. void
  157. f7 (I<int> i, const I<int> &x, const I<int> &y)
  158. {
  159. #pragma omp parallel
  160. #pragma omp single
  161. #pragma omp taskloop default(none) firstprivate (x, y)
  162. for (i = x - 10; i <= y + 10; i += N)
  163. baz (i);
  164. }
  165. template <int N>
  166. void
  167. f8 (J<int> j)
  168. {
  169. I<int> i;
  170. #pragma omp parallel
  171. #pragma omp single
  172. #pragma omp taskloop default(none) num_tasks(*I<int> (j.begin ())) firstprivate (j)
  173. for (i = j.begin (); i <= j.end () + N; i += 2)
  174. baz (i);
  175. }
  176. template <typename T, int N>
  177. void
  178. f9 (const I<T> &x, const I<T> &y)
  179. {
  180. #pragma omp parallel
  181. #pragma omp single
  182. #pragma omp taskloop grainsize(163)
  183. for (I<T> i = x; i <= y; i = i + N)
  184. baz (i);
  185. }
  186. template <typename T, int N>
  187. void
  188. f10 (const I<T> &x, const I<T> &y)
  189. {
  190. I<T> i;
  191. #pragma omp parallel
  192. #pragma omp single
  193. #pragma omp taskloop
  194. for (i = x; i > y; i = i + N)
  195. baz (i);
  196. }
  197. template <typename T>
  198. void
  199. f11 (const T &x, const T &y)
  200. {
  201. #pragma omp parallel
  202. {
  203. #pragma omp single nowait
  204. #pragma omp taskloop nogroup
  205. for (T i = x; i <= y; i += 3)
  206. baz (i);
  207. #pragma omp single nowait
  208. {
  209. T j = y + 3;
  210. baz (j);
  211. }
  212. }
  213. }
  214. template <typename T>
  215. void
  216. f12 (const T &x, const T &y)
  217. {
  218. T i;
  219. #pragma omp parallel
  220. #pragma omp single
  221. #pragma omp taskloop
  222. for (i = x; i > y; --i)
  223. baz (i);
  224. }
  225. template <int N>
  226. struct K
  227. {
  228. template <typename T>
  229. static void
  230. f13 (const T &x, const T &y)
  231. {
  232. #pragma omp parallel
  233. #pragma omp single
  234. #pragma omp taskloop
  235. for (T i = x; i <= y + N; i += N)
  236. baz (i);
  237. }
  238. };
  239. I<int>
  240. f14 (const I<int> &x, const I<int> &y)
  241. {
  242. I<int> i;
  243. #pragma omp parallel
  244. #pragma omp single
  245. #pragma omp taskloop lastprivate(i)
  246. for (i = x; i < y - 1; i = 1 - 6 + 7 + i)
  247. baz (i);
  248. return i;
  249. }
  250. template <typename T>
  251. I<int>
  252. f15 (const I<int> &x, const I<int> &y)
  253. {
  254. I<int> i;
  255. #pragma omp parallel
  256. #pragma omp single
  257. #pragma omp taskloop lastprivate(i)
  258. for (i = x + 2000 - 64; i > y + 10; --i)
  259. baz (i);
  260. return i;
  261. }
  262. template <int N>
  263. I<int>
  264. f16 (I<int> i, const I<int> &x, const I<int> &y)
  265. {
  266. #pragma omp parallel
  267. #pragma omp single
  268. #pragma omp taskloop lastprivate(i)
  269. for (i = x - 10; i <= y + 10; i += N)
  270. baz (i);
  271. return i;
  272. }
  273. template <int N>
  274. I<int>
  275. f17 (J<int> j)
  276. {
  277. static I<int> i;
  278. #pragma omp parallel
  279. #pragma omp single
  280. #pragma omp taskloop lastprivate(i)
  281. for (i = j.begin (); i <= j.end () + N; i += 2)
  282. baz (i);
  283. return i;
  284. }
  285. template <typename T, int N>
  286. I<T>
  287. f18 (const I<T> &x, const I<T> &y)
  288. {
  289. static I<T> i;
  290. #pragma omp parallel
  291. #pragma omp single
  292. #pragma omp taskloop lastprivate(i)
  293. for (i = x; i > y; i = i + N)
  294. baz (i);
  295. return i;
  296. }
  297. template <typename T>
  298. T
  299. f19 (const T &x, const T &y)
  300. {
  301. T i;
  302. #pragma omp parallel
  303. {
  304. #pragma omp single nowait
  305. #pragma omp taskloop nogroup lastprivate(i)
  306. for (i = x; i <= y; i += 3)
  307. baz (i);
  308. #pragma omp single nowait
  309. {
  310. T j = y + 3;
  311. baz (j);
  312. }
  313. }
  314. return i;
  315. }
  316. template <typename T>
  317. T
  318. f20 (const T &x, const T &y)
  319. {
  320. T i;
  321. #pragma omp parallel
  322. #pragma omp single
  323. #pragma omp taskloop lastprivate(i)
  324. for (i = x; i > y; --i)
  325. baz (i);
  326. return i;
  327. }
  328. #define check(expr) \
  329. for (int i = 0; i < 2000; i++) \
  330. if (expr) \
  331. { \
  332. if (results[i] != 1) \
  333. abort (); \
  334. results[i] = 0; \
  335. } \
  336. else if (results[i]) \
  337. abort ()
  338. int
  339. main ()
  340. {
  341. int a[2000];
  342. long b[2000];
  343. for (int i = 0; i < 2000; i++)
  344. {
  345. a[i] = i;
  346. b[i] = i;
  347. }
  348. f1 (&a[10], &a[1990]);
  349. check (i >= 10 && i <= 1990 && (i - 10) % 6 == 0);
  350. f2 (&a[0], &a[1999]);
  351. check (i < 1998 && (i & 1) == 0);
  352. f3<char> (&a[20], &a[1837]);
  353. check (i >= 20 && i <= 1837);
  354. f4<int> (&a[0], &a[30]);
  355. check (i > 40 && i <= 2000 - 64);
  356. f5 (&a[0], &a[100]);
  357. check (i >= 116 && i <= 2000 - 64 && (i - 116) % 10 == 0);
  358. f6<-10> (&a[10], &a[110]);
  359. check (i >= 116 && i <= 2000 - 64 && (i - 116) % 10 == 0);
  360. f7<6> (I<int> (), &a[12], &a[1800]);
  361. check (i >= 2 && i <= 1808 && (i - 2) % 6 == 0);
  362. f8<121> (J<int> (&a[14], &a[1803]));
  363. check (i >= 14 && i <= 1924 && (i & 1) == 0);
  364. f9<int, 7> (&a[33], &a[1967]);
  365. check (i >= 33 && i <= 1967 && (i - 33) % 7 == 0);
  366. f10<int, -7> (&a[1939], &a[17]);
  367. check (i >= 21 && i <= 1939 && (i - 21) % 7 == 0);
  368. f11<I<int> > (&a[16], &a[1981]);
  369. check (i >= 16 && i <= 1984 && (i - 16) % 3 == 0);
  370. f12<I<int> > (&a[1761], &a[37]);
  371. check (i > 37 && i <= 1761);
  372. K<5>::f13<I<int> > (&a[1], &a[1935]);
  373. check (i >= 1 && i <= 1936 && (i - 1) % 5 == 0);
  374. if (f14 (&a[0], &a[1999]) != I<int>(&a[1998]))
  375. abort ();
  376. check (i < 1998 && (i & 1) == 0);
  377. if (f15<int> (&a[0], &a[30]) != I<int>(&a[40]))
  378. abort ();
  379. check (i > 40 && i <= 2000 - 64);
  380. if (f16<6> (I<int> (), &a[12], &a[1800]) != I<int>(&a[1814]))
  381. abort ();
  382. check (i >= 2 && i <= 1808 && (i - 2) % 6 == 0);
  383. if (f17<121> (J<int> (&a[14], &a[1803])) != I<int>(&a[1926]))
  384. abort ();
  385. check (i >= 14 && i <= 1924 && (i & 1) == 0);
  386. if (f18<int, -7> (&a[1939], &a[17]) != I<int>(&a[14]))
  387. abort ();
  388. check (i >= 21 && i <= 1939 && (i - 21) % 7 == 0);
  389. if (f19<I<int> > (&a[16], &a[1981]) != I<int>(&a[1984]))
  390. abort ();
  391. check (i >= 16 && i <= 1984 && (i - 16) % 3 == 0);
  392. if (f20<I<int> > (&a[1761], &a[37]) != I<int>(&a[37]))
  393. abort ();
  394. check (i > 37 && i <= 1761);
  395. f9<long, 7> (&b[33], &b[1967]);
  396. check (i >= 33 && i <= 1967 && (i - 33) % 7 == 0);
  397. f10<long, -7> (&b[1939], &b[17]);
  398. check (i >= 21 && i <= 1939 && (i - 21) % 7 == 0);
  399. f11<I<long> > (&b[16], &b[1981]);
  400. check (i >= 16 && i <= 1984 && (i - 16) % 3 == 0);
  401. f12<I<long> > (&b[1761], &b[37]);
  402. check (i > 37 && i <= 1761);
  403. K<5>::f13<I<long> > (&b[1], &b[1935]);
  404. check (i >= 1 && i <= 1936 && (i - 1) % 5 == 0);
  405. if (f18<long, -7> (&b[1939], &b[17]) != I<long>(&b[14]))
  406. abort ();
  407. check (i >= 21 && i <= 1939 && (i - 21) % 7 == 0);
  408. if (f19<I<long> > (&b[16], &b[1981]) != I<long>(&b[1984]))
  409. abort ();
  410. check (i >= 16 && i <= 1984 && (i - 16) % 3 == 0);
  411. if (f20<I<long> > (&b[1761], &b[37]) != I<long>(&b[37]))
  412. abort ();
  413. check (i > 37 && i <= 1761);
  414. }