@@ -3,6 +3,7 @@ package config
33
44import (
55 "fmt"
6+ "strconv"
67
78 "github.com/sarchlab/akita/v4/mem/idealmemcontroller"
89 "github.com/sarchlab/akita/v4/mem/mem"
@@ -20,8 +21,8 @@ type DeviceBuilder struct {
2021 monitor * monitoring.Monitor
2122 //portFactory portFactory
2223 width , height int
23- memoryMode string // simple or shared or local
24- memoryShare [][][ ]int // [[ [x1, y1], [x2, y2], ...], [ [x1, y1], [x2, y2], ...], ...]
24+ memoryMode string // simple or shared or local
25+ memoryShare map [[ 2 ] int ]int //map[[x, y]]GroupID
2526}
2627
2728// type portFactory interface {
@@ -65,7 +66,7 @@ func (d DeviceBuilder) WithMemoryMode(mode string) DeviceBuilder {
6566}
6667
6768// WithMemoryShare sets the memory sharing configuration.
68- func (d DeviceBuilder ) WithMemoryShare (share [][][ ]int ) DeviceBuilder {
69+ func (d DeviceBuilder ) WithMemoryShare (share map [[ 2 ] int ]int ) DeviceBuilder {
6970 d .memoryShare = share
7071 return d
7172}
@@ -90,39 +91,47 @@ func (d DeviceBuilder) createSharedMemory(dev *device) {
9091 if d .memoryMode == "shared" {
9192 // Create shared memory controller
9293
93- // Connect tiles to shared memory based on memoryShare configuration
94- for groupIndex , group := range d .memoryShare {
95- // Create a connection for this memory group
94+ controllers := make (map [int ]* idealmemcontroller.Comp )
95+ connections := make (map [int ]* directconnection.Comp )
9696
97- sharedMem := idealmemcontroller .MakeBuilder ().
98- WithEngine (d .engine ).
99- WithNewStorage (4 * mem .GB ).
100- WithLatency (5 ).
101- Build ("SharedMemory" )
102-
103- connName := fmt .Sprintf ("MemConn_Group_%d" , groupIndex )
104- conn := directconnection .MakeBuilder ().
105- WithEngine (d .engine ).
106- WithFreq (d .freq ).
107- Build (connName )
108-
109- // Connect shared memory to the connection
110- conn .PlugIn (sharedMem .GetPortByName ("Top" ))
111-
112- // Connect each tile in this group to the shared memory
113- for _ , tileCoords := range group {
114- x , y := tileCoords [0 ], tileCoords [1 ]
115- if x >= 0 && x < d .width && y >= 0 && y < d .height {
116- tile := dev.Tiles [y ][x ]
117- // Create a memory port for this tile
118- memPort := tile .Core .GetPortByName ("Memory" )
119- conn .PlugIn (memPort )
120-
121- // Store the connection for later use
122-
123- // Configure the core to use shared memory
124- // This would need to be implemented in the core to set UseSharedMemory = true
125- // and set the SharedMemoryPort
97+ for x := 0 ; x < d .width ; x ++ {
98+ for y := 0 ; y < d .height ; y ++ {
99+ tile := dev.Tiles [y ][x ]
100+ // if has mapping
101+ if _ , ok := d .memoryShare [[2 ]int {x , y }]; ! ok {
102+ panic ("No mapping for tile " + strconv .Itoa (x ) + "," + strconv .Itoa (y ))
103+ }
104+ groupID := d .memoryShare [[2 ]int {x , y }]
105+ if _ , ok := controllers [groupID ]; ! ok {
106+ // has not been created yet, create it
107+ controller := idealmemcontroller .MakeBuilder ().
108+ WithEngine (d .engine ).
109+ WithNewStorage (4 * mem .GB ).
110+ WithLatency (5 ).
111+ Build ("SharedMemory" )
112+ controllers [groupID ] = controller
113+
114+ name := fmt .Sprintf ("SharedMemory%d%d" , x , y )
115+
116+ conn := directconnection .MakeBuilder ().
117+ WithEngine (d .engine ).
118+ WithFreq (d .freq ).
119+ Build (name )
120+ conn .PlugIn (controller .GetPortByName ("Top" ))
121+ conn .PlugIn (tile .Core .GetPortByName ("Router" ))
122+ connections [groupID ] = conn
123+ tile .SetRemotePort (cgra .Router , controller .GetPortByName ("Top" ).AsRemote ())
124+ tile .SharedMemoryController = controller
125+ dev .SharedMemoryControllers = append (dev .SharedMemoryControllers , controller )
126+
127+ fmt .Println ("Connect Tile (" , x , "," , y , ") to SharedMemory Controller (" , groupID , ") (new-created)" )
128+ } else {
129+ // plug in the controller to the tile
130+ fmt .Println ("Connect Tile (" , x , "," , y , ") to SharedMemory Controller (" , groupID , ") (already-created)" )
131+ connections [groupID ].PlugIn (tile .Core .GetPortByName ("Router" ))
132+ tile .SetRemotePort (cgra .Router , controllers [groupID ].GetPortByName ("Top" ).AsRemote ())
133+ tile .SharedMemoryController = controllers [groupID ]
134+ dev .SharedMemoryControllers = append (dev .SharedMemoryControllers , controllers [groupID ])
126135 }
127136 }
128137 }
0 commit comments