graph.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. complexType=CStructType("complex","MyComplex",8)
  35. src=Source("source",complexType,5)
  36. b=ProcessingNode("filter",complexType,7,5)
  37. b.addLiteralArg(4)
  38. b.addLiteralArg("Test")
  39. b.addVariableArg("someVariable")
  40. na = Sink("sa",complexType,5)
  41. nb = Sink("sb",complexType,5)
  42. nc = Sink("sc",complexType,5)
  43. nd = Sink("sd",complexType,5)
  44. #dup=Duplicate3("dup",complexType,5)
  45. g = Graph()
  46. g.connect(src.o,b.i)
  47. #g.connect(b.o,dup.i)
  48. #g.connect(dup.oa,na.i)
  49. #g.connect(dup.ob,nb.i)
  50. #g.connect(dup.oc,nc.i)
  51. g.connect(b.oa,na.i)
  52. g.connect(b.oa,nb.i)
  53. g.connect(b.oa,nc.i)
  54. g.connect(b.ob,nd.i)
  55. GEN_PYTHON = False
  56. print("Generate graphviz and code")
  57. conf=Configuration()
  58. #conf.dumpSchedule = True
  59. sched = g.computeSchedule(conf)
  60. print("Schedule length = %d" % sched.scheduleLength)
  61. print("Memory usage %d bytes" % sched.memory)
  62. # Generation of the schedule is modifying the original graph
  63. # (Introduction of duplicate nodes ...)
  64. # So we cannot reuse the graph to compute the Python and the C
  65. # code generation
  66. conf.debugLimit=1
  67. conf.cOptionalArgs="int someVariable"
  68. #conf.codeArray=True
  69. conf.memoryOptimization=True
  70. if not GEN_PYTHON:
  71. # C++ implementation
  72. sched.ccode("generated",conf)
  73. else:
  74. # Python implementation
  75. conf.pyOptionalArgs="someVariable"
  76. sched.pythoncode(".",config=conf)
  77. # When true it is displaying the name of the FIFO buffer
  78. # When false it is displaying the size of the FIFO (default)
  79. conf.displayFIFOBuf=False
  80. with open("test.dot","w") as f:
  81. sched.graphviz(f,config=conf)