graph.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from cmsisdsp.cg.scheduler import *
  2. ### Define new types of Nodes
  3. class SinkAsync(GenericSink):
  4. def __init__(self,name,theType,inLength):
  5. GenericSink.__init__(self,name)
  6. self.addInput("i",theType,inLength)
  7. @property
  8. def typeName(self):
  9. return "SinkAsync"
  10. class SourceOdd(GenericSource):
  11. def __init__(self,name,theType,inLength):
  12. GenericSource.__init__(self,name)
  13. self.addOutput("o",theType,inLength)
  14. @property
  15. def typeName(self):
  16. return "SourceOdd"
  17. class SourceEven(GenericSource):
  18. def __init__(self,name,theType,inLength):
  19. GenericSource.__init__(self,name)
  20. self.addOutput("o",theType,inLength)
  21. @property
  22. def typeName(self):
  23. return "SourceEven"
  24. class ProcessingOddEven(GenericNode):
  25. def __init__(self,name,theType,inLength,outLength):
  26. GenericNode.__init__(self,name)
  27. self.addInput("ia",theType,inLength)
  28. self.addInput("ib",theType,inLength)
  29. self.addOutput("o",theType,outLength)
  30. @property
  31. def typeName(self):
  32. return "ProcessingOddEven"
  33. ### Define nodes
  34. dataType=CType(SINT16)
  35. odd=SourceOdd("sourceOdd",dataType,1)
  36. even=SourceEven("sourceEven",dataType,1)
  37. proc=ProcessingOddEven("proc",dataType,1,1)
  38. comp=Unary("compute",dataType,1)
  39. sinka=SinkAsync("sinka",dataType,1)
  40. sinkb=SinkAsync("sinkb",dataType,1)
  41. debug=NullSink("debug",dataType,1)
  42. g = Graph()
  43. # Option to customize the default class
  44. # to use for Duplicate and FIFO
  45. # FIFO class can also be changed in the connect
  46. # function to change the class for a specific
  47. # connection
  48. g.defaultFIFOClass = "FIFO"
  49. g.duplicateNodeClassName = "Duplicate"
  50. g.connect(odd.o,proc.ia)
  51. g.connect(even.o,proc.ib)
  52. # Just for checking duplicate nodes
  53. # with scaling factor are working.
  54. # In practice, all edge of a duplicate nodes
  55. # should have same FIFO size
  56. g.connect(odd.o,debug.i,fifoScale=3.0)
  57. g.connect(proc.o,comp.i)
  58. g.connect(comp.o,sinka.i)
  59. g.connect(comp.o,sinkb.i)
  60. print("Generate graphviz and code")
  61. conf=Configuration()
  62. conf.debugLimit=10
  63. conf.cOptionalArgs=""
  64. # It is disabled in asynchronous mode
  65. # since the schedule has no mor any reason to
  66. # be periodic and thus the FIFO content can
  67. # overlap several executions of the schedule
  68. conf.memoryOptimization=True
  69. conf.codeArray = True
  70. conf.switchCase = True
  71. # Asynchronous mode enable. It implies
  72. # switchCase and codeArray true
  73. conf.asynchronous = True
  74. # Increase size of synchronous FIFOs by 100%
  75. # for the asynchronous case (so 2 samples
  76. # instead of 1 in this example)
  77. #conf.FIFOIncrease = 100 # percent
  78. conf.FIFOIncrease = 2.0
  79. #conf.displayFIFOSizes=True
  80. # Prefix for global FIFO buffers
  81. #conf.prefix="sched1"
  82. #conf.dumpSchedule = True
  83. sched = g.computeSchedule(config=conf)
  84. #print(sched.schedule)
  85. print("Schedule length = %d" % sched.scheduleLength)
  86. print("Memory usage %d bytes" % sched.memory)
  87. #
  88. #conf.postCustomCName = "post.h"
  89. #conf.CAPI = True
  90. #conf.prefix="global"
  91. #conf.dumpFIFO = True
  92. #conf.CMSISDSP = False
  93. #conf.codeArray = False
  94. #conf.switchCase = False
  95. # For pure functions (like CMSIS-DSP) which are not
  96. # packaged in a C++ class, we have to decide of the
  97. # asynchronous policy. What must be done in case of
  98. # FIFO overflow or underflow.
  99. # By default, the execution is skipped.
  100. conf.asyncDefaultSkip = True
  101. sched.ccode("./generated",conf)
  102. with open("test.dot","w") as f:
  103. sched.graphviz(f)