File size: 14,770 Bytes
2e045de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"""
Virtual RAM Module - 128GB System Memory Abstraction

This module implements a symbolic representation of 128GB system RAM using
efficient data structures and lazy allocation strategies. It avoids allocating
real memory and uses dictionaries or sparse mappings to simulate blocks.
"""

import time
from typing import Dict, Any, Optional, Union
from dataclasses import dataclass
import numpy as np


@dataclass
class RAMBlock:
    """Represents a block of memory in the symbolic RAM."""
    name: str
    size_bytes: int
    allocated_time: float
    last_accessed: float
    access_count: int = 0
    # We use a symbolic representation instead of actual data
    # The data field will be None for large blocks to avoid memory allocation
    data: Optional[Union[np.ndarray, bytes]] = None
    is_symbolic: bool = True  # True if this is a symbolic block (no real data)


class VirtualRAM:
    """
    Virtual RAM class that simulates 128GB of system memory symbolically.
    
    This class provides block allocation, tracking, and transfer capabilities
    without actually allocating large amounts of physical memory.
    """
    
    def __init__(self, capacity_gb: int = 128):
        self.capacity_bytes = capacity_gb * 1024 * 1024 * 1024  # Convert GB to bytes
        self.capacity_gb = capacity_gb
        
        # Block registry - stores metadata about allocated blocks
        self.blocks: Dict[str, RAMBlock] = {}
        
        # Memory usage tracking
        self.allocated_bytes = 0
        self.allocation_counter = 0
        
        # Access simulation parameters
        self.access_delay_ms = 0.1  # Simulated RAM access delay
        self.transfer_bandwidth_gbps = 51.2  # DDR5-6400 bandwidth
        
        # Statistics
        self.total_allocations = 0
        self.total_deallocations = 0
        self.total_accesses = 0
        self.total_transfers = 0
        
        print(f"VirtualRAM initialized with {capacity_gb}GB capacity")
        
    def allocate_block(self, name: str, size_bytes: int, 
                      store_data: bool = False) -> bool:
        """
        Allocate a block of memory symbolically.
        
        Args:
            name: Unique name for the block
            size_bytes: Size of the block in bytes
            store_data: If True, actually allocate small amounts of real data for testing
                       If False (default), only store metadata symbolically
        
        Returns:
            True if allocation successful, False if not enough space or name exists
        """
        # Check if name already exists
        if name in self.blocks:
            print(f"Block '{name}' already exists")
            return False
            
        # Check if we have enough capacity
        if self.allocated_bytes + size_bytes > self.capacity_bytes:
            print(f"Not enough capacity: requested {size_bytes:,} bytes, "
                  f"available {self.capacity_bytes - self.allocated_bytes:,} bytes")
            return False
            
        # Create the block
        current_time = time.time()
              # For all blocks, we store only metadata to avoid memory issues
        actual_data = None
        is_symbolic = True
        
        # If store_data is explicitly requested and size is very small, we can store actual data
        if store_data and size_bytes <= 1024 * 1024 * 10: # Up to 10MB for actual data
            actual_data = np.zeros(size_bytes, dtype=np.uint8)
            is_symbolic = False
            print(f"Allocated real data for block \'{name}\' ({size_bytes:,} bytes)")
        else:
            print(f"Created symbolic block \'{name}\' of {size_bytes:,} bytes")            
        block = RAMBlock(
            name=name,
            size_bytes=size_bytes,
            allocated_time=current_time,
            last_accessed=current_time,
            data=actual_data,
            is_symbolic=is_symbolic
        )
        
        self.blocks[name] = block
        self.allocated_bytes += size_bytes
        self.total_allocations += 1
        self.allocation_counter += 1
        
        print(f"Allocated block '{name}': {size_bytes:,} bytes "
              f"({'symbolic' if is_symbolic else 'real data'})")
        
        return True
        
    def get_block(self, name: str) -> Optional[RAMBlock]:
        """
        Retrieve a block by name and simulate access delay.
        
        Args:
            name: Name of the block to retrieve
            
        Returns:
            RAMBlock if found, None otherwise
        """
        if name not in self.blocks:
            return None
            
        # Simulate access delay
        time.sleep(self.access_delay_ms / 1000.0)
        
        # Update access statistics
        block = self.blocks[name]
        block.last_accessed = time.time()
        block.access_count += 1
        self.total_accesses += 1
        
        return block
        
    def release_block(self, name: str) -> bool:
        """
        Deallocate a block of memory.
        
        Args:
            name: Name of the block to deallocate
            
        Returns:
            True if deallocation successful, False if block not found
        """
        if name not in self.blocks:
            print(f"Block '{name}' not found")
            return False
            
        block = self.blocks[name]
        self.allocated_bytes -= block.size_bytes
        self.total_deallocations += 1
        
        del self.blocks[name]
        
        print(f"Released block '{name}': {block.size_bytes:,} bytes")
        return True
        
    def transfer_to_vram(self, block_name: str, vram_instance, 
                        vram_name: Optional[str] = None) -> Optional[str]:
        """
        Transfer a RAM block to VRAM with delay simulation.
        
        Args:
            block_name: Name of the RAM block to transfer
            vram_instance: Instance of VRAM to transfer to
            vram_name: Optional name for the block in VRAM
            
        Returns:
            VRAM block ID if successful, None otherwise
        """
        # Get the block from RAM
        block = self.get_block(block_name)
        if block is None:
            print(f"Block '{block_name}' not found in RAM")
            return None
            
        # Calculate transfer time based on bandwidth
        transfer_time_ms = (block.size_bytes / (self.transfer_bandwidth_gbps * 1e9)) * 1000
        
        print(f"Transferring '{block_name}' ({block.size_bytes:,} bytes) "
              f"from RAM to VRAM (estimated {transfer_time_ms:.2f}ms)")
        
        # Prepare data for transfer
        if block.is_symbolic:
            # For symbolic blocks, create a small representative data sample
            sample_size = min(1024, block.size_bytes)  # 1KB sample
            transfer_data = np.random.randint(0, 256, sample_size, dtype=np.uint8)
            print(f"Using {sample_size} byte sample for symbolic block transfer")
        else:
            # Use actual data
            transfer_data = block.data
            
        # Perform the transfer to VRAM
        if vram_name is None:
            vram_name = f"ram_transfer_{block_name}"
            
        vram_id = vram_instance.transfer_from_ram(vram_name, transfer_data, 
                                                 delay_ms=transfer_time_ms)
        
        if vram_id:
            self.total_transfers += 1
            print(f"Successfully transferred '{block_name}' to VRAM as '{vram_id}'")
        else:
            print(f"Failed to transfer '{block_name}' to VRAM")
            
        return vram_id
        
    def create_tensor_block(self, name: str, shape: tuple, dtype=np.float32) -> bool:
        """
        Create a tensor block with specified shape and data type.
        
        Args:
            name: Name for the tensor block
            shape: Shape of the tensor (e.g., (1024, 1024, 3))
            dtype: Data type of the tensor
            
        Returns:
            True if creation successful, False otherwise
        """
        # Calculate size in bytes
        element_size = np.dtype(dtype).itemsize
        total_elements = np.prod(shape)
        size_bytes = total_elements * element_size
        
        # Allocate the block symbolically
        success = self.allocate_block(name, size_bytes, store_data=False)
        
        if success:
            # Store tensor metadata
            block = self.blocks[name]
            block.tensor_shape = shape
            block.tensor_dtype = dtype
            print(f"Created tensor block '{name}' with shape {shape} and dtype {dtype}")
            
        return success
        
    def info(self) -> Dict[str, Any]:
        """
        Get comprehensive information about the Virtual RAM state.
        
        Returns:
            Dictionary containing RAM usage statistics and metadata
        """
        used_bytes = self.allocated_bytes
        free_bytes = self.capacity_bytes - used_bytes
        utilization_percent = (used_bytes / self.capacity_bytes) * 100
        
        # Calculate average block size
        avg_block_size = used_bytes / len(self.blocks) if self.blocks else 0
        
        # Find largest and smallest blocks
        largest_block = max(self.blocks.values(), key=lambda b: b.size_bytes) if self.blocks else None
        smallest_block = min(self.blocks.values(), key=lambda b: b.size_bytes) if self.blocks else None
        
        # Count symbolic vs real blocks
        symbolic_blocks = sum(1 for b in self.blocks.values() if b.is_symbolic)
        real_blocks = len(self.blocks) - symbolic_blocks
        
        info_dict = {
            "capacity_gb": self.capacity_gb,
            "capacity_bytes": self.capacity_bytes,
            "used_bytes": used_bytes,
            "free_bytes": free_bytes,
            "utilization_percent": utilization_percent,
            "total_blocks": len(self.blocks),
            "symbolic_blocks": symbolic_blocks,
            "real_data_blocks": real_blocks,
            "avg_block_size_bytes": avg_block_size,
            "largest_block_name": largest_block.name if largest_block else None,
            "largest_block_size": largest_block.size_bytes if largest_block else 0,
            "smallest_block_name": smallest_block.name if smallest_block else None,
            "smallest_block_size": smallest_block.size_bytes if smallest_block else 0,
            "total_allocations": self.total_allocations,
            "total_deallocations": self.total_deallocations,
            "total_accesses": self.total_accesses,
            "total_transfers": self.total_transfers,
            "block_names": list(self.blocks.keys())
        }
        
        return info_dict
        
    def print_info(self) -> None:
        """Print a formatted summary of Virtual RAM information."""
        info = self.info()
        
        print("\n" + "="*50)
        print("VIRTUAL RAM INFORMATION")
        print("="*50)
        print(f"Capacity: {info['capacity_gb']} GB ({info['capacity_bytes']:,} bytes)")
        print(f"Used: {info['used_bytes']:,} bytes ({info['utilization_percent']:.2f}%)")
        print(f"Free: {info['free_bytes']:,} bytes")
        print(f"Total Blocks: {info['total_blocks']}")
        print(f"  - Symbolic blocks: {info['symbolic_blocks']}")
        print(f"  - Real data blocks: {info['real_data_blocks']}")
        
        if info['total_blocks'] > 0:
            print(f"Average block size: {info['avg_block_size_bytes']:,.0f} bytes")
            print(f"Largest block: '{info['largest_block_name']}' ({info['largest_block_size']:,} bytes)")
            print(f"Smallest block: '{info['smallest_block_name']}' ({info['smallest_block_size']:,} bytes)")
            
        print(f"\nStatistics:")
        print(f"  - Total allocations: {info['total_allocations']}")
        print(f"  - Total deallocations: {info['total_deallocations']}")
        print(f"  - Total accesses: {info['total_accesses']}")
        print(f"  - Total transfers: {info['total_transfers']}")
        
        if info['block_names']:
            print(f"\nBlock names: {', '.join(info['block_names'])}")
            
        print("="*50)
        
    def simulate_workload(self, num_operations: int = 100) -> None:
        """
        Simulate a typical workload with allocations, accesses, and deallocations.
        
        Args:
            num_operations: Number of operations to simulate
        """
        print(f"\nSimulating workload with {num_operations} operations...")
        
        import random
        
        for i in range(num_operations):
            operation = random.choice(['allocate', 'access', 'deallocate'])
            
            if operation == 'allocate' and len(self.blocks) < 50:  # Limit to 50 blocks
                size = random.randint(1024, 100 * 1024 * 1024)  # 1KB to 100MB
                name = f"workload_block_{i}"
                self.allocate_block(name, size)
                
            elif operation == 'access' and self.blocks:
                block_name = random.choice(list(self.blocks.keys()))
                self.get_block(block_name)
                
            elif operation == 'deallocate' and self.blocks:
                block_name = random.choice(list(self.blocks.keys()))
                self.release_block(block_name)
                
        print(f"Workload simulation completed.")


if __name__ == "__main__":
    # Test the VirtualRAM module
    print("Testing VirtualRAM module...")
    
    # Create a VirtualRAM instance with 128GB capacity
    vram = VirtualRAM(capacity_gb=128)
    
    # Test basic allocation
    print("\n1. Testing basic allocation...")
    vram.allocate_block("small_buffer", 1024 * 1024, store_data=True)  # 1MB with real data
    vram.allocate_block("medium_buffer", 50 * 1024 * 1024)  # 50MB symbolic
    vram.allocate_block("large_tensor", 16 * 1024 * 1024 * 1024)  # 16GB symbolic
    
    # Test tensor creation
    print("\n2. Testing tensor creation...")
    vram.create_tensor_block("ai_weights", (1000, 1000, 512), np.float32)
    vram.create_tensor_block("image_batch", (32, 224, 224, 3), np.uint8)
    
    # Test block access
    print("\n3. Testing block access...")
    block = vram.get_block("small_buffer")
    if block:
        print(f"Accessed block: {block.name}, size: {block.size_bytes:,} bytes")
        
    # Test info display
    print("\n4. Testing info display...")
    vram.print_info()
    
    # Test workload simulation
    print("\n5. Testing workload simulation...")
    vram.simulate_workload(20)
    
    # Final info
    print("\n6. Final state...")
    vram.print_info()
    
    print("\nVirtualRAM test completed!")