PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/micronumpy/test/test_zjit.py

https://bitbucket.org/pypy/pypy/
Python | 953 lines | 943 code | 7 blank | 3 comment | 3 complexity | 1957e5c3527c1cd797df608417c8b827 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. """ Tests that check if JIT-compiled numpy operations produce reasonably
  2. good assembler
  3. """
  4. import py
  5. from rpython.jit.metainterp.test.support import LLJitMixin
  6. from rpython.jit.backend.x86.test.test_basic import Jit386Mixin
  7. from rpython.jit.metainterp.warmspot import reset_jit, get_stats
  8. from rpython.jit.metainterp.jitprof import Profiler
  9. from rpython.jit.metainterp import counter
  10. from rpython.rlib.jit import Counters
  11. from rpython.rlib.rarithmetic import intmask
  12. from pypy.module.micronumpy import boxes
  13. from pypy.module.micronumpy.compile import FakeSpace, Parser, InterpreterState
  14. from pypy.module.micronumpy.base import W_NDimArray
  15. from rpython.jit.backend.detect_cpu import getcpuclass
  16. CPU = getcpuclass()
  17. if not CPU.vector_extension:
  18. py.test.skip("this cpu %s has no implemented vector backend" % CPU)
  19. def get_profiler():
  20. from rpython.jit.metainterp import pyjitpl
  21. return pyjitpl._warmrunnerdesc.metainterp_sd.profiler
  22. class TestNumpyJit(LLJitMixin):
  23. enable_opts = "intbounds:rewrite:virtualize:string:earlyforce:pure:heap:unroll"
  24. graph = None
  25. interp = None
  26. def setup_method(self, method):
  27. if not self.CPUClass.vector_extension:
  28. py.test.skip("needs vector extension to run (for now)")
  29. def assert_float_equal(self, f1, f2, delta=0.0001):
  30. assert abs(f1-f2) < delta
  31. def setup_class(cls):
  32. default = """
  33. a = [1,2,3,4]
  34. z = (1, 2)
  35. c = a + b
  36. sum(c) -> 1::1
  37. a -> 3:1:2
  38. """
  39. d = {}
  40. p = Parser()
  41. allcodes = [p.parse(default)]
  42. for name, meth in cls.__dict__.iteritems():
  43. if name.startswith("define_"):
  44. code = meth()
  45. d[name[len("define_"):]] = len(allcodes)
  46. allcodes.append(p.parse(code))
  47. cls.code_mapping = d
  48. cls.codes = allcodes
  49. def compile_graph(self):
  50. if self.graph is not None:
  51. return
  52. space = FakeSpace()
  53. codes = self.codes
  54. def f(i):
  55. interp = InterpreterState(codes[i])
  56. interp.run(space)
  57. if not len(interp.results):
  58. raise Exception("need results")
  59. w_res = interp.results[-1]
  60. if isinstance(w_res, W_NDimArray):
  61. i, s = w_res.create_iter()
  62. w_res = i.getitem(s)
  63. if isinstance(w_res, boxes.W_Float64Box):
  64. return w_res.value
  65. if isinstance(w_res, boxes.W_Float32Box):
  66. return float(w_res.value)
  67. elif isinstance(w_res, boxes.W_Int64Box):
  68. return float(w_res.value)
  69. elif isinstance(w_res, boxes.W_Int32Box):
  70. return float(int(w_res.value))
  71. elif isinstance(w_res, boxes.W_Int16Box):
  72. return float(int(w_res.value))
  73. elif isinstance(w_res, boxes.W_Int8Box):
  74. return float(int(w_res.value))
  75. elif isinstance(w_res, boxes.W_UInt64Box):
  76. return float(intmask(w_res.value))
  77. elif isinstance(w_res, boxes.W_UInt32Box):
  78. return float(intmask(w_res.value))
  79. elif isinstance(w_res, boxes.W_UInt16Box):
  80. return float(intmask(w_res.value))
  81. elif isinstance(w_res, boxes.W_UInt8Box):
  82. return float(intmask(w_res.value))
  83. elif isinstance(w_res, boxes.W_LongBox):
  84. return float(w_res.value)
  85. elif isinstance(w_res, boxes.W_BoolBox):
  86. return float(w_res.value)
  87. print "ERROR: did not implement return type for interpreter"
  88. raise TypeError(w_res)
  89. if self.graph is None:
  90. interp, graph = self.meta_interp(f, [0],
  91. listops=True,
  92. listcomp=True,
  93. backendopt=True,
  94. graph_and_interp_only=True,
  95. ProfilerClass=Profiler,
  96. vec=True)
  97. self.__class__.interp = interp
  98. self.__class__.graph = graph
  99. def check_vectorized(self, expected_tried, expected_success):
  100. profiler = get_profiler()
  101. tried = profiler.get_counter(Counters.OPT_VECTORIZE_TRY)
  102. success = profiler.get_counter(Counters.OPT_VECTORIZED)
  103. assert tried >= success
  104. assert tried == expected_tried
  105. assert success == expected_success
  106. def run(self, name):
  107. self.compile_graph()
  108. profiler = get_profiler()
  109. profiler.start()
  110. reset_jit()
  111. i = self.code_mapping[name]
  112. retval = self.interp.eval_graph(self.graph, [i])
  113. return retval
  114. def define_float32_copy():
  115. return """
  116. a = astype(|30|, float32)
  117. x1 = a -> 7
  118. x2 = a -> 8
  119. x3 = a -> 9
  120. x4 = a -> 10
  121. r = x1 + x2 + x3 + x4
  122. r
  123. """
  124. def test_float32_copy(self):
  125. result = self.run("float32_copy")
  126. assert int(result) == 7+8+9+10
  127. self.check_vectorized(1, 1)
  128. def define_int32_copy():
  129. return """
  130. a = astype(|30|, int32)
  131. x1 = a -> 7
  132. x2 = a -> 8
  133. x3 = a -> 9
  134. x4 = a -> 10
  135. x1 + x2 + x3 + x4
  136. """
  137. def test_int32_copy(self):
  138. result = self.run("int32_copy")
  139. assert int(result) == 7+8+9+10
  140. self.check_vectorized(1, 1)
  141. def define_float32_add():
  142. return """
  143. a = astype(|30|, float32)
  144. b = a + a
  145. b -> 15
  146. """
  147. def test_float32_add(self):
  148. result = self.run("float32_add")
  149. self.assert_float_equal(result, 15.0 + 15.0)
  150. self.check_vectorized(2, 2)
  151. def define_float_add():
  152. return """
  153. a = |30|
  154. b = a + a
  155. b -> 17
  156. """
  157. def test_float_add(self):
  158. result = self.run("float_add")
  159. self.assert_float_equal(result, 17.0 + 17.0)
  160. self.check_vectorized(1, 1)
  161. def define_uint_add():
  162. return """
  163. a = astype(|30|, uint64)
  164. b = a + a
  165. b -> 17
  166. """
  167. def test_uint_add(self):
  168. result = self.run("uint_add")
  169. assert int(result) == 17+17
  170. self.check_vectorized(2, 1)
  171. def define_float32_add_const():
  172. return """
  173. a = astype(|30|, float32)
  174. b = a + 77.345
  175. b -> 29
  176. """
  177. def test_float32_add_const(self):
  178. result = self.run("float32_add_const")
  179. self.assert_float_equal(result, 29.0 + 77.345)
  180. self.check_vectorized(2, 2)
  181. def define_float_add_const():
  182. return """
  183. a = |30| + 25.5
  184. a -> 29
  185. """
  186. def test_float_add_const(self):
  187. result = self.run("float_add_const")
  188. self.assert_float_equal(result, 29.0 + 25.5)
  189. self.check_vectorized(1, 1)
  190. def define_int_add_const():
  191. return """
  192. a = astype(|30|, int)
  193. b = a + 1i
  194. d = astype(|30|, int)
  195. c = d + 2.0
  196. x1 = b -> 7
  197. x2 = b -> 8
  198. x3 = c -> 11
  199. x4 = c -> 12
  200. x1 + x2 + x3 + x4
  201. """
  202. def test_int_add_const(self):
  203. result = self.run("int_add_const")
  204. assert int(result) == 7+1+8+1+11+2+12+2
  205. self.check_vectorized(2, 2)
  206. def define_int_expand():
  207. return """
  208. a = astype(|30|, int)
  209. c = astype(|1|, int)
  210. c[0] = 16
  211. b = a + c
  212. x1 = b -> 7
  213. x2 = b -> 8
  214. x1 + x2
  215. """
  216. def test_int_expand(self):
  217. result = self.run("int_expand")
  218. assert int(result) == 7+16+8+16
  219. self.check_vectorized(2, 2)
  220. def define_int32_expand():
  221. return """
  222. a = astype(|30|, int32)
  223. c = astype(|1|, int32)
  224. c[0] = 16i
  225. b = a + c
  226. x1 = b -> 7
  227. x2 = b -> 8
  228. x1 + x2
  229. """
  230. def test_int32_expand(self):
  231. result = self.run("int32_expand")
  232. assert int(result) == 7+16+8+16
  233. self.check_vectorized(2, 1)
  234. def define_int16_expand():
  235. return """
  236. a = astype(|30|, int16)
  237. c = astype(|1|, int16)
  238. c[0] = 16i
  239. b = a + c
  240. d = b -> 7:15
  241. sum(d)
  242. """
  243. def test_int16_expand(self):
  244. result = self.run("int16_expand")
  245. i = 8
  246. assert int(result) == i*16 + sum(range(7,7+i))
  247. # currently is is not possible to accum for types with < 8 bytes
  248. self.check_vectorized(3, 0)
  249. def define_int8_expand():
  250. return """
  251. a = astype(|30|, int8)
  252. c = astype(|1|, int8)
  253. c[0] = 8i
  254. b = a + c
  255. d = b -> 0:17
  256. sum(d)
  257. """
  258. def test_int8_expand(self):
  259. result = self.run("int8_expand")
  260. assert int(result) == 17*8 + sum(range(0,17))
  261. # does not pay off to cast float64 -> int8
  262. # neither does sum
  263. # a + c should work, but it is given as a parameter
  264. # thus the accum must handle this!
  265. self.check_vectorized(3, 0)
  266. def define_int32_add_const():
  267. return """
  268. a = astype(|30|, int32)
  269. b = a + 1i
  270. d = astype(|30|, int32)
  271. c = d + 2.0
  272. x1 = b -> 7
  273. x2 = b -> 8
  274. x3 = c -> 11
  275. x4 = c -> 12
  276. x1 + x2 + x3 + x4
  277. """
  278. def test_int32_add_const(self):
  279. result = self.run("int32_add_const")
  280. assert int(result) == 7+1+8+1+11+2+12+2
  281. self.check_vectorized(2, 2)
  282. def define_float_mul_array():
  283. return """
  284. a = astype(|30|, float)
  285. b = astype(|30|, float)
  286. c = a * b
  287. x1 = c -> 7
  288. x2 = c -> 8
  289. x3 = c -> 11
  290. x4 = c -> 12
  291. x1 + x2 + x3 + x4
  292. """
  293. def test_float_mul_array(self):
  294. result = self.run("float_mul_array")
  295. assert int(result) == 7*7+8*8+11*11+12*12
  296. self.check_vectorized(2, 2)
  297. def define_int32_mul_array():
  298. return """
  299. a = astype(|30|, int32)
  300. b = astype(|30|, int32)
  301. c = a * b
  302. x1 = c -> 7
  303. x2 = c -> 8
  304. x3 = c -> 11
  305. x4 = c -> 12
  306. x1 + x2 + x3 + x4
  307. """
  308. def test_int32_mul_array(self):
  309. result = self.run("int32_mul_array")
  310. assert int(result) == 7*7+8*8+11*11+12*12
  311. self.check_vectorized(2, 2)
  312. def define_float32_mul_array():
  313. return """
  314. a = astype(|30|, float32)
  315. b = astype(|30|, float32)
  316. c = a * b
  317. x1 = c -> 7
  318. x2 = c -> 8
  319. x3 = c -> 11
  320. x4 = c -> 12
  321. x1 + x2 + x3 + x4
  322. """
  323. def test_float32_mul_array(self):
  324. result = self.run("float32_mul_array")
  325. assert int(result) == 7*7+8*8+11*11+12*12
  326. self.check_vectorized(2, 2)
  327. def define_conversion():
  328. return """
  329. a = astype(|30|, int8)
  330. b = astype(|30|, int)
  331. c = a + b
  332. sum(c)
  333. """
  334. def test_conversion(self):
  335. result = self.run("conversion")
  336. assert result == sum(range(30)) + sum(range(30))
  337. self.check_vectorized(4, 2) # only sum and astype(int) succeed
  338. def define_sum():
  339. return """
  340. a = |30|
  341. sum(a)
  342. """
  343. def test_sum(self):
  344. result = self.run("sum")
  345. assert result == sum(range(30))
  346. self.check_vectorized(1, 1)
  347. def define_sum():
  348. return """
  349. a = |30|
  350. sum(a)
  351. """
  352. def test_sum(self):
  353. result = self.run("sum")
  354. assert result == sum(range(30))
  355. self.check_vectorized(1, 1)
  356. def define_sum_int():
  357. return """
  358. a = astype(|65|,int)
  359. sum(a)
  360. """
  361. def test_sum_int(self):
  362. result = self.run("sum_int")
  363. assert result == sum(range(65))
  364. self.check_vectorized(2, 2)
  365. def define_sum_multi():
  366. return """
  367. a = |30|
  368. b = sum(a)
  369. c = |60|
  370. d = sum(c)
  371. b + d
  372. """
  373. def test_sum_multi(self):
  374. result = self.run("sum_multi")
  375. assert result == sum(range(30)) + sum(range(60))
  376. self.check_vectorized(1, 1)
  377. def define_sum_float_to_int16():
  378. return """
  379. a = |30|
  380. sum(a,int16)
  381. """
  382. def test_sum_float_to_int16(self):
  383. result = self.run("sum_float_to_int16")
  384. assert result == sum(range(30))
  385. # one can argue that this is not desired,
  386. # but unpacking exactly hits savings = 0
  387. self.check_vectorized(1, 1)
  388. def define_sum_float_to_int32():
  389. return """
  390. a = |30|
  391. sum(a,int32)
  392. """
  393. def test_sum_float_to_int32(self):
  394. result = self.run("sum_float_to_int32")
  395. assert result == sum(range(30))
  396. self.check_vectorized(1, 1)
  397. def define_sum_float_to_float32():
  398. return """
  399. a = |30|
  400. sum(a,float32)
  401. """
  402. def test_sum_float_to_float32(self):
  403. result = self.run("sum_float_to_float32")
  404. assert result == sum(range(30))
  405. self.check_vectorized(1, 1)
  406. def define_sum_float_to_uint64():
  407. return """
  408. a = |30|
  409. sum(a,uint64)
  410. """
  411. def test_sum_float_to_uint64(self):
  412. result = self.run("sum_float_to_uint64")
  413. assert result == sum(range(30))
  414. self.check_vectorized(1, 0) # unsigned
  415. def define_cumsum():
  416. return """
  417. a = |30|
  418. b = cumsum(a)
  419. b -> 5
  420. """
  421. def test_cumsum(self):
  422. result = self.run("cumsum")
  423. assert result == 15
  424. def define_axissum():
  425. return """
  426. a = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
  427. b = sum(a,0)
  428. b -> 1
  429. """
  430. def test_axissum(self):
  431. result = self.run("axissum")
  432. assert result == 30
  433. # XXX note - the bridge here is fairly crucial and yet it's pretty
  434. # bogus. We need to improve the situation somehow.
  435. self.check_vectorized(1, 0)
  436. def define_reduce():
  437. return """
  438. a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  439. sum(a)
  440. """
  441. def test_reduce_compile_only_once(self):
  442. self.compile_graph()
  443. reset_jit()
  444. i = self.code_mapping['reduce']
  445. # run it twice
  446. retval = self.interp.eval_graph(self.graph, [i])
  447. assert retval == sum(range(1,11))
  448. retval = self.interp.eval_graph(self.graph, [i])
  449. assert retval == sum(range(1,11))
  450. # check that we got only one loop
  451. assert len(get_stats().loops) == 1
  452. self.check_vectorized(2, 1)
  453. def test_reduce_axis_compile_only_once(self):
  454. self.compile_graph()
  455. reset_jit()
  456. i = self.code_mapping['axissum']
  457. # run it twice
  458. retval = self.interp.eval_graph(self.graph, [i])
  459. retval = self.interp.eval_graph(self.graph, [i])
  460. # check that we got only one loop
  461. assert len(get_stats().loops) == 1
  462. self.check_vectorized(3, 1)
  463. def define_prod():
  464. return """
  465. a = [1,2,3,4,1,2,3,4]
  466. prod(a)
  467. """
  468. def define_prod_zero():
  469. return """
  470. a = [1,2,3,4,1,2,3,0]
  471. prod(a)
  472. """
  473. def test_prod(self):
  474. result = self.run("prod")
  475. assert int(result) == 576
  476. self.check_vectorized(1, 1)
  477. def test_prod_zero(self):
  478. result = self.run("prod_zero")
  479. assert int(result) == 0
  480. self.check_vectorized(1, 1)
  481. def define_max():
  482. return """
  483. a = |30|
  484. a[13] = 128.0
  485. max(a)
  486. """
  487. def test_max(self):
  488. result = self.run("max")
  489. assert result == 128
  490. self.check_vectorized(1, 0)
  491. def define_min():
  492. return """
  493. a = |30|
  494. a[13] = -128
  495. min(a)
  496. """
  497. def test_min(self):
  498. result = self.run("min")
  499. assert result == -128
  500. self.check_vectorized(1, 0)
  501. def define_any():
  502. return """
  503. a = astype([0,0,0,0,0,0,0,1,0,0,0],int8)
  504. any(a)
  505. """
  506. def define_any_int():
  507. return """
  508. a = astype([0,0,0,0,256,0,0,0,0,0,0],int16)
  509. any(a)
  510. """
  511. def define_any_ret_0():
  512. return """
  513. a = astype([0,0,0,0,0,0,0,0,0,0,0],int64)
  514. any(a)
  515. """
  516. def define_float_any():
  517. return """
  518. a = [0,0,0,0,0,0,0,0.1,0,0,0]
  519. any(a)
  520. """
  521. def define_float32_any():
  522. return """
  523. a = astype([0,0,0,0,0,0,0,0.1,0,0,0], float32)
  524. any(a)
  525. """
  526. def test_any_float(self):
  527. result = self.run("float_any")
  528. assert int(result) == 1
  529. self.check_vectorized(1, 1)
  530. def test_any_float32(self):
  531. result = self.run("float32_any")
  532. assert int(result) == 1
  533. self.check_vectorized(2, 2)
  534. def test_any(self):
  535. result = self.run("any")
  536. assert int(result) == 1
  537. self.check_vectorized(2, 1)
  538. def test_any_int(self):
  539. result = self.run("any_int")
  540. assert int(result) == 1
  541. self.check_vectorized(2, 1)
  542. def test_any_ret_0(self):
  543. result = self.run("any_ret_0")
  544. assert int(result) == 0
  545. self.check_vectorized(2, 2)
  546. def define_all():
  547. return """
  548. a = astype([1,1,1,1,1,1,1,1],int32)
  549. all(a)
  550. """
  551. def define_all_int():
  552. return """
  553. a = astype([1,100,255,1,3,1,1,1],int32)
  554. all(a)
  555. """
  556. def define_all_ret_0():
  557. return """
  558. a = astype([1,1,1,1,1,0,1,1],int32)
  559. all(a)
  560. """
  561. def define_float_all():
  562. return """
  563. a = [1,1,1,1,1,1,1,1]
  564. all(a)
  565. """
  566. def define_float32_all():
  567. return """
  568. a = astype([1,1,1,1,1,1,1,1],float32)
  569. all(a)
  570. """
  571. def test_all_float(self):
  572. result = self.run("float_all")
  573. assert int(result) == 1
  574. self.check_vectorized(1, 1)
  575. def test_all_float32(self):
  576. result = self.run("float32_all")
  577. assert int(result) == 1
  578. self.check_vectorized(2, 2)
  579. def test_all(self):
  580. result = self.run("all")
  581. assert int(result) == 1
  582. self.check_vectorized(2, 2)
  583. def test_all_int(self):
  584. result = self.run("all_int")
  585. assert int(result) == 1
  586. self.check_vectorized(2, 2)
  587. def test_all_ret_0(self):
  588. result = self.run("all_ret_0")
  589. assert int(result) == 0
  590. self.check_vectorized(2, 2)
  591. def define_logical_xor_reduce():
  592. return """
  593. a = [1,1,1,1,1,1,1,1]
  594. logical_xor_reduce(a)
  595. """
  596. def test_logical_xor_reduce(self):
  597. result = self.run("logical_xor_reduce")
  598. assert result == 0
  599. self.check_vectorized(0, 0) # TODO reduce
  600. def define_already_forced():
  601. return """
  602. a = |30|
  603. b = a + 4.5
  604. b -> 5 # forces
  605. c = b * 8
  606. c -> 5
  607. """
  608. def test_already_forced(self):
  609. result = self.run("already_forced")
  610. assert result == (5 + 4.5) * 8
  611. self.check_vectorized(2, 2)
  612. def define_ufunc():
  613. return """
  614. a = |30|
  615. b = unegative(a)
  616. b -> 3
  617. """
  618. def test_ufunc(self):
  619. result = self.run("ufunc")
  620. assert result == -3
  621. self.check_vectorized(1, 1)
  622. def define_specialization():
  623. return """
  624. a = |30|
  625. b = a + a
  626. c = unegative(b)
  627. c -> 3
  628. d = a * a
  629. unegative(d)
  630. d -> 3
  631. d = a * a
  632. unegative(d)
  633. d -> 3
  634. d = a * a
  635. unegative(d)
  636. d -> 3
  637. d = a * a
  638. unegative(d)
  639. d -> 3
  640. """
  641. def test_specialization(self):
  642. result = self.run("specialization")
  643. assert result == (3*3)
  644. self.check_vectorized(3, 3)
  645. def define_multidim():
  646. return """
  647. a = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
  648. b = a + a
  649. b -> 1 -> 1
  650. """
  651. def test_multidim(self):
  652. result = self.run('multidim')
  653. assert result == 8
  654. self.check_vectorized(1, 1)
  655. def define_broadcast():
  656. return """
  657. a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
  658. b = [1, 2, 3, 4]
  659. c = a + b
  660. c -> 1 -> 2
  661. """
  662. def test_broadcast(self):
  663. result = self.run("broadcast")
  664. assert result == 10
  665. self.check_vectorized(1, 0) # TODO check on broadcast
  666. def define_setslice():
  667. return """
  668. a = |30|
  669. b = |10|
  670. b[1] = 5.5
  671. a[0:30:3] = b
  672. a -> 3
  673. """
  674. def test_setslice(self):
  675. result = self.run("setslice")
  676. assert result == 5.5
  677. self.check_vectorized(1, 1)
  678. def define_virtual_slice():
  679. return """
  680. a = |30|
  681. c = a + a
  682. d = c -> 1:20
  683. d -> 1
  684. """
  685. def test_virtual_slice(self):
  686. result = self.run("virtual_slice")
  687. assert result == 4
  688. self.check_vectorized(1, 1)
  689. def define_flat_iter():
  690. return '''
  691. a = |30|
  692. b = flat(a)
  693. c = b + a
  694. c -> 3
  695. '''
  696. def test_flat_iter(self):
  697. result = self.run("flat_iter")
  698. assert result == 6
  699. self.check_vectorized(1, 1)
  700. def define_flat_getitem():
  701. return '''
  702. a = |30|
  703. b = flat(a)
  704. b -> 4: -> 6
  705. '''
  706. def test_flat_getitem(self):
  707. result = self.run("flat_getitem")
  708. assert result == 10.0
  709. self.check_vectorized(1,1)
  710. def define_flat_setitem():
  711. return '''
  712. a = |30|
  713. b = flat(a)
  714. b[4:] = a->:26
  715. a -> 5
  716. '''
  717. def test_flat_setitem(self):
  718. result = self.run("flat_setitem")
  719. assert result == 1.0
  720. self.check_vectorized(1,0) # TODO this can be improved
  721. def define_dot():
  722. return """
  723. a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
  724. b = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
  725. c = dot(a, b)
  726. c -> 1 -> 2
  727. """
  728. def test_dot(self):
  729. result = self.run("dot")
  730. assert result == 184
  731. self.check_trace_count(4)
  732. self.check_vectorized(1,1)
  733. def define_argsort():
  734. return """
  735. a = |30|
  736. argsort(a)
  737. a->6
  738. """
  739. def test_argsort(self):
  740. result = self.run("argsort")
  741. assert result == 6
  742. self.check_vectorized(1,1) # vec. setslice
  743. def define_where():
  744. return """
  745. a = [1, 0, 1, 0]
  746. x = [1, 2, 3, 4]
  747. y = [-10, -20, -30, -40]
  748. r = where(a, x, y)
  749. r -> 3
  750. """
  751. def test_where(self):
  752. result = self.run("where")
  753. assert result == -40
  754. def define_searchsorted():
  755. return """
  756. a = [1, 4, 5, 6, 9]
  757. b = |30| -> ::-1
  758. c = searchsorted(a, b)
  759. c -> -1
  760. """
  761. def test_searchsorted(self):
  762. result = self.run("searchsorted")
  763. assert result == 0
  764. self.check_trace_count(6)
  765. def define_int_mul_array():
  766. return """
  767. a = astype(|30|, int32)
  768. b = astype(|30|, int32)
  769. c = a * b
  770. x1 = c -> 7
  771. x2 = c -> 8
  772. x3 = c -> 11
  773. x4 = c -> 12
  774. x1 + x2 + x3 + x4
  775. """
  776. def test_int_mul_array(self):
  777. # note that int64 mul has not packed machine instr
  778. # for SSE4 thus int32
  779. result = self.run("int_mul_array")
  780. assert int(result) == 7*7+8*8+11*11+12*12
  781. self.check_vectorized(2, 2)
  782. def define_slice():
  783. return """
  784. a = |30|
  785. b = a -> ::3
  786. c = b + b
  787. c -> 3
  788. """
  789. def test_slice(self):
  790. result = self.run("slice")
  791. assert result == 18
  792. self.check_vectorized(1,1)
  793. def define_multidim_slice():
  794. return """
  795. a = [[1, 2, 3, 4], [3, 4, 5, 6], [5, 6, 7, 8], [7, 8, 9, 10], [9, 10, 11, 12], [11, 12, 13, 14], [13, 14, 15, 16], [16, 17, 18, 19]]
  796. b = a -> ::2
  797. c = b + b
  798. d = c -> 1
  799. d -> 1
  800. """
  801. def test_multidim_slice(self):
  802. result = self.run('multidim_slice')
  803. assert result == 12
  804. self.check_trace_count(3)
  805. # ::2 creates a view object -> needs an inner loop
  806. # that iterates continous chunks of the matrix
  807. self.check_vectorized(1,0)
  808. def define_dot_matrix():
  809. return """
  810. mat = |16|
  811. m = reshape(mat, [4,4])
  812. vec = [0,1,2,3]
  813. a = dot(m, vec)
  814. a -> 3
  815. """
  816. def test_dot_matrix(self):
  817. result = self.run("dot_matrix")
  818. assert int(result) == 86
  819. self.check_vectorized(1, 1)
  820. # NOT WORKING
  821. def define_pow():
  822. return """
  823. a = |30| ** 2
  824. a -> 29
  825. """
  826. def test_pow(self):
  827. result = self.run("pow")
  828. assert result == 29 ** 2
  829. self.check_trace_count(1)
  830. def define_pow_int():
  831. return """
  832. a = astype(|30|, int)
  833. b = astype([2], int)
  834. c = a ** b
  835. c -> 15
  836. """
  837. def test_pow_int(self):
  838. result = self.run("pow_int")
  839. assert result == 15 ** 2
  840. self.check_trace_count(4) # extra one for the astype