graph.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from cmsisdsp.cg.scheduler import *
  2. ### Define new types of Nodes
  3. class ProcessingNode(GenericNode):
  4. def __init__(self,name,theType,inLength,outLength):
  5. GenericNode.__init__(self,name)
  6. self.addInput("i",theType,inLength)
  7. self.addOutput("oa",theType,outLength)
  8. self.addOutput("ob",theType,outLength)
  9. @property
  10. def typeName(self):
  11. return "ProcessingNode"
  12. class Sink(GenericSink):
  13. def __init__(self,name,theType,inLength):
  14. GenericSink.__init__(self,name)
  15. self.addInput("i",theType,inLength)
  16. @property
  17. def typeName(self):
  18. return "Sink"
  19. class Source(GenericSource):
  20. def __init__(self,name,theType,inLength):
  21. GenericSource.__init__(self,name)
  22. self.addOutput("o",theType,inLength)
  23. @property
  24. def typeName(self):
  25. return "Source"
  26. ### Define nodes
  27. # Node datatype
  28. # WARNING : In Python there is reference semantic for
  29. # the objects. But in C++, the struct have value semantic.
  30. # So in Python implementation of the node, the reference
  31. # shoudl never be shared.
  32. # Modify the fields of the objects, or create a totally new
  33. # object.
  34. GEN_PYTHON = False
  35. if GEN_PYTHON:
  36. complexType=PythonClassType("MyComplex")
  37. else:
  38. complexType=CStructType("complex",8)
  39. src=Source("source",complexType,5)
  40. b=ProcessingNode("filter",complexType,7,5)
  41. b.addLiteralArg(4)
  42. b.addLiteralArg("Test")
  43. b.addVariableArg("someVariable")
  44. na = Sink("sa",complexType,5)
  45. nb = Sink("sb",complexType,5)
  46. nc = Sink("sc",complexType,5)
  47. nd = Sink("sd",complexType,5)
  48. #dup=Duplicate3("dup",complexType,5)
  49. g = Graph()
  50. g.connect(src.o,b.i)
  51. #g.connect(b.o,dup.i)
  52. #g.connect(dup.oa,na.i)
  53. #g.connect(dup.ob,nb.i)
  54. #g.connect(dup.oc,nc.i)
  55. g.connect(b.oa,na.i)
  56. g.connect(b.oa,nb.i)
  57. g.connect(b.oa,nc.i)
  58. g.connect(b.ob,nd.i)
  59. print("Generate graphviz and code")
  60. conf=Configuration()
  61. #conf.dumpSchedule = True
  62. sched = g.computeSchedule(conf)
  63. print("Schedule length = %d" % sched.scheduleLength)
  64. print("Memory usage %d bytes" % sched.memory)
  65. # Generation of the schedule is modifying the original graph
  66. # (Introduction of duplicate nodes ...)
  67. # So we cannot reuse the graph to compute the Python and the C
  68. # code generation
  69. conf.debugLimit=1
  70. conf.cOptionalArgs="int someVariable"
  71. #conf.codeArray=True
  72. conf.memoryOptimization=True
  73. if not GEN_PYTHON:
  74. # C++ implementation
  75. sched.ccode("generated",conf)
  76. else:
  77. # Python implementation
  78. conf.pyOptionalArgs="someVariable"
  79. sched.pythoncode(".",config=conf)
  80. # When true it is displaying the name of the FIFO buffer
  81. # When false it is displaying the size of the FIFO (default)
  82. conf.displayFIFOBuf=False
  83. with open("test.dot","w") as f:
  84. sched.graphviz(f,config=conf)