cornell-zhang / heterocl

HeteroCL: A Multi-Paradigm Programming Infrastructure for Software-Defined Heterogeneous Computing
https://cornell-zhang.github.io/heterocl/
Apache License 2.0
322 stars 92 forks source link

[API] Adding printing function to HeteroCL #178

Closed seanlatias closed 4 years ago

seanlatias commented 4 years ago

Main Features

  1. In this PR, we introduce a new API hcl.print, which can print HeteroCL objects. Currently, the functionality is still very basic. In other words, we cannot generate pretty print like NumPy.
  2. I also removed the need for using hcl.Stage at the top-level. A default top-level Stage will be generated automatically. Please refer to test_dsl_basic.py for more examples.

Examples for hcl.print

  1. Print an expression
A = hcl.compute((10,), lambda x: x)
hcl.print(A[0])
# output: 0
hcl.print(A[0], "A[0]: %d\n")
# output: A[0]: 0
hcl.print([A[0], A[1]])
# output: A[0] A[1]
hcl.print([A[0], A[1]], "0: %d, 1: %d\n")
# output: 0: 0, 1: 1
hcl.print(5)
# output: 5

More details: for integers, by default we use "%d". For all other data types including floating points and fixed points, we use "%f".

  1. Print a Tensor or a slice of Tensor
A = hcl.compute((3, 3), lambda x, y: x+y)
hcl.print(A[0])
# output: [0, 1, 2]
hcl.print(A)
# output:
# [[0, 1, 2],
# [1, 2, 3],
# [2, 3, 4]]

As can be seen from the example above, currently we do not have pretty print with indentations and also find the largest common number of digits. This will be introduced later.

seanlatias commented 4 years ago

Solve issue #174.